-
Notifications
You must be signed in to change notification settings - Fork 4
/
convert.sh
270 lines (244 loc) · 10.6 KB
/
convert.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# $1 = path to unique session directory
# $2 = path to selected stylesheet
# $3 = html extension, lowercase because that's what's typically used for extensions
# $4 = pdf extension
# $5 = epub3 extension
# $6 = docx extension
# $7 = stand-alone option
# Move into unique directory created for a given session
cd $1
echo "PID: $$" > debug.txt
echo $1 >> debug.txt
echo $2 >> debug.txt
echo $3 >> debug.txt
echo $4 >> debug.txt
echo $5 >> debug.txt
echo $6 >> debug.txt
echo $7 >> debug.txt
# If a .zip file is present in unique directory, unzip it. Otherwise move on
# to conversion.
if [ -e *.zip ]
then
archive="$(ls *.zip)"
unzip $archive
dir="${archive%.zip}"
echo "$(pwd)" >> debug.txt
echo $archive >> debug.txt
echo $dir >> debug.txt
echo "__MACOSX ----" >> debug.txt
echo "$(ls __MACOSX)" >> debug.txt
rm -rf __MACOSX
echo "dir/.* ----" >> debug.txt
echo "$(ls $dir/.*)" >> debug.txt
rm -rf $dir/.*
echo "dir/* ----" >> debug.txt
echo "$(ls $dir/*)" >> debug.txt
mv $dir/* .
echo "ls before----" >> debug.txt
echo "$(ls)" >> debug.txt
echo "----" >> debug.txt
rm -rf $dir
rm -rf $archive
echo "ls after----" >> debug.txt
echo "$(ls)" >> debug.txt
echo "----" >> debug.txt
fi
# If an example stylesheet has been selected, remove all styleshees in favor of that which is selected.
#echo $2 >> debug.txt
#echo "'"$(ls *.css)"'" >> debug.txt
if [ $2 == "custom" ] && [ "$(ls *.css)" == "" ];
then
#echo "Not using example, no custom style provided" >> debug.txt # Working
example="0"
custom="0"
stylesheet=""
persistentStylesheet=$stylesheet
fi
if [ $2 == "custom" ] && [ "$(ls *.css)" != "" ];
then
#echo "Not using example, custom style provided" >> debug.txt # Working
example="0"
custom="1"
stylesheet=$(ls *.css | head -1)
persistentStylesheet=$stylesheet
fi
if [ $2 != "custom" ] && [ "$(ls *.css)" != "" ];
then
#echo "Using example, custom style provided" >> debug.txt # Working
example="1"
custom="1"
rm *.css # Override provided stylesheet in favor of example stylesheet
cp ../../$2 stylesheet.css
stylesheet=$(ls *.css | head -1)
persistentStylesheet=$stylesheet
fi
if [ $2 != "custom" ] && [ "$(ls *.css)" == "" ]; # If something other than default state is selected, do this . . .
then
#echo "Using Example, no custom style provided" >> debug.txt # Working
example="1"
custom="0"
rm *.css # If an example stylesheet has been selected, remove all uploaded stylesheets
cp ../../$2 stylesheet.css
stylesheet=$(ls *.css | head -1)
persistentStylesheet=$stylesheet
fi
# Repeat conversion for each of the selected output formats passed in on variables $3 - $5.
for output in $3 $4 $5 $6
do
# Convert all .md files found in directory $1.
# Apply only the first .css file returned by ls. Pandoc only allows 1 .css
# file. Others will be ignored.
# User should condense multiple stylesheets into one as a programatic approach
# would willy nilly overwrite CSS attributes.
for filename in *.md
do
filename=$(echo $filename | cut -f 1 -d '.')
# HTML conversion # Working 1/8/17 1:30pm
stylesheet=$persistentStylesheet
if [ $output == "html" ];
then
if [ $example == "0" ] && [ $custom == "0" ];
then
#echo "HTML Not using example, no custom style provided" >> debug.txt # Working
stylesheet="" # Working
fi
if [ $example == "0" ] && [ $custom == "1" ];
then
#echo "HTML Not using example, custom style provided" >> debug.txt # Working
stylesheet="-c $stylesheet" # Working
fi
if [ $example == "1" ] && [ $custom == "0" ];
then
#echo "HTML Using Example, no custom style provided" >> debug.txt # Working
stylesheet="-c $stylesheet" # Working
fi
if [ $example == "1" ] && [ $custom == "1" ];
then
#echo "HTML Using example, custom style provided" >> debug.txt # Working
stylesheet="-c $stylesheet" # Working
fi
echo "HTML Conversion ----------------------" >> debug.txt
#echo $stylesheet >> debug.txt
if [ $7 == "null" ];
then
# if not creating stand-alone pages, go ahead and use custom stylesheet
# if provided
pandoc $filename.md -f markdown $stylesheet --mathjax -s -o $filename.html 2>>debug.txt
else
# if creating stand-alone pages, ignore all stylesheets. These should
# be provided in the provided header
pandoc $filename.md -f markdown -o $filename.html 2>>debug.txt
fi
fi
# end HTML conversion ---------------------
# PDF conversion # Working 1/8/17 10:27pm
stylesheet=$persistentStylesheet
if [ $output == "pdf" ];
then
if [ $example == "0" ] && [ $custom == "0" ];
then
#echo "PDF Not using example, no custom style provided" >> debug.txt # Working
stylesheet="" # Working
fi
if [ $example == "0" ] && [ $custom == "1" ];
then
#echo "PDF Not using example, custom style provided" >> debug.txt # Working
stylesheet="-c $stylesheet" # Working, problem with font. Maybe font needs to be embedded? test condition: skeleton.css
fi
if [ $example == "1" ] && [ $custom == "0" ];
then
#echo "PDF Using Example, no custom style provided" >> debug.txt # Working
stylesheet="-c $stylesheet" # Working
fi
if [ $example == "1" ] && [ $custom == "1" ];
then
#echo "PDF Using example, custom style provided" >> debug.txt # Working
stylesheet="-c $stylesheet" # Working
fi
echo "temp HTML Conversion -----------------" >> debug.txt
#echo $stylesheet >> debug.txt
pandoc $filename.md -f markdown $stylesheet --mathjax -s -o temp.html 2>>debug.txt
# --run-script removes letter-spacing from the most common text tags.
# WKHTMLTOPDF has a known error that causes anything other than
# letter-spacing of 0px to be extremely exaggerated. This script sets
# letter-spacing to 0px for most common text tags.
if [ $OSTYPE == "linux-gnu" ];
then
echo "PDF Conversion -----------------------" >> debug.txt
xvfb-run -a wkhtmltopdf --quiet --javascript-delay 1000 --user-style-sheet ../../print.css --run-script 'var elements = document.querySelectorAll("html,body,h1,h2,h3,h4,h5,h6,p,li,ol,pre,b,i,code,q,s"); for(var i = 0; i < elements.length; i++) { elements[i].style.letterSpacing = "0px"; }' temp.html $filename.pdf 2>>debug.txt
else
echo "PDF Conversion -----------------------" >> debug.txt
wkhtmltopdf --quiet --javascript-delay 1000 --user-style-sheet ../../print.css --run-script 'var elements = document.querySelectorAll("html,body,h1,h2,h3,h4,h5,h6,p,li,ol,pre,b,i,code,q,s"); for(var i = 0; i < elements.length; i++) { elements[i].style.letterSpacing = "0px"; }' temp.html $filename.pdf 2>>debug.txt
fi
rm temp.html
fi
# end HTML conversion ---------------------
# EPUB conversion # Working 1/8/17 11:25pm
stylesheet=$persistentStylesheet
if [ $output == "epub3" ];
then
if [ $example == "0" ] && [ $custom == "0" ];
then
#echo "EPUB Not using example, no custom style provided" >> debug.txt # Working
stylesheet="" # Working
fi
if [ $example == "0" ] && [ $custom == "1" ];
then
#echo "EPUB Not using example, custom style provided" >> debug.txt # Working
stylesheet="--epub-stylesheet $stylesheet" # Working
fi
if [ $example == "1" ] && [ $custom == "0" ];
then
#echo "EPUB Using Example, no custom style provided" >> debug.txt # Working
stylesheet="--epub-stylesheet $stylesheet" # Working
fi
if [ $example == "1" ] && [ $custom == "1" ];
then
#echo "EPUB Using example, custom style provided" >> debug.txt # Working
stylesheet="--epub-stylesheet $stylesheet" # Working
fi
echo "EPUB Conversion ----------------------" >> debug.txt
#echo $stylesheet >> debug.txt
pandoc $filename.md -f markdown -t epub3 $stylesheet -o $filename.epub 2>>debug.txt
fi
# end EPUB conversion ---------------------
# DOCX conversion # Working 1/8/17 11:38pm
if [ $output == "docx" ];
then
echo "DOCX Conversion ----------------------" >> debug.txt
pandoc $filename.md -f markdown -o $filename.docx 2>>debug.txt # Working
fi
# end EPUB conversion ---------------------
done
done
# stand-alone generation ---------------------
if [ $7 == "stand-alone" ];
then
echo "Stand-Alone Generation ----------------------" >> debug.txt
if [ -a head.html ];
then
echo "head.html found!" >> debug.txt
else
echo "head.html not found! If you'd like to indlude a header, add a file named tail.html containing the header's contents" >> debug.txt
fi
if [ -a tail.html ];
then
echo "tail.html found!" >> debug.txt
else
echo "tail.html not found! If you'd like to indlude a footer, add a file named tail.html containing the footer's contents" >> debug.txt
fi
mkdir stand-alone/
for html_file in *.html
do
if [ $html_file == "head.html" ] || [ $html_file == "tail.html" ];
then
: # do nothing
else
cat head.html $html_file tail.html > stand-alone/$html_file
rm $html_file
fi
done
fi
# end stand-alone generation ---------------------
# zip all files in working directory into an archive excluding any .zip files
zip -r converted.zip * -x *.zip