From bead1d53e5025ceb35ac385e49cb1ab143ba3fdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 29 Jan 2021 17:29:10 +0000 Subject: Add block-level elements --- em | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 em diff --git a/em b/em new file mode 100755 index 0000000..13beb29 --- /dev/null +++ b/em @@ -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 "\n" + while (itemlevel-- > 1) + printf "\n", openblock + itemlevel = 1 + } + if (opendef) printf "\n" + if (openblock) printf "\n", openblock + openitem = 0 + opendef = 0 + openblock = 0 + expectblock = 1 +} + +function heading(level, line) { + sub("^=* ", "", line) + sub(" =*$", "", line) + printf "%s\n", level, line, level + # should inline formatting be supported in headings? +} + +function item(level, type, line) { + if (openitem) printf "" + openitem = 1 + if (level > itemlevel) printf "\n" + itemlevel = level + if (type == "ul") sub("^ +- ", "") + if (type == "ol") sub("^ +[0-9]+\. ", "") + printf "
  • " +} + +function term(line, t) { # t is a local variable + if (opendef) printf "" + opendef = 1 + t = $0; d = $0 + sub("^ ", "", t) + sub(": .*$", "", t) + sub("^ [^:]+: ", "") + printf "
    %s
    ", 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 "
    \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 } -- cgit v1.2.3