Skip to content

Instantly share code, notes, and snippets.

@EvanEdwards
Last active January 27, 2024 01:43
Show Gist options
  • Save EvanEdwards/a65ea29f2d86fbef9341d15e0c05399a to your computer and use it in GitHub Desktop.
Save EvanEdwards/a65ea29f2d86fbef9341d15e0c05399a to your computer and use it in GitHub Desktop.
Convert pandoc template to handlebars (partial)
#!/bin/bash
if [ $# -lt 1 ] || [ "$1" == "--help" ] || [ "$1" == "-h" ]
then
echo "pandoc2handlebars.sh FILE [FILE...]
Converts (partially) the given Pandoc templates to Handlebars files.
Hand edit the \$for(var)\$ and other unconverted bits.
"
exit 1
fi
for FN in "$@"
do
# Verify or skip
[ -f "$FN" ] || echo "WARNING: '$FN' is not a file. (skipping)" 1>&2
[ -f "$FN" ] || continue
[ -f "${FN}.handlebars" ] && echo "WARNING: Refusing to overwrite '${FN}.handlebars' (skipping)" 1>&2
[ -f "${FN}.handlebars" ] && continue
sed -E 's/\$endif\$/{{\/if}}/g;s/\$if\(([^\)]*)\)\$/{{#if \1}}/g;s/\$([A-Za-z][A-Za-z0-9.]*)\$/{{\1}}/g' <"$FN" >"${FN}.handebars"
done
@EvanEdwards
Copy link
Author

This handles a good chunk of the simple textual conversion, but doesn't convert $for(var)$ because Handlebars uses {{this}}, while Pandoc uses var.property. I didn't need to mass convert, so I just used this and hand edited the remainder.

Might could use some whitespace handling, but I have never seen whitespace in $pandoc.vars$ and am not even sure they are valid. This is a practical sed quickie.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment