summaryrefslogtreecommitdiffhomepage
path: root/build.sh
blob: c66b6be6a2c229eaf87936663fca34635a3d913a (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
#!/bin/sh
#
# ISC License (ISC)
#
# Copyright (c) 2021 Paul W. Rankin <pwr@bydasein.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# Configuration:

# SITE_URL			your URL
# SITE_AUTHOR		your name
# SITE_EMAIL		your email
# SITE_TITLE		title for html pages
# BLOG_TITLE		title of generated blog page
# BLOG_DATE_FMT		date format for blog index as per strftime(3)
# SITE_TZ			site timezone offset with separator as per ISO 8601
# TITLE_SEP			separator between SITE_TITLE and page titles
# SRC_DIR			directory of source files to convert
# STATIC_DIR		directory of static source files
# HTML_TMPL			location of m4 template for html files
# FEED_TMPL			location of m4 template for atom file
# HTML_DIR			output directory
# BLOG_INDEX		output location of generated blog page, relative to HTML_DIR
# FEED				output location of generated atom file, relative to HTML_DIR

prog=$(basename "$0")

site_url="${SITE_URL:-https://bydasein.com}"
site_author="${SITE_AUTHOR:-Paul W. Rankin}"
site_email="${SITE_EMAIL:-pwr@bydasein.com}"
site_title="${SITE_TITLE:-By Dasein}"
blog_title="${BLOG_TITLE:-Blog}"
blog_date_fmt="${BLOG_DATE_FMT:-+%d %b %Y}"
site_tz="${SITE_TZ:-+10:00}"
title_sep="${TITLE_SEP:- &ndash; }"

src_dir="${SRC_DIR:-src}"
static_dir="${STATIC_DIR:-static}"
html_tmpl="${HTML_TMPL:-index.html.in}"
feed_tmpl="${FEED_TMPL:-feed.xml.in}"

html_dir="${HTML_DIR:-html}"
gmi_dir="${GMI_DIR:-gemini}"
blog_index="${BLOG_INDEX:-words/index.html}"
feed="${FEED:-feed.xml}"

usage() {
	cat <<EOF
usage: $prog [ -v ] [ -g ]
EOF
	exit 1
}

# get_src(extension)
# returns: list of files
get_src() {
	find "$src_dir" -type f -name "*.$1"
}

# make_db(markdownfile1, markdownfile2, ...)
# returns: TSV of ISO date, formatted date, URL, title for each Markdown file
make_db() {
	while read -r file; do
		date=$(lowdown -X date "$file" 2>/dev/null || echo NULL)
		title=$(lowdown -X title "$file" 2>/dev/null || echo NULL)
		build="${file%.md}.html"
		url="${build#$src_dir}"; url="${url#/}"
		# requires Unix date *not* GNUdate
		fmt_date=$(date -jf %F "$date" "$blog_date_fmt" 2>/dev/null || echo NULL)
		printf '%s\t%s\t%s\t%s\n' "$date" "$fmt_date" "$url" "$title"
	done | sort -r
}

# make_index(database)
# returns: formatted HTML table
make_index() {
	rows=$(
		awk -F \\t '$1 == "NULL" { next }
			{ printf "<tr><td>%s</td><td><a href=\"/%s\">%s</a></td></tr>\n", $2, $3, $4 }' \
				< "$1")
	printf '<table>\n<tbody>\n%s\n</tbody>\n</table>\n' "$rows"
}

# convert_md(markdownfile)
# returns: formatted HTML
convert_md() {
	src="$1"
	dst="${src%.md}.html"
	[ "$verbose" -eq 1 ] && echo "converting $src > $dst"
	lowdown -Thtml < "$src" > "$dst"
}

# make_gemi(markdownfile)
# returns: formatted Gemini
make_gmi() {
	src="$1"
	dst="${src%.md}.gmi"; dst="${dst#$src_dir}"; dst="${dst#/}"
	mkdir -p $(dirname "${gmi_dir}/${dst}")
	[ "$verbose" -eq 1 ] && echo "converting $src > ${gmi_dir}/${dst}"
	lowdown -Tgemini --gemini-link-noref < "$src" > "${gmi_dir}/${dst}"
}

# make_htmlpage(htmlfile)
# returns: formatted standalone HTML
make_htmlpage() {
	src="$1"
	dst="${src#$src_dir}"; dst="${dst#/}"
	mkdir -p $(dirname "${html_dir}/${dst}")

	if [ "$src" == "${src_dir}/${blog_index}" ]; then
		title="$blog_title"
	else
		title=$(awk -F \\t -v dst="$dst" '$3 == dst { print $4 }' < "$db_tmp")
	fi

	if [ "$title" == NULL -o -z "$title" ]; then
		title="$site_title"
	else
		title="${site_title}${title_sep}${title}"
	fi

	[ "$verbose" -eq 1 ] && echo "generating ${html_dir}/${dst} ($title)"
	m4 -D__TITLE__="$title" \
	   -D__CONTENT__="$src" \
	   -P "$html_tmpl" > "${html_dir}/${dst}"
}

# make_feed_entries(database)
# returns: formatted XML
make_feed_entries() {
	while read -r line; do
		date=$(echo "$line" | awk -F \\t '{ print $1 }')
		url=$(echo "$line" | awk -F \\t '{ print $3 }')
		title=$(echo "$line" | awk -F \\t '{ print $4 }')
		[ "$date" != NULL ] &&
			cat <<-EOF
				<entry>
				<title>${title}</title>
				<link href="${site_url}/${url}"/>
				<id>${site_url}/${url}</id>
				<updated>${date}T00:00:00${site_tz}</updated>
				<content type="html"><![CDATA[
				$(cat ${src_dir}/${url})
				]]></content>
				</entry>
				EOF
	done < "$1"
}

# make_feed(feedcontent)
# returns: formatted XML Atom feed
make_feed() {
	src="$1"
	m4 -D__TITLE__="$site_title" \
	   -D__AUTHOR__="$site_author" \
	   -D__EMAIL__="$site_email" \
	   -D__DOMAIN__="$site_domain" \
	   -D__URL__="$feed" \
	   -D__DATE__=$(date "+%FT%R:%S${site_tz}") \
	   -D__CONTENT__="$src" \
	   -P "$feed_tmpl"
}

main() {
	verbose=0; gemini=0
	while getopts 'vg' opt
	do
		case "$opt" in
			(v)		verbose=1 ;;
			(g)		gemini=1 ;;
			(*)		usage ;;
		esac
	done

	db_tmp=$(mktemp)
	get_src "md" | make_db > "$db_tmp"

	get_src "md" | while read -r file; do
		convert_md "$file"
	done

	[ "$verbose" -eq 1 ] && echo "generating ${src_dir}/${blog_index}"
	mkdir -p $(dirname "${src_dir}/${blog_index}")
	make_index "$db_tmp" > "${src_dir}/${blog_index}"

	mkdir -p "$html_dir"
	get_src "html" | while read -r file; do
		make_htmlpage "$file"
	done

	[ "$verbose" -eq 1 ] && echo "generating ${html_dir}/${feed}"
	feed_tmp=$(mktemp)
	make_feed_entries "$db_tmp" > "$feed_tmp"
	make_feed "$feed_tmp" > "${html_dir}/${feed}"

	cp -R "$static_dir"/* "$html_dir"

	if [ "$gemini" -eq 1 ]; then
		get_src "md" | while read -r file; do
			make_gmi "$file"
		done
	fi

	[ "$verbose" -eq 1 ] && echo "removing temporary files..."
	rm -rf "${src_dir}/${blog_index}" "$db_tmp" "$feed_tmp"
	get_src "md" | while read -r file; do
		target="${file%.md}.html"
		if [ -e "$target" ]; then
			[ "$verbose" -eq 1 ] && echo "removing $target"
			rm -rf "$target"
		fi
	done
}

main "$@"

# Local Variables:
# tab-width: 4
# End: