summaryrefslogtreecommitdiff
path: root/bin/markup
blob: 5b2ca88fb63b6ae62f9e20cb1059311bc82767d1 (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
#!/bin/gawk -f

BEGIN {
    list = ""
    quote = 0
    code = 0
}

function handle_meta(    i, n, cats, link)
{
    if ($0 ~ /^; /) {
        if (meta_count == 0) {
        } else if (meta_count == 1) {
            sub(/^; /, "")
            n = split($0, cats, " ")
            printf "<nav class=\"categories\">"
            printf "#<a class=\"category\" href=\"/writings/\">all</a> "
            for (i = 1; i <= n; i++) {
                link = "/writings/" cats[i]
                printf "<a class=\"category\" href=\"" link "\">#" cats[i] "</a> "
            }
            print "</nav>"
        } else if (meta_count == 2) {
            sub(/^; /, "")
            print "<p class=\"date\">" $0 "</p>"
        }
        meta_count++
        next
    }
}
function handle_list_item(condition, type)
{
    if ($0 ~ condition) {
        if (list != type) {
            if (list != "") print "</" list ">"
            print "<" type ">"
            list = type
        }
        sub(condition, "")
        print "<li>" $0 "</li>"
        next
    }
}

function handle_quote()
{
    if ($0 == "\"\"\"") {
        if (quote == 0) {
            print "<blockquote>"
            quote = 1
        } else {
            print "</blockquote>"
            quote = 0
        }
        next
    }
}

function handle_codeblock()
{
    if ($0 ~ /^'''/) {
        if (code == 0) {
            if (list != "") {
                print "</" list ">"
                list = ""
            }
            print "<pre><code>"
            code = 1
        } else {
            print "</code></pre>"
            code = 0
        }
        next
    }

    if (code == 1) {
        gsub(/&/, "\\&amp;")
        gsub(/</, "\\&lt;")
        gsub(/>/, "\\&gt;")
        print
        next
    }
}

function inline(delim, tag, escaped, pattern)
{
    escaped = delim
    gsub(/\*/, "\\*", escaped)
    gsub(/\./, "\\.", escaped)

    pattern = escaped "([^" escaped "]+)" escaped
    while (match($0, pattern)) {
        $0 = substr($0, 1, RSTART-1) \
             "<" tag ">" substr($0, RSTART+length(delim), \
             RLENGTH-length(delim)*2) "</" tag ">" \
             substr($0, RSTART+RLENGTH)
    }
}

function inline_link(pattern, result, pre, arr)
{
    pattern = "\\[([^]]+)\\]\\(([^)]+)\\)"
    result = ""
    while (match($0, pattern, arr)) {
        pre    = substr($0, 1, RSTART-1)
        result = result pre "<a href=\"" arr[2] "\">" arr[1] "</a>"
        $0     = substr($0, RSTART+RLENGTH)
    }
    $0 = result $0
}

function inline_image(pattern, result, pre, arr)
{
    pattern = "!\\[([^]]+)\\]\\(([^)]+)\\)"
    result = ""
    while (match($0, pattern, arr)) {
        pre    = substr($0, 1, RSTART-1)
        result = result pre "<a href=\"" arr[2] "\">" "<img alt=\"" \
             arr[1] "\" src=\"" arr[2] "\">" "</a>"
        $0     = substr($0, RSTART+RLENGTH)
    }
    $0 = result $0
}

function inline_footnote_ref(pattern, result, pre, arr)
{
    pattern = ".\\[\\^([^]]+)\\]"
    result = ""
    while (match($0, pattern, arr)) {
        pre    = substr($0, 1, RSTART)
        result = result pre "<sup id=\"fnref-" \
             arr[1] "\"><a href=\"#fn-" arr[1] "\">" arr[1] "</a></sup>"
        $0     = substr($0, RSTART+RLENGTH)
    }
    $0 = result $0
}

function handle_header(i, level)
{
    if ($0 ~ /^#+[ ]/) {
        level = 0
        while (substr($0, level+1, 1) == "#") level++
        sub(/^#+[ ]+/, "")
        print "<h" level ">" $0 "</h" level ">"
        next
    }
}

function handle_footnote(pattern, arr)
{
    pattern = "^\\[\\^([^]]+)\\]:[ ]*(.*)"
    if (match($0, pattern, arr)) {
        if (footnote != 1) {
            print "<ol class=\"footnotes\">"
            footnote = 1
        }
        print "<li id=\"fn-" arr[1] "\">" arr[2] \
            "<a href=\"#fnref-" arr[1] "\">↩</a></li>"
        list = "ol"
        next
    }
}

{
    handle_meta()
    handle_quote()
    handle_codeblock()

    inline("**", "b")
    inline("*", "em")
    inline("--", "s")
    inline("'''", "code")

    inline_image()
    inline_link()
    
    inline_footnote_ref()

    handle_list_item("^- ", "ul")
    handle_list_item("^[0-9]+\\.[ ]+", "ol")
    handle_footnote()

    if (list != "") {
        print "</" list ">"
        list = ""
    }

    handle_header()

    if ($0 != "") {
        print "<p>" $0 "</p>"
    }
}

END {
    if (list != "")
        print "</" list ">"
}