diff --git a/markdown.php b/markdown.php index f048d41..5154522 100644 --- a/markdown.php +++ b/markdown.php @@ -3,17 +3,14 @@ @package domframework @author Dominique Fournier */ -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 { + + /** debug variable */ + public $debug = false; + /** Convert the markdown language to HTML Return the HTML string @param string $mark Message in markdown syntax to display */ @@ -58,6 +55,13 @@ class markdown $search[] = "/\\n^[*_-] ?[*_-] ?[*_-]$/Um"; $replace[] = "

\n
\n

"; + // Titles short + // == TITRE1 + $search[] = "/^==+ (.+)( ==+)?$/Um"; + $replace[] = "

\n

\\1

\n

"; + // -- TITRE2 + $search[] = "/^--+ (.+)( --+)?$/Um"; + $replace[] = "

\n

\\1

\n

"; // EMPHASIS : _ or * for normal, __ or ** for strong $search[] = "/__(.+)__/"; $replace[] = "\\1"; $search[] = "/_(.+)_/"; $replace[] = "\\1"; @@ -128,15 +132,15 @@ class markdown $timeInit = microtime (TRUE) - $timeStart; foreach ($lines as $nb=>$line) { - debugMKD ("DEBUT:$line"); + $this->debugMKD ("DEBUT:$line"); if (substr (ltrim ($line), 0, 1) === "<") { - debugMKD ("HTML : Skipped"); + $this->debugMKD ("HTML : Skipped"); $res .= $line; continue; } $type = $this->paragraphType ($line); - debugMKD ("DEBUT: Type='$type'"); + $this->debugMKD ("DEBUT: Type='$type'"); $matches = array (); switch ($type) { @@ -159,18 +163,18 @@ class markdown } $indent = strspn ($line, " "); - debugMKD ("DEBUT: Indent='$indent'"); - debugMKD ("DEBUT: indentStack=".print_r ($indentStack, TRUE)); - debugMKD ("DEBUT: typeStack=".print_r ($typeStack, TRUE)); + $this->debugMKD ("DEBUT: Indent='$indent'"); + $this->debugMKD ("DEBUT: indentStack=".print_r ($indentStack, TRUE)); + $this->debugMKD ("DEBUT: typeStack=".print_r ($typeStack, TRUE)); // Spacing if ($indent < end ($indentStack)) { - debugMKD ("DEB1 : Ending of block"); + $this->debugMKD ("DEB1 : Ending of block"); if (end ($htmlStack) === "li") { - debugMKD ("Pending

  • : closing"); - debugMKD ("
  • "); + $this->debugMKD ("Pending
  • : closing"); + $this->debugMKD ("
  • "); $res .= ""; array_pop ($htmlStack); } @@ -180,29 +184,29 @@ class markdown $oldType = array_pop ($typeStack); if ($oldType === "code") { - debugMKD (""); + $this->debugMKD (""); $res .= "\n"; array_pop ($htmlStack); array_pop ($indentStack); } else { - debugMKD (str_repeat (" ", end ($indentStack)).""); + $this->debugMKD (str_repeat (" ", end ($indentStack)).""); $res .= str_repeat (" ", end ($indentStack))."\n"; } array_pop ($indentStack); array_pop ($htmlStack); if ($type === "ol" || $type === "ul") { - debugMKD ("DEB2 : Pending
  • : closing"); - debugMKD ("
  • "); + $this->debugMKD ("DEB2 : Pending
  • : closing"); + $this->debugMKD ("
  • "); $res .= "\n"; array_pop ($htmlStack); } if ($type === "") { - debugMKD ("DEB3 : End of block"); + $this->debugMKD ("DEB3 : End of block"); array_pop ($indentStack); $res .= "\n"; continue; @@ -211,24 +215,24 @@ class markdown if ($indent == end ($indentStack)) { - debugMKD ("DEB1 : Continuous block $type/".end ($typeStack)); - if (end ($htmlStack) === "li") + $this->debugMKD ("DEB1 : Continuous block $type/".end ($typeStack)); + if (end ($htmlStack) === "li" && $type !== "p") { - debugMKD ("Pending
  • : closing"); - debugMKD ("
  • "); + $this->debugMKD ("Pending
  • : closing"); + $this->debugMKD ("
  • "); $res .= ""; array_pop ($htmlStack); } if ($type !== end ($typeStack)) { - debugMKD ("DEB2 : Continuous Block but type change"); + $this->debugMKD ("DEB2 : Continuous Block but type change"); if (end ($typeStack) !== FALSE) { // Remove last \n to put closing tag at the end of line $res = substr ($res, 0, -1); $oldType = array_pop ($typeStack); - debugMKD (str_repeat (" ", end ($indentStack)).""); + $this->debugMKD (str_repeat (" ", end ($indentStack)).""); $res .= str_repeat (" ", end ($indentStack)).""; array_pop ($indentStack); array_pop ($htmlStack); @@ -236,7 +240,7 @@ class markdown if ($type !== "") { - debugMKD (str_repeat (" ", end ($indentStack))."<$type>"); + $this->debugMKD (str_repeat (" ", end ($indentStack))."<$type>"); $res .= "\n".str_repeat (" ", end ($indentStack))."<$type>"; $htmlStack[] = $type; array_push ($indentStack, $indent); @@ -246,9 +250,9 @@ class markdown if ($type === "ol" || $type === "ul") { - debugMKD ("DEB2 : Adding li"); + $this->debugMKD ("DEB2 : Adding li"); $htmlStack[] = "li"; - debugMKD (str_repeat (" ", $indent)."
  • "); + $this->debugMKD (str_repeat (" ", $indent)."
  • "); $res .= "\n".str_repeat (" ", $indent)."
  • "; } @@ -258,10 +262,10 @@ class markdown { // The code indentation should not be parsed as a new code : the // continue until the end of paragraph - debugMKD ("DEB1 : Starting a new block"); + $this->debugMKD ("DEB1 : Starting a new block"); if ($type === "") { - debugMKD ("No type : skipped"); + $this->debugMKD ("No type : skipped"); continue; } @@ -272,11 +276,11 @@ class markdown // Code need a pre before code if (end ($typeStack)) { - debugMKD ("DEB2 : CODE : Close older HTML"); + $this->debugMKD ("DEB2 : CODE : Close older HTML"); // Remove last \n to put closing tag at the end of line $res = substr ($res, 0, -1); $oldType = array_pop ($typeStack); - debugMKD (str_repeat (" ", end ($indentStack)).""); + $this->debugMKD (str_repeat (" ", end ($indentStack)).""); $res .= str_repeat (" ", end ($indentStack)).""; array_pop ($indentStack); array_pop ($htmlStack); @@ -293,24 +297,24 @@ class markdown array_push ($typeStack, $type); } array_push ($indentStack, $indent); - debugMKD (str_repeat (" ", $indent)."<$typetmp>"); + $this->debugMKD (str_repeat (" ", $indent)."<$typetmp>"); $res .= "\n".str_repeat (" ", $indent)."<$typetmp>"; if ($type === "ol" || $type === "ul") { - debugMKD ("DEB2 : Adding li"); + $this->debugMKD ("DEB2 : Adding li"); $htmlStack[] = "li"; - debugMKD (str_repeat (" ", $indent)."
  • "); + $this->debugMKD (str_repeat (" ", $indent)."
  • "); $res .= "\n".str_repeat (" ", $indent)."
  • "; } } if ($type === "" && end ($indentStack)) { - debugMKD ("DEB2 : Empty type"); + $this->debugMKD ("DEB2 : Empty type"); // Remove last \n to put closing tag at the end of line $res = substr ($res, 0, -1); $oldType = array_pop ($typeStack); - debugMKD (str_repeat (" ", end ($indentStack)).""); + $this->debugMKD (str_repeat (" ", end ($indentStack)).""); $res .= "\n".str_repeat (" ", end ($indentStack)).""; array_pop ($htmlStack); } @@ -323,22 +327,22 @@ class markdown $timeregex += (microtime (TRUE) - $timetmp); } - debugMKD ("$lineTxt"); + $this->debugMKD ("$lineTxt"); $res .= "$lineTxt\n"; } - debugMKD ("DEB1 : End of loop"); + $this->debugMKD ("DEB1 : End of loop"); $htmlStack = array_reverse ($htmlStack); foreach ($htmlStack as $type) { - debugMKD ("FIN"); + $this->debugMKD ("FIN"); $res .= "\n"; } - debugMKD ("TimeInit=".($timeInit*1000)."ms"); - debugMKD ("TimeRegex=".($timeregex*1000)."ms"); - debugMKD ("TimeAll=".((microtime (TRUE) - $timeStart)*1000)."ms"); - debugMKD ("-----------\n"); + $this->debugMKD ("TimeInit=".($timeInit*1000)."ms"); + $this->debugMKD ("TimeRegex=".($timeregex*1000)."ms"); + $this->debugMKD ("TimeAll=".((microtime (TRUE) - $timeStart)*1000)."ms"); + $this->debugMKD ("-----------\n"); return $res; } @@ -356,4 +360,13 @@ class markdown return "code"; return "p"; } + + /** Function to display the MarkDown debug */ + private function debugMKD ($msg) + { + if ($this->debug === false) return; + $trace = debug_backtrace(); + $back = reset ($trace); + file_put_contents ("/tmp/debug", "[".$back["line"]."] $msg\n", FILE_APPEND); + } }