Skip to content

Commit

Permalink
Bug 1174341 - only treat emphasis markdown based on spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
gerv authored and dylanwh committed Feb 26, 2017
1 parent 4480c8c commit d045206
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
16 changes: 8 additions & 8 deletions Bugzilla/Markdown.pm
Original file line number Diff line number Diff line change
Expand Up @@ -350,38 +350,38 @@ sub _DoItalicsAndBold {
$text =~ s{ ^\* (?=\S) (.+?) (?<=\S) \* }{<em>$1</em>}gsx;

# <strong> must go first:
$text =~ s{ ( (?<=\W) __ (?=\S) (.+?[*_]*) (?<=\S) __ (?!\S) ) }
$text =~ s{ ( (?<=\s) __ (?=\S) (.+?[*_]*) (?<=\S) __ (?!\S) ) }
{
my $result = _has_multiple_underscores($2) ? $1 : "<strong>$2</strong>";
$result;
}gsxe;


$text =~ s{ (?<=\W) \*\* (?=\S) (.+?[*_]*) (?<=\S) \*\* }{<strong>$1</strong>}gsx;
$text =~ s{ (?<=\s) \*\* (?=\S) (.+?[*_]*) (?<=\S) \*\* }{<strong>$1</strong>}gsx;

$text =~ s{ ( (?<=\W) _ (?=\S) (.+?) (?<=\S) _ (?!\S) ) }
$text =~ s{ ( (?<=\s) _ (?=\S) (.+?) (?<=\S) _ (?!\S) ) }
{
my $result = _has_multiple_underscores($2) ? $1 : "<em>$2</em>";
$result;
}gsxe;

$text =~ s{ (?<=\W) \* (?=\S) (.+?) (?<=\S) \* }{<em>$1</em>}gsx;
$text =~ s{ (?<=\s) \* (?=\S) (.+?) (?<=\S) \* }{<em>$1</em>}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 : "<strong>$2</strong>$3";
$result;
}gsxe;

$text =~ s{ (?<=\W) \*\* (?=\S) (.+?[*_]*) (?<=\S) \*\* }{<strong>$1</strong>}gsx;
$text =~ s{ ( (?<=\W) _ (?=\S) (.+?) (?<=\S) _ (\S*) ) }
$text =~ s{ (?<=\s) \*\* (?=\S) (.+?[*_]*) (?<=\S) \*\* }{<strong>$1</strong>}gsx;
$text =~ s{ ( (?<=\s) _ (?=\S) (.+?) (?<=\S) _ (\S*) ) }
{
my $result = _has_multiple_underscores($3) ? $1 : "<em>$2</em>$3";
$result;
}gsxe;

$text =~ s{ (?<=\W) \* (?=\S) (.+?) (?<=\S) \* }{<em>$1</em>}gsx;
$text =~ s{ (?<=\s) \* (?=\S) (.+?) (?<=\S) \* }{<em>$1</em>}gsx;

return $text;
}
Expand Down
34 changes: 21 additions & 13 deletions t/100markdown.t
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);
Expand All @@ -71,19 +74,24 @@ my $comment = Bugzilla::Comment->new(already_wrapped => 0);

Bugzilla->set_user($user);

my $markdown_text = <<MARKDOWN;
```
this is a block
> 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("<pre><code>this is a block\n"
. "&gt; with an embedded blockquote</code></pre>\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();
9 changes: 9 additions & 0 deletions t/markdown/embedded-blockquote.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```
this is a block
> with an embedded blockquote
```

---

<pre><code>this is a block
&gt; with an embedded blockquote</code></pre>
19 changes: 19 additions & 0 deletions t/markdown/embedded-underscore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<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__

---

<p>&lt;lib&gt;_foo_bar.c<br>
&lt;lib&gt;_foo_bar_baz.c<br>
&lt;prefix&gt;_bar.c and &lt;prefix&gt;_bif.c<br>
hello_bar.c and there_bif.c</p>

<p>var this__is_a_variable__ = &quot;&quot;;</p>

<p><strong>this is not</strong></p>

0 comments on commit d045206

Please sign in to comment.