From bf7a2ec379da265c90ee24d859405539a89545c2 Mon Sep 17 00:00:00 2001 From: Christophe HENRY Date: Fri, 5 Mar 2021 22:38:43 +0100 Subject: [PATCH] wip --- TODO | 2 + htmgem.php | 127 ++++++++++++++++++++++++++++-------------------- tests/test.gmi | 128 ++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 188 insertions(+), 69 deletions(-) diff --git a/TODO b/TODO index 875dc2b..f9b464c 100644 --- a/TODO +++ b/TODO @@ -3,6 +3,7 @@ * manage 404: Display better errors. * alt texts on pre and quote? * a way to get the source of a page, using urlrewriting +* options to activate the text decoration * HTML caching: Nginx tries the html, if not found use this script to build it * any error on one read line logs and goes to the next line, resetting modes * configuration: Fetch configuration in current dir, tries parents. @@ -19,3 +20,4 @@ * Be able to navigate (custom the links) when using htmgen.php?url=… * Text localisation * Search all cases where a narrow no-break space would apply +* Manage logging (cache refresh, debug) diff --git a/htmgem.php b/htmgem.php index 826d248..b64cd4d 100644 --- a/htmgem.php +++ b/htmgem.php @@ -73,8 +73,9 @@ $mode = null; $mode_textAttributes = true; foreach ($fileLines as $line) { $reDoCount = 0; + $mode_textAttributes_temp = false; while (true) { - if ($reDoCount>1) die("Too many loops"); + if ($reDoCount>2) die("Too many loops: ".$mode); $reDoCount += 1; $line1 = substr($line, 0, 1); // $line can be modified $line2 = substr($line, 0, 2); // in the meantime. @@ -82,6 +83,16 @@ foreach ($fileLines as $line) { if (is_null($mode)) { if (empty($line)) { echo "

 

\n"; + } elseif ('^^^' == $line3) { + $mode_textAttributes = !$mode_textAttributes; + } elseif ('^' == $line1) { + if (preg_match("/^\^\s*(.*)$/", $line, $parts)) { + $line = $parts[1]; + $mode_textAttributes_temp = true; + } else { + $mode = "raw"; + } + continue; } elseif ("#" == $line1) { preg_match("/^(#{1,3})\s*(.*)/", $line, $sharps); $h_level = strlen($sharps[1]); @@ -93,18 +104,20 @@ foreach ($fileLines as $line) { case 3: echo "

".$text."

\n"; break; } } elseif ("=>" == $line2) { - preg_match("/^=>\s*([^\s]+)(\s+(.*))?$/", $line, $linkParts); - $url_link = $linkParts[1]; - $url_label = @$linkParts[2]; - if (empty(trim($url_label))) { - $url_label = $url_link; + if (preg_match("/^=>\s*([^\s]+)(?:\s+(.*))?$/", $line, $linkParts)) { + $url_link = $linkParts[1]; + $url_label = @$linkParts[2]; + if (empty(trim($url_label))) { + $url_label = $url_link; + } else { + // the label is humain-made, apply formatting + htmlPrepare($url_label); + } + echo "

".$url_label."

\n"; } else { - // the label is humain-made, apply formatting - htmlPrepare($url_label); + $mode = "raw"; + continue; } - echo "

".$url_label."

\n"; - } elseif ('"""' == $line3) { - $mode_textAttributes = !$mode_textAttributes; } elseif ("```" == $line3) { $mode="pre"; echo "
\n";
@@ -117,56 +130,66 @@ foreach ($fileLines as $line) {
                     echo "

 

\n"; else htmlPrepare($quote); - if ($mode_textAttributes) addTextAttributes($line); + if ($mode_textAttributes xor $mode_textAttributes_temp) addTextAttributes($line); echo "

".$quote."

\n"; - } elseif ("*" == $line1 && "**" != $line2) { - $mode = "ul"; + } elseif ("* " == $line2) { echo "
\n"; - } else { - htmlPrepare($line); - echo $line."\n"; - } - } elseif ("quote"==$mode) { - if (">" == $line1) { - preg_match("/^>\s*(.*)$/", $line, $quoteParts); - $quote = $quoteParts[1]; - if (empty($quote)) - echo "

 

\n"; - else - htmlPrepare($quote); - echo "

".$quote."

\n"; - } else { - $mode=null; - echo "\n"; - continue; - } - } elseif ("ul"==$mode) { - if ("*" == $line1 && "**" != $line2) { - preg_match("/^\*\s*(.*)$/", $line, $ulParts); - $li = $ulParts[1]; - if (empty($li)) - echo "
  •  \n"; - else - htmlPrepare($li); - addTextAttributes($li); - echo "
  • ".$li."\n"; - } else { $mode = null; - echo "\n"; - continue; + } elseif ("pre"==$mode) { + if ("```" == $line3) { + echo "\n"; + $mode = null; + } else { + htmlPrepare($line); + echo $line."\n"; + } + } elseif ("quote"==$mode) { + if (">" == $line1) { + preg_match("/^>\s*(.*)$/", $line, $quoteParts); + $quote = $quoteParts[1]; + if (empty($quote)) + echo "

     

    \n"; + else + htmlPrepare($quote); + echo "

    ".$quote."

    \n"; + } else { + echo "\n"; + $mode = null; + continue; + } + } elseif ("ul"==$mode) { + if ("* " == $line2) { + preg_match("/^\*\s*(.*)$/", $line, $ulParts); + $li = $ulParts[1]; + if (empty($li)) { + echo "
  •  \n"; + } else { + htmlPrepare($li); + if ($mode_textAttributes xor $mode_textAttributes_temp) addTextAttributes($line); + echo "
  • ".$li."\n"; + } + } else { + echo "\n"; + $mode = null; + continue; + } + } else { + die("Unexpected mode: $mode!"); } } - break; + break; // exits the while(true) as no continue occured } } $body = ob_get_contents(); diff --git a/tests/test.gmi b/tests/test.gmi index 79838fe..58548ba 100644 --- a/tests/test.gmi +++ b/tests/test.gmi @@ -1,19 +1,117 @@ # Test page +* puce +=> lien1 +=> lien2 + +``` +The text as it is in the source code, in a preformatted block. +``` +The text as it is rendered, of course the following examples will have identical preformatted content and rendered content. + +``` +* single line +``` +* single line + +``` +* line one +* line two +``` +* line one +* line two + +``` +*incorrect list because there's no space right after the star. +``` +*incorrect list because there's no space right after the star. + +``` +Below, a line with just one star, must be rendered as is. +* +``` +Below, a line with just one star, must be rendered as is. +* + +``` +Below, a line with one star and one space, must be a single
  • +* +``` +Below, a line with one star and one space, must be a single
  • +* + +``` +Below, a line with one star and two spaces, must be a single
  • +* +``` +Below, a line with one star and two spaces, must be a single
  • +* + +``` +Below, two lines with one star, must be single
  • +* +* +``` +Below, two lines with one star, must be single
  • +* +* + +``` +# h1 +## h2 +### h3 +#### h4 (Should be read as h3) +``` +# h1 ## h2 ### h3 #### h4 (Should be read as h3) -Four spaces between the quotes " " +``` +Four spaces between the quotes " ", shrinked in HTML +``` +Four spaces between the quotes " ", shrinked in HTML -** Text using . +``` +^^^ +Plain text link: https://website.com/ +^^^ +``` +^^^ +Plain text link: https://website.com/ +^^^ -//Text using . -~~Text using . -__Text using . +``` +Link with double slash: http://truc +``` +Link with double slash: http://truc -Text having words in **bold**, //italic//, ~~deleted~~, __underlined__. -And some other mixed: **//bold italic// ~~bold deleted~~** //~~**italic deleted bold. +``` +^ Link inside a single non-decorated line: http://truc +``` +^ Link inside a single non-decorated line: http://truc + +``` +Below this line, just "=>" will be ignored and displayed as is. +=> +``` +Below this line, just "=> " will be ignored and displayed as is. +=> + +``` +=> gemini://somesite +``` +=> gemini://somesite + +``` +=> gemini://somesite label of the website +``` +=> gemini://somesite label of the website + + + +Du **gras**… +Encore du **gras** ! __This line starts with two _ and none at the end. The formatting stays in the physical line. @@ -77,16 +175,12 @@ At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praese > > -# Unordered list -Below, four lines. The first and last won't have a star at the beginning. -Line without * at the beginning -* line one -*line two -Line without * at the beginning -* another first line -*another second line - -*single line +** Text using . +//Text using . +~~Text using . +__Text using . +Text having words in **bold**, //italic//, ~~deleted~~, __underlined__. +And some other mixed: **//bold italic// ~~bold deleted~~** //~~**italic deleted bold.