From a27e58ed6acc8e9719bd8ed7c226d2734ff7a654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 29 Jan 2021 21:09:02 +0000 Subject: Escape &, <, > --- aux/emparse | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'aux') diff --git a/aux/emparse b/aux/emparse index 65644e9..38bec54 100755 --- a/aux/emparse +++ b/aux/emparse @@ -2,6 +2,18 @@ # aux/emparse -- parse em source +function escape(s) { + if (s == "") { + gsub("&", "\\&") + gsub("<", "\\<") + gsub(">", "\\>") + } else { + gsub("&", "\\&", s) + gsub("<", "\\<", s) + gsub(">", "\\>", s) + } +} + function beginblock(name) { if (name == "nl") printf "
    \n" else printf "<%s>\n", name @@ -37,6 +49,7 @@ function breakblock() { function heading(level, line) { sub("^=* ", "", line) sub(" =*$", "", line) + escape(line) printf "%s\n", level, line, level # should inline formatting be supported in headings? } @@ -68,10 +81,11 @@ function item(level, type, line) { function term(line, t) { # t is a local variable if (opendef) printf "" opendef = 1 - t = $0; d = $0 + t = $0 sub("^ ", "", t) sub(": .*$", "", t) sub("^ [^:]+: ", "") + escape(t) printf "
    %s
    ", t } @@ -83,6 +97,7 @@ function ref(v) { } function format(line) { + escape(line) n = split(line, w, "[ ]") for (i = 0; i <= n; i++) { if (w[i] == "") continue; @@ -146,16 +161,18 @@ expectblock && /^ - / { newblock("ul") } expectblock && /^ [0-9a-z]+\. / { newblock("ol") } expectblock && /^ \[[0-9a-z]\]+ / { newblock("nl") } expectblock && /^ .*: / { newblock("dl") } -expectblock && /^ / { newblock("pre"); sub("^ ", "") } +expectblock && /^ / { newblock("pre") } expectblock && /^---$/ { expectblock = 0; printf "
    \n"; next } expectblock && /^= .* =$/ { heading(1, $0); next } -expectblock && /^== .* ==$/ { heading(3, $0); next } +expectblock && /^== .* ==$/ { heading(2, $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 = "pre" { sub("^ ", ""); escape(); printf "%s\n", $0; next } + block = "ul" && /^ - / { item(1, "ul", line) } block = "ol" && /^ [0-9a-z]+\. / { item(1, "ol", line) } block = "nl" && /^ \[[0-9a-z]\]+ [^ ]+$/ { next } # hyperlink reference @@ -173,6 +190,4 @@ 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) } -- cgit v1.2.3