Skip to content
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

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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: 34 additions & 4 deletions extra/markdown-extra/MarkdownRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

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.

$body = $this->removeIndentation($body);

return $this->converter->convert($body);
}

protected function commonWhitespace(string $body): string
{
return str_replace(["\t", "\0", "\x0B"], [' ', '', ''], $body);
}

protected function removeIndentation(string $body): string
Sarke marked this conversation as resolved.
Show resolved Hide resolved
{
$indent = $this->minIndentations($body);
if ($indent > 0) {
$body = preg_replace("{^ {{$indent}}}m", '', $body);
}

return $body;
}

protected function minIndentations(string $body): int
{
$non_empty_lines = preg_split('%(\r|\n)%', $body, -1, PREG_SPLIT_NO_EMPTY);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$non_empty_lines = preg_split('%(\r|\n)%', $body, -1, PREG_SPLIT_NO_EMPTY);
$nonEmptyLines = preg_split('%(\r|\n)%', $body, -1, PREG_SPLIT_NO_EMPTY);


$list = [];
foreach ($non_empty_lines as $line)
{
$len = strspn($line, " ");
if ($len === 0)
return 0;

$list[] = $len;
}

return min($list);
}
}
85 changes: 52 additions & 33 deletions extra/markdown-extra/Tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be on the previous line

return new $c(new $this->class());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing empty line above.

}
}
});
return $twig;
}
}