underlined. * @param $instruction the characters to replace, ex. _ * @param $markup the markup to replace to, ex. "u" to get * @param &$text where to replace. */ function markupPreg($instruction, $markup, &$text) { $output = $text; # Replaces couples "__word__" into "word". $output = mb_ereg_replace("${instruction}(.+?)${instruction}", "<{$markup}>\\1", $output); # Replaces a remaining __ into "" to the end of the line. $output = mb_ereg_replace("${instruction}(.+)?", "<{$markup}>\\1", $output); $text = $output; } /** * Adds text attributes sucj as underline, bold, … to $line * @param $line the line to process */ function addTextAttributes(&$line) { markupPreg("__", "u", $line); markupPreg("\*\*", "strong", $line); markupPreg("//", "em", $line); markupPreg("~~", "del", $line); } define("NARROW_NO_BREAK_SPACE", " "); /** * Prepares the raw text to be displayed in HTML environment: * * Escapes the HTML entities yet contained in the Gemtext. * * Puts thin unbrakable spaces before some characters. * @param $text1, $text2 texts to process */ function htmlPrepare(&$text) { $text = htmlspecialchars($text, ENT_HTML5|ENT_NOQUOTES, "UTF-8", false); $text = mb_ereg_replace("\ ([?!:;»€$])", NARROW_NO_BREAK_SPACE."\\1", $text); $text = mb_ereg_replace("([«])\ ", "\\1".NARROW_NO_BREAK_SPACE, $text); # Espace fine insécable } ob_start(); $mode = null; $mode_textAttributes = true; foreach ($fileLines as $line) { $reDoCount = 0; while (true) { if ($reDoCount>1) die("Too many loops"); $reDoCount += 1; $line1 = substr($line, 0, 1); // $line can be modified $line2 = substr($line, 0, 2); // in the meantime. $line3 = substr($line, 0, 3); if (is_null($mode)) { if (empty($line)) { echo "

 

\n"; } 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) { 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"; } elseif ('"""' == $line3) { $mode_textAttributes = !$mode_textAttributes; } elseif ("```" == $line3) { $mode="pre"; echo "
\n";
            } 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) addTextAttributes($line); echo "

".$quote."

\n"; } elseif ("*" == $line1 && "**" != $line2) { $mode = "ul"; 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; } } break; } } $body = ob_get_contents(); ob_clean(); # Gets the page title: the first occurrence with # at the line start mb_ereg("#\s*([^\n]+)\n", $fileContents, $matches); $page_title = @$matches[1]; # echo << $page_title EOL; echo "\n".$body; echo "\n\n"; ob_end_flush(); ?>