-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmd2pdf
executable file
·112 lines (102 loc) · 2.71 KB
/
md2pdf
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env sh
# vim: ft=sh ts=4 sw=4 sts=4 et :
# Get absolute path without using realpath/readlink
abs_path() {
case "$1" in
/*) printf '%s\n' "$1" ;;
*) printf '%s/%s\n' "$PWD" "$1" ;;
esac
}
md2pdf() {
pandoc \
--lua-filter="${HOME}/.pandoc/md2pdf_filter.lua" \
--pdf-engine="$PDF_ENGINE" \
-V block-headings \
-V colorlinks=true \
-V CJKmainfont="Source Han Serif CN" \
-V geometry:margin=1in \
-f markdown-implicit_figures \
-t pdf \
"$1" \
-o "${1%.md}.pdf"
}
usage() {
printf "Usage: %s [OPTIONS] files\n" "$(basename "$0")"
printf "Options:\n"
printf " -l, --lualatex Use lualatex engine\n"
printf " -x, --xelatex Use xelatex engine\n"
printf " -m, --latexmk Use latexmk engine\n"
printf " -w, --wkhtmltopdf Use wkhtmltopdf engine\n"
printf " -p, --pdflatex Use pdflatex engine (default)\n"
printf " --pdf-engine=NAME Use specified PDF engine\n"
printf " -h, --help Show this help message\n"
}
main() {
PDF_ENGINE="pdflatex"
while [ $# -gt 0 ]; do
case $1 in
-l | --lualatex)
PDF_ENGINE="lualatex"
;;
-x | --xelatex)
PDF_ENGINE="xelatex"
;;
-m | --latexmk)
PDF_ENGINE="latexmk"
;;
-w | --wkhtmltopdf)
PDF_ENGINE="wkhtmltopdf"
;;
-p | --pdflatex)
PDF_ENGINE="pdflatex"
;;
-h | --help | -\?)
usage
exit 0
;;
--pdf-engine=*)
PDF_ENGINE="${1#*=}"
;;
--pdf-engine)
if [ -z "$2" ]; then
echo "Error: --pdf-engine requires an argument" >&2
exit 1
fi
PDF_ENGINE="$2"
shift
;;
--)
shift
break
;;
-*)
echo "Error: unknown option '$1'" >&2
usage
exit 1
;;
*)
break
;;
esac
shift
done
if [ $# -eq 0 ]; then
echo "Error: no input files" >&2
usage
exit 1
fi
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 ! md2pdf "$(basename "$file")"; then
rc=$?
echo "md2pdf: error: pandoc failed to convert '$file' with exit code $rc" >&2
cd "$oldpwd" >/dev/null 2>&1
exit $rc
fi
cd "$oldpwd" >/dev/null 2>&1
done
}
main "$@"