*/ function debugMKD ($msg) { return; $trace = debug_backtrace(); $back = reset ($trace); file_put_contents ("/tmp/debug", "[".$back["line"]."] $msg\n", FILE_APPEND); } error_reporting (E_ALL); /** Markdown management */ class markdown { /** Convert the markdown language to HTML Return the HTML string @param string $mark Message in markdown syntax to display */ public function html ($mark) { $res = ""; $search = array (); $replace = array (); $mark = htmlentities ($mark, ENT_QUOTES); // SEPARATORS : *** --- ___ * * * - - - _ _ _ // Must be placed before EMPHASIS $search[] = "/\\n^[*_-] ?[*_-] ?[*_-]$/Um"; $replace[] = "
\n";
// EMPHASIS : _ or * for normal, __ or ** for strong
$search[] = "/__(.+)__/"; $replace[] = "\\1";
$search[] = "/_(.+)_/"; $replace[] = "\\1";
$search[] = "/\*\*(.+)\*\*/"; $replace[] = "\\1";
$search[] = "/\*(.+)\*/"; $replace[] = "\\1";
// CODE : `code` ->
$search[] = "/\\n?\`((\\n|.)+)\`/Um";
$replace[] = " \\1";
// LINKS
// [Google Site](http://google.fr/ "With help bubble")
$search[] = "(\[(.+)\]\((https?://.+) \"(.+)\"\))";
$replace[] = "\\1";
// [Google Site](http://google.fr/)
$search[] = "(\[(.+)\]\((http.+)\))"; $replace[] = "\\1";
// Automatics links :
//
"; // ##### Title5 $search[] = "/\\n^##### ([^#]+)(#*)$\\n/Um"; $replace[] = "
\n"; // #### Title4 $search[] = "/\\n^#### ([^#]+)(#*)$\\n/Um"; $replace[] = "
\n"; // ### Title3 $search[] = "/\\n^### ([^#]+)(#*)$\\n/Um"; $replace[] = "
\n"; // ## Title2 $search[] = "/\\n^## ([^#]+)(#*)$\\n/Um"; $replace[] = "
\n"; // # Title1 $search[] = "/\\n^# ([^#]+)(#*)$\\n/Um"; $replace[] = "
\n"; // Titles with underline (SeText) // Titre1 // ====== $search[] = "/\\n^(.+)\\n==+$\\n/Um"; $replace[] = "
\n"; // Titre2 // ------ $search[] = "/^(.+)\\n--+$\\n/Um"; $replace[] = "\n
continue until the end of paragraph
debugMKD ("DEB1 : Starting a new block");
if ($type === "")
{
debugMKD ("No type : skipped");
continue;
}
if (end ($indentStack))
array_pop ($indentStack);
if ($type === "code")
{
// Code need a pre before code
if (end ($typeStack))
{
debugMKD ("DEB2 : CODE : Close older HTML");
$oldType = array_pop ($typeStack);
debugMKD (str_repeat (" ", end ($indentStack))."$oldType>");
$res .= str_repeat (" ", end ($indentStack))."$oldType>";
array_pop ($htmlStack);
}
$type = "pre>");
$res .= "\n".str_repeat (" ", $indent)."<$type>";
if ($type === "ol" || $type === "ul")
{
debugMKD ("DEB2 : Adding li");
$htmlStack[] = "li";
debugMKD (str_repeat (" ", $indent)."");
$res .= "\n".str_repeat (" ", $indent)." ";
}
}
if ($type === "" && end ($indentStack))
{
debugMKD ("DEB2 : Empty type");
$oldType = array_pop ($typeStack);
debugMKD (str_repeat (" ", end ($indentStack))."$oldType>");
$res .= "\n".str_repeat (" ", end ($indentStack))."$oldType>";
array_pop ($htmlStack);
}
debugMKD ("$lineTxt");
$res .= "$lineTxt\n";
}
debugMKD ("DEB1 : End of loop");
$htmlStack = array_reverse ($htmlStack);
foreach ($htmlStack as $type)
{
debugMKD ("FIN$type>");
$res .= "$type>\n";
}
debugMKD ("-----------\n");
return $res;
}
/** Return the Type of object in the provided line
p, ul, ol, code */
private function paragraphType ($line)
{
if (! isset ($line{0}))
return "";
if (preg_match ("/^[ \t]*[+*-] /", $line) === 1)
return "ul";
if (preg_match ("/^[ \t]*[0-9]+\. /", $line) === 1)
return "ol";
if (preg_match ("/^( |\t)+/", $line) === 1)
return "code";
return "p";
}
}