From eb8d329d93efa56c8afa4106932b0dfe62eef50e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 29 Jan 2021 19:09:51 +0000 Subject: Add references and reference lists --- em | 95 +++++++++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 64 insertions(+), 31 deletions(-) diff --git a/em b/em index 4977ef3..010fa63 100755 --- a/em +++ b/em @@ -9,15 +9,20 @@ function newblock(name) { printf "<%s>", name } +function closeblock(name) { + if (name == "nl") printf "\n" + else printf "\n", name +} + function breakblock() { if (openitem) { printf "\n" while (itemlevel-- > 1) - printf "\n", openblock + closeblock(openblock) itemlevel = 1 } if (opendef) printf "\n" - if (openblock) printf "\n", openblock + if (openblock) closeblock(openblock) openitem = 0 opendef = 0 openblock = 0 @@ -37,9 +42,22 @@ function item(level, type, line) { if (level > itemlevel) printf "\n" itemlevel = level - if (type == "ul") sub("^ +- ", "") - if (type == "ol") sub("^ +[0-9]+\. ", "") - printf "
  • " + 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 @@ -52,29 +70,40 @@ function term(line, t) { # t is a local variable 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 (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, RLENGTH-2) "" + w[i] = substr(w[i], 1, RSTART-1) "" else if (match(w[i], "\\*$")) - w[i] = substr(w[i], 1, RLENGTH-2) "" + w[i] = substr(w[i], 1, RSTART-1) "" else if (match(w[i], "_$")) - w[i] = substr(w[i], 1, RLENGTH-2) "" + 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 " " } @@ -84,13 +113,14 @@ function format(line) { 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 } +/^$/ { 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 } @@ -99,19 +129,22 @@ 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) } +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) } -- cgit v1.2.3