2024-07-31 15:05:10 -05:00
|
|
|
#! /usr/bin/env nix-shell
|
2024-08-02 11:21:47 -05:00
|
|
|
#! nix-shell -p typst dmtx-utils nushell -i nu
|
|
|
|
|
|
|
|
def get-chunked-key [keyid?: string] {
|
|
|
|
let flags = [
|
|
|
|
"--export-secret-key", "--export-options", "export-minimal"
|
|
|
|
] | do { if keyid == null { $in } else { $in | append $keyid}}
|
|
|
|
let key = (gpg ...$flags)
|
|
|
|
let length = ($key | bytes length)
|
|
|
|
# 1555 is the maximum length in bytes split data evenly
|
|
|
|
let n = ($length / (($length / 1555)| math ceil)) | math ceil
|
|
|
|
0..$n..$length
|
|
|
|
| each {|i| $key | bytes at $i..<($i + $n)}
|
|
|
|
}
|
|
|
|
|
|
|
|
def generate-typst-doc [tmp: string, length: int] {
|
|
|
|
0..<$length
|
|
|
|
| each { |i|
|
|
|
|
let path = $tmp
|
|
|
|
| path join $"dmtx-($i).svg"
|
|
|
|
| path relative-to $env.PWD
|
|
|
|
|
|
|
|
['#image("', $path, '")'] | str join
|
|
|
|
}
|
|
|
|
| str join "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
def main [keyid?: string, outfile = "dmtxdata.pdf"] {
|
|
|
|
let tmpdir = mktemp -d -p . -t tmp.key-2-matrix-XXX
|
|
|
|
let byte_chunks = get-chunked-key $keyid
|
|
|
|
|
|
|
|
$byte_chunks | enumerate | each {|it|
|
|
|
|
$it.item | dmtxwrite -e 8 -o ($tmpdir | path join $"dmtx-($it.index).svg")
|
|
|
|
}
|
|
|
|
|
|
|
|
let doc = generate-typst-doc $tmpdir ($byte_chunks | length)
|
|
|
|
$doc | typst compile - $outfile
|
|
|
|
|
|
|
|
rm -r $tmpdir
|
|
|
|
}
|