Summary Files Commits Branches @commit @diff @stats
path: root / single.awk
diff options
context:
space:
mode:
Commit885be8887eb05fff87c9ed150edff94cc2d3d6d7 (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-12-31 20:19:45 +0200
Committer
 Syndamia <kamen@syndamia.com>
2024-12-31 20:19:45 +0200
Files25ceb545157fc96d070791f6d2fb491bf22766d5 /single.awk
csg-885be8887eb05fff87c9ed150edff94cc2d3d6d7.tar
csg-885be8887eb05fff87c9ed150edff94cc2d3d6d7.tar.gz
csg-885be8887eb05fff87c9ed150edff94cc2d3d6d7.zip
Parent76971f093f3e2990dda2daadfe78f98994758c06 (diff)
feat(single): Implement escaping
1 files changed, 24 insertions, 2 deletions
-rw-r--r--single.awk26
diff --git a/single.awk b/single.awk
index ab207a1..e19117d 100644
--- a/single.awk
+++ b/single.awk
@@ -18,9 +18,9 @@
# [x] Images
# [x] Inline
# [x] Reference
-# [.] Other
+# [x] Other
# [x] Autolinks
-# [ ] Escapes
+# [x] Escapes
#
# [ ] Non-standard features
# [ ] Strikethrough ~~...~~
@@ -83,6 +83,15 @@ BEGIN {
for (i = 1; i < 97; ++i) {
char_to_dec[char(ascii, i)] = 31 + i
}
+
+ ## We do character escaping by substituting the escape with some
+ ## "useless" characters, which we're confident won't appear in actual text.
+ ## These are from decimal value 14 to 28 inclusive. They're blank characters,
+ ## related to shifting, device control and data transmission.
+ escapable = "\\`*_{}[]()#+-.!"
+ for (i = 1; i < 16; ++i) {
+ escapes[char(escapable, i)] = sprintf("%c", 13 + i)
+ }
}
{
@@ -108,6 +117,13 @@ BEGIN {
next
}
+# Escapes
+!inside_codeblock && /\\[-\\`*_{}\[\]()#+.!]/ {
+ while (match($0, /\\[-\\`*_{}\[\]()#+.!]/)) {
+ sub_match(escapes[char($0, RSTART + 1)])
+ }
+}
+
# Blockquotes
blockquote_level > 0 && empty0 {
for (P1 = P1 ORS; blockquote_level > 0; --blockquote_level) {
@@ -330,6 +346,12 @@ function __buffered_print() {
N1 = ""
}
}
+
+ ## Fix escaped characters
+ for (i = 1; i < 16; ++i) {
+ esc = char(escapable, i)
+ gsub(escapes[esc], esc, P2)
+ }
}
END {