Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions lib/Text/Markdown.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1160,16 +1160,42 @@ sub _ProcessListItemsUL {

sub _DoCodeBlocks {
#
# Process Markdown code blocks (indented with 4 spaces or 1 tab):
# Process Markdown code blocks (indented with 4 spaces or 1 tab, or wrapped in 3 or more backticks [```]):
# * outdent the spaces/tab
# * encode <, >, & into HTML entities
# * escape Markdown special characters into MD5 hashes
# * trim leading and trailing newlines
#

my ($self, $text) = @_;

# handle backticks
$text =~ s@
(?:\n|\A) # backtick fence must be preceded by newline or beginning of string
(`{3,}) # $1 = Opening run of `
(.+?) # $2 = The code block
(?<!`)
\1 # Matching closer
(?!`)
@
my $codeblock = $2;
my $result; # return value

$codeblock = $self->_EncodeCode($codeblock); # no outdent necessary
$codeblock = $self->_Detab($codeblock);
$codeblock =~ s/\A\n+//; # trim leading newlines
$codeblock =~ s/\n+\z//; # trim trailing newlines

$result = "\n\n<pre><code>" . $codeblock . "\n</code></pre>\n\n";

$result;
@egsx;

# hash found codeblocks so they do not get reprocessed by the second step
$text = $self->_HashHTMLBlocks($text);

$text =~ s{
# handle tabs/spaces
$text =~ s{
(?:\n\n|\A)
( # $1 = the code block -- one or more lines, starting with a space/tab
(?:
Expand Down Expand Up @@ -1224,9 +1250,9 @@ sub _DoCodeSpans {
my ($self, $text) = @_;

$text =~ s@
(?<!\\) # Character before opening ` can't be a backslash
(`+) # $1 = Opening run of `
(.+?) # $2 = The code block
(?<!\\) # Character before opening ` can't be a backslash
(`+) # $1 = Opening run of `
(.+?) # $2 = The code block
(?<!`)
\1 # Matching closer
(?!`)
Expand All @@ -1235,7 +1261,7 @@ sub _DoCodeSpans {
$c =~ s/^[ \t]*//g; # leading whitespace
$c =~ s/[ \t]*$//g; # trailing whitespace
$c = $self->_EncodeCode($c);
"<code>$c</code>";
"<code>$c</code>";
@egsx;

return $text;
Expand Down