From 9ee5a8744adeeb193c60d589989a98e707c8fed7 Mon Sep 17 00:00:00 2001 From: Christophe HENRY Date: Thu, 11 Mar 2021 10:55:49 +0100 Subject: [PATCH] Improves the regex --- index.php | 82 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/index.php b/index.php index 6a02b41..1a05672 100644 --- a/index.php +++ b/index.php @@ -160,27 +160,29 @@ function translateGemToHtml($fileContents) { if (empty($line)) { echo "

 

\n"; } elseif ('^^^' == $line3) { - if (preg_match("/^\^\^\^\s+(.*)$/", $line)) { - $mode_textAttributes = !$mode_textAttributes; + $mode_textAttributes = !$mode_textAttributes; + } elseif ('^' == $line1 and !$mode_textAttributes_temp) { + if (preg_match("/^\^\s*(.+)$/", $line, $parts)) { + $line = $parts[1]; + $mode_textAttributes_temp = true; + } else { + $mode = "raw"; + } + continue; + } elseif ("#" == $line1) { + if (preg_match("/^(#{1,3})\s*(.+)/", $line, $sharps)) { + $h_level = strlen($sharps[1]); + $text = $sharps[2]; + htmlPrepare($text); + switch ($h_level) { + case 1: echo "

".$text."

\n"; break; + case 2: echo "

".$text."

\n"; break; + case 3: echo "

".$text."

\n"; break; + } } else { $mode = "raw"; continue; } - } elseif ('^' == $line1 and !$mode_textAttributes_temp) { - preg_match("/^\^\s*(.*)$/", $line, $parts); - $line = $parts[1]; - $mode_textAttributes_temp = true; - continue; - } elseif ("#" == $line1) { - preg_match("/^(#{1,3})\s*(.*)/", $line, $sharps); - $h_level = strlen($sharps[1]); - $text = $sharps[2]; - htmlPrepare($text); - switch ($h_level) { - case 1: echo "

".$text."

\n"; break; - case 2: echo "

".$text."

\n"; break; - case 3: echo "

".$text."

\n"; break; - } } elseif ("=>" == $line2) { if (preg_match("/^=>\s*([^\s]+)(?:\s+(.*))?$/", $line, $linkParts)) { $url_link = $linkParts[1]; @@ -193,6 +195,7 @@ function translateGemToHtml($fileContents) { } else { // the label is humain-made, apply formatting htmlPrepare($url_label); + if ($mode_textAttributes xor $mode_textAttributes_temp) addTextAttributes($url_label); } echo "

$url_label

\n"; } else { @@ -200,27 +203,19 @@ function translateGemToHtml($fileContents) { continue; } } elseif ("```" == $line3) { - if (preg_match("/^```\s*(.*)$/", $line, $matches)) { - $alt_text = trim(@$matches[1]); - if (!(empty($alt_text))) { - echo "
\n";
-                        } else {
-                            echo "
\n";
-                        }
+                    preg_match("/^```\s*(.*)$/", $line, $matches);
+                    $alt_text = trim($matches[1]);
+                    if (empty($alt_text)) {
+                        echo "
\n";
+                    } else {
+                        echo "
\n";
                     }
                     $mode="pre";
                 } elseif (">" == $line1) {
-                    $mode = "quote";
-                    preg_match("/^>\s*(.*)$/", $line, $quoteParts);
-                    $quote = $quoteParts[1];
                     echo "
\n"; - if (empty($quote)) - echo "

 

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

".$quote."

\n"; - } elseif ("* " == $line2) { + $mode = "quote"; + continue; + } elseif ("*" == $line1) { echo "
    \n"; $mode = "ul"; continue; @@ -230,9 +225,13 @@ function translateGemToHtml($fileContents) { } } else { if ("raw"==$mode) { - htmlPrepare($line); - if ($mode_textAttributes xor $mode_textAttributes_temp) addTextAttributes($line); - if (empty($line)) $line = " "; + if (empty($line)) { + $line = " "; + } else { + htmlPrepare($line); + if ($mode_textAttributes xor $mode_textAttributes_temp) + addTextAttributes($line); + } echo "

    $line

    \n"; $mode = null; } elseif ("pre"==$mode) { @@ -249,16 +248,19 @@ function translateGemToHtml($fileContents) { $quote = $quoteParts[1]; if (empty($quote)) echo "

     

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

    ".$quote."

    \n"; + } } else { echo "
\n"; $mode = null; continue; } } elseif ("ul"==$mode) { - if ("* " == $line2) { + if ("*" == $line1) { preg_match("/^\*\s*(.*)$/", $line, $ulParts); $li = $ulParts[1]; if (empty($li)) { @@ -278,7 +280,7 @@ function translateGemToHtml($fileContents) { } } break; // exits the while(true) as no continue occured - } + } // while(true) } $html = ob_get_contents(); ob_clean();