aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-01-29 18:00:56 +0000
committerJohn Ankarström <john@ankarstrom.se>2021-01-29 18:00:56 +0000
commita08db38f245f68bf218f55f7a7a58defe27ca054 (patch)
tree457a6506ee0406b895de176747ffb681d13b4b5a
parent59ce3d3afd41fca58eb776963e94af92daa22e83 (diff)
downloadem-a08db38f245f68bf218f55f7a7a58defe27ca054.tar.gz
Add inline formatting
This is arguably the ugliest part of the implementation, but that's because awk isn't built for inline text processing.
-rwxr-xr-xem27
1 files changed, 26 insertions, 1 deletions
diff --git a/em b/em
index 13beb29..1b45893 100755
--- a/em
+++ b/em
@@ -52,6 +52,31 @@ function term(line, t) { # t is a local variable
printf "<dt>%s</dt><dd>", t
}
+function format(line) {
+ n = split(line, w, "[ ]")
+ for (i = 0; i <= n; i++) {
+ if (w[i] == "")
+ continue;
+ if (match(w[i], "^`.+`$"))
+ printf "<tt>%s</tt>", substr(w[i], 2, RLENGTH-2)
+ else if (match(w[i], "^\\*.+\\*$"))
+ printf "<i>%s</i>", substr(w[i], 2, RLENGTH-2)
+ else if (match(w[i], "^_.+_$"))
+ printf "<b>%s</b>", substr(w[i], 2, RLENGTH-2)
+ else if (match(w[i], "^`.+`[.,:;?!]$"))
+ printf "<tt>%s</tt>%s", substr(w[i], 2, RLENGTH-3), substr(w[i], RLENGTH)
+ else if (match(w[i], "^\\*.+\\*[.,:;?!]$"))
+ printf "<i>%s</i>%s", substr(w[i], 2, RLENGTH-3), substr(w[i], RLENGTH)
+ else if (match(w[i], "^_.+_[.,:;?!]$"))
+ printf "<b>%s</b>%s", substr(w[i], 2, RLENGTH-3), substr(w[i], RLENGTH)
+ else
+ printf "%s", w[i]
+ if (i < n)
+ printf " "
+ }
+ printf "\n"
+}
+
BEGIN { expectblock = 1; itemlevel = 1 }
END { breakblock() }
@@ -85,4 +110,4 @@ block = "ol" && /^ [0-9]+\./ { item(5, "ol", line) }
block = "ul" && /^ -/ { item(6, "ul", line) }
block = "ol" && /^ [0-9]+\./ { item(6, "ol", line) }
-{ print $0 }
+{ format($0) }