EOL); $mode = null; 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]; 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; print("

".$url_label."

\n"); } 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 print("

".$quoteParts[1]."

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

 

\n"); else print("

".$quote."

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