From 67a5d6cb71ccde36ffa31335d307fd82cda92861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 29 Jan 2021 20:01:12 +0000 Subject: Add hyperlink references This requires two passes, the first done by aux/emcollect, the second by aux/emparse (the main awk script). --- aux/emcollect | 18 +++++++ aux/emparse | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ em | 157 +++++-------------------------------------------------- 3 files changed, 195 insertions(+), 143 deletions(-) create mode 100755 aux/emcollect create mode 100755 aux/emparse diff --git a/aux/emcollect b/aux/emcollect new file mode 100755 index 0000000..55434ac --- /dev/null +++ b/aux/emcollect @@ -0,0 +1,18 @@ +#!/bin/awk -f + +# aux/emcollect -- collect hyperlink references in em source + +function collect(line) { + left = $0 + right = $0 + sub("^ \\[", "", left) + sub("\\].*$", "", left) + sub("^ \\[[0-9a-z]\\]+ ", "", right) + printf "%s=%s\n", left, right +} + +BEGIN { expectblock = 1 } + +/^$/ { expectblock = 1; getline } +expectblock && /^ \[[0-9a-z]\]+ / { block = "nl"; expectblock = 0 } +block = "nl" && /^ \[[0-9a-z]\]+ [^ ]+$/ { collect($0); next } diff --git a/aux/emparse b/aux/emparse new file mode 100755 index 0000000..d6073f3 --- /dev/null +++ b/aux/emparse @@ -0,0 +1,163 @@ +#!/bin/awk -f + +# aux/emparse -- parse em source + +function beginblock(name) { + if (name == "nl") printf "
    \n" + else printf "<%s>\n", name +} + +function endblock(name) { + if (name == "nl") printf "
\n" + else printf "\n", name +} + +function newblock(name) { + beginblock(name) + openblock = name + openitem = 0 + expectblock = 0 +} + +function breakblock() { + if (openitem) { + printf "\n" + while (itemlevel-- > 1) + endblock(openblock) + itemlevel = 1 + } + if (opendef) printf "\n" + if (openblock) endblock(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("^ +- ", "") + printf "
  • " + } + if (type == "ol") { + match($0, "[0-9a-z]+\.") + v = substr($0, RSTART, RLENGTH-1) + sub("^ +[0-9a-z]+\. ", "") + printf "
  • ", v + } + if (type == "nl") { + match($0, "\\[[0-9a-z]+\\]") + v = substr($0, RSTART+1, RLENGTH-2) + sub("^ +\\[[0-9a-z]+\\] ", "") + printf "
  • ", v, v + } +} + +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 +} + +function ref(v) { + if (ENVIRON["ref" v] != "") + return "(" linktext ")" + else + return "[" v "]" +} + +function format(line) { + n = split(line, w, "[ ]") + for (i = 0; i <= n; i++) { + if (w[i] == "") continue; + + if (match(w[i], "^\\[[0-9a-z]+\\]$")) + w[i] = ref(substr(w[i], 2, RLENGTH-2)) + else if (match(w[i], "^\[[0-9a-z]+\][.,:;?!]$")) + w[i] = ref(substr(w[i], 2, RLENGTH-3)) substr(w[i], RLENGTH) + + if (match(w[i], "^`")) + w[i] = "" substr(w[i], 2) + else if (match(w[i], "^\\*")) + w[i] = "" substr(w[i], 2) + else if (match(w[i], "^_")) + w[i] = "" substr(w[i], 2) + + if (match(w[i], "`$")) + w[i] = substr(w[i], 1, RSTART-1) "" + else if (match(w[i], "\\*$")) + w[i] = substr(w[i], 1, RSTART-1) "" + else if (match(w[i], "_$")) + w[i] = substr(w[i], 1, RSTART-1) "" + else if (match(w[i], "`[.,:;?!]$")) + w[i] = substr(w[i], 1, RSTART-1) "" substr(w[i], RSTART+1) + else if (match(w[i], "\\*\.$")) + w[i] = substr(w[i], 1, RSTART-1) "" substr(w[i], RSTART+1) + else if (match(w[i], "_[.,:;?!]$")) + w[i] = substr(w[i], 1, RSTART-1) "" substr(w[i], RSTART+1) + + printf "%s", w[i] + if (i < n) printf " " + } + printf "\n" +} + +BEGIN { + expectblock = 1 + itemlevel = 1 + linktext = ENVIRON["linktext"] + if (!linktext) linktext = "link" +} +END { breakblock() } + +/^$/ { breakblock(); getline } +expectblock && /^ / { newblock("table") } +expectblock && /^ - / { newblock("ul") } +expectblock && /^ [0-9a-z]+\. / { newblock("ol") } +expectblock && /^ \[[0-9a-z]\]+ / { newblock("nl") } +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-9a-z]+\. / { item(1, "ol", line) } +block = "nl" && /^ \[[0-9a-z]\]+ / { item(1, "nl", line) } +block = "dl" && /^ .*: / { term(line) } + +block = "ul" && /^ -/ { item(2, "ul", line) } +block = "ol" && /^ [0-9a-z]+\./ { item(2, "ol", line) } +block = "ul" && /^ -/ { item(3, "ul", line) } +block = "ol" && /^ [0-9a-z]+\./ { item(3, "ol", line) } +block = "ul" && /^ -/ { item(4, "ul", line) } +block = "ol" && /^ [0-9a-z]+\./ { item(4, "ol", line) } +block = "ul" && /^ -/ { item(5, "ul", line) } +block = "ol" && /^ [0-9a-z]+\./ { item(5, "ol", line) } +block = "ul" && /^ -/ { item(6, "ul", line) } +block = "ol" && /^ [0-9a-z]+\./ { item(6, "ol", line) } + +block = "pre" && /^ / { sub("^ ", ""); printf "%s\n", $0; next } + +{ format($0) } diff --git a/em b/em index 010fa63..dc8684e 100755 --- a/em +++ b/em @@ -1,150 +1,21 @@ -#!/bin/awk -f +#!/bin/rc # em -- limited hypertext markup language -function newblock(name) { - openblock = name - openitem = 0 - expectblock = 0 - printf "<%s>", name +if(~ $#* 0){ + ramfs -p + file=/tmp/file + cat > $file } +if not + file=$1 -function closeblock(name) { - if (name == "nl") printf "\n" - else printf "\n", name +linktext=link +hrefs=`' +'{aux/emcollect $file} +for(href in $hrefs){ + parts=`'='{echo $href} + ref$parts(1)=$parts(2) } -function breakblock() { - if (openitem) { - printf "
  • \n" - while (itemlevel-- > 1) - closeblock(openblock) - itemlevel = 1 - } - if (opendef) printf "\n" - if (openblock) closeblock(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("^ +- ", "") - printf "
  • " - } - if (type == "ol") { - match($0, "[0-9]+\.") - n = substr($0, RSTART, RLENGTH-1) - sub("^ +[0-9]+\. ", "") - printf "
  • ", n - } - if (type == "nl") { - match($0, "\[[0-9]+\]") - n = substr($0, RSTART+1, RLENGTH-2) - sub("^ +\[[0-9]+\] ", "") - printf "
  • ", n, n - } -} - -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 -} - -function ref(n) { - return "[" n "]" -} - -function format(line) { - n = split(line, w, "[ ]") - for (i = 0; i <= n; i++) { - if (w[i] == "") continue; - - if (match(w[i], "^\[[0-9]+\]$")) - w[i] = ref(substr(w[i], 2, RLENGTH-2)) - else if (match(w[i], "^\[[0-9]+\][.,:;?!]$")) - w[i] = ref(substr(w[i], 2, RLENGTH-3)) substr(w[i], RLENGTH) - - if (match(w[i], "^`")) - w[i] = "" substr(w[i], 2) - else if (match(w[i], "^\\*")) - w[i] = "" substr(w[i], 2) - else if (match(w[i], "^_")) - w[i] = "" substr(w[i], 2) - - if (match(w[i], "`$")) - w[i] = substr(w[i], 1, RSTART-1) "" - else if (match(w[i], "\\*$")) - w[i] = substr(w[i], 1, RSTART-1) "" - else if (match(w[i], "_$")) - w[i] = substr(w[i], 1, RSTART-1) "" - else if (match(w[i], "`[.,:;?!]$")) - w[i] = substr(w[i], 1, RSTART-1) "" substr(w[i], RSTART+1) - else if (match(w[i], "\\*\.$")) - w[i] = substr(w[i], 1, RSTART-1) "" substr(w[i], RSTART+1) - else if (match(w[i], "_[.,:;?!]$")) - w[i] = substr(w[i], 1, RSTART-1) "" substr(w[i], RSTART+1) - - printf "%s", w[i] - if (i < n) printf " " - } - printf "\n" -} - -BEGIN { expectblock = 1; itemlevel = 1 } -END { breakblock() } - -/^$/ { breakblock(); getline } -expectblock && /^ / { newblock("table") } -expectblock && /^ - / { newblock("ul") } -expectblock && /^ [0-9]+\. / { newblock("ol") } -expectblock && /^ \[[0-9]\]+ / { newblock("nl") } -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 = "nl" && /^ \[[0-9]\]+ / { item(1, "nl", 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) } - -block = "pre" && /^ / { sub("^ ", ""); printf "%s\n", $0; next } - -{ format($0) } +aux/emparse $file -- cgit v1.2.3