From f0865cbb53a19475209734997bb2ad22fce6aac0 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Tue, 29 Oct 2019 05:30:26 -0700 Subject: [PATCH] Markdown.pl: correct tab expansion for shifted ``` blocks A "backticks-delimited code block" must be at the left margin, however, the entire block can be shifted 1, 2 or 3 characters to the right using spaces. These are all the same: ``` line ``` ``` line ``` ``` line ``` ``` line ``` When support for the slightly shifted code blocks was originally added, the effect on tab expansion was overlooked. The shifting in of the code block serves to make the raw markdown source perhaps a tiny bit more readable, but the shifting in is NOT a logical part of the lines in the code block themselves. Therefore, remove any "shift in" spaces before expanding tabs within the code block so that the result ends up looking exactly the same no matter whether the code block has been shifted in at all or not. Signed-off-by: Kyle J. McKay --- Markdown.pl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Markdown.pl b/Markdown.pl index 8b02879..af0d313 100755 --- a/Markdown.pl +++ b/Markdown.pl @@ -564,10 +564,9 @@ sub _HashBTCodeBlocks { 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 = _Detab($codeblock, 8, $leadsp); # 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
"; @@ -2495,6 +2494,8 @@ sub _Detab { # my $text = shift; my $ts = shift || $opt{tab_width}; + my $leadsp = shift || 0; + $text =~ s/^ {1,$leadsp}//mg if $leadsp; # trim leading space(s) # From the Perl camel book "Fluent Perl" section (slightly modified) $text =~ s/(.*?)(\t+)/$1 . ' ' x (length($2) * $ts - length($1) % $ts)/ge; return $text;