summaryrefslogtreecommitdiffhomepage
path: root/www/scripts/build.sh
diff options
context:
space:
mode:
Diffstat (limited to 'www/scripts/build.sh')
-rwxr-xr-xwww/scripts/build.sh55
1 files changed, 46 insertions, 9 deletions
diff --git a/www/scripts/build.sh b/www/scripts/build.sh
index a0c971d..cec10fd 100755
--- a/www/scripts/build.sh
+++ b/www/scripts/build.sh
@@ -13,13 +13,35 @@ template_dir=${template_dir:-`pwd`/templates}
13 13
14# functions {{{ 14# functions {{{
15 15
16# add indent for each line except <pre> 16# add indent for each line except <pre> {{{
17
17indent() { 18indent() {
18 indent="$(printf "%${1}s")" 19 indent="$(printf "%${1}s")"
19 sed "s/^/${indent}/; /<pre>/!b; :pre; N; /<\/pre>/!b pre" 20 sed "/^$/ !s/^/${indent}/; /<pre>/!b; :pre; N; /<\/pre>/!b pre"
20} 21}
21 22
22# use heredoc to generate html from .md file and templates 23# }}}
24# print comment block {{{
25comment_block() {
26 [ "$comment" = true ] || return 0
27 <<-COMMENT cat
28 <div style="border-radius: 6px; background: lightyellow">
29 <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>
30 <object type="text/html" data="/${path/.html}.comment.html" onload="observeResize(this)" style="width: 100%;"></object>
31 <script>
32 function observeResize(commentBlock) {
33 const doc = commentBlock.contentDocument.documentElement
34 new ResizeObserver(() => {
35 commentBlock.style.height = doc.clientHeight + 'px';
36 }).observe(doc);
37 }
38 </script>
39 </div>
40 COMMENT
41}
42
43# }}}
44# use heredoc to generate html from .md file and templates {{{
23html() { 45html() {
24 <<-END_OF_HTML sed '1d;$d' 46 <<-END_OF_HTML sed '1d;$d'
25 47
@@ -32,6 +54,7 @@ html() {
32 <main> 54 <main>
33 $(${markdown_bin} | indent 4) 55 $(${markdown_bin} | indent 4)
34 </main> 56 </main>
57 $(comment_block | indent 2)
35 <br><hr> 58 <br><hr>
36 ${footer} 59 ${footer}
37 </body> 60 </body>
@@ -40,19 +63,25 @@ html() {
40 END_OF_HTML 63 END_OF_HTML
41} 64}
42 65
43# list of latest posts in markdown format 66# }}}
67# list of latest posts in markdown format {{{
68
44latest_posts() { 69latest_posts() {
45 (IFS=$'\n'; echo "${index_list[*]}") | sort -r | head -20 | while read date path title; do 70 (IFS=$'\n'; echo "${index_list[*]}") | sort -r | head -20 | while read date path title; do
46 echo "- <time datetime="$date">$date</time> [$title](/$path)" 71 echo "- <time datetime="$date">$date</time> [$title](/$path)"
47 done 72 done
48} 73}
49 74
50# print frontmatter from markdown file with format: "<key> <value>" 75# }}}
76# print frontmatter from markdown file with format: "<key> <value>" {{{
77
51get_frontmatter() { 78get_frontmatter() {
52 sed -n '1 {/<!--/ !q; n}; /-->/q; s/"//g; s/://p' 79 sed -n '1 {/<!--/ !q; n}; /-->/q; s/"//g; s/://p'
53} 80}
54 81
55# process frontmatter 82# }}}
83# process frontmatter {{{
84
56add_index() { 85add_index() {
57 unset title public index date draft 86 unset title public index date draft
58 87
@@ -76,12 +105,16 @@ add_index() {
76 index_list+=("$iso8601 $path $title") 105 index_list+=("$iso8601 $path $title")
77} 106}
78 107
79# remove SGML comments but keep the top one as frontmatter 108# }}}
109# remove SGML comments but keep the top one as frontmatter {{{
110
80ignore_comment() { 111ignore_comment() {
81 sed '1 !{ /^<!--$/,/^-->$/ d }' 112 sed '1 !{ /^<!--$/,/^-->$/ d }'
82} 113}
83 114
84# Generate the feed file 115# }}}
116# Generate the feed file {{{
117
85make_rss() { 118make_rss() {
86 echo -n "Making RSS " 119 echo -n "Making RSS "
87 120
@@ -126,6 +159,7 @@ make_rss() {
126} 159}
127 160
128# }}} 161# }}}
162# }}}
129# prepare directory for outputs {{{ 163# prepare directory for outputs {{{
130 164
131mkdir -p $output_dir/ 165mkdir -p $output_dir/
@@ -150,7 +184,7 @@ declare -i counter
150for file in $files; do 184for file in $files; do
151 # set variables 185 # set variables
152 path=$(<<<"$file" sed "s#^${input_dir}/##; s/\.md$//").html; mkdir -p $(dirname $output_dir/$path) 186 path=$(<<<"$file" sed "s#^${input_dir}/##; s/\.md$//").html; mkdir -p $(dirname $output_dir/$path)
153 content="$(cat ${file} | ignore_comment)" 187 content="$(<${file} ignore_comment)"
154 188
155 # use frontmatter to decide making html file or not 189 # use frontmatter to decide making html file or not
156 <<<"$content" add_index || continue 190 <<<"$content" add_index || continue
@@ -160,10 +194,13 @@ for file in $files; do
160 194
161 # make html file for draft 195 # make html file for draft
162 h1="$(<<<"$content" get_frontmatter | sed -n 's/^title *//p')" 196 h1="$(<<<"$content" get_frontmatter | sed -n 's/^title *//p')"
197 comment="$(<<<"$content" get_frontmatter | sed -n 's/^comment *//p')"
163 echo "$content" \ 198 echo "$content" \
164 | tee $output_dir/${file#${input_dir}/} \ 199 | tee $output_dir/${file#${input_dir}/} \
165 | { [ -n "$h1" ] && echo "# $h1"; cat; } \ 200 | { [ -n "$h1" ] && echo "# $h1"; cat; } \
166 | html >$output_dir/$path 201 | html >$output_dir/$path
202
203 unset h1 comment
167done 204done
168echo 205echo
169 206