-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxpandoc.sh
executable file
·176 lines (156 loc) · 3.83 KB
/
xpandoc.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
#!/bin/bash
#
# Powerful pandoc markdown converter (markdown ==> html, pdf)
#
# Author: OuyangXY <[email protected]>
# pax <[email protected]>
usage() {
cat <<EOF
Usage: xpandoc [options] [markdown_files]...
OPTIONS:
-C CORE_CSS core css file path
-S SIDEBAR_CSS sidebar css file path
-A ACCON_CSS accondion css file path
-t toc style 0: no toc, 1: regular toc, 2: sidebar toc (default)
-p pure html without any css
-n output html without number sections
-s output html + javescript slide presentation
By default, xpandoc converts pandoc's markdown files to
html and pdf files embedded a sidebar.
EOF
exit 1
}
ERR=0
CORE_CSS=~/.pandoc/css/t.css
SIDEBAR_CSS=~/.pandoc/css/t_sidebar.css
ACCON_CSS=~/.pandoc/css/t_accondion.css
TEMPLATE="--template=t.html"
TOC="--toc"
TOC_STYLE=2
PURE=0
NUMBER_SECTIONS="--number-sections"
HIGHLIGHT_STYLE="--highlight-style=haddock"
STANDALONE=""
SELF_CONTAINED="--self-contained"
TO="html"
SLIDE_SHOW=0
while getopts ":C:S:A:t:pns" OPTION
do
case $OPTION in
C ) CORE_CSS="$OPTARG";;
S ) SIDEBAR_CSS="$OPTARG";;
A ) ACCON_CSS="$OPTARG";;
t ) TOC_STYLE="$OPTARG";;
p ) PURE=1;;
n ) NUMBER_SECTIONS="";;
s ) SLIDE_SHOW=1;;
* ) usage
esac
done
if [[ -z "$CORE_CSS" ]]; then
CSS_C=""
else
CSS_C="-c "$CORE_CSS""
fi
if [[ -z "$SIDEBAR_CSS" ]]; then
CSS_S=""
else
CSS_S="-c "$SIDEBAR_CSS""
fi
if [[ -z "$ACCON_CSS" ]]; then
CSS_A=""
else
CSS_A="-c "$ACCON_CSS""
fi
if [[ $TOC_STYLE == 0 ]]; then
CSS_S=""
TOC=""
elif [[ $TOC_STYLE == 1 ]]; then
CSS_S=""
elif [[ $TOC_STYLE == 2 ]]; then
echo "--toc" > /dev/null
else
echo "*** arg invalid" && usage
fi
if [[ $PURE == 1 ]]; then
CSS_C=""
CSS_S=""
CSS_A=""
fi
if [[ $SLIDE_SHOW == 1 ]]; then
TO="slidy"
CSS_C=""
CSS_S=""
CSS_A=""
TEMPLATE=""
STANDALONE="-s"
SELF_CONTAINED=""
fi
shift $(( $OPTIND - 1 ))
[ -z "$1" ] && echo "*** need markdown files" && usage
for arg in "$@"; do
echo "### converting "$arg""
name=${arg%.*}
# convert to html
# cmd: pandoc xxx -t html -o xxx.html
echo ""
echo "pandoc \
-f markdown+ignore_line_breaks \
-t "$TO" \
"$arg" \
-o "$name".html \
"$CSS_C" \
"$CSS_S" \
"$CSS_A" \
"$TOC" \
"$TEMPLATE" \
"$NUMBER_SECTIONS" \
"$HIGHLIGHT_STYLE" \
"$STANDALONE" \
"$SELF_CONTAINED | tee /tmp/xpandoc.cmd && sh < /tmp/xpandoc.cmd
echo ""
ERR=$?
rm -f /tmp/xpandoc.cmd
if [[ $ERR == 0 ]]; then
echo ">>> output: "$name".html"
else
echo ">>> output: failed to create "$name".html!"
exit 1
fi
# slide show doesn't need convert to pdf
if [[ ! $SLIDE_SHOW == 1 ]]; then
if command -v wkhtmltopdf > /dev/null 2>&1; then
# convert to pdf (via wkhtmltopdf or wkhtmltox if installed)
# cmd: wkhtmltopdf xxx.html xxx.pdf
echo ""
echo "wkhtmltopdf --page-size A4 -T 15 -R 15 -B 15 -L 15 "$name".html "$name".pdf" | tee /tmp/xpandoc.cmd && sh < /tmp/xpandoc.cmd
echo ""
ERR=$?
rm -f /tmp/xpandoc.cmd
if [ $ERR == 0 ]; then
echo ">>> output: "$name".pdf"
else
echo ">>> output: failed to create "$name".pdf!"
exit 1
fi
else
if command -v latex > /dev/null 2>&1 && command -v /usr/bin/xelatex > /dev/null 2>&1; then
# convert to pdf (via LaTeX if installed)
# cmd: pandoc xxx -t latex -o xxx.pdf
echo ""
echo "pandoc -t latex --latex-engine=xelatex -V mainfont=WenQuanYi\ Micro\ Hei\ Mono -V papersize=A4 -V geometry:margin=1.5cm "$name".html -o "$name".pdf" | tee /tmp/xpandoc.cmd && sh < /tmp/xpandoc.cmd
echo ""
ERR=$?
rm -f /tmp/xpandoc.cmd
if [ $ERR == 0 ]; then
echo ">>> output: "$name".pdf"
else
echo ">>> output: failed to create "$name".pdf!"
exit 1
fi
else
echo "*** dependence error: install wkhtmltopdf or latex to support convert to PDF."
fi
fi
fi
done