diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2021-05-10 16:55:26 +0100 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2021-05-10 16:55:26 +0100 |
| commit | 7c375848a271cb179938e35c94fb3a18a49bf43a (patch) | |
| tree | b39673e5f120748bc57d4a7fbb31b79fb2b90b62 | |
| parent | 1fe1534e9e2b26d1ce42f992dc7b1835441fc180 (diff) | |
Refactor gemtext2html
| -rw-r--r-- | scripts/functions.php | 85 |
1 files changed, 18 insertions, 67 deletions
diff --git a/scripts/functions.php b/scripts/functions.php index de563fc..f1f4e1e 100644 --- a/scripts/functions.php +++ b/scripts/functions.php @@ -158,36 +158,29 @@ function buildHtmlFile(array $fileMetaData, string $contents, string $htmlTempla function gemtext2hmtl(string $gemtext): string { - return renderGemtextTokens(parseGemtext($gemtext)); -} - -function parseGemtext(string $gemtext): array -{ - $tokens = []; + $html = []; $lines = preg_split('/\r?\n/', htmlspecialchars($gemtext)); + $line = fn ($index) => trim($lines[$index]); + $index = 0; + $numLines = count($lines); - $line = fn($index) => trim($lines[$index]); while($index < $numLines) { if (preg_match(HEADER, $line($index), $matches) === 1) { [, $levels, $content] = $matches; - $tokens[] = [ - 'type' => HEADER, - 'level' => strlen($levels), - 'content' => $content, - ]; + $level = strlen($levels); + + $html[] = "<h$level>$content</h$level>"; } elseif (preg_match(LINK, $line($index), $matches) === 1) { [, $href, $content] = $matches; - $tokens[] = [ - 'type' => LINK, - 'href' => $href, - 'content' => $content != '' ? $content : $href, - ]; + $content = $content ?: $href; + + $html[] = "<a href=\"$href\">$content</a>"; } elseif (preg_match(LIST_ITEM, $line($index), $matches) === 1) { $items = []; @@ -196,20 +189,14 @@ function parseGemtext(string $gemtext): array break; } - [, $content] = $matches; - - $items[] = $content; + $items[] = "<li>$matches[1]</li>"; $index++; - } $index--; - $tokens[] = [ - 'type' => LIST_ITEM, - 'items' => $items, - ]; + $html[] = sprintf("<ul>%s</ul>", implode('', $items)); } elseif (preg_match(PRE, $line($index), $matches) === 1) { [, $alt] = $matches; @@ -229,55 +216,19 @@ function parseGemtext(string $gemtext): array $index++; } - $tokens[] = [ - 'type' => PRE, - 'alt' => $alt, - 'items' => $items, - ]; - } elseif (preg_match(QUOTE, $line($index), $matches) === 1) { - [, $content] = $matches; + $items = implode("\n", $items); - $tokens[] = [ - 'type' => QUOTE, - 'content' => $content, - ]; + $html[] = $alt ? "<pre><code class=\"$alt\">$items</code></pre>" : "<pre>$items</pre>"; + } elseif (preg_match(QUOTE, $line($index), $matches) === 1) { + $html[] = "<blockquote>$matches[1]</blockquote>"; } else { - $tokens[] = [ - 'type' => null, - 'content' => $line($index), - ]; + $html[] = "<p>{$line($index)}</p>"; } $index++; } - return $tokens; -} - -function renderGemtextTokens(array $tokens): string -{ - return implode("\n", array_filter(array_map(function ($token) { - switch ($token['type']) { - case HEADER: - return sprintf('<h%s>%s</h%s>', $token['level'], $token['content'], $token['level']); - case LINK: - return sprintf('<a href="%s">%s</a>', $token['href'], $token['content']); - case LIST_ITEM: - return sprintf( - "<ul>\n%s\n</ul>", - implode("\n", array_map(function ($item) { return sprintf("<li>%s</li>", $item);}, $token['items'])) - ); - case PRE: - return $token['alt'] ? - sprintf("<pre><code class=\"%s\">\n%s\n</code></pre>", $token['alt'], implode("\n", $token['items'])) - : - sprintf("<pre>\n%s\n</pre>", $token['alt'], implode('', $token['items'])); - case QUOTE: - return sprintf('<blockquote>%s</blockquote>', $token['content']); - default: - return $token['content'] !== '' ? sprintf('<p>%s</p>', $token['content']) : null; - } - }, $tokens))); + return implode('', $html); } function copyWWWAssets(string $assetsDiretory, string $output): void |
