JHHM

site

my personal website
git clone https://jhhm.no/repos/site.git

site/bin/markup

get raw file: markup

1#!/usr/bin/awk -f
2
3BEGIN {
4    state = "NORMAL"
5}
6
7function format_inline(marker, tag, text,
8                       escaped, pattern,
9                       start, len,
10                       inner, before, after) {
11    escaped = marker
12    gsub(/[][\\.^$*+?(){}|]/, "\\\\&", escaped)
13
14    pattern = escaped "([^" substr(marker,1,1) "]+" ")" escaped
15
16    while (match(text, pattern)) {
17        start = RSTART
18        len   = RLENGTH
19
20        before = substr(text, 1, start - 1)
21        inner = substr(text, start + length(marker), len - 2 * length(marker))
22        after = substr(text, start + len)
23
24        text = before "<" tag ">" inner "</" tag ">" after
25    }
26
27    return text
28}
29
30function format_footnotes(text,
31                          start, end,
32                          before, note, id,
33                          ref) {
34    while (match(text, /\{\^[^}]+\}/)) {
35        start = RSTART
36        end   = RLENGTH
37
38        before = substr(text, 1, start - 1)
39        note = substr(text, start + 2, end - 3)
40
41        fn_count++
42        id = fn_count
43
44        fn_text[id] = note
45
46        ref = "<sup id=\"fnref-" id "\">" \
47              "<a href=\"#fn-" id "\">[" id "]</a></sup>"
48
49        text = before ref substr(text, start + end)
50    }
51
52    return text
53}
54
55function format_links(text,
56                      start, end,
57                      before, inside,
58                      label, url, mid) {
59    while (match(text, /\[[^]]+\]\([^)]*\)/)) {
60        start = RSTART
61        end   = RLENGTH
62
63        before = substr(text, 1, start - 1)
64        inside = substr(text, start, end)
65
66        match(inside, /\[[^]]+\]/)
67        label = substr(inside, RSTART + 1, RLENGTH - 2)
68
69        match(inside, /\([^)]*\)/)
70        url = substr(inside, RSTART + 1, RLENGTH - 2)
71
72        text = before "<a href=\"" url "\">" label "</a>" \
73               substr(text, start + end)
74    }
75
76    return text
77}
78
79function escape_html(text) {
80    gsub(/&/, "\\&amp;", text);
81    gsub(/</, "\\&lt;", text);
82    gsub(/>/, "\\&gt;", text);
83
84    return text
85}
86
87function apply_inline(text) {
88    text = escape_html(text)
89    text = format_footnotes(text)
90    text = format_links(text)
91
92    text = format_inline("'''", "code",   text)
93    text = format_inline("```", "code",   text)
94    text = format_inline("`",   "code",   text)
95    text = format_inline("**",  "strong", text)
96    text = format_inline("*",   "em",     text)
97
98    return text
99}
100
101function open_block(type) {
102    state = type
103
104    if (type == "PARAGRAPH")
105        printf "<p>"
106
107    else if (type == "LIST")
108        print "<ul>"
109
110    else if (type == "BLOCKQUOTE")
111        print "<blockquote>"
112
113    else if (type == "CODEBLOCK")
114        printf "<pre><code>"
115}
116
117function close_block() {
118    if (state == "PARAGRAPH")
119        print "</p>"
120
121    else if (state == "LIST")
122        print "</ul>"
123
124    else if (state == "BLOCKQUOTE")
125        print "</blockquote>"
126
127    else if (state == "CODEBLOCK")
128        print "</code></pre>"
129
130    state = "NORMAL"
131}
132
133function close_if_open() {
134    if (state != "NORMAL")
135        close_block()
136
137    if (list_level > 0) {
138       while (list_level > 0) {
139           print "</ul>"
140           list_level--
141       }
142    }   
143}
144
145function list_indent_level(line,    indent) {
146    match(line, /^ */)
147    indent = RLENGTH
148    return int(indent / 2)
149}
150
151function list_open(target,    i) {
152    for (i = list_level; i < target; i++) {
153        print "<ul>"
154        list_level++
155    }
156}
157
158function list_close(target,    i) {
159    for (i = list_level; i > target; i--) {
160        print "</ul>"
161        list_level--
162    }
163}
164
165function write_line(line) {
166    if (state == "PARAGRAPH") {
167        if (paragraph_has_content)
168            printf " "
169
170        printf "%s", apply_inline(line)
171
172        paragraph_has_content = 1
173    }
174
175    else if (state == "BLOCKQUOTE") {
176        print apply_inline(line)
177    }
178
179    else if (state == "CODEBLOCK") {
180        print escape_html(line)
181    }
182}
183
184function print_header(line,
185                      level, text) {
186    match(line, /^#+/)
187
188    level = RLENGTH
189
190    if (level > 6)
191        level = 6
192
193    text = substr(line, level + 2)
194
195    printf "<h%d>%s</h%d>\n",
196           level,
197           apply_inline(text),
198           level
199}
200
201function handle_image(line,
202                      alt, src, cap,
203                      is_banner, class) {
204    is_banner = 0
205    if (line ~ /^!\[/) {
206        is_banner = 1
207        class = "banner"
208    } else {
209        class = ""
210    }
211
212    match(line, /\[[^]]+\]/)
213    alt = substr(line, RSTART + 1, RLENGTH - 2)
214
215    match(line, /\([^)]*\)/)
216    src = substr(line, RSTART + 1, RLENGTH - 2)
217
218    cap = ""
219    if (match(line, /\{[^}]+\}/)) {
220        cap = substr(line, RSTART + 1, RLENGTH - 2)
221    }
222
223    printf "<figure"
224
225    if (class != "")
226        printf " class=\"%s\"", class
227
228    printf ">"
229
230    printf "<img src=\"%s\" alt=\"%s\">", src, alt
231
232    if (cap != "")
233        printf "<figcaption>%s</figcaption>", apply_inline(cap)
234
235    print "</figure>"
236}
237
238function print_footnotes(    i) {
239    if (fn_count == 0)
240        return
241
242    print "<ol class=\"footnotes\">"
243
244    for (i = 1; i <= fn_count; i++) {
245
246        printf "<li id=\"fn-%d\">%s" \
247               "<a href=\"#fnref-%d\">↩</a></li>\n",
248               i,
249               apply_inline(fn_text[i]),
250               i
251    }
252
253    print "</ol>"
254}
255
256{
257    line = $0
258
259    if (line ~ /^;/)
260        next
261
262    if (state == "CODEBLOCK") {
263
264        if (line ~ /^'''/ || line ~ /^```/)
265            close_block()
266        else
267            write_line(line)
268
269        next
270    }
271
272    if (state == "BLOCKQUOTE") {
273
274        if (line == "\"\"\"")
275            close_block()
276        else
277            write_line(line)
278
279        next
280    }
281
282    if (line == "") {
283        close_if_open()
284
285        paragraph_has_content = 0
286
287        next
288    }
289
290    if (line ~ /^'''/ || line ~ /^```/) {
291        close_if_open()
292
293        open_block("CODEBLOCK")
294        next
295    }
296
297    if (line == "\"\"\"") {
298        close_if_open()
299
300        open_block("BLOCKQUOTE")
301        next
302    }
303
304    if (line ~ /^</) {
305        close_if_open()
306
307        print line
308        next
309    }
310
311    if (line ~ /^!?!\[/) {
312        close_if_open()
313    
314        handle_image(line)
315    
316        next
317    }
318
319    if (line ~ /^#{1,6} /) {
320        close_if_open()
321
322        print_header(line)
323        next
324    }
325
326    if (line ~ /^ *- /) {
327        if (state == "PARAGRAPH")
328            close_block()
329    
330        level = list_indent_level(line) + 1
331    
332        list_open(level)
333        list_close(level)
334    
335        sub(/^ *- /, "", line)
336    
337        printf "<li>%s</li>\n",
338               apply_inline(line)
339    
340        next
341    }
342
343    if (state != "PARAGRAPH") {
344        close_if_open()
345
346        paragraph_has_content = 0
347
348        open_block("PARAGRAPH")
349    }
350
351    write_line(line)
352}
353
354END {
355    close_if_open()
356    
357    print_footnotes()
358}