From ade38ab69c5fe8cd7e35395582addac9e037bc3f Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 31 Dec 2024 16:47:18 +0200 Subject: feat(single): Impelement emphasis span elements --- single.awk | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'single.awk') diff --git a/single.awk b/single.awk index 726f519..9195576 100644 --- a/single.awk +++ b/single.awk @@ -9,12 +9,12 @@ # [ ] Lists # [x] Code blocks # [x] Horizontal rules -# [ ] Span elements +# [.] Span elements # [ ] Links # [ ] Inline # [ ] Reference -# [ ] Emphasis # [ ] Code +# [x] Emphasis # [ ] Images # [ ] Inline # [ ] Reference @@ -150,6 +150,43 @@ inside_codeblock && ! /^[ \t]*$/ { not_paragraph = 1 } +# Emphasis +function sub_match(replacement) { + $0 = substr($0, 1, RSTART - 1) replacement substr($0, RSTART + RLENGTH) +} +function surround(brace, left, right, blen) { + if (blen == "") { blen = length(brace) } + if (match($0, brace "[^" brace "]+" brace)) { + elem = left substr($0, RSTART + blen, RLENGTH - 2*blen) right + sub_match(elem) + return 1; + } + return 0 +} +function gsurround(brace, left, right, blen) { + while (surround(brace, left, right, blen)) {} + + if (blen == 1) { + gsub("\\\\", "", brace) + ## Surrounding a single character span with spaces should make it a + ## literal char. This is inelegant but functional. + gsub(" " left " ", " " brace " ") + gsub(" " right " ", " " brace " ") + } +} + +{ + ## Make sure is on the inside for long chains + ## E.g. *****a***** will be the same as ****_a_**** + surround("___" , "", "") + surround("\\*\\*\\*", "", "", 3) + + gsurround("__" , "", "") + gsurround("_" , "" , "") + gsurround("\\*\\*", "", "", 2) + gsurround("\\*" , "" , "", 1) +} + # Paragraph emptyP1 && !empty0 && !not_paragraph { $0 = "

" $0 } !emptyP1 && empty0 && !not_paragraph_p1 { P1 = P1 "

" } -- cgit v1.3.1-23-gd82d