-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix for twigphp/Twig/issues/3685 (markdown leading linebreaks and indentation) #3688
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -22,11 +22,41 @@ public function __construct(MarkdownInterface $converter) | |||||
|
||||||
public function convert(string $body): string | ||||||
{ | ||||||
// remove indentation | ||||||
if ($white = substr($body, 0, strspn($body, " \t\r\n\0\x0B"))) { | ||||||
$body = preg_replace("{^$white}m", '', $body); | ||||||
} | ||||||
$body = $this->commonWhitespace($body); | ||||||
$body = $this->removeIndentation($body); | ||||||
|
||||||
return $this->converter->convert($body); | ||||||
} | ||||||
|
||||||
private function commonWhitespace(string $body): string | ||||||
{ | ||||||
return str_replace(["\t", "\0", "\x0B"], [' ', '', ''], $body); | ||||||
} | ||||||
|
||||||
private function removeIndentation(string $body): string | ||||||
{ | ||||||
$indent = $this->minIndentations($body); | ||||||
if ($indent > 0) { | ||||||
$body = preg_replace("{^ {{$indent}}}m", '', $body); | ||||||
} | ||||||
|
||||||
return $body; | ||||||
} | ||||||
|
||||||
private function minIndentations(string $body): int | ||||||
{ | ||||||
$non_empty_lines = preg_split('%(\r|\n)%', $body, -1, PREG_SPLIT_NO_EMPTY); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
$list = []; | ||||||
foreach ($non_empty_lines as $line) | ||||||
{ | ||||||
$len = strspn($line, " "); | ||||||
if ($len === 0) | ||||||
return 0; | ||||||
|
||||||
$list[] = $len; | ||||||
} | ||||||
|
||||||
return min($list); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,60 +27,79 @@ class FunctionalTest extends TestCase | |
/** | ||
* @dataProvider getMarkdownTests | ||
*/ | ||
public function testMarkdown(string $template, string $expected): void | ||
public function testMarkdown(string $markdown, string $expected): void | ||
{ | ||
foreach ([LeagueMarkdown::class, ErusevMarkdown::class, /*MichelfMarkdown::class,*/ DefaultMarkdown::class] as $class) { | ||
$twig = new Environment(new ArrayLoader([ | ||
'index' => $template, | ||
'html' => <<<EOF | ||
Hello | ||
===== | ||
$twig = $this->getTwig($class, [ | ||
'apply' => "{% apply markdown_to_html %}\n{$markdown}\n{% endapply %}", | ||
'include' => "{{ include('md')|markdown_to_html }}", | ||
'indent' => "{{ include('indent_md')|markdown_to_html }}", | ||
'md' => $markdown, | ||
'indent_md' => ltrim(str_replace("\n", "\n\t", "\n$markdown"), "\n"), | ||
]); | ||
|
||
Great! | ||
EOF | ||
])); | ||
$twig->addExtension(new MarkdownExtension()); | ||
$twig->addRuntimeLoader(new class($class) implements RuntimeLoaderInterface { | ||
private $class; | ||
$twig_md = trim($twig->render('apply')); | ||
$this->assertMatchesRegularExpression('{'.$expected.'}m', $twig_md); | ||
|
||
public function __construct(string $class) | ||
{ | ||
$this->class = $class; | ||
} | ||
$twig_md = trim($twig->render('include')); | ||
$this->assertMatchesRegularExpression('{'.$expected.'}m', $twig_md); | ||
|
||
public function load($c) | ||
{ | ||
if (MarkdownRuntime::class === $c) { | ||
return new $c(new $this->class()); | ||
} | ||
} | ||
}); | ||
$this->assertMatchesRegularExpression('{'.$expected.'}m', trim($twig->render('index'))); | ||
$twig_md = trim($twig->render('indent')); | ||
$this->assertMatchesRegularExpression('{'.$expected.'}m', $twig_md); | ||
|
||
$lib_md = trim((new $class)->convert($markdown)); | ||
$this->assertEquals($lib_md, $twig_md, "Twig output versus {$class} output."); | ||
} | ||
} | ||
|
||
public function getMarkdownTests() | ||
{ | ||
return [ | ||
[<<<EOF | ||
{% apply markdown_to_html %} | ||
Hello | ||
===== | ||
|
||
Great! | ||
{% endapply %} | ||
EOF | ||
, "<h1>Hello</h1>\n+<p>Great!</p>"], | ||
|
||
[<<<EOF | ||
{% apply markdown_to_html %} | ||
Hello | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we keep this example in the tests? |
||
===== | ||
|
||
Great! | ||
{% endapply %} | ||
Leading | ||
|
||
Linebreak | ||
EOF | ||
, "<h1>Hello</h1>\n+<p>Great!</p>"], | ||
["{{ include('html')|markdown_to_html }}", "<h1>Hello</h1>\n+<p>Great!</p>"], | ||
, "<p>Leading</p>\n+<p>Linebreak</p>"], | ||
|
||
[<<<EOF | ||
Code | ||
|
||
Paragraph | ||
EOF | ||
, "<pre><code>Code\n?</code></pre>\n+<p>Paragraph</p>"], | ||
]; | ||
} | ||
|
||
private function getTwig(string $class, array $templates): Environment | ||
{ | ||
$twig = new Environment(new ArrayLoader($templates)); | ||
$twig->addExtension(new MarkdownExtension()); | ||
$twig->addRuntimeLoader(new class($class) implements RuntimeLoaderInterface { | ||
private $class; | ||
|
||
public function __construct(string $class) | ||
{ | ||
$this->class = $class; | ||
} | ||
|
||
public function load($c) | ||
{ | ||
if (MarkdownRuntime::class === $c) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be on the previous line |
||
return new $c(new $this->class()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing empty line above. |
||
} | ||
} | ||
}); | ||
return $twig; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should change the content, except for maybe the beginning of the lines. Here, you are removing all occurrences.