aboutsummaryrefslogtreecommitdiff
path: root/build.sh
blob: 2a486868743e2f8644eeff59ce716f16c1bcfd7a (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
#!/bin/sh

basedir="$(pwd)"
bottomtmp="$(mktemp)"

recursivebuild() {
	local destdir=$(echo "$1" | sed 's|^src|http|')
	mkdir -p "$destdir"
	for file in "$1"/*; do
		if [ -d "$file" ]; then
			mkdir -p "$destdir/$(basename "$file")"
			recursivebuild "$file"
		else
			copyfile "$file" "$destdir"
		fi
	done
}

mdpreprocess() {
	file="$1"
	if [ "$(echo "$file" | sed 's/.*\.//')" = "m4" ]; then
		printf 'm4_include(macros.m4)m4_dnl\n%s\n' "$(cat "$file")" | \
			m4 -P -I "$basedir/utils"
	else
		cat "$file"
	fi
}

copyfile() {
	file=$1
	dest=$2
	ind=$dest/index.html
	extension=$(echo "$file" | sed 's/.*\.//')
	case "$extension" in
	# m4 preprocess is currently disabled
	#m4)
	#	noext=$(echo "$file" | sed 's/\..*//')
	#	mdpreprocess "$file" > $noext.md
	#	;;
	md)
		t="$(markdowntitle "$file")"
		sed "s/TITLE/$t/" < top.html > "$ind"
		mdpreprocess "$file" | \
			lowdown --html-no-skiphtml --html-no-escapehtml \
			>> "$ind"
		cat "$bottomtmp" >> "$ind"
		;;
	noheadermd)
		t="$(markdowntitle "$file")"
		sed "s/TITLE/$t/" < top-nobar.html > "$ind"
		mdpreprocess "$file" | \
			lowdown --html-no-skiphtml --html-no-escapehtml \
			>> "$ind"
		;;
	html)
		t="$(htmltitle "$file")"
		cat top.html "$file" "$bottomtmp" | sed "s/TITLE/$t/" > "$ind"
		;;
	raw)
		namenoraw="$(basename "$file" | sed 's/\.raw$//')"
		cp "$file" "$dest/$namenoraw"
		;;
	*)
		cp "$file" "$dest/$(basename "$file")"
	esac
}

markdowntitle() {
	grep '^# ' "$1" | head -n 1 | sed 's/^# //'
}

htmltitle() {
	grep '<!--TITLE: ' "$1" | sed 's/^<!--TITLE: //' | sed 's/-->$//'
}

feedify() {
	printf '\n<![CDATA[\n'
	printf '<p><emph>Note: internal links to '
	printf '<a href="https://sebastiano.tronto.net">sebastiano.tronto.net</a> '
	printf 'may be broken in feed view. Read this page online or fix them '
	printf 'manually if you want to follow them.</emph></p>\n\n'
	sed -n '/<main>/,/<\/main>/p; s/]]>/]]]]>\n<![CDATA[/g' "$1" \
		| head -n -2 | tail -n +3
	printf '\n]]>\n'
}

makeblogindexandfeed() {
	mkdir -p http/blog
	bf=http/blog/index.html
	ff=http/blog/feed.xml

	sed "s/TITLE/Blog/" < top.html > "$bf"
	{ echo '<h1 id="blog">Blog</h1>';
	  echo '<table id="blog">';
	  echo '<a href="../series">Blog series</a> - ';
	  echo '<a href="feed.xml">RSS feed</a>'; } >> "$bf"

	cp feed-top.xml "$ff"

	for i in $(ls src/blog | sort -r); do
		[ -d "src/blog/$i" ] || continue

		f="src/blog/$i/*.md"
		d="$(echo "$i" | grep -oE '^[0-9]{4}-[0-9]{2}-[0-9]{2}')"
		t="$(markdowntitle $f)"
		link="https://sebastiano.tronto.net/blog/$i"
		#cont="$(sed -n '/<main>/,/<\/main>/p' http/blog/$i/index.html |\
			#head -n -2 | tail -n +3)"
		cont="$(feedify http/blog/$i/index.html)"

		thisyear="$(echo "$d" | sed 's/-.*//')"
		if [ "$thisyear" != "$lastyear" ]; then
			echo "<tr><td><h2>$thisyear</h2></td></tr>" >> "$bf"
			lastyear=$thisyear
		fi

		{ echo "<tr><td>$d</td>";
		  echo "<td><a href=\"$i\">$t</a></td></tr>"; } >> "$bf"

		dd="$(echo "$d" | sed 's/.*-//')"
		mm="$(echo "$d" | sed 's/....-//' | sed 's/-.*//')"
		mon="$(month "$mm")"
		{ echo "<item>";
		  echo "<title>$t</title>";
		  echo "<link>$link</link>";
		  echo "<guid isPermaLink=\"true\">$link</guid>";
		  echo "<description>$t</description>";
		  echo "<content:encoded>$cont</content:encoded>";
		  echo "<pubDate>$dd $mon $thisyear 00:00:00 GMT</pubDate>";
		  echo "</item>";
		  echo ""; } >> $ff
	done

	echo '</table>' >> "$bf"
	cat "$bottomtmp" >> "$bf"

	{ echo ""; echo "</channel>"; echo "</rss>"; } >> "$ff"
}

month() {
	case "$1" in
	01)
		echo "Jan"
		;;
	02)
		echo "Feb"
		;;
	03)
		echo "Mar"
		;;
	04)
		echo "Apr"
		;;
	05)
		echo "May"
		;;
	06)
		echo "Jun"
		;;
	07)
		echo "Jul"
		;;
	08)
		echo "Aug"
		;;
	09)
		echo "Sep"
		;;
	10)
		echo "Oct"
		;;
	11)
		echo "Nov"
		;;
	12)
		echo "Dec"
		;;
	esac
}

sed "s/LASTUPDATE/$(date +'%Y-%m-%d')/" <bottom.html >"$bottomtmp"
recursivebuild src
makeblogindexandfeed

Generated with cgit - Back to sebastiano.tronto.net