Browse Source

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 <mackyle@gmail.com>
master
Kyle J. McKay 5 years ago
parent
commit
f0865cbb53
  1. 5
      Markdown.pl

5
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 = "<div class=\"$opt{style_prefix}code-bt\"><pre style=\"display:none\"></pre><pre><code>"
. $codeblock . "\n</code></pre></div>";
@ -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;

Loading…
Cancel
Save