diff options
author | John Ankarström <john@ankarstrom.se> | 2021-01-29 20:01:12 +0000 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-01-29 20:01:12 +0000 |
commit | 67a5d6cb71ccde36ffa31335d307fd82cda92861 (patch) | |
tree | 04786470bfd5cb34d99bd650f301bbe1d1861c5c /em | |
parent | eb8d329d93efa56c8afa4106932b0dfe62eef50e (diff) | |
download | em-67a5d6cb71ccde36ffa31335d307fd82cda92861.tar.gz |
Add hyperlink references
This requires two passes, the first done by aux/emcollect,
the second by aux/emparse (the main awk script).
Diffstat (limited to 'em')
-rwxr-xr-x | em | 157 |
1 files changed, 14 insertions, 143 deletions
@@ -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 "</ol>\n" - else printf "</%s>\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 "</li>\n" - while (itemlevel-- > 1) - closeblock(openblock) - itemlevel = 1 - } - if (opendef) printf "</dd>\n" - if (openblock) closeblock(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("^ +- ", "") - printf "<li>" - } - if (type == "ol") { - match($0, "[0-9]+\.") - n = substr($0, RSTART, RLENGTH-1) - sub("^ +[0-9]+\. ", "") - printf "<li value=\"%s\">", n - } - if (type == "nl") { - match($0, "\[[0-9]+\]") - n = substr($0, RSTART+1, RLENGTH-2) - sub("^ +\[[0-9]+\] ", "") - printf "<li value=\"%d\" id=\"ref%d\">", n, n - } -} - -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 -} - -function ref(n) { - return "[<a href=\"#ref" n "\">" n "</a>]" -} - -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] = "<tt>" substr(w[i], 2) - else if (match(w[i], "^\\*")) - w[i] = "<i>" substr(w[i], 2) - else if (match(w[i], "^_")) - w[i] = "<b>" substr(w[i], 2) - - if (match(w[i], "`$")) - w[i] = substr(w[i], 1, RSTART-1) "</tt>" - else if (match(w[i], "\\*$")) - w[i] = substr(w[i], 1, RSTART-1) "</i>" - else if (match(w[i], "_$")) - w[i] = substr(w[i], 1, RSTART-1) "</b>" - else if (match(w[i], "`[.,:;?!]$")) - w[i] = substr(w[i], 1, RSTART-1) "</tt>" substr(w[i], RSTART+1) - else if (match(w[i], "\\*\.$")) - w[i] = substr(w[i], 1, RSTART-1) "</i>" substr(w[i], RSTART+1) - else if (match(w[i], "_[.,:;?!]$")) - w[i] = substr(w[i], 1, RSTART-1) "</b>" 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 "<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 = "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 |