blob: 985725aa293b9495e18e3f98556496ed329856a8 (
plain)
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
|
#! /bin/bash
set -e
# Executable command for markdown
declare -r MARKDOWN_BIN="markdown -f fencedcode,autolink,alphalist,autolink,footnote,definitionlist"
# Directory for input/output
declare -r INPUT_DIR=${INPUT_DIR:?ENV \"INPUT_DIR\" is not set}
declare -r OUTPUT_DIR=${OUTPUT_DIR:?ENV \"OUTPUT_DIR\" is not set}
declare -r ASSET_DIR=${ASSET_DIR:-`pwd`/assets}
declare -r TEMPLATE_DIR=${TEMPLATE_DIR:-`pwd`/templates}
declare -r RSSFILE=feed.rss
declare -r HOST=${HOST:?}
# functions {{{
# add indent for each line except <pre> {{{
indent() {
indent="$(printf "%${1}s")"
sed "/^$/ !s/^/${indent}/; /<pre>/!b; :pre; N; /<\/pre>/!b pre"
}
# }}}
# print comment block {{{
comment_block() {
[ "$comment" = true ] || return 0
<<-COMMENT cat
<div style="border-radius: 6px; background: lightyellow">
<a style="display: inline-block; margin: 0.5em 0.5em 0 0; float: right" href="mailto:comment@topo.tw?subject=Comment on page: ${path}">[Comment on this page]</a>
<object type="text/html" data="/${path/%html/comment.html}" onload="observeResize(this)" style="width: 100%;"></object>
<script>
function observeResize(commentBlock) {
const doc = commentBlock.contentDocument.documentElement
new ResizeObserver(() => {
commentBlock.style.height = doc.clientHeight + 'px';
}).observe(doc);
}
</script>
</div>
COMMENT
}
# }}}
# generate html by heredoc from .md file and templates {{{
html() {
<<-END_OF_HTML sed '1d;$d'
<!DOCTYPE html>
<html lang="en">
$(echo "$HEAD" | { [ -z "${title}" ] && cat || sed "/^ *<title>/c \ \ <title>${title}</title>"; } )
<body>
${HEADER}
<hr><br>
<main>
$(${MARKDOWN_BIN} | indent 4)
</main>
$(comment_block | indent 2)
<br><hr>
${FOOTER}
</body>
</html>
END_OF_HTML
}
# }}}
# list of latest posts in markdown format {{{
latest_posts() {
(IFS=$'\n'; echo "${index_list[*]}") | sort -r | head -20 | while read date path title; do
echo "- <time title=\"$(date --rfc-email -d "$date")\" datetime=\"$date\">$date</time> [$title](/$path)"
done
}
# }}}
# process frontmatter {{{
get_frontmatter() {
sed -n '1 {/<!--/ !q; n}; /-->/q; s/"//g; s/://p'
}
check_frontmatter() {
unset title public index type date draft
# define local variables for frontmatter
while read key value; do
export ${key,,}="$value"
done <<<"$(get_frontmatter)"
# don't process draft after function call
test "$draft" != "" && return 1
# skip making index in some cases
test "$title" = "" && return 0
test "$public" = false && return 0
test "$index" = false && return 0
test "$type" = demo && return 0
iso8601=$(date --iso --date "${date:-NULL}" 2>/dev/null)
test "$iso8601" = "" && return 0
# put frontmatter info into variable "index" if title and date are valid
index_list+=("$iso8601 $path $title")
}
# }}}
# remove SGML comments but keep the top one as frontmatter {{{
ignore_comment() {
sed '1! { /^<!--$/,/^-->$/ d }'
}
# }}}
# RSS Feed {{{
get_channel_item() {
path=${1#${OUTPUT_DIR}}
<<-ITEM cat
<item>
$(grep -Po '<title>.+</title>' ${1} | head -1)
<link>https://${HOST}${path}</link>
<guid isPermaLink="false">$(md5sum ${1} | cut -d' ' -f1)</guid>
<pubDate>$(date --rfc-email -r ${1})</pubDate>
</item>
ITEM
}
make_rss() {
# Use modified time to get latest html files
items="$(
ls -1t $OUTPUT_DIR/**/**html \
| head -10 \
| while read html_file; do
get_channel_item $html_file
done \
| indent 2 \
)"
<<-RSS cat
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
<title>Dummy Website</title>
<link>https://${HOST}</link>
<atom:link href="http://${HOST}/${RSSFILE}" rel="self" type="application/rss+xml" />
<description>$global_description</description>
<language>zh-tw</language>
<pubDate>$(date --rfc-email)</pubDate>
<docs>https://www.rssboard.org/rss-specification</docs>
${items}
</channel>
</rss>
RSS
}
# }}}
# }}}
# prepare directory for outputs {{{
mkdir -p $OUTPUT_DIR/
rm -rf $OUTPUT_DIR/**
ln -s $ASSET_DIR/* $OUTPUT_DIR/
# }}}
# content of templates {{{
declare -r HEAD="$(cat $TEMPLATE_DIR/head.html)"
declare -r HEADER="$(<$TEMPLATE_DIR/header.html indent 2)"
declare -r FOOTER="$(<$TEMPLATE_DIR/footer.html indent 2)"
declare -r INDEX_TEMPLATE="$(cat $TEMPLATE_DIR/index.md)"
index_list=()
# }}}
# for each markdown file {{{
files="$(find "$INPUT_DIR" -type f,l -name '*md')"
total=$(wc -l <<<"$files")
declare -i counter
for file in $files; do
# set variables
path=$(<<<"$file" sed "s#^${INPUT_DIR}/##; s/\.md$//").html; mkdir -p $(dirname $OUTPUT_DIR/$path)
content="$(<${file} ignore_comment)"
# use frontmatter to decide making html file or not
<<<"$content" check_frontmatter || continue
# log
echo -e "\033[1K\r$((counter+=1))/$total\t\tprocessing $path"
# add original content to $OUTPUT_DIR
# and generate html file with <h1>
echo "$content" \
| tee $OUTPUT_DIR/${file#${INPUT_DIR}/} \
| { [ -n "$title" ] && echo "# $title"; cat; } \
| html >$OUTPUT_DIR/$path
# Set modified time for index/RSS
mtime='@0'
[ ! "$public" = false ] && [ -n "$iso8601" ] && mtime="$iso8601"
touch -m -d "$mtime" $OUTPUT_DIR/$path
unset h1 comment
done
echo
# }}}
# make index.html {{{
echo -en "Making\t\tindex.html"
export title=""
{
echo "${INDEX_TEMPLATE}"
echo -e '<br><br>\n\n'
latest_posts
} \
| tee $OUTPUT_DIR/index.md \
| html >$OUTPUT_DIR/index.html
touch -m -d @0 $OUTPUT_DIR/index.html
echo -e "\tgenerated"
# }}}
# make RSS file {{{
echo -en "Making\t\tRSS"
make_rss >$OUTPUT_DIR/$RSSFILE
echo -e "\t\tgenerated"
# }}}
# vim: fdm=marker fdl=0
|