EOL); /** * Replaces markups things like __underlined__ to 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) { #return preg_replace("#{$instruction}((?!{$instruction}.)+)(?:{$instruction})?#", "<{$markup}>$1", $text); $output = $text; # Replaces couples "__word__" into "word". $output = preg_replace("#${instruction}(.+?)${instruction}#", "<{$markup}>$1", $output); # Replaces a remaining __ into "" to the end of the line. $output = preg_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("\*\*", "b", $line); markupPreg("//", "i", $line); markupPreg("~~", "del", $line); } /** * Escapes the HTML entities yet contained in the Gemtext, keeps multiple spaces. * @param $text1, $text2 texts to process */ function htmlEscape(&$text) { $text = htmlspecialchars($text, ENT_HTML5, "UTF-8", false); } function keepSpaces(&$text) { # https://en.wikipedia.org/wiki/Whitespace_character#Unicode $text = preg_replace("# #", "  ", $text); } $mode = null; $mode_textAttributes = true; foreach ($fileLines as $line) { $reDo = true; $line1 = substr($line, 0, 1); $line2 = substr($line, 0, 2); $line3 = substr($line, 0, 3); while ($reDo) { $reDo = false; # Change in modes need to redo one loop as they can’t handle the case if (is_null($mode)) { if (empty($line)) { print("

 

\n"); } elseif ("#" == $line1) { preg_match("/^(#{1,3})\s*(.*)/", $line, $sharps); $h_level = strlen($sharps[1]); $text = $sharps[2]; htmlEscape($text); keepSpaces($text); switch ($h_level) { case 1: print("

".$text."

\n"); break; case 2: print("

".$text."

\n"); break; case 3: print("

".$text."

\n"); break; } } elseif ("=>" == $line2) { preg_match("/^=>\s*([^\s]+)\s*(.*)$/", $line, $linkParts); $url_link = $linkParts[1]; $url_label = $linkParts[2]; if (empty($url_label)) $url_label = $url_link; htmlEscape($url_link); keepSpaces($url_link); htmlEscape($url_label); keepSpaces($url_label); if ($mode_textAttributes) addTextAttributes($url_label); print("

".$url_label."

\n"); } elseif ('"""' == $line3) { $mode_textAttributes = !$mode_textAttributes; } elseif ("```" == $line3) { $mode="pre"; print("
\n");
            } elseif (">" == $line1) {
                $mode = "quote";
                preg_match("/^>\s*(.*)$/", $line, $quoteParts);
                $quote = $quoteParts[1];
                print("
\n"); if (empty($quote)) print("

 

\n"); else htmlEscape($quote); keepSpaces($quote); print("

".$quote."

\n"); } elseif ("*" == $line1 && "**" != $line2) { $mode = "ul"; $reDo = true; print("
\n"); } else { htmlEscape($line); if ($mode_textAttributes) addTextAttributes($line); print($line."\n"); } } elseif ("quote"==$mode) { if (">" == $line1) { preg_match("/^>\s*(.*)$/", $line, $quoteParts); $quote = $quoteParts[1]; if (empty($quote)) print("

 

\n"); else htmlEscape($quote); keepSpaces($quote); print("

".$quote."

\n"); } else { print("\n"); $mode=null; $reDo=true; } } elseif ("ul"==$mode) { if ("*" == $line1 && "**" != $line2) { preg_match("/^\*\s*(.*)$/", $line, $ulParts); $li = $ulParts[1]; if (empty($li)) print("
  •  \n"); else htmlEscape($li); keepSpaces($li); print("
  • ".$li."\n"); } else { $mode = null; print("\n"); $reDo = true; } } } } echo "\n\n"; ob_end_flush(); ?>