Wednesday, August 18, 2021

Compiling dom-to-svg

This is about compiling dom-to-svg, but it probably applies to most libraries that are set up for a webpack build process.
  1. create ./src and ./dist dirs
  2. create index.html in dist
  3. install libraries npm install --save dom-to-svg etc.
  4. set up package.json like here https://webpack.js.org/guides/getting-started/
  5. create src/index.js and refer to the libraries in it import { documentToSVG, elementToSVG, inlineResources, formatXML } from 'dom-to-svg'
  6. run webpack npx webpack
a minimal index.js is as follows: import { documentToSVG, elementToSVG, inlineResources, formatXML } from 'dom-to-svg' window.domToSVG = {} domToSVG.documentToSVG = documentToSVG domToSVG.elementToSVG = elementToSVG domToSVG.inlineResources = inlineResources domToSVG.formatXML = formatXML

Wednesday, March 24, 2021

Quickly factoring out a component in vue.js

If your single file component has grown too large and unwieldy, it might be time to refactor it, and parcel out a portion to a sub-component. In order to minimize the amount of coding in doin so things can be kept to a minimum like this:

  • a new component is created, and referenced in the parent
  • only the HTML is factored out in the sub-component
  • all parent data gets passed to the child
  • events stay in the parent element

First create a minimal new component, let’s call it list, like so:

<template>
  <div>
  </div>
</template>
<script>
</script>

The second step is quite simple: cut the parts you want to factor out of the parent component, and paste them in your new child component. So what in the parent component looked like this:

<table>
<!-- lots of stuff inside here -->
</table>

now becomes

<list></list>

with all the <table></table> code coing into the list component which then looks like this:

<template>
  <table>
    <!-- lots of stuff inside here -->
  </table>
</template>
<script>
module.exports = {
}
</script>

Then you ensure that all the parent data becomes child props. If your parent data looks like this:

data: function () {
    return {
        parts: [],
        blLookup: {},
        prices: {},
        skipParts: {},
        updateId: 0,
        totalCost: 0,
        totalPieces: 0,
        partCount: 0,
        partsFound: 0,
        config: { price: 'min', loc: 'EU', state: 'new' },
    };
},

your child props will look like this:

props: {
    parts: Array,
    blLookup: Object,
    prices: Object,
    skipParts: Object,
    updateId: Number,
    totalCost: Number,
    totalPieces: Number,
    partCount: Number,
    partsFound: Number,
    config: Object
},

Finally, you have to edit the component so that events are called at the parent level. What in the parent component looked like this:

<td><input @change="editPart($event, p.DesignID, i)" class="designID" :value="p.DesignID"></td>

in the child components needs to be changed like this:

<td><input @change="$parent.editPart($event, p.DesignID, i)" class="designID" :value="p.DesignID"></td>

so that the event calls a function inside the parent - as opposed to inside the component.

Now the final step - referencing the sub-component inside the parent, using the path from which the component is served - obviously.

components: {
    list: httpVueLoader('components/set/list.vue')
},

That’s it - you have factored out a part of your parent component in five easy steps.

Sunday, December 27, 2020

Installing OpenWrt on tp-link 841

  1. install tftpd like explained here
  2. set logging to verbose in /etc/default/tftpd-hpa
    TFTP_OPTIONS="--secure --verbose"
  3. set the wired network card address to 192.168.0.66:
    ifconfig eth0 192.168.0.66
  4. copy the firmware file in the tftpd directory with the name wr841nv11_tp_recovery.bin
    cp [name of firmware file] /srv/tftp
  5. reboot the router holding the reset button
  6. check that the firmware file gets downloaded
    tail -f  /var/log/syslog | grep tftp
  7. wait until the router has rebooted
At this point the router is at 192.168.1.1, so in order to reach it you have to change the ip address of your network interface:
ifconfig eth0 192.168.1.66
ssh as root into the router:
ssh root@192.168.1.1
and set up the network as explained here and reboot. If this is not your only router you may want to disable dhcp in /etc/config/dhcp
option dhcpv6 'disabled'
option dhcpv4 'disabled'
Don't forget to set up a root password.

Sunday, July 14, 2019

Vectorizing Palladio's engravings

Get the image from rarebookroom.org
wget http://www.rarebookroom.org/NewOctavoImages/pldarc/LargePhotos/pldarc0153.jpg
Create a threshold image
convert detail.png -threshold 75% mask.png
Use that as mask to clean up the original image
convert detail.png mask.png    -compose Screen  -composite  detail_mask_screen.png
Vectorize the image
convert detail_mask_screen.png -format pnm pnm:- |potrace -s > detail_mask_screen.svg

Sunday, October 15, 2017

Manually enabling https on AWS with Mojo::ACME and Plack

It took me some time but it turns out that setting up a Plack/Mojolicious stack to serve https on Amazon's web services is straightforward if not easy. There are a few pitfalls (keeping ports open, mostly) so I wrote it up for my own future memory

setting up

This is based on mojo listening on port 3001

  • open ports 80 and 443 on AWS
  • create a minimal Mojo::ACME application - let's say it's called cert.pl: mojo generate lite_app cert.pl
  • reroute port 80 to 3001: sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3001
  • launch the app to listen on port 3001: morbo -l http://[::]:3001 cert.pl
  • set your DNS records to point at the instance (this depends on what your domainname setup is
  • check that the app is really available at the desired address and on port 80

creating keys

This is described at Mojo::ACME docs page

  • register an account key if necessary
  • make sure port 5000 is free (netstat -tulpn | grep :5000 gives you the list of processes using port 5000)
  • generate your domain cert

running starman

  • reroute 443, 80 and 8080 to 5000 (same as command above)
  • check your iptables sudo iptables -t nat --list --line-numbers
  • launch starman starman --enable-ssl --ssl-cert cert.crt --ssl-key cert.key

caveats

I haven't figured out yet how to deal with the fact that apps think they're running under http, so protocols on url_for are (often? mostly? always?) wrong. I'll write up once I figure it out.

Wednesday, October 11, 2017

Template name for Mojolicious templates

Very easy:
<%= __FILE__ %>
That's it;

Tuesday, May 23, 2017

Editing Mac keybindings on the command line

This sets the "Set Print Area" command on Excel to 'Cmd-Shift-Alt-Ctrl-k'

defaults delete com.microsoft.Excel NSUserKeyEquivalents -dict-add "Set Print Area" -string "@$~^k"

It does work, but you get cramps in your fingers.

You can delete all keybindings in an app with

defaults delete com.microsoft.Excel NSUserKeyEquivalents

I can't seem to delete only one keybinding.

Listing keybindings works like this:

defaults read com.microsoft.Excel NSUserKeyEquivalents

which will give you:

{
    "Set Print Area" = "@$~^k";
}

and in case you can't find your app

defaults domains|perl -lpe "s/\, /\n/g"

will neatly list all the domains and

defaults find NSUserKeyEquivalents

will list all the user keybindings

Sources: