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

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
}

copyfile() {
	file=$1
	dest=$2
	ind=$dest/index.html
	extension=$(echo "$file" | sed 's/.*\.//')
	case "$extension" in
	md)
		t="$(markdowntitle "$file")"
		sed "s/TITLE/$t/" < top.html > "$ind"
		lowdown "$file" >> "$ind"
		cat bottom.html >> "$ind"
		;;
	html)
		t="$(htmltitle "$file")"
		cat top.html "$file" bottom.html | sed "s/TITLE/$t/" > "$ind"
		;;
	*)
		cp "$file" "$dest/$(basename "$file")"
	esac
}

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

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

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="$(head -n 1 $f | sed 's/# //')"

		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"

		{ echo "<item>";
		  echo "<title>$t</title>";
		  echo "<link>https://sebastiano.tronto.net/blog/$i</link>";
		  echo "<description>$t</description>";
		  echo "<pubDate>$d</pubDate>";
		  echo "</item>";
		  echo ""; } >> $ff
	done

	echo '</table>' >> "$bf"
	cat bottom.html >> "$bf"

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

makeblogindexandfeed
recursivebuild src

Generated with cgit - Back to sebastiano.tronto.net