|
37 | 37 | use Twig\Environment;
|
38 | 38 | use Twig\Extension\AbstractExtension;
|
39 | 39 | use Twig\Extension\GlobalsInterface;
|
| 40 | +use Twig\Extra\Markdown\MarkdownRuntime; |
40 | 41 | use Twig\Runtime\EscaperRuntime;
|
41 | 42 | use Twig\TwigFilter;
|
42 | 43 | use Twig\TwigFunction;
|
@@ -124,6 +125,7 @@ public function getFilters(): array
|
124 | 125 | new TwigFilter('medalType', $this->awards->medalType(...)),
|
125 | 126 | new TwigFilter('numTableActions', $this->numTableActions(...)),
|
126 | 127 | new TwigFilter('extensionToMime', $this->extensionToMime(...)),
|
| 128 | + new TwigFilter('domjudge_markdown_to_html', $this->domjudgeMarkdownToHTML(...), ['is_safe' => ['html']]), |
127 | 129 | ];
|
128 | 130 | }
|
129 | 131 |
|
@@ -1406,4 +1408,35 @@ public function extensionToMime(string $extension): string
|
1406 | 1408 | {
|
1407 | 1409 | return DOMJudgeService::EXTENSION_TO_MIMETYPE[$extension];
|
1408 | 1410 | }
|
| 1411 | + |
| 1412 | + public function domjudgeMarkdownToHTML(string $markdown): string |
| 1413 | + { |
| 1414 | + $latexFound = []; |
| 1415 | + while(true) { |
| 1416 | + $start = strpos($markdown,'$$'); |
| 1417 | + $end = strpos(substr($markdown, $start+2), '$$'); |
| 1418 | + if ($start === false || $end === false) { |
| 1419 | + break; |
| 1420 | + } |
| 1421 | + $latexFound[] = substr($markdown, $start, $end+2+2); |
| 1422 | + $newMarkdown = substr($markdown, 0, $start); |
| 1423 | + $newMarkdown .= '$LaTeX$'; |
| 1424 | + $newMarkdown .= substr($markdown, $end+$start+4); |
| 1425 | + $markdown = $newMarkdown; |
| 1426 | + } |
| 1427 | + |
| 1428 | + /** @var MarkdownRuntime $runtime */ |
| 1429 | + $runtime = $this->twig->getRuntime(MarkdownRuntime::class); |
| 1430 | + $markdown = (string)$runtime->convert($markdown); |
| 1431 | + |
| 1432 | + $new = ''; |
| 1433 | + foreach ($latexFound as $inlineLatex) { |
| 1434 | + $replacedStart = strpos($markdown,'$LaTeX$'); |
| 1435 | + $new = substr($markdown, 0, $replacedStart); |
| 1436 | + $new .= $inlineLatex; |
| 1437 | + $new .= substr($markdown, $replacedStart+strlen('$LaTeX$')); |
| 1438 | + $markdown = $new; |
| 1439 | + } |
| 1440 | + return $markdown; |
| 1441 | + } |
1409 | 1442 | }
|
0 commit comments