summaryrefslogtreecommitdiff
path: root/mh.awk
blob: a961ca185bcc3a40875044a6030ce85fa9c28183 (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
#!/bin/awk -f
function eschtml(t) {
gsub("<", "\\&lt;", t);
gsub("%", "\\&#37;", t);
 return t; }
function oprint(t) { if(nr == 0) otext = otext t; else otext = otext t; }
function nextil(t) {
        if(!match(t, /[`<\[*_\\]|(\!\[)/)) return t
        t1 = substr(t, 1, RSTART - 1)
        tag = substr(t, RSTART, RLENGTH)
        t2 = substr(t, RSTART + RLENGTH)
        if(ilcode && tag != "`")
                return eschtml(t1 tag) nextil(t2);
        if(tag == "`"){
                if(sub(/^`/, "", t2)){
                        if(!match(t2, /``/))
                                return t1 "&#8221;" nextil(t2);
                        ilcode2 = !ilcode2;
                }
                else if(ilcode2)
                        return t1 tag nextil(t2);
                tag = "<pre>";
                if(ilcode){
#                       t1 = eschtml(t1);
                        tag = "</pre>";
                }
                ilcode = !ilcode;
                return t1 tag nextil(t2);
        }
        if(tag == "\\"){ if(match(t2, /^[\\*_{}\[\]()#+\-\.!]/)){ tag = substr(t2, 1, 1)
        t2 = substr(t2, 2); }
        return t1 tag nextil(t2); }
        if(tag == "<"){
                if(match(t2, /^[A-Za-z\/!][^>]*>/)){
                        tag = tag substr(t2, RSTART, RLENGTH)
                        t2 = substr(t2, RLENGTH + 1)
                        return t1 tag nextil(t2);
                }
                return t1 "&lt;" nextil(t2);
        }
        if(tag == "["){
                if(!match(t2, /(\[.*\])|(\(.*\))/)) return t1 tag nextil(t2); match(t2, /^[^\]]*(\[[^\]]*\][^\]]*)*/)
                linktext = substr(t2, 1, RLENGTH)
                t2 = substr(t2, RLENGTH + 2); if(match(t2, /^\(/)){ match(t2, /^[^\)]+(\([^\)]+\)[^\)]*)*/)
                url = substr(t2, 2, RLENGTH - 1)
                pt2 = substr(t2, RLENGTH + 2)
                title = ""
                if(match(url, /[ ]+\".*\"[ ]*$/)){ title = substr(url, RSTART, RLENGTH)
                url = substr(url, 1, RSTART - 1)
                match(title, /\".*\"/)
                title = " title=\"" substr(title, RSTART + 1, RLENGTH - 2) "\""; } if(match(url, /^<.*>$/)) url = substr(url, 2, RLENGTH - 2)
#               url = eschtml(url)
                return t1 "<a href="url""title">"nextil(linktext)"</a>" nextil(pt2); }
        }
         if(match(tag, /[*_]/)){ ntag = tag; if(sub("^" tag, "", t2)){ if(stag[ns] == tag && match(t2, "^" tag)) t2 = tag t2; else ntag = tag tag; } n = length(ntag)
        tag = (n == 2) ? "b" : "i"
        if(match(t1, / $/) && match(t2, /^ /)) return t1 tag nextil(t2)
        if(stag[ns] == ntag){ tag = "/" tag; ns--; } else stag[++ns] = ntag
        tag = "<" tag ">"
        return t1 tag nextil(t2); } }
function inline(t){ ilcode = 0; ilcode2 = 0; ns = 0; return nextil(t); }
function printp(tag){ if(!match(text, /^[ ]*$/)){ text = inline(text);if(tag != "") oprint("<" tag ">" text "</" tag ">"); else oprint(text); } text = ""; }
# function printp(tag){if(match(text,/^[ ]*$/)){text="";return}text=inline(text);if(tag=="p"){oprint("<"tag">"text)}else{oprint("<"tag">"text"</"tag">")};text=""}
# ELSE function printp(tag){if(!match(text,/^[ ]*$/)){text=inline(text);if(tag=="p"){oprint("<"tag">"text)}else{oprint("<"tag">"text"</"tag">")}}text=""}
BEGIN{blank=0;hr=0;nl=0;nr=0;text="";par="p";}
{
        for(nnl = 0; nnl < nl; nnl++)
        if((match(block[nnl + 1], /[ou]l/) && !sub(/^(    |     )/, "")) || \
        (block[nnl + 1] == "blockquote" && !sub(/^> ?/, "")))
            break;
}
nnl < nl && !blank && text && ! /^ ? ? ?([*+-]|([0-9]+\.)+)( +| )/ { nnl = nl; }
{ hr = 0; }
block[nl] ~ /[ou]l/ && /^$/ {
        blank = 1;
        next;
}
{ newli = 0; }
!hr && (nnl != nl || !text || block[nl] ~ /[ou]l/) && /^ ? ? ?[*+-]( +| )/ {
    sub(/^ ? ? ?[*+-]( +|       )/, "");
    nnl++;
    nblock[nnl] = "ul";
    newli = 1;
}
newli {
    if(blank && nnl == nl && !par)
        par = "p";
    blank = 0;
    printp(par);
    if(nnl == nl && block[nl] == nblock[nl])
         oprint("<li>");
}
blank && ! /^$/ {
    if(match(block[nnl], /[ou]l/) && !par)
        par = "p";
    printp(par);
    par = "p";
    blank = 0;
}
nnl != nl || nblock[nl] != block[nl] {
    printp(par);
    b = (nnl > nl) ? nblock[nnl] : block[nl];
    par = (match(b, /[ou]l/)) ? "" : "p";
}
nnl < nl || (nnl == nl && nblock[nl] != block[nl]) {
    for(; nl > nnl || (nnl == nl && pblock[nl] != block[nl]); nl--){
        oprint("</" block[nl] ">");
    }
}
nnl > nl {
    for(; nl < nnl; nl++){
        block[nl + 1] = nblock[nl + 1];
        oprint("<" block[nl + 1] ">");
        if(match(block[nl + 1], /[ou]l/))
            oprint("<li>");
    }
}
/^#+/ && (!newli || par=="p" || /^##/) { for(n = 0; n < 6 && sub(/^# */, ""); n++) sub(/#$/, ""); par = "h" n; }
/^$/ { printp(par); par = "p"; next; }
{ text = (text ? text " " : "") $0; }
END{printp(par);printf(otext);printf("ENTER FOOTER HERE")}
# maybe treat par as you do #h1 so to avoid closing tag