diff options
author | John Ankarström <john@ankarstrom.se> | 2021-01-29 17:29:10 +0000 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-01-29 17:29:10 +0000 |
commit | bead1d53e5025ceb35ac385e49cb1ab143ba3fdd (patch) | |
tree | 6ea982e441fd4af36cc630ca8fca7c6ce36c7636 | |
download | em-bead1d53e5025ceb35ac385e49cb1ab143ba3fdd.tar.gz |
Add block-level elements
-rwxr-xr-x | em | 88 |
1 files changed, 88 insertions, 0 deletions
@@ -0,0 +1,88 @@ +#!/bin/awk -f + +# em -- limited hypertext markup language + +function newblock(name) { + openblock = name + openitem = 0 + expectblock = 0 + printf "<%s>", name +} + +function breakblock() { + if (openitem) { + printf "</li>\n" + while (itemlevel-- > 1) + printf "</%s>\n", openblock + itemlevel = 1 + } + if (opendef) printf "</dd>\n" + if (openblock) printf "</%s>\n", openblock + openitem = 0 + opendef = 0 + openblock = 0 + expectblock = 1 +} + +function heading(level, line) { + sub("^=* ", "", line) + sub(" =*$", "", line) + printf "<h%d>%s</h%d>\n", level, line, level + # should inline formatting be supported in headings? +} + +function item(level, type, line) { + if (openitem) printf "</li>" + openitem = 1 + if (level > itemlevel) printf "<ul>\n" + if (level < itemlevel) printf "</ul>\n" + itemlevel = level + if (type == "ul") sub("^ +- ", "") + if (type == "ol") sub("^ +[0-9]+\. ", "") + printf "<li>" +} + +function term(line, t) { # t is a local variable + if (opendef) printf "</dd>" + opendef = 1 + t = $0; d = $0 + sub("^ ", "", t) + sub(": .*$", "", t) + sub("^ [^:]+: ", "") + printf "<dt>%s</dt><dd>", t +} + +BEGIN { expectblock = 1; itemlevel = 1 } +END { breakblock() } + +/^$/ { breakblock(); getline } +expectblock && /^ / { newblock("table") } +expectblock && /^ - / { newblock("ul") } +expectblock && /^ [0-9]+\. / { newblock("ol") } +expectblock && /^ .*: / { newblock("dl") } +expectblock && /^ / { newblock("pre"); sub("^ ", "") } +expectblock && /^---$/ { expectblock = 0; printf "<hr/>\n"; next } +expectblock && /^= .* =$/ { heading(1, $0); next } +expectblock && /^== .* ==$/ { heading(3, $0); next } +expectblock && /^=== .* ===$/ { heading(3, $0); next } +expectblock && /^==== .* ====$/ { heading(4, $0); next } +expectblock && /^===== .* =====$/ { heading(5, $0); next } +expectblock && /^====== .* ======$/ { heading(6, $0); next } +expectblock { newblock("p") } + +block = "ul" && /^ -/ { item(1, "ul", line) } +block = "ol" && /^ [0-9]+\./ { item(1, "ol", line) } +block = "dl" && /^ .*:/ { term(line) } + +block = "ul" && /^ -/ { item(2, "ul", line) } +block = "ol" && /^ [0-9]+\./ { item(2, "ol", line) } +block = "ul" && /^ -/ { item(3, "ul", line) } +block = "ol" && /^ [0-9]+\./ { item(3, "ol", line) } +block = "ul" && /^ -/ { item(4, "ul", line) } +block = "ol" && /^ [0-9]+\./ { item(4, "ol", line) } +block = "ul" && /^ -/ { item(5, "ul", line) } +block = "ol" && /^ [0-9]+\./ { item(5, "ol", line) } +block = "ul" && /^ -/ { item(6, "ul", line) } +block = "ol" && /^ [0-9]+\./ { item(6, "ol", line) } + +{ print $0 } |