-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml-gen.sh
548 lines (523 loc) · 17.6 KB
/
html-gen.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
blog_url=`git config --get pangitive.blog-url`
if [ "$blog_url" = "" ]; then
echo -n "[pangitive] WARNING: git config pangitive.blog-url is empty and "
echo "should not be, please set it as soon as possible."
fi
blog_owner=`git config --get pangitive.blog-owner`
blog_title=`git config --get pangitive.blog-title`
blog_years=`git log --format='%ai' | \
sed -n '1{s/-.*//;h};${G;s/\(-.*\)\?\n/−/;s/^\(.*\)−\1$/\1/;p}'`
templates_dir=`git config --get pangitive.templates-dir`
public_dir=`git config --get pangitive.public-dir`
if [ ! -d "$public_dir" ]; then mkdir -p "$public_dir"; fi
articles_dir=`git config --get pangitive.articles-dir`
pages_dir=`git config --get pangitive.pages-dir`
pandoc=`git config --get pangitive.pandoc`
pandoc_opt=`git config --get pangitive.pandoc-options`
if [ ! -f "$pandoc" -o ! -x "$pandoc" ]; then
echo "Cannot access pandoc executable. Aborting." && exit
fi
# If template changed, regenerate all articles
tpl_change=`echo "$added_files" "$modified_files" "$deleted_files" | \
grep -c "$templates_dir/"`
if [ "$tpl_change" -gt 0 ]; then
first=`git log --format="%H" --reverse | head -1`
modified_files=`git log $first..HEAD^ --name-status --pretty="format:" | \
grep -E '^A' | cut -f2 | sort | uniq`
deleted_files=
tmpust=`mktemp pangitiveXXXXXX`
tmpart=`mktemp pangitiveXXXXXX`
tmpmod=`mktemp pangitiveXXXXXX`
ls "$articles_dir"/* > "$tmpust"
ls "$pages_dir"/* >> "$tmpust"
sort "$tmpust" > "$tmpart"
echo "$modified_files" | tr " " "\n" > "$tmpmod"
modified_files=`comm -12 --nocheck-order "$tmpmod" "$tmpart"`
rm "$tmpust" "$tmpart" "$tmpmod"
echo "[pangitive] Templates changed, regenerating everything..."
fi
generated_files=`mktemp pangitiveXXXXXX`
# List of articles (base names), sorted by time stamp, one per line
articles_sorted=`mktemp pangitiveXXXXXX`
for f in "$articles_dir"/*; do
ts=`git log --format="%at" -- "$f" | tail -1`
if [ "$ts" != "" ]; then
echo "$ts ${f#$articles_dir/}"
fi
done | sort -k1,1nr | cut -d' ' -f2 > "$articles_sorted"
if [ "`head -1 $articles_sorted`" = "" ]; then
echo "[pangitive] WARNING: there's no article, errors may occur." >&2
fi
# List of articles including deleted ones
articles_sorted_with_delete=`mktemp pangitiveXXXXXX`
for f in "$articles_dir"/* $deleted_files; do
ts=`git log --format="%at" -- "$f" | tail -1`
if [ "$ts" != "" ]; then
echo "$ts ${f#$articles_dir/}"
fi
done | sort -k1,1nr | cut -d' ' -f2 > "$articles_sorted_with_delete"
commits=`mktemp pangitiveXXXXXX`
git log --oneline | cut -d' ' -f1 > "$commits"
get_article_info() {
git log --format="$1" -- "$2/$3"
}
get_article_next_file() {
next=`grep -B1 "^$1$" "$articles_sorted" | head -1`
if [ "$next" != "$1" ]; then
echo "$next"
fi
}
get_article_previous_file() {
previous=`grep -A1 "^$1$" "$articles_sorted" | tail -1`
if [ "$previous" != "$1" ]; then
echo "$previous"
fi
}
get_deleted_next_file() {
next=`grep -B1 "^$1$" "$articles_sorted_with_delete" | head -1`
if [ "`echo $deleted_files | grep -c \"$next\"`" = "0" ]; then
echo "$next"
fi
}
get_deleted_previous_file() {
previous=`grep -A1 "^$1$" "$articles_sorted_with_delete" | tail -1`
if [ "`echo $deleted_files | grep -c \"$previous\"`" = "0" ]; then
echo "$previous"
fi
}
get_article_title() {
if [ "$2" != "" ]; then
sed -n 's/^\s*title\s*:\s*\(.*\)/\1/p;T;q' "$1/$2" | sed "s/^'\(.*\)'$/\1/"
fi
}
get_commit_info() {
git show -s --format="$1" "$2"
}
get_commit_body() {
tmp=`mktemp pangitiveXXXXXX`
git show -s --format="%b" "$1" > "$tmp"
if [ "`cat \"$tmp\" | sed \"/^$/d\" | wc -l`" != "0" ]; then
echo "$tmp"
else
rm "$tmp"
fi
}
# Used with following function to obfuscate emails for bots
convert_to_html() {
if [ "$1" = "" ] ; then
return
fi
i=1;
while [ $i -le ${#1} ] ; do
l=`echo -n "$1" | cut -c$i`
# pandoc does not do exactly the same here, it has another algorithm to
# switch between decimal and hexadecimal notation (don't know what it is)
if [ $((i%2)) -eq 0 ] ; then
printf '&#%d;' \'"$l"
else
printf '&#x%x;' \'"$l"
fi
i=$((i+1))
done
}
# Obfuscate emails, pandoc style (more or less)
sanit_mail() {
if [ "$1" = "" -o `echo -n "$1" | sed 's/[^@]//g' | wc -c` -ne 1 ] ; then
echo "[preview mode]"
return
fi
name=`echo $1 | cut -d@ -f1`
host=`echo $1 | cut -d@ -f2`
n=`convert_to_html "$name"`
a='@'
h=`convert_to_html "$host"`
n_ns=`echo "$name" | sed 's/\./ dot /g'`
n_ns=`convert_to_html "$n_ns"`
a_ns=`convert_to_html ' at '`
h_ns=`echo "$host" | sed 's/\./ dot /g'`
h_ns=`convert_to_html "$h_ns"`
if [ "$2" != "" ] ; then
text="'$2'"
noscript_mail="`convert_to_html \"$2\"` ($n_ns$a_ns$h_ns)"
else
text="e"
noscript_mail="$n_ns$a_ns$h_ns"
fi
echo '<script type="text/javascript">'
echo '<!--'
echo "h='"$h"';a='"$a"';n='"$n"';e=n+a+h;"
echo "document.write('<a h'+'ref'+'=\"ma'+'ilto'+':'+e+'\">'+\
"$text"+'<\/'+'a'+'>');"
echo '// -->'
echo "</script><noscript>$noscript_mail</noscript>"
}
# Declare global variables
commit_Hash=""
commit_hash=""
commit_author=""
commit_author_email=""
commit_date=""
commit_date_html5=""
commit_date_day=""
commit_date_time=""
commit_timestamp=""
commit_comment=""
commit_slug=""
commit_body=""
# Load variables to directly feed pandoc with
load_commit_info() {
commit_Hash="--variable=commit-Hash:`get_commit_info '%H' \"$1\"`"
commit_hash="--variable=commit-hash:`get_commit_info '%h' \"$1\"`"
author_name=`get_commit_info '%an' "$1"`
author_email=`get_commit_info '%ae' "$1"`
commit_author="--variable=commit-author:$author_name"
commit_author_email="--variable=commit-author-email:`sanit_mail \
\"$author_email\" \"$author_name\"`"
datetime=`get_commit_info '%ai' "$1"`
commit_date="--variable=commit-date:$datetime"
commit_date_html5="--variable=commit-date-html5:`echo \"$datetime\" | \
sed 's/ /T/;s/ \(+\|-\)\([0-9][0-9]\)/\1\2:/'`"
commit_date_day="--variable=commit-date-day:`echo $datetime | cut -d' ' -f1`"
commit_date_time="--variable=commit-date-time:`echo $datetime | \
cut -d' ' -f2`"
commit_timestamp="--variable=commit-timestamp:`get_commit_info '%at' \"$1\"`"
commit_comment="--variable=commit-comment:`get_commit_info '%s' \"$1\"`"
commit_slug="--variable=commit-slug:`get_commit_info '%f' \"$1\"`"
commit_body="--variable=commit-body:`get_commit_body \"$1\"`"
}
# Declare global variables
article_file=""
article_title=""
article_cdate=""
article_cdate_html5=""
article_cdate_day=""
article_cdate_time=""
article_ctimestamp=""
article_mdate=""
article_mdate_html5=""
article_mdate_day=""
article_mdate_time=""
article_mtimestamp=""
article_author=""
article_cauthor=""
article_cauthor_email=""
article_mauthor=""
article_mauthor_email=""
article_previous=""
article_previous_title=""
article_next=""
article_next_title=""
article_centent=""
# Load variables to directly feed pandoc with
load_article_info() {
article_file="--variable=article-file:$2"
article_title="--variable=article-title:`get_article_title \"$1\" \"$2\"`"
cdatetime=`get_article_info '%ai' "$1" "$2" | tail -1`
article_cdate="--variable=article-cdate:$cdatetime"
article_cdate_html5="--variable=article-cdate-html5:`echo \"$cdatetime\" | \
sed 's/ /T/;s/ \(+\|-\)\([0-9][0-9]\)/\1\2:/'`"
article_cdate_day="--variable=article-cdate-day:`echo \"$cdatetime\" | \
cut -d' ' -f1`"
article_cdate_time="--variable=article-cdate-time:`echo \"$cdatetime\" | \
cut -d' ' -f2`"
article_ctimestamp="--variable=article-ctimestamp:`get_article_info \
'%at' \"$1\" \"$2\" | tail -1`"
u=`get_article_info "%ai" "$1" "$2" | wc -l`
mdatetime=`if test "$u" -gt 1; then get_article_info '%ai' "$1" "$2" | \
head -1; fi`
article_mdate="--variable=article-mdate:$mdatetime"
article_mdate_html5="--variable=article-mdate-html5:`echo \"$mdatetime\" | \
sed 's/ /T/;s/ \(+\|-\)\([0-9][0-9]\)/\1\2:/'`"
article_mdate_day="--variable=article-mdate-day:`echo \"$mdatetime\" | \
cut -d' ' -f1`"
article_mdate_time="--variable=article-mdate-time:`echo \"$mdatetime\" | \
cut -d' ' -f2`"
article_mtimestamp="--variable=article-mtimestamp:`if test \"$u\" -gt 1; \
then get_article_info '%at' \"$1\" \"$2\" | head -1; fi`"
tmp_cauthor=`get_article_info '%an' "$1" "$2" | tail -1`
tmp_cauthor_email=`get_article_info '%ae' "$1" "$2" | tail -1`
article_author="--variable=author:$tmp_cauthor"
article_cauthor="--variable=article-cauthor:$tmp_cauthor"
article_cauthor_email="--variable=article-cauthor-email:`sanit_mail \
\"$tmp_cauthor_email\" \"$tmp_cauthor\"`"
tmp_mauthor=`get_article_info '%an' "$1" "$2" | head -1`
tmp_mauthor_email=`get_article_info '%ae' "$1" "$2" | head -1`
article_mauthor="--variable=article-mauthor:$tmp_mauthor"
article_mauthor_email="--variable=article-mauthor-email:`sanit_mail \
\"$tmp_mauthor_email\" \"$tmp_mauthor\"`"
if [ "$1" != "$pages_dir" ]; then
previous_file=`get_article_previous_file "$2"`
article_previous="--variable=article-previous:$previous_file"
article_previous_title="--variable=article-previous-title:`\
get_article_title \"$1\" \"$previous_file\"`"
next_file=`get_article_next_file "$2"`
article_next="--variable=article-next:$next_file"
article_next_title="--variable=article-next-title:`get_article_title \
\"$1\" \"$next_file\"`"
else # don't keep remnant data from previous generated file in global vars
article_previous="--variable=deactivated"
article_previous_title="--variable=deactivated"
article_next="--variable=deactivated"
article_next_title="--variable=deactivated"
fi
}
# Processing body of the article, without any template
get_article_content() {
$pandoc -f markdown+smart --from=markdown --to=html "$1" | tr '\n' ' '
}
generate_archives() {
tmpfile=$2
echo '---' > "$tmpfile"
echo 'article:' >> "$tmpfile"
for i in `cat "$1"`; do
load_article_info "$articles_dir" "$i"
echo $article_file | \
sed "s/--variable=article-\([^:]*\):\(.*\)/ - \1: '\2'/" >> "$tmpfile"
for j in \
"$article_title" \
"$article_cdate" \
"$article_cdate_html5" \
"$article_cdate_day" \
"$article_cdate_time" \
"$article_ctimestamp" \
"$article_mdate" \
"$article_mdate_html5" \
"$article_mdate_day" \
"$article_mdate_time" \
"$article_mtimestamp" \
"$article_cauthor" \
"$article_mauthor" \
"$article_previous" \
"$article_previous_title" \
"$article_next" \
"$article_next_title"; do
echo $j | \
sed "s/--variable=article-\([^:]*\):\(.*\)/ \1: '\2'/" >> "$tmpfile"
done
for j in \
"$article_cauthor_email" \
"$article_mauthor_email"; do
echo $j | \
sed "s/--variable=article-\([^:]*\):\(.*\)/ \1: \2/" >> "$tmpfile"
done
if [ "$3" != "" ]; then # If anything provided as third arg, add content
echo -n " content: '" >> "$tmpfile"
# Enclose in quotes in case it contains a colon; change inner quotes
# into curly quotes so as to preserve outer enclosing
get_article_content "$articles_dir/$i" | sed "s/'/’/g" >> "$tmpfile"
echo "'" >> "$tmpfile"
fi
done
echo '---' >> "$tmpfile"
}
generate_article() {
temp=`mktemp pangitiveXXXXXX`
chmod a+r "$temp"
if [ "$1" != "${1#$articles_dir}" ]; then
art="${1#$articles_dir/}"
dir=$articles_dir
tpl="tpl.html"
elif [ "$f" != "${1#$pages_dir}" ]; then
art="${1#$pages_dir/}"
dir=$pages_dir
tpl="tpl.html"
fi
if [ "$art" != "" ]; then
title=`get_article_title "$dir" "$art"`
biblio=""
if [ "`head \"$1\" | grep '^bibliography: true$'`" != "" ] ; then
biblio="--filter pandoc-citeproc"
fi
load_commit_info "-1"
load_article_info "$dir" "$art"
$pandoc $pandoc_opt \
--variable=pagetitle:"$title" \
--variable=blog-url:"$blog_url" \
--variable=blog-owner:"$blog_owner" \
--variable=blog-title:"$blog_title" \
--variable=blog-years:"$blog_years" \
--template="$templates_dir/article.html" \
$biblio \
"$commit_Hash" \
"$commit_hash" \
"$commit_author" \
"$commit_author_email" \
"$commit_date" \
"$commit_date_html5" \
"$commit_date_day" \
"$commit_date_time" \
"$commit_timestamp" \
"$commit_comment" \
"$commit_slug" \
"$commit_body" \
"$article_file" \
"$article_title" \
"$article_cdate" \
"$article_cdate_html5" \
"$article_cdate_day" \
"$article_cdate_time" \
"$article_ctimestamp" \
"$article_mdate" \
"$article_mdate_html5" \
"$article_mdate_day" \
"$article_mdate_time" \
"$article_mtimestamp" \
"$article_author" \
"$article_cauthor" \
"$article_cauthor_email" \
"$article_mauthor" \
"$article_mauthor_email" \
"$article_previous" \
"$article_previous_title" \
"$article_next" \
"$article_next_title" \
"$1" > "$temp"
mv "$temp" "$public_dir/$art.html"
fi
}
regenerate_previous_and_next_article_maybe() {
if [ "$1" != "" -a \
"`grep -c \"^$1$\" \"$generated_files\"`" = "0" ]; then
echo -n "[pangitive] Regenerating $public_dir/$1.html"
echo -n " (as previous article) from $articles_dir/$1... "
generate_article "$articles_dir/$1"
echo "done."
echo "$1" >> "$generated_files"
fi
if [ "$2" != "" -a \
"`grep -c \"^$2$\" \"$generated_files\"`" = "0" ]; then
echo -n "[pangitive] Regenerating $public_dir/$2.html"
echo -n " (as next article) from $articles_dir/$2... "
generate_article "$articles_dir/$2"
echo "done."
echo "$2" >> "$generated_files"
fi
}
modification=0
# Generate / regen added or modified files
for f in $added_files $modified_files; do
art=""
if [ "$f" != "${f#$articles_dir}" ]; then
art="${f#$articles_dir/}"
modification=$((modification + 1))
elif [ "$f" != "${f#$pages_dir}" ]; then
art="${f#$pages_dir/}"
fi
if [ "$art" != "" ]; then
echo -n "[pangitive] Generating $public_dir/${art}.html from"
echo -n " $f... "
generate_article "$f"
echo "done."
echo "$art" >> "$generated_files"
fi
done
# Update links to next and previous articles
for f in $added_files; do
art=""
if [ "$f" != "${f#$articles_dir}" ]; then
art="${f#$articles_dir/}"
elif [ "$f" != "${f#$pages_dir}" ]; then
art="${f#$pages_dir/}"
fi
if [ "$art" != "" ]; then
if [ "$preview" = "" ]; then
echo -n "[pangitive] Adding $public_dir/$art.html to git ignore list... "
echo "$public_dir/$art.html" >> .git/info/exclude
echo "done."
fi;
if [ "$f" != "${f#$articles_dir}" ]; then
previous=`get_article_previous_file "$art"`
next=`get_article_next_file "$art"`
regenerate_previous_and_next_article_maybe "$previous" "$next"
fi;
fi
done
if [ "$preview" = "" ]; then
# Delete removed articles and update links
for f in $deleted_files; do
art=""
if [ "$f" != "${f#$articles_dir}" ]; then
art="${f#$articles_dir/}"
modification=$((modification + 1))
elif [ "$f" != "${f#$pages_dir}" ]; then
art="${f#$pages_dir/}"
fi
if [ "$art" != "" ]; then
echo -n "[pangitive] Deleting $public_dir/$art.html... "
rm "$public_dir/$art.html"
echo "done."
echo -n "[pangitive] Removing $art.html from git ignore list... "
sed -i "/^$public_dir\/$art.html$/d" .git/info/exclude
echo "done."
if [ "$f" != "${f#$articles_dir}" ]; then
previous=`get_deleted_previous_file "$art"`
next=`get_deleted_next_file "$art"`
regenerate_previous_and_next_article_maybe "$previous" "$next"
fi
fi
done
# Generate archives
if [ $modification -gt 0 ]; then
temp=`mktemp pangitiveXXXXXX`
archivetemp=`mktemp pangitiveXXXXXX`
chmod a+r "$temp"
echo -n "[pangitive] Generating $public_dir/archives.html... "
load_commit_info "-1"
generate_archives "$articles_sorted" "$archivetemp"
$pandoc $pandoc_opt \
--variable=pagetitle:archives \
--variable=blog-url:"$blog_url" \
--variable=blog-owner:"$blog_owner" \
--variable=blog-title:"$blog_title" \
--variable=blog-years:"$blog_years" \
--template="$templates_dir/archives.html" \
"$commit_Hash" \
"$commit_hash" \
"$commit_author" \
"$commit_author_email" \
"$commit_date" \
"$commit_date_html5" \
"$commit_date_day" \
"$commit_date_time" \
"$commit_timestamp" \
"$commit_comment" \
"$commit_slug" \
"$commit_body" \
"$archivetemp" > "$temp"
cp "$temp" "$public_dir/archives.html"
echo "done."
# Generate feed
echo -n "[pangitive] Generating $public_dir/feed.xml... "
last_5_articles=`mktemp pangitiveXXXXXX`
head -5 "$articles_sorted" > "$last_5_articles"
generate_archives "$articles_sorted" "$archivetemp" "with_content"
$pandoc $pandoc_opt \
--variable=pagetitle:feed \
--variable=blog-url:"$blog_url" \
--template="$templates_dir/feed.xml" \
"$commit_Hash" \
"$commit_hash" \
"$commit_author" \
"$commit_author_email" \
"$commit_date" \
"$commit_date_html5" \
"$commit_date_day" \
"$commit_date_time" \
"$commit_timestamp" \
"$commit_comment" \
"$commit_slug" \
"$commit_body" \
"$archivetemp" > "$temp"
cp "$temp" "$public_dir/feed.xml"
echo "done."
rm "$archivetemp" "$last_5_articles" "$temp"
echo -n "[pangitive] Using last published article as index page... "
cp "$public_dir/`head -1 $articles_sorted`.html" "$public_dir/index.html"
echo "done".
echo "[pangitive] Blog update complete."
fi
fi
rm "$articles_sorted"
rm "$articles_sorted_with_delete"
rm "$commits"
rm "$generated_files"