-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmkhtml.sh
executable file
·338 lines (284 loc) · 11.7 KB
/
mkhtml.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/bin/bash
# =========================================================================== #
# THIS IS A FRAGILE SYSTEM, HANDLE WITH CARE. #
# =========================================================================== #
MAIN="Behind_The_Smart_World.mdsh"
HTML="__/html/komplett.html"
# --------------------------------------------------------------------------- #
# INTERACTIVE CHECKS (DISABLE IF YOU KNOW WHAT YOU'RE DOING ;))
# --------------------------------------------------------------------------- #
clear
if [ ! -f $MAIN ] ||
[ `echo "$HTML" | wc -c` -lt 2 ] ||
[ `echo "$HTML" | grep ".html$" | wc -l` -lt 1 ] ||
[ `echo "$MAIN" | wc -c` -lt 2 ]
then echo; echo "Define input and output file, please!"
echo "e.g. $0 input.mdsh output.html"; echo
exit 0;
fi
if [ -f $HTML ]; then
echo "$HTML does exist"
read -p "overwrite $HTML? [y/n] " ANSWER
if [ X$ANSWER != Xy ] ; then echo "BYE BYE!"; exit 1; fi
else
touch $HTML
fi
HTMLBASE=`realpath $HTML | rev | cut -d "/" -f 2- | rev`
HTMLSRCDIRNAME="_"
HTMLSRCDIR="$HTMLBASE/$HTMLSRCDIRNAME"
if [ ! -d $HTMLSRCDIR ]; then
echo
echo "$HTMLSRCDIR does not exist"
read -p "create $HTMLSRCDIR? [y/n] " ANSWER
if [ X$ANSWER == Xy ] ; then
echo "creating $HTMLSRCDIR"; mkdir $HTMLSRCDIR
else echo "BYE BYE!"; exit 1; fi
fi
# --------------------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
TMPDIR=. ; TMPID=$TMPDIR/TMP`date +%Y%m%H``echo $RANDOM | cut -c 1-4`
SRCDUMP=${TMPID}.maindump
REFURL="http://freeze.sh/etherpad/export/_/references.bib"
HEADMARK="= FROM MDSH START ="
FOOTMARK="= FROM MDSH END ="
SELECTLINES="tee" # OBSOLETE ?
# =========================================================================== #
# CONFIGURATION #
# --------------------------------------------------------------------------- #
# INCLUDE/COMBINE FUNCTIONS
# --------------------------------------------------------------------------- #
FUNCTIONSBASIC=lib/sh/201602_basic.functions
FUNCTIONSPLUS=lib/sh/160220_html.functions
FUNCTIONS=$TMPID.functions
cat $FUNCTIONSBASIC $FUNCTIONSPLUS > $FUNCTIONS
source $FUNCTIONS
# =========================================================================== #
# DEFINITIONS SPECIFIC TO OUTPUT
# --------------------------------------------------------------------------- #
PANDOCACTION="pandoc --ascii -r markdown -w html"
# --------------------------------------------------------------------------- #
# FOOTNOTES [^]{the end is near, the text is here}
# ---------
FOOTNOTEOPEN="FOOTNOTEOPEN$RANDOM{" ; FOOTNOTECLOSE="}FOOTNOTECLOSE$RANDOM"
# --------------------------------------------------------------------------- #
# CITATIONS [@xx:0000:aa] / [@[p.44]xx:0000:aa]
# ---------
CITEOPEN="CITEOPEN$RANDOM" ; CITECLOSE="CITECLOSE$RANDOM"
CITEPOPEN="$CITEOPEN" ; CITEPCLOSE="CITEPCLOSE$RANDOM"
# --------------------------------------------------------------------------- #
# COMMENT
# -------
COMSTART='<!--'; COMCLOSE='-->'
# =========================================================================== #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# --------------------------------------------------------------------------- #
# ACTION STARTS HERE!
# --------------------------------------------------------------------------- #
mdsh2src $MAIN
# --------------------------------------------------------------------------- #
# PROCESS ALL HTML
# --------------------------------------------------------------------------- #
# http://tidy.sourceforge.net/docs/quickref.html#show-body-only
echo "show-body-only: y" > tc.txt
echo "wrap-attributes: n" >> tc.txt
echo "quiet:y" >> tc.txt
echo "vertical-space:y" >> tc.txt
# COPY MAINHTML TO MATCH SRCDUMP (AS SPLITDUMPS DO)
MAINHTML="$HTML"
cp $HTML `echo $SRCDUMP | sed 's/\.[a-z]*$/.html/'`
# GET REFERENCE FILE
BIBTMP=${TMPID}.bibtmp
wget --no-check-certificate \
-q -O - $REFURL | #
sed 's/@movie/@misc/g' | #
bib2xml | #
sed ":a;N;\$!ba;s/\n//g" | #
sed 's/>[ ]*</></g' | #
sed 's/<mods /\n&/g' | #
sed 's/<\/mods>/&\n/g' | #
tee > $BIBTMP
for THISDUMP in $SRCDUMP `cat ${TMPID}.splitlist | sort -u`
do
HTML=`echo $THISDUMP | sed 's/\.[a-z]*$/.html/'`
# ------------------------------------------------------------------------- #
# MAKE CITATIONS
# ------------------------------------------------------------------------- #
for CITATION in `sed "s/$CITEOPEN/\n&/g" $THISDUMP | #
sed "s/$CITECLOSE/&\n/g" | #
grep "^$CITEOPEN"`
do
CITEKEY=`echo $CITATION | #
sed "s/$CITECLOSE//g" | #
sed "s/$CITEOPEN//g" | #
sed "s/^.*$CITEPCLOSE//"`
OPTIONAL=`echo $CITATION | #
grep ${CITEPCLOSE} | #
sed "s/$CITEOPEN//g" | #
sed "s/${CITEPCLOSE}.*$//"`
LINENUM=`grep -n $CITATION $THISDUMP | #
head -n 1 | #
cut -d ":" -f 1`
if [ "X$CITEKEY" != "X$LASTKEY" ]; then
COLLECTNAMES="";TITLE=""
BIBENTRY=`grep $CITEKEY $BIBTMP`
XID="<genre"
GENRE=`echo $BIBENTRY | sed "s/$XID/\n&/g" | #
grep "^$XID" | head -n 1 | #
cut -d ">" -f 2 | cut -d "<" -f 1` #
XID="<title>"
TITLE=`echo $BIBENTRY | sed "s/$XID/\n&/g" | #
grep "^$XID" | head -n 1 | #
cut -d ">" -f 2 | cut -d "<" -f 1` #
XID="<subTitle>"
STITLE=`echo $BIBENTRY | sed "s/$XID/\n&/g" | #
grep "^$XID" | head -n 1 | #
cut -d ">" -f 2 | cut -d "<" -f 1` #
if [ `echo $STITLE | wc -c` -gt 2 ]; then
TITLE="${TITLE}: ${STITLE}"
else
TITLE="$TITLE"
fi
if [ `echo $TITLE | wc -c` -gt 2 ]; then
TITLE="<span class=\"title\">$TITLE</span>, "
else
TITLE=""
fi
XID="<dateIssued>"
DATE=`echo $BIBENTRY | sed "s/$XID/\n&/g" | #
grep "^$XID" | head -n 1 | #
cut -d ">" -f 2 | cut -d "<" -f 1` #
if [ `echo $DATE | wc -c` -gt 2 ]; then
DATE="<span class=\"date\">${DATE}</span>"
else
DATE=""
fi
XID="<publisher>"
PUBLISHER=`echo $BIBENTRY | sed "s/$XID/\n&/g" | #
grep "^$XID" | head -n 1 | #
cut -d ">" -f 2 | cut -d "<" -f 1` #
if [ `echo $PUBLISHER | wc -c` -gt 2 ]; then
PUBLISHER="<span class=\"publisher\">${PUBLISHER}</span>, "
else
PUBLISHER=""
fi
XID="<placeTerm type=\"text\""
PLACE=`echo $BIBENTRY | sed "s/$XID/\n&/g" | #
grep "^$XID" | head -n 1 | #
cut -d ">" -f 2 | cut -d "<" -f 1` #
if [ `echo $PLACE | wc -c` -gt 2 ]; then
PLACE="<span class=\"place\">${PLACE}</span>, "
else
PLACE=""
fi
K=$RANDOM ; L=`echo $K | rev`
XID="<name type=\"personal\""
for PERSONS in `echo $BIBENTRY | #
sed "s/$XID/\n&/g" | #
grep "^$XID" | #
sed 's/ /5pA4c3e/g'`
do
PERSONS=`echo $PERSONS | sed 's/5pA4c3e/ /g'`
XID="<namePart type=\"given\""
GNAME=`echo $PERSONS | sed "s/$XID/\n&/g" | #
grep "^$XID" | head -n 4 | #
cut -d ">" -f 2 | cut -d "<" -f 1` #
XID="<namePart type=\"family\""
FNAME=`echo $PERSONS | sed "s/$XID/\n&/g" | #
grep "^$XID" | head -n 4 | #
cut -d ">" -f 2 | cut -d "<" -f 1` #
COLLECTNAMES="${COLLECTNAMES}${K} <span class=\"fname\">${FNAME}</span>,\
<span class=\"gname\">${GNAME}</span>"
done
COLLECTNAMES=`echo $COLLECTNAMES | #
sed "s/^$K //" | #
rev | sed "s/$L/ dna /" | #
rev | sed "s/$K/,/g"`
CITEINSERT=`echo "$FOOTNOTEOPEN<span class=\"$GENRE\">\
${COLLECTNAMES}: ${TITLE}${PUBLISHER}${PLACE}${DATE}. \
$OPTIONAL \
</span>$FOOTNOTECLOSE" | #
sed 's/>[ ]*</></g' | #
tr -s ' ' | #
sed 's/"/\\\\"/g' | #
sed 's/&/\\\\&/g' | #
sed 's/\\//\\\\\//g' | #
sed 's/]/\\\\]/g' | #
sed 's/\[/\\\\\[/g'` #
sed -i "${LINENUM}s/$CITATION/$CITEINSERT/" $THISDUMP
else
CITEINSERT="$FOOTNOTEOPEN<span class=\"ibid\">ibid. $OPTIONAL<\/span>$FOOTNOTECLOSE"
sed -i "${LINENUM}s/$CITATION/$CITEINSERT/" $THISDUMP
fi
LASTKEY="$CITEKEY"
done
# ------------------------------------------------------------------------- #
# MAKE FOOTNOTES
# ------------------------------------------------------------------------- #
( IFS=$'\n' ; COUNT=1
for FOOTNOTE in `sed "s/$FOOTNOTEOPEN/\n&/g" $THISDUMP | #
sed "s/$FOOTNOTECLOSE/&\n/" | #
grep "^$FOOTNOTEOPEN"`
do
if [ $COUNT -eq 1 ]; then
echo "<div class=\"footnotes\">" >> $THISDUMP
echo "<ol>" >> $THISDUMP
FOOTNOTEBLOCKSTARTED="YES"
fi
LNUM=`grep -n $FOOTNOTE $THISDUMP | head -n 1 | cut -d ":" -f 1`
FOOTNOTETXT=`echo $FOOTNOTE | #
cut -d "{" -f 2 | #
cut -d "}" -f 1` #
ID=`echo $FOOTNOTETXT | md5sum | #
cut -c 1-8` #
FOOTNOTE=`echo $FOOTNOTE | #
sed 's/\[/\\\[/g' | #
sed 's/|/\\|/g'`
OLDFOOTNOTE=$FOOTNOTE
NEWFOOTNOTE="<sup><a href=\"#$ID\">$COUNT</a><\/sup>"
sed -i "$((LNUM))s|$OLDFOOTNOTE|$NEWFOOTNOTE|" $THISDUMP
echo "<li id=\"$ID\"> $FOOTNOTETXT </li>" >> $THISDUMP
COUNT=`expr $COUNT + 1`
done
if [ "X${FOOTNOTEBLOCKSTARTED}" == "XYES" ]; then
echo "</ol>" >> $THISDUMP
echo "</div>" >> $THISDUMP
sed -i "s|$FOOTNOTECLOSE||g" $THISDUMP # WORKAROUND (BUG!!)
fi
)
# --------------------------------------------------------------------------- #
tac $HTML | #
sed -n "/<!--.*${HEADMARK}.*-->$/,\$p" | #
tac > ${HTML}.tmp
echo "" >> ${HTML}.tmp
cat $THISDUMP | #
sed "s/[ \t]*$APND//g" | # DOUBLED FROM FUNCTIONS (BUG?!)
sed "s/[ \t]*$ESC//g" | # DOUBLED FROM FUNCTIONS (BUG?!)
tidy -config tc.txt | #
sed -e :a \
-e '$!N;s/=[ ]*\n"/="/;ta' \
-e 'P;D' | # REAPPEND UNTIDY TIDY BREAK (=")
tee >> ${HTML}.tmp
sed -n "/<!--.*${FOOTMARK}.*-->$/,\$p" ${HTML} >> ${HTML}.tmp
mv ${HTML}.tmp $HTML
# BUG: INCLUDES INTERPUNCTUATION
sed -i '/^<!-- /!s,\([>]*\)\(http.\?://[^ <]*\),\1<a href="\2" class="linkify">\2</a>,g' $HTML
rm $THISDUMP
done
mv `echo $SRCDUMP | sed 's/\.[a-z]*$/.html/'` $MAINHTML
# =========================================================================== #
# CLEAN UP
# SHOULD HAVE rmif FUNCTION
rm tc.txt
rm ${TMPID}*.[1-9]*.*
rm ${TMPID}*.functions
rm ${TMPID}*.included
rm ${TMPID}*.tmp
rm ${TMPID}*.txt
rm ${TMPID}*.pdf
rm ${TMPID}*.png
rm ${TMPID}*.wget
rm ${TMPID}*.*list
#rm ${TMPID}bib*
rm ${TMPID}.bibtmp
rm ${TMPID}*.fid
exit 0;