Skip to content

Instantly share code, notes, and snippets.

@jn0
Created June 2, 2021 13:07
Show Gist options
  • Save jn0/87f892ebce96e27b5c2571546570d9d6 to your computer and use it in GitHub Desktop.
Save jn0/87f892ebce96e27b5c2571546570d9d6 to your computer and use it in GitHub Desktop.
markdown to PDF converter (and filter)
#!/bin/bash
# sudo apt install them first:
# pandoc evince # pandoc + evince
# texlive-xetex # xelatex
# ttf-mscorefonts-installer # Georgia TTF
args=( "$@" )
tmpd="$(mktemp -p /tmp -d md2pdf.$$.XXXXXXXXXX.tmp)"
trap 'rm -rf "$tmpd"' EXIT
chmod g+s "$tmpd"
STDIN="$tmpd/STDIN.md"
[ -t 0 -o -t 1 ] || { args+=( "$STDIN" ); cat > "$STDIN"; }
for f in "${args[@]}"; do
[ "$f" = "$STDIN" ] && as_filter=yes || as_filter=no
pdf="${f%%.md}.pdf"
pt="$(grep -m1 '^#' "$f" | sed -e 's/^#\+\s\+//' -)"
[ -z "$pt" ] && pt="$(basename "${f%%.md}")"
[ "$as_filter" = yes ] && echo -n "[-] -> [-]" >&2 || echo -n "[$f] -> [$pdf]" >&2
echo " ($pt)" >&2
sed -e 's/\.md)/\.pdf)/g' "$f" |
pandoc --from=markdown_github+smart+yaml_metadata_block \
--metadata=pagetitle="$pt" \
--standalone \
--wrap=none \
--variable=linkcolor=blue \
--variable=fontsize=12pt \
--variable=mainfont=Georgia \
--pdf-engine=xelatex \
--output="$pdf"
[ "$as_filter" = yes ] && cat "$pdf"
done
# EOF #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment