aboutsummaryrefslogtreecommitdiff
path: root/aux/emparse
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-01-29 21:09:02 +0000
committerJohn Ankarström <john@ankarstrom.se>2021-01-29 21:09:02 +0000
commita27e58ed6acc8e9719bd8ed7c226d2734ff7a654 (patch)
tree6688be56874c5384e6e95c08fc377b9b12556310 /aux/emparse
parent22958707c64b2e6d08595e517e86c9fe61c2b72c (diff)
downloadem-a27e58ed6acc8e9719bd8ed7c226d2734ff7a654.tar.gz
Escape &, <, >
Diffstat (limited to 'aux/emparse')
-rwxr-xr-xaux/emparse25
1 files changed, 20 insertions, 5 deletions
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("&", "\\&amp;")
+ gsub("<", "\\&lt;")
+ gsub(">", "\\&gt;")
+ } else {
+ gsub("&", "\\&amp;", s)
+ gsub("<", "\\&lt;", s)
+ gsub(">", "\\&gt;", s)
+ }
+}
+
function beginblock(name) {
if (name == "nl") printf "<ol>\n"
else printf "<%s>\n", name
@@ -37,6 +49,7 @@ function breakblock() {
function heading(level, line) {
sub("^=* ", "", line)
sub(" =*$", "", line)
+ escape(line)
printf "<h%d>%s</h%d>\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 "</dd>"
opendef = 1
- t = $0; d = $0
+ t = $0
sub("^ ", "", t)
sub(": .*$", "", t)
sub("^ [^:]+: ", "")
+ escape(t)
printf "<dt>%s</dt><dd>", 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 "<hr/>\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) }