From d045206593953ebfeb22858baa25e8c2fe565e33 Mon Sep 17 00:00:00 2001 From: Gervase Markham Date: Sun, 26 Feb 2017 02:54:49 +0000 Subject: Bug 1174341 - only treat emphasis markdown based on spaces --- Bugzilla/Markdown.pm | 16 ++++++++-------- t/100markdown.t | 34 +++++++++++++++++++++------------- t/markdown/embedded-blockquote.md | 9 +++++++++ t/markdown/embedded-underscore.md | 19 +++++++++++++++++++ 4 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 t/markdown/embedded-blockquote.md create mode 100644 t/markdown/embedded-underscore.md diff --git a/Bugzilla/Markdown.pm b/Bugzilla/Markdown.pm index 72e788fd0..b7d7b4393 100644 --- a/Bugzilla/Markdown.pm +++ b/Bugzilla/Markdown.pm @@ -350,38 +350,38 @@ sub _DoItalicsAndBold { $text =~ s{ ^\* (?=\S) (.+?) (?<=\S) \* }{$1}gsx; # must go first: - $text =~ s{ ( (?<=\W) __ (?=\S) (.+?[*_]*) (?<=\S) __ (?!\S) ) } + $text =~ s{ ( (?<=\s) __ (?=\S) (.+?[*_]*) (?<=\S) __ (?!\S) ) } { my $result = _has_multiple_underscores($2) ? $1 : "$2"; $result; }gsxe; - $text =~ s{ (?<=\W) \*\* (?=\S) (.+?[*_]*) (?<=\S) \*\* }{$1}gsx; + $text =~ s{ (?<=\s) \*\* (?=\S) (.+?[*_]*) (?<=\S) \*\* }{$1}gsx; - $text =~ s{ ( (?<=\W) _ (?=\S) (.+?) (?<=\S) _ (?!\S) ) } + $text =~ s{ ( (?<=\s) _ (?=\S) (.+?) (?<=\S) _ (?!\S) ) } { my $result = _has_multiple_underscores($2) ? $1 : "$2"; $result; }gsxe; - $text =~ s{ (?<=\W) \* (?=\S) (.+?) (?<=\S) \* }{$1}gsx; + $text =~ s{ (?<=\s) \* (?=\S) (.+?) (?<=\S) \* }{$1}gsx; # And now, a second pass to catch nested strong and emphasis special cases - $text =~ s{ ( (?<=\W) __ (?=\S) (.+?[*_]*) (?<=\S) __ (\S*) ) } + $text =~ s{ ( (?<=\s) __ (?=\S) (.+?[*_]*) (?<=\S) __ (\S*) ) } { my $result = _has_multiple_underscores($3) ? $1 : "$2$3"; $result; }gsxe; - $text =~ s{ (?<=\W) \*\* (?=\S) (.+?[*_]*) (?<=\S) \*\* }{$1}gsx; - $text =~ s{ ( (?<=\W) _ (?=\S) (.+?) (?<=\S) _ (\S*) ) } + $text =~ s{ (?<=\s) \*\* (?=\S) (.+?[*_]*) (?<=\S) \*\* }{$1}gsx; + $text =~ s{ ( (?<=\s) _ (?=\S) (.+?) (?<=\S) _ (\S*) ) } { my $result = _has_multiple_underscores($3) ? $1 : "$2$3"; $result; }gsxe; - $text =~ s{ (?<=\W) \* (?=\S) (.+?) (?<=\S) \* }{$1}gsx; + $text =~ s{ (?<=\s) \* (?=\S) (.+?) (?<=\S) \* }{$1}gsx; return $text; } diff --git a/t/100markdown.t b/t/100markdown.t index 70dece55f..217912bf9 100644 --- a/t/100markdown.t +++ b/t/100markdown.t @@ -12,7 +12,8 @@ use strict; use warnings; use lib qw(. lib local/lib/perl5 t); -use Test2::Bundle::Extended; +use Test2::Tools::Mock; +use Test::More; use Bugzilla::Util; BEGIN { my $terms = { @@ -45,6 +46,8 @@ use Bugzilla::Bug; use Bugzilla::Comment; use Bugzilla::User; use Bugzilla::Markdown; +use Bugzilla::Util; +use File::Basename; Bugzilla->usage_mode(USAGE_MODE_TEST); Bugzilla->error_mode(ERROR_MODE_DIE); @@ -71,19 +74,24 @@ my $comment = Bugzilla::Comment->new(already_wrapped => 0); Bugzilla->set_user($user); -my $markdown_text = < with an embedded blockquote -``` -MARKDOWN +my @testfiles = glob("t/markdown/*.md"); -my $markdown = Bugzilla::Markdown->new(); +plan(tests => scalar(@testfiles) + 1); +my $markdown = Bugzilla::Markdown->new(); ok($markdown, "got a new markdown object"); -my $markdown_html = $markdown->markdown($markdown_text, $bug, $comment); -is("
this is a block\n"
-    . "> with an embedded blockquote
\n", - $markdown_html, "code block with embedded block quote"); -done_testing; +foreach my $testfile (@testfiles) { + my $data = read_text($testfile); + + my ($markdown_text, $expected_html) = split(/---/, $data); + $markdown_text = trim($markdown_text); + $expected_html = trim($expected_html); + + my $actual_html = $markdown->markdown($markdown_text, $bug, $comment); + $actual_html = trim($actual_html); + + is($actual_html, $expected_html, basename($testfile)); +} + +done_testing(); diff --git a/t/markdown/embedded-blockquote.md b/t/markdown/embedded-blockquote.md new file mode 100644 index 000000000..6c34434e9 --- /dev/null +++ b/t/markdown/embedded-blockquote.md @@ -0,0 +1,9 @@ +``` +this is a block +> with an embedded blockquote +``` + +--- + +
this is a block
+> with an embedded blockquote
diff --git a/t/markdown/embedded-underscore.md b/t/markdown/embedded-underscore.md new file mode 100644 index 000000000..15093cccf --- /dev/null +++ b/t/markdown/embedded-underscore.md @@ -0,0 +1,19 @@ +_foo_bar.c +_foo_bar_baz.c +_bar.c and _bif.c +hello_bar.c and there_bif.c + +var this__is_a_variable__ = ""; + +__this is not__ + +--- + +

<lib>_foo_bar.c
+<lib>_foo_bar_baz.c
+<prefix>_bar.c and <prefix>_bif.c
+hello_bar.c and there_bif.c

+ +

var this__is_a_variable__ = "";

+ +

this is not

-- cgit v1.2.3-24-g4f1b