*/ 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 (); // 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--+$\\n/Um"; $replace[] = "
\n";
// End of line with double space :
$search[] = "/( )$/Um"; $replace[] = "
";
$res = preg_replace ($search, $replace, $mark);
$res = $this->paragraph ($res);
return $res;
}
/** Translate the Markdown paragraph in HTML
return the html */
private function paragraph ($mark)
{
$mark = str_replace ("\t", " ", $mark);
$mark = trim ($mark);
if ($mark === "")
return "";
$spacer = " ";
$res = "";
// P, OL, UL (but not LI !)
// Use to found the changing of types
$typeStack = array ();
// Number of spaces
$indentStack = array (-1);
// All the HTML stack (with LI)
$htmlStack = array ();
$lines = explode ("\n", $mark);
foreach ($lines as $line)
{
debugMKD ( "DEBUT:$line");
$type = $this->paragraphType ($line);
debugMKD ( "DEBUT: Type='$type'");
$matches = array ();
switch ($type)
{
case "ol" :
preg_match ("/^( *)[0-9]+\. +(.*)/", $line, $matches);
$lineTxt = $matches[2];
break ;
case "ul" :
preg_match ("/^( *)[-+*] +(.*)/", $line, $matches);
$lineTxt = $matches[2];
break ;
default:
$lineTxt = $line;
}
$indent = strspn ($line, " ");
debugMKD ( "DEBUT: Indent='$indent'");
// Spacing
if ($indent < end ($indentStack))
{
debugMKD ( "DEB1 : Ending of block");
if (end ($htmlStack) === "li")
{
debugMKD ("Pending