From 885be8887eb05fff87c9ed150edff94cc2d3d6d7 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 31 Dec 2024 20:19:45 +0200 Subject: feat(single): Implement escaping --- single.awk | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'single.awk') 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 { -- cgit v1.3.1-23-gd82d