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
|
|
|
|
|
2024-08-05 12:14:24 -05:00
|
|
|
def append-if [cond: bool, item: any] {
|
|
|
|
if $cond { $in | append $item } else { $in }
|
|
|
|
}
|
|
|
|
|
2024-08-02 11:21:47 -05:00
|
|
|
def get-chunked-key [keyid?: string] {
|
|
|
|
let flags = [
|
|
|
|
"--export-secret-key", "--export-options", "export-minimal"
|
2024-08-05 12:14:24 -05:00
|
|
|
] | append-if ($keyid != null) $keyid
|
2024-08-02 11:21:47 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2024-08-05 12:14:24 -05:00
|
|
|
let doc = generate-typst-doc $tmpdir ($byte_chunks | length)
|
2024-08-02 11:21:47 -05:00
|
|
|
$doc | typst compile - $outfile
|
|
|
|
|
|
|
|
rm -r $tmpdir
|
|
|
|
}
|