From 77226b4f56a25cb9b791f93b0d9cea66f3b1ff46 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 31 Dec 2024 10:21:24 +0200 Subject: fix(single): Surround properly handles long chains of * or _ --- single.awk | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'single.awk') diff --git a/single.awk b/single.awk index bf80054..2ac29cf 100644 --- a/single.awk +++ b/single.awk @@ -299,20 +299,29 @@ blankline { } # Emphasis -function surround(brace, opening, closing) { - while ($0 ~ brace ".*" brace) { - sub(brace, opening) - sub(brace, closing) +function surround(brace, opening, closing, blen, single_surround) { + if (blen == "") { blen = length(brace) } + + while (match($0, brace "[^" brace "]+" brace)) { + elem = opening substr($0, RSTART + blen, RLENGTH - 2 * blen) closing + $0 = substr($0, 1, RSTART - 1) elem substr($0, RSTART + RLENGTH) + + if (single_surround != "") { return; } } } { + ## Make sure is on the inside for long chains + ## E.g. *****a***** will be the same as ****_a_**** + surround("___" , "", "", 3, "oneshot") + surround("\\*\\*\\*", "", "", 3, "oneshot") + surround("__" , "", "") surround("_" , "" , "") - surround("\\*\\*", "", "") - surround("\\*" , "" , "") + surround("\\*\\*", "", "", 2) + surround("\\*" , "" , "", 1) surround("~~" , "" , "") surround("~" , "" , "") - surround("\\^" , "" , "") + surround("\\^" , "" , "", 1) surround("``" , "" , "") surround("`" , "" , "") } -- cgit v1.3.1-23-gd82d