From 434dfdb6bd723f4435410c4660818515f5d25805 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Wed, 27 Dec 2017 04:44:02 -0800 Subject: [PATCH] Markdown.pl: be more flexible parsing backticks-delimited code blocks Allow leading spaces before the backticks delimiters on the starting and ending lines (up to one less than the indent width). Then remove upto that number of leading spaces (based on the starting backticks delimiter line) from each of the lines in the code block itself. This better matches how lax some other formatters are with backticks- delimited code blocks parsing. Signed-off-by: Kyle J. McKay --- Markdown.pl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Markdown.pl b/Markdown.pl index 5498982..66e6d45 100755 --- a/Markdown.pl +++ b/Markdown.pl @@ -489,23 +489,27 @@ sub _HashBTCodeBlocks { # Process Markdown backticks (```) delimited code blocks # my $text = shift; + my $less_than_indent = $opt{indent_width} - 1; $text =~ s{ (?:(?<=\n)|\A) - ``(`+)[ \t]*(?:([\w.+-]+)[ \t]*)?\n - ( # $3 = the code block -- one or more lines, starting with ``` + ([ ]{0,$less_than_indent})``(`+)[ \t]*(?:([\w.+-]+)[ \t]*)?\n + ( # $4 = the code block -- one or more lines, starting with ``` (?: .*\n+ )+? ) - (?:(?:``\1[ \t]*(?:\n|\Z))|\Z) # and ending with ``` or end of document + # and ending with ``` or end of document + (?:(?:[ ]{0,$less_than_indent}``\2[ \t]*(?:\n|\Z))|\Z) }{ # $2 contains syntax highlighting to use if defined - my $codeblock = $3; + my $leadsp = length($1); + my $codeblock = $4; $codeblock =~ s/[ \t]+$//mg; # trim trailing spaces on lines $codeblock = _Detab($codeblock, 8); # physical tab stops are always 8 $codeblock =~ s/\A\n+//; # trim leading newlines $codeblock =~ s/\s+\z//; # trim trailing whitespace + $codeblock =~ s/^ {1,$leadsp}//mg if $leadsp; # trim leading space(s) $codeblock = _EncodeCode($codeblock); # or run highlighter here $codeblock = "
"
 		. $codeblock . "\n
";