Browse Source

Markdown.pl: support ``` code blocks

Non-indented code blocks may be used by preceding them
with a line consisting of 3 (or more) ` characters and
following them with a line consisting of the same
number of backtick characters.

Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
master
Kyle J. McKay 9 years ago
parent
commit
aa017f700c
  1. 23
      Markdown.pl
  2. 6
      basics.text
  3. 29
      syntax.text

23
Markdown.pl

@ -964,6 +964,29 @@ sub _DoCodeBlocks {
$result; $result;
}egmx; }egmx;
$text =~ s{
(?:\n|\A)
``(`+)\w*\n
( # $1 = the code block -- one or more lines, starting with ```
(?:
.*\n+
)+?
)
(?:(?:``\1(?:\n|\Z))|\Z) # and ending with ``` or end of document
}{
my $codeblock = $2;
my $result; # return value
$codeblock = _EncodeCode($codeblock);
$codeblock = _Detab($codeblock);
$codeblock =~ s/\A\n+//; # trim leading newlines
$codeblock =~ s/\s+\z//; # trim trailing whitespace
$result = "\n\n<pre><code>" . $codeblock . "\n</code></pre>\n\n";
$result;
}egmx;
return $text; return $text;
} }

6
basics.text

@ -296,7 +296,11 @@ Output:
To specify an entire block of pre-formatted code, indent every line of To specify an entire block of pre-formatted code, indent every line of
the block by 4 spaces or 1 tab. Just like with code spans, `&`, `<`, the block by 4 spaces or 1 tab. Just like with code spans, `&`, `<`,
and `>` characters will be escaped automatically. and `>` characters will be escaped automatically. Alternatively an
entire block of pre-formatted code may be preceded with a line consisting
of 3 backtick quotes (or more) and followed by a line consisting of the
same number of backtick quotes -- in which case the code itself does not
need to be additionally indented.
Markdown: Markdown:

29
syntax.text

@ -481,12 +481,24 @@ of a code block are interpreted literally. Markdown wraps a code block
in both `<pre>` and `<code>` tags. in both `<pre>` and `<code>` tags.
To produce a code block in Markdown, simply indent every line of the To produce a code block in Markdown, simply indent every line of the
block by at least 4 spaces or 1 tab. For example, given this input: block by at least 4 spaces or 1 tab. Alternatively precede the block with
a line consisting of 3 backtick quotes (or more) and follow it with a
line consisting of the same number of backtick quotes -- in this case the
code lines themselves do not require any additional indentation.
For example, given this input:
This is a normal paragraph: This is a normal paragraph:
This is a code block. This is a code block.
Or this equivalent input:
This is a normal paragraph.
```
This is a code block.
```
Markdown will generate: Markdown will generate:
<p>This is a normal paragraph:</p> <p>This is a normal paragraph:</p>
@ -494,8 +506,11 @@ Markdown will generate:
<pre><code>This is a code block. <pre><code>This is a code block.
</code></pre> </code></pre>
One level of indentation -- 4 spaces or 1 tab -- is removed from each Note that when using the 3 backtick quotes technique, the blank line
line of the code block. For example, this: before the start of the code block is optional. One level of
indentation -- 4 spaces or 1 tab -- is removed from each
line of the code block unless the 3 backtick quotes are used.
For example, this:
Here is an example of AppleScript: Here is an example of AppleScript:
@ -513,7 +528,13 @@ will turn into:
</code></pre> </code></pre>
A code block continues until it reaches a line that is not indented A code block continues until it reaches a line that is not indented
(or the end of the article). (or the end of the article) when using the indentation technique or
until a line consisting of the same number of backtick quotes is found
when using the 3 backtick quotes technique.
Note that the 3 backtick quotes (or more) must appear at the beginning
of the line. To include a code block within a list (or other indented
element), the indentation technique must be used.
Within a code block, ampersands (`&`) and angle brackets (`<` and `>`) Within a code block, ampersands (`&`) and angle brackets (`<` and `>`)
are automatically converted into HTML entities. This makes it very are automatically converted into HTML entities. This makes it very

Loading…
Cancel
Save