summaryrefslogtreecommitdiff
path: root/src/md2html.sh
blob: 13719e4f36ed1bad333bc62f046f136754955fa5 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/bin/sh

ESC_SEQ='\0'

# remove traling whitespace from empty lines
#
_pre_strip () {
    while IFS= read -r line; do
        set -- $line
        [ "$*" ] && {
            local l c
            l="$line"
            line=
            while [ "$l" != "${l#?}" ]; do
                c="${l%*"${l#?}"}"
                case "$c" in
                    "    ") line="$line    ";;
                    *) line="$line$c" ;;
                esac
                l="${l#?}"
            done

            printf "%s\n" "$line"
        } || printf "\n"
    done
}

# replace all * with _ for easier processing
#
_pre_emph () {
    while IFS= read -r line; do
        case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac
        while [ "$line" != "${line%%\**}" ]; do
            printf "%s_" "${line%%\**}"
            line="${line#*\*}"
        done
        printf "%s\n" "${line}"
    done
}


# fix any misaligned <em> and <strong> closign tags
#
_post_emph () {
    while IFS= read -r line; do
        case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac
        # TODO: avoid this problem entirely?

        local wrong="</strong></em>" right="</em></strong>"

        while [ "$line" != "${line%%${wrong}*}" ]; do
            printf "%s${right}" "${line%%${wrong}*}"
            line="${line#*${wrong}}"
        done
        printf "%s\n" "${line}"
    done
}

# parse emphasis in a line
#
#   emph [bound] <lefttag> <righttag>
#
_emph () {
    local bound="$1"
    local lefttag="$2" righttag="$3"

    while IFS= read -r line; do
        case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac
        next="${line}"
        line=
        rightofbold=

        while [ "$next" != "${next#*${bound}}" ]; do
            leftofbold="${next%%${bound}*}"
            rightofbold="${next#$leftofbold${bound}*}"
            bold="${rightofbold%%${bound}*}"
            next="${rightofbold#*${bound}}"
            printf "%s%s%s%s" "${leftofbold}" "${lefttag}" "${bold}" "${righttag}"
        done
        printf "%s\n" "${next}"
    done
}

# parse heading
#
#   h [heading no.]
#
_h () {
    local num=$1
    while IFS= read -r line; do
        case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac
        s=

        n=$num
        while [ "$n" -gt "0" ]; do
            s="#$s"
            n=$((n-1))
        done

        case "$line" in
            "$s "*)
                printf "<h$num>%s</h$num>\n" "${line#$s }"
                ;;
            *)
                printf "%s\n" "$line"
                ;;
        esac
    done
}

# parse paragraphs
#
_p () {
    local empty=true
    while IFS= read -r line; do
        case "$line" in
            "$ESC_SEQ"*) printf "%s\n" "$line" && continue;;
            "#"*|">"*|'``'*|'<'*'>'*)
                $empty || {
                    printf "</p>\n"
                    empty=true 
                }
                printf "%s\n" "$line"
                ;;
            "") 
                $empty || {
                    printf "</p>\n"
                    empty=true 
                }
                ;;
            *) 
                $empty &&
                    printf "<p>\n%s\n" "$line" ||
                    printf "%s\n" "$line"

                empty=false ;;
        esac

    done

    $empty || {
        printf "</p>\n"
    }
}

# parse ref-style links
#
#_ref () {
#}

# parse links
#
_a_img () {
    local open="[" mid="](" close=")"
    while IFS= read -r line; do
        case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac
        next="$line"
        while [ "$next" != "${next#*$close}" ]; do
            case "$next" in
                *"["*"]("*")"*);;
                *) break;
            esac
            before="${next%%$open*}"
            text=${next#*$open} text=${text%%$mid*}
            url=${next#*$mid} url=${url%%$close*}

            title=${url#* } url=${url%% *}

            [ "$title" != "$url" ] \
                && title=" title=$title" \
                || title=

            case "$before" in
                *!) h="%s\n%s<img src=\"%s\"%s alt=\"%s\"></img>\n" 
                    before="${before%!}" ;;
                *) h="%s\n%s<a href=\"%s\"%s>\n%s</a>\n" ;;
            esac

            printf "$h" "$before" "$ESC_SEQ" "$url" "$title" "$text"

            next="${next#*$close}"
        done
        printf "%s\n" "$next";
    done
}

# get indentation level of a line
#
_get_indent () {
    indent=0
    l="$*"
    while [ "$l" ]; do
        c="${l%*"${l#?}"}"
        case "$c" in
            " ") indent=$((indent+1)) ;;
            *) 
                l="${l#?}"
                break 
            ;;
        esac
        l="${l#?}"
    done
    printf "$indent"
}

# print a string x times
#
#   print_x x "string"
print_x () {
    x=$1; shift
    until [ "$((x=x-1))" -lt "0" ]; do
        printf "$*"
    done
}


# parse unordered lists
#
_ul () {
    local indent_level=-1
    local to_close=0
    while IFS= read -r line; do
        case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac
        set -- $line
        case "$1" in 
            "-"|"_"|"+")
                indent=$(_get_indent "$line")

                [ "$indent_level" -lt "$indent" ] && {
                    printf "<ul>\n"
                    to_close=$((to_close+1))
                }
                [ "$indent_level" -gt "$indent" ] && {
                    printf "</ul>\n" 
                    to_close=$((to_close-1))
                }
                indent_level=$indent

                printf "<li>%s</li>\n" "${line#*$1 }"
                ;;
            *) 
                [ $to_close -gt 0 ] && {
                    print_x $to_close "</ul>\n"
                    to_close=0
                    indent_level=-1
                }
                printf "%s\n" "$line"
                ;;
        esac
    done
    print_x $to_close "</ul>\n"
}

# parse ordered lists
#
_ol () {
    local indent_level=-1
    local to_close=0
    while IFS= read -r line; do
        case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac
        set -- $line
        case "$1" in 
            *.|*\))
                indent=$(_get_indent "$line")

                [ "$indent_level" -lt "$indent" ] && {
                    printf "<ol>\n"
                    to_close=$((to_close+1))
                }
                [ "$indent_level" -gt "$indent" ] && {
                    printf "</ol>\n" 
                    to_close=$((to_close-1))
                }
                indent_level=$indent

                printf "<li>%s</li>\n" "${line#*$1 }"
                ;;
            *) 
                [ $to_close -gt 0 ] && {
                    print_x $to_close "</ol>\n"
                    to_close=0
                    indent_level=-1
                }
                printf "%s\n" "$line"
                ;;
        esac
    done
    print_x $to_close "</ol>\n"
}

# parse inline codeblocks
#
_inline_code () {
    _emph '`' "
$ESC_SEQ<code>" "</code>
"
}

# parse multiline codeblocks
#
_code () {
    local codeblock=false content=true
    while IFS= read -r line; do
        case "$line" in
            "    "*)
                # prefix lines with newline to avoid trailing newline
                $codeblock &&
                    printf "\n%s" "$ESC_SEQ${line#    }" ||
                $content || {
                    printf "%s<pre><code>%s" "$ESC_SEQ" "${line#    }"
                    codeblock=true
                }
                ;;
            "")
                $codeblock \
                    && printf "\n%s" "$ESC_SEQ" \
                    || printf "\n"
                    ;;
            *)
                $codeblock && {
                    printf "</code></pre>\n"
                    codeblock=false
                }

                printf "%s\n" "$line"
                ;;
        esac
        case "$line" in
            "") content=false ;;
            *) content=true ;;
        esac
    done
}

# parse quotes
#
_blockquote () {
    local indent_level=0
    while IFS= read -r line; do
        case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac
        set - $line
        case "$1" in
            ">"*)
                indent=0
                while [ "$line" ]; do
                    c="${line%*${line#?}}"
                    case "$c" in
                        ">") indent=$((indent+1)) ;;
                        " "*) 
                            line="${line#?}"
                            break
                        ;;
                    esac
                    line="${line#?}"
                done

                print_x $((indent-indent_level)) "<blockquote>\n"
                print_x $((indent_level-indent)) "</blockquote>\n"
                indent_level=$indent
                ;;
        esac
        printf "%s\n" "$line"
    done
    print_x $((indent_level)) "</blockquote>\n"
}

# add html header
#
_html () {
    printf "<!DOCTYPE html>\n"
    while IFS= read -r line; do
        printf "%s\n" "$line"
    done
}

# remove all unecessary newlines 
# 
_squash () {
    while IFS= read -r line; do
        case "$line" in
            "$ESC_SEQ"*)
                printf "\n%s" "${line#??}"
                ;;
            *)
                printf "%s" "$line"
                ;;
        esac
    done
    printf "\n"
}


# convert the markdown from stdin into html
#
md2html () {
    # the order of these somewhat matters
            _pre_strip \
            | _code \
            | _pre_emph \
            | _blockquote \
            | _ul \
            | _ol \
            | _p \
            | _a_img \
            | _inline_code \
            | _emph '__' "<strong>" "</strong>" \
            | _emph '_' "<em>" "</em>" \
            | _post_emph \
            | _h 6 \
            | _h 5 \
            | _h 4 \
            | _h 3 \
            | _h 2 \
            | _h 1 \
            | _squash \
            | _html
}

md2html