From 6efa98325afb5a95c93a9049a4521be3a896c74c Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Mon, 15 Mar 2021 23:32:02 -0700 Subject: [PATCH] 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 --- Markdown.pl | 32 ++++++++++++++++++++++++++++++-- syntax.md | 13 +++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Markdown.pl b/Markdown.pl index cbc240b..e953f23 100755 --- a/Markdown.pl +++ b/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 = "
"
+		. $codeblock . "\n
"; + + 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); diff --git a/syntax.md b/syntax.md index e65b46b..74d537c 100644 --- a/syntax.md +++ b/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