From 6a0cf3b57a51d5f3c60bd1bcc53619dce9cf0355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sun, 31 Jan 2021 00:14:05 +0000 Subject: Remove (linktext) links, add links --- emparse | 74 +++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 40 insertions(+), 34 deletions(-) (limited to 'emparse') diff --git a/emparse b/emparse index 3184488..5ddb585 100755 --- a/emparse +++ b/emparse @@ -2,19 +2,14 @@ # aux/emparse -- parse em source -BEGIN { - expectblock = 1 - itemlevel = 1 - linktext = ENVIRON["linktext"] - if (!linktext) linktext = "link" -} +BEGIN { expectblock = 1; itemlevel = 1 } END { breakblock() } /^$/ { breakblock(); getline } expectblock && /^ / { newblock("table") } expectblock && /^ - / { newblock("ul") } -expectblock && /^ [0-9a-z]+\. / { newblock("ol") } -expectblock && /^ \[[0-9a-z]+\] / { newblock("nl") } +expectblock && /^ [0-9]+\. / { newblock("ol") } +expectblock && /^ \[[0-9]+\] / { newblock("nl") } expectblock && /^ .*: / { newblock("dl") } expectblock && /^ / { newblock("pre") } expectblock && /^---$/ { expectblock = 0; printf "
\n"; next } @@ -28,36 +23,43 @@ expectblock { newblock("p") } openblock == "pre" { sub("^ ", ""); $0 = escape($0); printf "%s\n", $0; next } -openblock == "ul" && /^ - / { item(1, "ul", line) } -openblock == "ol" && /^ [0-9a-z]+\. / { item(1, "ol", line) } -openblock == "nl" && /^ \[[0-9a-z]+\] [^ ]+$/ { next } # hyperlink reference -openblock == "nl" && /^ \[[0-9a-z]+\] / { item(1, "nl", line) } # text reference -openblock == "dl" && /^ .*: / { term(line) } - -(openblock == "ul" || openblock == "ol") && /^ -/ { item(2, "ul", line) } -(openblock == "ul" || openblock == "ol") && /^ [0-9a-z]+\./ { item(2, "ol", line) } -(openblock == "ul" || openblock == "ol") && /^ -/ { item(3, "ul", line) } -(openblock == "ul" || openblock == "ol") && /^ [0-9a-z]+\./ { item(3, "ol", line) } -(openblock == "ul" || openblock == "ol") && /^ -/ { item(4, "ul", line) } -(openblock == "ul" || openblock == "ol") && /^ [0-9a-z]+\./ { item(4, "ol", line) } -(openblock == "ul" || openblock == "ol") && /^ -/ { item(5, "ul", line) } -(openblock == "ul" || openblock == "ol") && /^ [0-9a-z]+\./ { item(5, "ol", line) } -(openblock == "ul" || openblock == "ol") && /^ -/ { item(6, "ul", line) } -(openblock == "ul" || openblock == "ol") && /^ [0-9a-z]+\./ { item(6, "ol", line) } +openblock == "ul" && /^ - / { item(1, "ul", line) } +openblock == "ol" && /^ [0-9]+\. / { item(1, "ol", line) } +#openblock == "nl" && /^ \[[0-9]+\] [^ ]+$/ { next } # hyperlink reference +openblock == "nl" && /^ \[[0-9]+\] / { item(1, "nl", line) } # text reference +openblock == "dl" && /^ .*: / { term(line) } + +(openblock == "ul" || openblock == "ol") && /^ -/ { item(2, "ul", line) } +(openblock == "ul" || openblock == "ol") && /^ [0-9]+\./ { item(2, "ol", line) } +(openblock == "ul" || openblock == "ol") && /^ -/ { item(3, "ul", line) } +(openblock == "ul" || openblock == "ol") && /^ [0-9]+\./ { item(3, "ol", line) } +(openblock == "ul" || openblock == "ol") && /^ -/ { item(4, "ul", line) } +(openblock == "ul" || openblock == "ol") && /^ [0-9]+\./ { item(4, "ol", line) } +(openblock == "ul" || openblock == "ol") && /^ -/ { item(5, "ul", line) } +(openblock == "ul" || openblock == "ol") && /^ [0-9]+\./ { item(5, "ol", line) } +(openblock == "ul" || openblock == "ol") && /^ -/ { item(6, "ul", line) } +(openblock == "ul" || openblock == "ol") && /^ [0-9]+\./ { item(6, "ol", line) } { format($0) } # inline formatting function format(line) { line = escape(line) - n = split(line, w, "[ ]") + n = split(line, w, "[ ]") # TODO: this removes spaces... for (i = 0; i <= n; i++) { if (w[i] == "") continue; - if (match(w[i], "^\\[[0-9a-z]+\\]$")) + if (match(w[i], "^<(\\./.*|\\.\\./.*|.*/.*)>$")) + w[i] = link(substr(w[i], 5, RLENGTH-8)) + else if (match(w[i], "^<(\\./.*|\\.\\./.*|.*/.*)>[.,:;?!)]$")) + w[i] = link(substr(w[i], 5, RLENGTH-9)) substr(w[i], RLENGTH) + else if (match(w[i], "^<(\\./.*|\\.\\./.*|.*/.*)>\\)[.,:;?!]$")) + w[i] = link(substr(w[i], 5, RLENGTH-10)) substr(w[i], RLENGTH-1) + + if (match(w[i], "^\\[[0-9]+\\]$")) w[i] = ref(substr(w[i], 2, RLENGTH-2)) - else if (match(w[i], "^\\[[0-9a-z]+\\][.,:;?!)]$")) + else if (match(w[i], "^\\[[0-9]+\\][.,:;?!)]$")) w[i] = ref(substr(w[i], 2, RLENGTH-3)) substr(w[i], RLENGTH) - else if (match(w[i], "^\\[[0-9a-z]+\\]\\)[.,:;?!]$")) + else if (match(w[i], "^\\[[0-9]+\\]\\)[.,:;?!]$")) w[i] = ref(substr(w[i], 2, RLENGTH-4)) substr(w[i], RLENGTH-1) if (!openformat) { @@ -112,7 +114,7 @@ function format(line) { } function opentag(name) { - if (name == "nl") printf "
    \n" + if (name == "nl") printf "
      \n" else printf "<%s>\n", name } @@ -175,15 +177,15 @@ function item(level, type, line) { printf "
    1. " } if (type == "ol") { - match($0, "[0-9a-z]+\.") + match($0, "[0-9]+\.") v = substr($0, RSTART, RLENGTH-1) - sub("^ +[0-9a-z]+\. ", "") + sub("^ +[0-9]+\. ", "") printf "
    2. ", v } if (type == "nl") { - match($0, "\\[[0-9a-z]+\\]") + match($0, "\\[[0-9]+\\]") v = substr($0, RSTART+1, RLENGTH-2) - sub("^ +\\[[0-9a-z]+\\] ", "") + sub("^ +\\[[0-9]+\\] ", "") printf "
    3. ", v, v } } @@ -201,10 +203,14 @@ function term(line, t) { # t is a local variable function ref(v) { if (ENVIRON["ref" v] != "") - return "(" linktext ")" + return "[" v "]" return "[" v "]" } +function link(h) { + return "" h "" +} + function escape(s) { gsub("&", "\\&", s) gsub("<", "\\<", s) -- cgit v1.2.3