blob: a0c971d55f290609abc2e643dc42c7e0f508c739 (
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
|
#! /bin/bash
set -e
# Executable command for markdown
markdown_bin="markdown -f fencedcode,autolink,alphalist,autolink,footnote"
# Directory for input/output
input_dir=${input_dir:?ENV \"input_dir\" is not set}
output_dir=${output_dir:?ENV \"output_dir\" is not set}
assets_dir=${assets_dir:-`pwd`/assets}
template_dir=${template_dir:-`pwd`/templates}
# functions {{{
# add indent for each line except <pre>
indent() {
indent="$(printf "%${1}s")"
sed "s/^/${indent}/; /<pre>/!b; :pre; N; /<\/pre>/!b pre"
}
# use heredoc to generate html from .md file and templates
html() {
<<-END_OF_HTML sed '1d;$d'
<!DOCTYPE html>
<html lang="en">
${head}
<body>
${header}
<hr><br>
<main>
$(${markdown_bin} | indent 4)
</main>
<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 datetime="$date">$date</time> [$title](/$path)"
done
}
# print frontmatter from markdown file with format: "<key> <value>"
get_frontmatter() {
sed -n '1 {/<!--/ !q; n}; /-->/q; s/"//g; s/://p'
}
# process frontmatter
add_index() {
unset title public index date draft
# define local variables for frontmatter
while read key value; do
local -r $key="$value"
done <<<"$(get_frontmatter)"
# don't process draft after function call
test "$draft" != "" && return 1
# skip making index in some cases
test "$public" = false && return 0
test "$index" = false && return 0
test "$type" = demo && return 0
test "$title" = "" && 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 }'
}
# Generate the feed file
make_rss() {
echo -n "Making RSS "
rssfile=$blog_feed.$RANDOM
while [[ -f $rssfile ]]; do rssfile=$blog_feed.$RANDOM; done
{
pubdate=$(LC_ALL=C date +"$date_format_full")
cat <<-EOF
<?xml version="1.0" encoding="UTF-8" ?>'
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">'
<channel><title>Dummy Website</title><link>https://topo.tw/index.xml</link>"
<description>$global_description</description><language>en</language>"
<lastBuildDate>$pubdate</lastBuildDate>"
<pubDate>$pubdate</pubDate>"
<atom:link href="$global_url/$blog_feed" rel="self" type="application/rss+xml" />"
EOF
n=0
while IFS='' read -r i; do
is_boilerplate_file "$i" && continue
((n >= number_of_feed_articles)) && break # max 10 items
echo -n "." 1>&3
echo '<item><title>'
get_post_title "$i"
echo '</title><description><![CDATA['
get_html_file_content 'text' 'entry' $cut_do <"$i"
echo "]]></description><link>$global_url/${i#./}</link>"
echo "<guid>$global_url/$i</guid>"
echo "<dc:creator>$(get_post_author "$i")</dc:creator>"
echo "<pubDate>$(LC_ALL=C date -r "$i" +"$date_format_full")</pubDate></item>"
n=$(( n + 1 ))
done < <(ls -t ./*.html)
echo '</channel></rss>'
} 3>&1 >"$rssfile"
echo ""
mv "$rssfile" "$blog_feed"
chmod 644 "$blog_feed"
}
# }}}
# prepare directory for outputs {{{
mkdir -p $output_dir/
rm -rf $output_dir/**
ln -s $assets_dir/* $output_dir/
# }}}
# content of templates {{{
head="$(cat $template_dir/head.html)"
header="$(cat $template_dir/header.html | indent 2)"
footer="$(cat $template_dir/footer.html | indent 2)"
index_list=()
index_template="$(cat $template_dir/index.md)"
# }}}
# for each markdown file {{{
files="$(find "$input_dir" -type f -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="$(cat ${file} | ignore_comment)"
# use frontmatter to decide making html file or not
<<<"$content" add_index || continue
# log
echo -e "\033[1K\r$((counter+=1))/$total \t\t processing $path"
# make html file for draft
h1="$(<<<"$content" get_frontmatter | sed -n 's/^title *//p')"
echo "$content" \
| tee $output_dir/${file#${input_dir}/} \
| { [ -n "$h1" ] && echo "# $h1"; cat; } \
| html >$output_dir/$path
done
echo
# }}}
# make index.html {{{
{
echo "${index_template}"
echo -e '<br><br>\n\n'
latest_posts
} \
| tee $output_dir/index.md \
| html >$output_dir/index.html
echo -e index.html "\t" generated
# }}}
# make index.xml {{{
# }}}
|