Browse Source

Markdown.pl: add limited tilde-delimited code block support

To avoid conflicting (too much) with setext-style H3 headers
that are delimited with a line of tildes, require exactly three
tildes to introduce a tilde-delimited code block.

And, while in there, clean up the backticks-delimited code
blocks pattern a tiny amount and allow either kind of code block
to be closed by more than the number of opening delimiters in
addition to exactly the same number of opening delimiters.

Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
master
Kyle J. McKay 3 years ago
parent
commit
6efa98325a
  1. 32
      Markdown.pl
  2. 13
      syntax.md

32
Markdown.pl

@ -1445,6 +1445,7 @@ sub Markdown {
sub _HashBTCodeBlocks {
#
# Process Markdown backticks (```) delimited code blocks
# Process some (limited recognition) tilde (~~~) delimited code blocks
#
my $text = shift;
my $less_than_indent = $opt{indent_width} - 1;
@ -1454,11 +1455,38 @@ sub _HashBTCodeBlocks {
([ ]{0,$less_than_indent})``(`+)[ \t]*(?:([\w.+-]+[#]?)(?:[ \t][ \t\w.+-]*)?)?\n
( # $4 = the code block -- one or more lines, starting with ```
(?:
.*\n+
.*\n
)+?
)
# and ending with ``` or end of document
(?:(?:[ ]{0,$less_than_indent}``\2[ \t]*(?:\n|\Z))|\Z)
(?:(?:[ ]{0,$less_than_indent}``\2`*[ \t]*(?:\n|\Z))|\Z)
}{
# $2 contains syntax highlighting to use if defined
my $leadsp = length($1);
my $codeblock = $4;
$codeblock =~ s/[ \t]+$//mg; # trim trailing spaces on lines
$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 = _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>";
my $key = block_id($codeblock);
$g_html_blocks{$key} = $codeblock;
"\n\n" . $key . "\n\n";
}egmx;
$text =~ s{
(?:(?<=\n)|\A)
([ ]{0,$less_than_indent})~~(~)[ \t]*(?:([\w.+-]+[#]?)(?:[ \t][ \t\w.+-]*)?)?\n
( # $4 = the code block -- one or more lines, starting with ~~~
(?:
.*\n
)+?
)
# 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 $leadsp = length($1);

13
syntax.md

@ -241,8 +241,12 @@ headers). For example:
This is an H3
~~~~~~~~~~~~~
Any number of underlining `=`'s, `-`'s or `~`'s will work. An optional
matching "overline" may precede the header like so:
Any number of underlining `=`'s will work. Any number of underlining
`-`'s will work but be careful it's not mistaken for a horizontal rule.
For `~`'s, try to use at least four to avoid being mistaken for
strike through text or a `~~~`-delimited code block.
An optional matching "overline" may precede the header like so:
=============
This is an H1
@ -767,6 +771,11 @@ element), the indentation technique must be used.
Also note that within a backticks-delimited code block, tab characters
are always expanded with the tab stop locations 8 characters apart.
As an alternative to using backticks, limited recognition is available
for tilde-delimited code blocks. Instead of backtick quotes, exactly 3
tildes (`~~~`) may be used to introduce the code block in which case
it must also be closed by tildes instead of backtick quotes.
Within a code block, ampersands (`&`) and angle brackets (`<` and `>`)
are automatically converted into HTML entities. This makes it very
easy to include example HTML source code using Markdown -- just paste

Loading…
Cancel
Save