diff options
Diffstat (limited to 'emparse')
-rwxr-xr-x | emparse | 75 |
1 files changed, 51 insertions, 24 deletions
@@ -2,15 +2,20 @@ # aux/emparse -- parse em source -BEGIN { expectblock = 1; itemlevel = 1 } +BEGIN { + expectblock = 1 + itemlevel = 1 + for (n=0; n < 256; n++) + ord[sprintf("%c", n)] = n +} END { breakblock() } /^$/ { breakblock(); getline } expectblock && /^ / { newblock("table") } expectblock && /^ > / { newblock("blockquote") } expectblock && /^ - / { newblock("ul") } -expectblock && /^ [0-9]+\. / { newblock("ol") } -expectblock && /^ \[[0-9]+\] / { newblock("nl") } +expectblock && /^ [0-9a-z]+\. / { newblock("ol") } +expectblock && /^ \[[0-9a-z]+\] / { newblock("nl") } expectblock && /^ .*: / { newblock("dl") } expectblock && /^ / { newblock("pre") } expectblock && /^---$/ { expectblock = 0; printf "<hr/>\n"; next } @@ -26,20 +31,20 @@ openblock == "pre" { sub("^ ", ""); $0 = escape($0); printf "%s\n", $0; next } openblock == "blockquote" && /^ > / { item(1, "blockquote", line) } openblock == "ul" && /^ - / { item(1, "ul", line) } -openblock == "ol" && /^ [0-9]+\. / { item(1, "ol", line) } -openblock == "nl" && /^ \[[0-9]+\] / { item(1, "nl", line) } +openblock == "ol" && /^ [0-9a-z]+\. / { item(1, "ol", line) } +openblock == "nl" && /^ \[[0-9a-z]+\] / { item(1, "nl", line) } openblock == "dl" && /^ .*: / { term(line) } -(openblock == "ul" || openblock == "ol") && /^ -/ { item(2, "ul", line) } -(openblock == "ul" || openblock == "ol") && /^ [0-9]+\./ { item(2, "ol", line) } -(openblock == "ul" || openblock == "ol") && /^ -/ { item(3, "ul", line) } -(openblock == "ul" || openblock == "ol") && /^ [0-9]+\./ { item(3, "ol", line) } -(openblock == "ul" || openblock == "ol") && /^ -/ { item(4, "ul", line) } -(openblock == "ul" || openblock == "ol") && /^ [0-9]+\./ { item(4, "ol", line) } -(openblock == "ul" || openblock == "ol") && /^ -/ { item(5, "ul", line) } -(openblock == "ul" || openblock == "ol") && /^ [0-9]+\./ { item(5, "ol", line) } -(openblock == "ul" || openblock == "ol") && /^ -/ { item(6, "ul", line) } -(openblock == "ul" || openblock == "ol") && /^ [0-9]+\./ { item(6, "ol", line) } +(openblock == "ul" || openblock == "ol") && /^ -/ { item(2, "ul", line) } +(openblock == "ul" || openblock == "ol") && /^ [0-9a-z]+\./ { item(2, "ol", line) } +(openblock == "ul" || openblock == "ol") && /^ -/ { item(3, "ul", line) } +(openblock == "ul" || openblock == "ol") && /^ [0-9a-z]+\./ { item(3, "ol", line) } +(openblock == "ul" || openblock == "ol") && /^ -/ { item(4, "ul", line) } +(openblock == "ul" || openblock == "ol") && /^ [0-9a-z]+\./ { item(4, "ol", line) } +(openblock == "ul" || openblock == "ol") && /^ -/ { item(5, "ul", line) } +(openblock == "ul" || openblock == "ol") && /^ [0-9a-z]+\./ { item(5, "ol", line) } +(openblock == "ul" || openblock == "ol") && /^ -/ { item(6, "ul", line) } +(openblock == "ul" || openblock == "ol") && /^ [0-9a-z]+\./ { item(6, "ol", line) } { format($0) } # inline formatting @@ -56,11 +61,11 @@ function format(line) { else if (match(w[i], "^<(\\./.*|\\.\\./.*|#.*|.*/.*)>\\)[.,:;?!]$")) w[i] = link(substr(w[i], 5, RLENGTH-10)) substr(w[i], RLENGTH-1) - if (match(w[i], "^\\[[0-9]+\\]$")) + if (match(w[i], "^\\[[0-9a-z]+\\]$")) w[i] = ref(substr(w[i], 2, RLENGTH-2)) - else if (match(w[i], "^\\[[0-9]+\\][.,:;?!)]$")) + else if (match(w[i], "^\\[[0-9a-z]+\\][.,:;?!)]$")) w[i] = ref(substr(w[i], 2, RLENGTH-3)) substr(w[i], RLENGTH) - else if (match(w[i], "^\\[[0-9]+\\]\\)[.,:;?!]$")) + else if (match(w[i], "^\\[[0-9a-z]+\\]\\)[.,:;?!]$")) w[i] = ref(substr(w[i], 2, RLENGTH-4)) substr(w[i], RLENGTH-1) if (!openformat) { @@ -186,16 +191,18 @@ function item(level, type, line) { printf "<li>" } if (type == "ol") { - match($0, "[0-9]+\.") + match($0, "[0-9a-z]+\.") v = substr($0, RSTART, RLENGTH-1) - sub("^ +[0-9]+\. ", "") - printf "<li value=\"%s\">", v + sub("^ +[0-9a-z]+\. ", "") + listvalid(v) + printf "<li value=\"%s\" style=\"list-style-type: %s\">", listnum(v), listtype(v) } if (type == "nl") { - match($0, "\\[[0-9]+\\]") + match($0, "\\[[0-9a-z]+\\]") v = substr($0, RSTART+1, RLENGTH-2) - sub("^ \\[[0-9]+\\] ", "") - printf "<li value=\"%s\" id=\"ref%s\">", v, v + sub("^ \\[[0-9a-z]+\\] ", "") + listvalid(v) + printf "<li value=\"%s\" style=\"list-style-type: %s\" id=\"ref%s\">", listnum(v), listtype(v), v } } @@ -220,6 +227,26 @@ function link(h) { return "<a href=\"" h "\">" h "</a>" } +function listvalid(v) { + if (match(v, "^[0-9]+$")) return + if (match(v, "^[a-z]$")) return + if (match(v, "^[A-Z]$")) return + printf "error: '%s' is not a valid list item value\n", v > "/dev/stderr" + exit 1 +} + +function listnum(v) { + if (match(v, "^[0-9]+$")) return v + if (match(v, "^[a-z]$")) return ord[v]-96 + if (match(v, "^[A-Z]$")) return ord[v]-64 +} + +function listtype(v) { + if (match(v, "^[0-9]+$")) return "decimal" + if (match(v, "^[a-z]$")) return "lower-alpha" + if (match(v, "^[A-Z]$")) return "upper-alpha" +} + function escape(s) { gsub("&", "\\&", s) gsub("<", "\\<", s) |