From 07ef050b87dcc8aa7a18301cf47e0f0a424c62a7 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Thu, 14 Nov 2019 19:08:05 -0700 Subject: [PATCH] Markdown.pl: carefully handle nested italics and bold Be very careful to make sure that this: open **FILE*** with *write* Does not turn into the broken: open FILE with *write But instead turns into the correct: open FILE* with write Handle italics inside bold by nesting a callout that finishes up by "hiding" any leftover bold/italics markup characters. Signed-off-by: Kyle J. McKay --- Markdown.pl | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Markdown.pl b/Markdown.pl index d9b5e55..1ddb3a1 100755 --- a/Markdown.pl +++ b/Markdown.pl @@ -2001,11 +2001,30 @@ sub _EncodeCode { sub _DoItalicsAndBoldAndStrike { my $text = shift; + my $doital1 = sub { + my $text = shift; + $text =~ s{ \* (?=\S) (.+?) (?<=\S) \* } + {$1}gsx; + # We've got to encode any of these remaining to + # avoid conflicting with other italics and bold. + $text =~ s!([*])!$g_escape_table{$1}!g; + $text; + }; + my $doital2 = sub { + my $text = shift; + $text =~ s{ (?$1}gsx; + # We've got to encode any of these remaining to + # avoid conflicting with other italics and bold. + $text =~ s!([_])!$g_escape_table{$1}!g; + $text; + }; + # must go first: $text =~ s{ \*\* (?=\S) (.+?[*_]*) (?<=\S) \*\* } - {$1}gsx; + {"".&$doital1($1).""}gsex; $text =~ s{ (?$1}gsx; + {"".&$doital2($1).""}gsex; $text =~ s{ ~~ (?=\S) (.+?[*_]*) (?<=\S) ~~ } {$1}gsx;