summaryrefslogtreecommitdiff
path: root/src/md2html.sh
blob: 1c06e2da31781ba58ff8cffff08efd7530c85f89 (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
#!/bin/sh

# replace all * with _ for easier processing
#
_pre_emph () {
    while IFS= read -r line; do
        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
        # 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
        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
        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
_p () {
    empty=true
    while IFS= read -r line; do
        case "$line" in
            "#"*)
                printf "%s\n" "$line"
                ;;
            "") 
                $empty ||
                    printf "</p>\n"
                
                empty=true ;;
            *) 
                $empty &&
                    printf "<p>%s " "$line" ||
                    printf "%s " "$line"

                empty=false ;;
        esac

    done

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

# parse links
#
_a_img () {
    local open="[" mid="](" close=")"
    while IFS= read -r line; do
                next="$line"
                while [ "$next" != "${next#*$close}" ]; do
                    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<img src=\"%s\"%s alt=\"%s\"></img>" 
                            before="${before%!}" ;;
                        *) h="%s<a href=\"%s\"%s>%s</a>" ;;
                    esac

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

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

# parse unordered lists
#
_ul () {
}

# parse ordered lists
#
_ol () {
}



# convert the markdown from stdin into html
#
md2html () {

            _p \
            | _pre_emph \
            | _emph '__' "<strong>" "</strong>" \
            | _emph '_' "<em>" "</em>" \
            | _emph '`' "<code>" "</code>" \
            | _post_emph \
            | _a_img \
            | _h 6 \
            | _h 5 \
            | _h 4 \
            | _h 3 \
            | _h 2 \
            | _h 1 
}

md2html