aboutsummaryrefslogtreecommitdiff
path: root/barmark-html
blob: 419491266af61c7247c6c25e0af93953a738ed10 (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
function findtag(t, reg) {
    while(match($0, reg)) {
        sub(reg, simpletag(t, stylize(substr($0, RSTART+2, RLENGTH-3))), $0);
        changes = 1;
    }
}

function stylize(str) {
    gsub(/&/, "\\\\\\&", str);
    if(match(str, /^\[[^\]]*\]/)) {
        style = " class=\"" substr(str, RSTART+1, RLENGTH-2) "\"";
        return substr(str, RLENGTH+2);
    }
    style = "";
    return substr(str, 2);
}

function simpletag(t, str) {
    return "<" t style ">" str "</" t ">";
}

function link(str) {
    i = index(str, " ");
    return "<a href=\"" substr(str, 1, i-1) "\"" style ">" substr(str, i+1) "</a>"
}

function img(str) {
    i = index(str, " ")
    return "<img src=\"" substr(str, 1, i-1) "\" alt=\"" substr(str, i+1) "\"" style " />"
}

function replacetags() {
    changes = 0;
    while(match($0, /\|a(\[[^\]]*\])? [^ ]+ [^\|]+\|/)) {
        sub(/\|a(\[[^\]]*\])? [^ ]+ [^\|]+\|/,
            link(stylize(substr($0, RSTART+2, RLENGTH-3))), $0);
        changes = 1;
    }
    while(match($0, /\|img(\[[^\]]*\])? [^ ]+ [^\|]+\|/)) {
        sub(/\|img(\[[^\]]*\])? [^ ]+ [^\|]+\|/,
            img(stylize(substr($0, RSTART+4, RLENGTH-5))), $0);
        changes = 1;
    }
    findtag("strong", "\\|b(\\[[^\\]]*\\])? [^\\|]+\\|");
    findtag("em", "\\|i(\\[[^\\]]*\\])? [^\\|]+\\|");
    findtag("span", "\\|s(\\[[^\\]]*\\])? [^\\|]+\\|");
}

BEGIN {
    tag = "p";
    block = "";
    changes = 0;
    style = "";
}

/^\|code$/ {
    block = "code";
    print "<pre>";
    next;
}

/^code\$\|$/ {
    block = "";
    print "</pre>";
    next;
}

block == "code" {
    print;
    next;
}

/^\|#.*\|/ {
    next;
}

/^\|\^ ([^\$]*[^\|]*)+\$\|$/ {
    sub(/^\|\^ /, "", $0);
    sub(/\$\|$/, "", $0);
    replacetags();
    while(changes) { replacetags() }
    print;
    next;
}

/^\|h[1-6](\[[^\]]*\])? [^\|]+\|$/ {
    print simpletag("h" substr($0, 3, 1),
        stylize(substr($0, 4, length($0) - 4)));
    next;
}

/^\|div(\[[^\]]*\])?$/ {
    stylize(substr($0, 5));
    print "<div" style ">" ;
    next;
}

/^div\$\|$/ {
    print "</div>" ;
    next;
}

/^\|list(\[[^\]]*\])?$/ {
    block = "list";
    stylize(substr($0, 6));
    print "<ul" style ">" ;
    next;
}

/^list\$\|$/ {
    block = "";
    print "</ul>" ;
    next;
}

NF {
    if(block == "list") { tag  = "li" }
    replacetags();
    while(changes) { replacetags() }
    print "<" tag ">" $0 "</" tag ">";
    tag = "p";
}