From 7c375848a271cb179938e35c94fb3a18a49bf43a Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Mon, 10 May 2021 16:55:26 +0100 Subject: Refactor gemtext2html --- scripts/functions.php | 85 +++++++++++---------------------------------------- 1 file 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[] = "$content"; } elseif (preg_match(LINK, $line($index), $matches) === 1) { [, $href, $content] = $matches; - $tokens[] = [ - 'type' => LINK, - 'href' => $href, - 'content' => $content != '' ? $content : $href, - ]; + $content = $content ?: $href; + + $html[] = "$content"; } 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[] = "
  • $matches[1]
  • "; $index++; - } $index--; - $tokens[] = [ - 'type' => LIST_ITEM, - 'items' => $items, - ]; + $html[] = sprintf("", 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 ? "
    $items
    " : "
    $items
    "; + } elseif (preg_match(QUOTE, $line($index), $matches) === 1) { + $html[] = "
    $matches[1]
    "; } else { - $tokens[] = [ - 'type' => null, - 'content' => $line($index), - ]; + $html[] = "

    {$line($index)}

    "; } $index++; } - return $tokens; -} - -function renderGemtextTokens(array $tokens): string -{ - return implode("\n", array_filter(array_map(function ($token) { - switch ($token['type']) { - case HEADER: - return sprintf('%s', $token['level'], $token['content'], $token['level']); - case LINK: - return sprintf('%s', $token['href'], $token['content']); - case LIST_ITEM: - return sprintf( - "", - implode("\n", array_map(function ($item) { return sprintf("
  • %s
  • ", $item);}, $token['items'])) - ); - case PRE: - return $token['alt'] ? - sprintf("
    \n%s\n
    ", $token['alt'], implode("\n", $token['items'])) - : - sprintf("
    \n%s\n
    ", $token['alt'], implode('', $token['items'])); - case QUOTE: - return sprintf('
    %s
    ', $token['content']); - default: - return $token['content'] !== '' ? sprintf('

    %s

    ', $token['content']) : null; - } - }, $tokens))); + return implode('', $html); } function copyWWWAssets(string $assetsDiretory, string $output): void -- cgit v1.2.3-13-gbd6f