summaryrefslogtreecommitdiff
path: root/build.sh
blob: 39a286850511f332923aa40396ff5c4e33c600ef (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
#!/bin/sh

md2html() {
# Usage:
# 	md2html file.md > file.html
# Options: -v esc=false to not escape html
	awk '
	function newblock(nblock) {
		if (text)
			print "<" block ">" text "</" block ">"
		text = ""
		block = nblock ? nblock : "p"
	}

	function subinline(tgl, inl) {
		while (match($0, tgl)){
			if (inline[ni] == inl)
				ni -= sub(tgl, "</" inl ">")
			else if (sub(tgl, "<" inl ">"))
				inline[++ni] = inl
		}
	}

	function dolink(href, lnk) {
		# Undo escaped html in uris
		gsub(/&amp;/, "\\&", href)
		gsub(/&lt;/, "<", href)
		gsub(/&gt;/, ">", href)
		# & can be tricky, and not standard:
		gsub(/&/, "\\\\\\&", href)
		gsub(/&/, "\\\\\\&", lnk)
		return "<a href=\"" href "\">" lnk "</a>"
	}

	BEGIN {
		ni = 0;	# inlines
		nl = 0;	# nested lists
		text = ""
		block = "p"
	}

	# Escape html
	esc != "false" {
		gsub("&", "\\&amp;")
		gsub("<", "\\&lt;")
		gsub(">", "\\&gt;")
	}

	# Horizontal rules (_ is not in markdown)
	/^[ 	]*([-*_] ?)+[ 	]*$/ && text == "" {
		print "<hr>"
		next
	}

	# Tables (not in markdown)
	# Syntax:
	# 		Right Align| 	Center Align	|Left Align
	/([ 	]\|)|(\|[ 	])/ {
		if (block != "table")
			newblock("table")
		nc = split($0, cells, "|")
		$0 = "<tr>\n"
		for (i = 1; i <= nc; i++){
			align = "left"
			if (sub(/^[ 	]+/, "", cells[i])){
				if (sub(/[ 	]+$/, "", cells[i]))
					align = "center"
				else
					align = "right"
			}
			sub(/[ 	]+$/,"", cells[i])
			$0 = $0 "<td align=\"" align "\">" cells[i] "</td>\n"
		}
		$0 = $0 "</tr>"
	}

	# Ordered and unordered (possibly nested) lists
	/^[ 	]*([*+-]|(([0-9]+[\.-]?)+))[ 	]/ {
		newblock("li")
		nnl = 1
		while (match($0, /^[ 	]/)){
			sub(/^[ 	]/,"")
			nnl++
		}
		while (nl > nnl)
			print "</" list[nl--] ">"
		while (nl < nnl){
			list[++nl] = "ol"
			if (match($0, /^[*+-]/))
				list[nl] = "ul"
			print "<" list[nl] ">"
		}
		sub(/^([*+-]|(([0-9]+[\.-]?)+))[ 	]/,"")
	}

	# Multi line list items
	block == "li" {
		sub(/^( *)|(	*)/,"")
	}

	# Code blocks
	/^(    |	)/ {
		if (block != "code")
			newblock("code")
		sub(/^(    |	)/, "")
		text = text $0 "\n"
		next
	}

	# Paragraphs
	/^$/ {
		newblock()
		while (nl > 0)
			print "</" list[nl--] ">"
	}

	# Headers
	/^#/ {
		newblock()
		match($0, /#+/)
		n = RLENGTH
		if (n > 6)
			n = 6
		text = substr($0, RLENGTH + 1)
		block = "h" n
		next
	}

	# Alternate headers (underlined)
	/^=+$/ {
		block = "h" 1
		next
	}

	/^-+$/ {
		block = "h" 2
		next
	}

	{
		# Images
		while (match($0, /!\[[^\]]+\]\([^\)]+\)/)){
			split(substr($0, RSTART, RLENGTH), a, /(!\[)|\)|(\]\()/)
			sub(/!\[[^\]]+\]\([^\)]+\)/, "<img src=\"" a[3] "\" alt=\"" a[2] "\">")
		}
		# Links
		while (match($0, /\[[^\]]+\]\([^\)]+\)/)){
			split(substr($0, RSTART, RLENGTH), a, /[\[\)]|(\]\()/)
			sub(/\[[^\]]+\]\([^\)]+\)/, dolink(a[3], a[2]))
		}
		# Auto links (uri matching is poor)
		na = split($0, a, /(^\()|[ 	]|([,\.\)]([ 	]|$))/)
		for (i = 1; i <= na; i++)
			if (match(a[i], /^(((https?|ftp|file|news|irc):\/\/)|(mailto:)).+$/))
				sub(a[i], dolink(a[i], a[i]))
		# Inline
		subinline("(\\*\\*)|(__)", "strong")
		subinline("\\*", "em")
		subinline("`", "code")
		text = text (text ? " " : "") $0
	}

	END {
		while (ni > 0)
			text = text "</" inline[ni--] ">"
		newblock()
		while (nl > 0)
			print "</" list[nl--] ">"
	}' "$@"
}

genhtml() (
	. ./meta
	cd "$1" || return

    . ./meta

	exec 1>index.html

	# Header
	cat <<- EOT
		<!doctype html>
		<html lang="${lang:-en}">
		<title>$title</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width">
		<link rel="stylesheet" href="/style.css">
	EOT

	# Body
	[ "$1" != . ] && echo "<a href=\"..\">$blog_title</a><hr>"
	# pandoc *.md
	md2html *.md

	# Footer
	[ "$1" = . ] && return
	echo "<hr>From: $author, $date. <a href=\"mailto:marc@vertes.org?subject=$(urlenc "Re: $link/${PWD##*/}")\">Feedback</a>"
)

urlenc() {
    while [ -n "$1" ]; do
        set -- "${1#?}" "${1%${1#?}}"
        case $2 in
        ([-._~0-9A-Za-z]) printf %c "$2" ;;
        (*)               printf %%%02x "'$2" ;;
        esac
    done
    echo
}

for d in *; do
	[ -d "$d" ] && genhtml "$d"
done
genhtml .

# Put a license in index footer.
echo '<footer>
<hr>
<a href="feed.xml">RSS feed</a>. &emsp; &emsp; Licensed under
<a href="http://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>.
</footer>' >>index.html