-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·172 lines (158 loc) · 4.84 KB
/
build.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
#!/usr/bin/env bash
OUTDIR=__site
OUTTMP=${OUTDIR}_tmp
IDENTITY=("css/*" "js/*" "lyrics/*" "argentina/**" "static/**" "robots.txt" "favicon.ico")
POSTS=posts/*
INDEX="index.html"
TEMPLATES=templates/*
POSTLIST="templates/post-list.html"
TEMPLATE="templates/default.html"
POST="templates/post.html"
P=pandoc
PM="pandoc --mathjax"
postprocess_html() {
sed -i -e 's/href="\/\(.*\)"/href=".\/\1"/g' $1
sed -i -e 's/script async/script async="async"/g' $1
for ((i=1; i<=10; i++))
do
sed -i -e "s/title=\"$i\"/data-line-number=\"$i\"/g" $1
done
}
no_diff_hacks() {
sed -i -e 's/^\(.*\)post_link\(.*\)<br>\(.*\)$/\1post_link\2<!-- br-->\3/g' \
$OUTDIR/index.html
sed -i -e 's/<span class="ot">=<\/span>/<span class="fu">=<\/span>/g' \
$OUTDIR/propositions-as-types.html
sed -i -e 's/ i\.e\.[^,]/ i.e. /' $OUTDIR/propositions-as-types.html
sed -i -e 's/<span class="fu">id<\/span>/id/' \
$OUTDIR/propositions-as-types.html
sed -i -e 's/href="\//href=".\//g' $OUTDIR/about.html
# twice to resolve all appearances.
sed -i -e 's/^\(.*\)sourceLine\(.*\)\\\\\(.*\)$/\1sourceLine\2\\\3/' \
$OUTDIR/propositions-as-types.html
sed -i -e 's/^\(.*\)sourceLine\(.*\)\\\\\(.*\)$/\1sourceLine\2\\\3/' \
$OUTDIR/propositions-as-types.html
}
remove_yaml_header() {
sed -i '0,/^---$/d' $1
sed -i '0,/^---$/d' $1
}
get_post_date() {
basename $1 .markdown | cut -c-10 | xargs -0 date '+%B %e, %Y' -d
}
# xargs echo -n trims whitespace.
get_post_title() {
head $1 | grep "title:" | sed -e "s/title://" | xargs echo -n
}
get_post_url() {
head $1 | grep "url:" | sed -e "s/url://" | xargs echo -n
}
run_build() {
rm -r $OUTDIR
rm -r $OUTTMP
mkdir -p $OUTDIR
mkdir -p $OUTTMP
# Resolve files that are copied verbatim.
echo "Resolving verbatim files..."
for f in "${IDENTITY[@]}"
do
cp -R --parents $f $OUTDIR;
done
# Resolve about page.
echo "Resolving about..."
$P --template=$TEMPLATE -o $OUTDIR/about.html ./about.markdown
postprocess_html $OUTDIR/about.html
# Resolve posts.
echo "Resolving posts..."
for f in $POSTS
do
echo "Resolving $f..."
post_url=$(get_post_url $f)
post_date=$(get_post_date $f)
outtmp=$OUTTMP/$(basename $f .markdown).1.markdown
echo "---" > $outtmp
echo "title: $(get_post_title $f)" >> $outtmp
echo "url: $post_url" >> $outtmp
echo "date: $post_date" >> $outtmp
echo "---" >> $outtmp
$PM --metadata date="$post_date" --template=$POST -o- $f >> $outtmp
sed -i 's/\\/\\\\/g' $outtmp
sed -e 's/.*\$body\$.*/$body$/' $TEMPLATE > $OUTTMP/default.4.html
$P --template=$OUTTMP/default.4.html -o $OUTDIR/$post_url $outtmp
postprocess_html $OUTDIR/$post_url
done
# Resolve index.
echo "Resolving index..."
index_tmp=$OUTTMP/index.1.html
sed -e 's/\$partial("\(.*\)")\$/$\1()$/' $INDEX > $index_tmp
remove_yaml_header $index_tmp # unneeded
cp templates/post-list.html $OUTTMP/post-list.1.html
for field in url title date teaser
do
sed -i "s/\\\$$field\\$/\$posts.$field\$/" $OUTTMP/post-list.1.html
done
index_title=$(get_post_title $INDEX)
post_list=$(basename $POSTLIST .html);
post_list_yaml=$OUTTMP/$post_list.2.yaml
edited_default=$OUTTMP/default.1.html
cp $TEMPLATE $edited_default
sed -i -e "/\\\$body\\$/{r $OUTTMP/post-list.1.html" -e 'd}' $edited_default
echo "title: $index_title" >> $post_list_yaml
echo "posts:" >> $post_list_yaml
REVPOSTS=()
for f in $POSTS
do
echo "Resolving index.html teaser for $f..."
post_url=$(head $f | grep "url:" | sed -e "s/url://" | xargs echo -n)
post_date=$(get_post_date $f)
teasermd=$OUTTMP/post_url.2.markdown
teaseryaml=$OUTTMP/$post_url.1.yaml
cp $f $teasermd
remove_yaml_header $teasermd
sed -i '/<!--more-->/q' $teasermd
sed -i 's/<!--more-->//' $teasermd
sed -i '1d' $teasermd
sed -i -e 's/^/ /' $teasermd
echo "- title: $(get_post_title $f)" >> $teaseryaml
echo " url: $post_url" >> $teaseryaml
echo " date: $post_date" >> $teaseryaml
echo " teaser: |" >> $teaseryaml
sed -e 's/_/\_/g' $teasermd >> $teaseryaml
echo " " >> $teaseryaml
REVPOSTS[${#REVPOSTS[@]}]=$teaseryaml
done
for ((i=${#REVPOSTS[@]}-1; i>=0; i--))
do
cat ${REVPOSTS[i]} >> $post_list_yaml
done
$PM --metadata-file=$post_list_yaml --template=$edited_default -o- /dev/null \
| sed -e 's/<\/ br>/<br>/' > $OUTDIR/$INDEX
postprocess_html $OUTDIR/$INDEX
no_diff_hacks
echo "Done!"
}
run_tidy() {
tidy -config tidy.conf $1 | sed -e "s/^[ \t]*//"
}
run_diff () {
echo "Diffing..."
for new_f in $OUTDIR/**.html
do
echo "Diffing $new_f..."
diff <(run_tidy ${new_f:1}) <(run_tidy $new_f)
done
}
if [ $1 == "build" ];
then
run_build
elif [ $1 == "watch" ];
then
run_build
python -m http.server 9999 --directory $OUTDIR
elif [ $1 == "diff" ];
then
run_build
run_diff
else
echo "Nothing to do"
fi