-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmd2html2pdf
executable file
·58 lines (50 loc) · 1.36 KB
/
md2html2pdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env sh
# vim: ft=sh ts=4 sw=4 sts=4 et :
md2html2pdf() {
pandoc \
--katex \
--pdf-engine="$PDF_ENGINE" \
-V margin-top=10 \
-V margin-bottom=10 \
-V margin-left=8 \
-V margin-right=8 \
--css ~/.pandoc/css/paper.css \
-f markdown-implicit_figures \
-t pdf \
"$1" \
-o "${1%.md}.pdf"
}
main() {
PDF_ENGINE="wkhtmltopdf"
while getopts "lxmhp" opt; do
case $opt in
l) PDF_ENGINE="lualatex" ;;
x) PDF_ENGINE="xelatex" ;;
m) PDF_ENGINE="latexmk" ;;
h) PDF_ENGINE="wkhtmltopdf" ;;
p) PDF_ENGINE="pdflatex" ;;
?)
printf "Usage: %s [-l|-x|-m|-h|-p] files\n" "$(basename "$0")" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
[ $# -eq 0 ] && {
echo "Error: no input files" >&2
exit 1
}
for file in "$@"; do
oldpwd=$PWD
# Pictures in markdown cannot be fetched if we are not in the same
# directory
cd "$(dirname $(realpath "$file"))" >/dev/null 2>&1 || exit 1
if ! md2html2pdf "$(basename "$file")"; then
echo "md2html2pdf: error: pandoc failed to convert '$file'" >&2
cd "$oldpwd" >/dev/null 2>&1
exit 1
fi
cd "$oldpwd" >/dev/null 2>&1
done
}
main "$@"