-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown2pdf.sh
executable file
·235 lines (193 loc) · 4.26 KB
/
markdown2pdf.sh
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash
set -e
msg(){ echo "$@" > /dev/stderr; }
usage(){
[ -n "$@" ] && (msg $@ ; echo)
cat <<EOF
usage:
$0 [OPTIONS] INPUT-FILENAME
-o, --output FILENAME
Filename for output pdf file
-g, --geometry GEOMETRY
Page geometry. Supported GEOMETRYs are "tight", "normal". (default "tight")
--layout LAYOUT
Page layout. Supported LAYOUTs are:
- "2x1" (two-up, side-by-side)
- "1x2"
- "normal" (default)
-2
alias of --layout 2x1 (2-up side-by-side layout)
--highlight-style STYLE
Style of syntax highlight. Supported STYLEs are "tango", "haddock", "kate", etc. a
See pandoc's manual for details.
--toc
Generate table of content
--config FILENAME
Filename for configration. (default: ~/.markdown2pdf)
EOF
exit 1
}
error(){ msg $@; exit 1; }
# Parse command line arguments
#
unset OUTPUT
unset INPUT
NumPerPage=1
highlightstyle="tango"
geometry="tight"
layout="normal"
unset enabletoc
configfile=${HOME}/.markdown2pdf
# Save command-line args
CMDARGS="$@"
# Parse command-line (1st stage)
while [ $# -ne 0 ]; do
case "$1" in
--config)
configfile="$2"
[ -f "$configfile" ] || error "No such config file, $configfile"
shift
;;
esac
shift
done
[ -f "$configfile" ] && source "$configfile"
# Parse command-line (2nd stage)
# Restore command-line args
set -- $CMDARGS
while [ $# -ne 0 ]; do
case "$1" in
--config)
shift
;;
-o|--output)
OUTPUT="$2"
shift
;;
-g|--geometry)
geometry="$2"
shift
;;
--layout)
layout="$2"
shift
;;
-2)
layout="2x1"
;;
--highlight-style)
highlightstyle="$2"
shift
;;
--help|-h)
usage
;;
--toc)
enabletoc=1
;;
-*)
usage "Error; bad option '$1' detected"
;;
*)
[ -n "$INPUT" ] && error "Error; too many arguments."
INPUT="$1"
;;
esac
shift
done
[ -n "$INPUT" ] || usage
if [ -z "$OUTPUT" ]; then
tmp=${INPUT/.txt/}
tmp=${tmp/.md/}
OUTPUT=${tmp}.pdf
unset tmp
fi
set +e
REQS="pandoc"
for exe in $REQS; do
type $exe > /dev/null 2>&1
if [ $? -ne 0 ]; then
msg "$exe is not installed. "
msg "You may use a package manager to install it, as follows:"
msg " sudo apt install $exe (debian, ubuntu)"
msg " sudo dnf install $exe (redhat, fedora, centos)"
msg " sudo port install $exe (macport)"
exit 1
fi
done
REQS="pdfjam lualatex"
for exe in $REQS; do
type $exe > /dev/null 2>&1
if [ $? -ne 0 ]; then
msg "$exe is not installed. "
msg "You may use a package manager to install it, as follows:"
msg " sudo tlmgr install $exe (TeXLive)"
msg " sudo apt install $exe (debian, ubuntu)"
msg " sudo dnf install $exe (redhat, fedora, centos)"
msg " sudo port install $exe (macport)"
exit 1
fi
done
set -e
TMPDIR=${TMPDIR=/tmp}
TMPFILTER=`mktemp $TMPDIR/filter-XXXXXXX.lua`
cat<<EOF > $TMPFILTER
function raw_tex (t)
return pandoc.RawBlock('tex', t)
end
--- Wrap code blocks in tcolorbox environments
function CodeBlock (cb)
return {raw_tex'\\\\begin{tcolorbox}', cb, raw_tex '\\\\end{tcolorbox}'}
end
--- Ensure that the longfbox package is loaded.
function Meta (m)
m['header-includes'] = {raw_tex '\\\\usepackage{tcolorbox}'}
return m
end
EOF
OPTS=""
if [ -n "$luatexjapresetoptions" ]; then
OPTS+=" -V luatexjapresetoptions=$luatexjapresetoptions"
fi
OPTS+=" -V colorlinks=true -V linkcolor=blue -V urlcolor=red -V toccolor=gray "
if [ -n "$enabletoc" ]; then
OPTS+=" --table-of-contents "
fi
case "$geometry" in
tight)
OPTS+=" -V geometry:margin=2cm -V geometry:top=1.5cm -V geometry:bottom=1.5cm "
;;
normal)
;;
*)
error "Bad arguments."
;;
esac
TMPFILE=`mktemp $TMPDIR/tmp-XXXXXXX.pdf`
pandoc -f markdown \
--pdf-engine=lualatex \
-V documentclass=ltjsarticle -V classoption=pandoc \
--highlight-style=$highlightstyle \
--lua-filter $TMPFILTER \
$OPTS \
-o $TMPFILE $INPUT
case "$layout" in
2x1)
pdfjam --nup 2x1 --landscape $TMPFILE --outfile $OUTPUT
;;
1x2)
pdfjam --nup 1x2 $TMPFILE --outfile $OUTPUT
;;
normal)
cp $TMPFILE $OUTPUT
;;
*)
error "Internal error"
;;
esac
rm $TMPFILE
rm $TMPFILTER
echo
echo
echo "Finished. Output was writtern to '$OUTPUT'."
exit 0