Skip to content
Closed
Show file tree
Hide file tree
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
34 changes: 30 additions & 4 deletions MarkdownRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,37 @@ 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);
}

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

protected function removeIndentation(string $body): string
{
$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);

$list = [];
foreach ($non_empty_lines as $line)
{
$list[] = strspn($line, " ");
}

return min($list);
}
}
85 changes: 52 additions & 33 deletions 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
=====

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)
{
return new $c(new $this->class());
}
}
});
return $twig;
}
}