diff --git a/markdown.php b/markdown.php index 2d69220..5402a14 100644 --- a/markdown.php +++ b/markdown.php @@ -3,8 +3,9 @@ @package domframework @author Dominique Fournier */ -function debug ($msg) +function debugMKD ($msg) { + //return; $trace = debug_backtrace(); $back = reset ($trace); file_put_contents ("/tmp/debug", "[".$back["line"]."] $msg\n", FILE_APPEND); @@ -22,10 +23,6 @@ class markdown $search = array (); $replace = array (); - // VALIDATION -// if (substr ($mark, -1) !== "\n") -// $mark .= "\n"; - // SEPARATORS : *** --- ___ * * * - - - _ _ _ // Must be placed before EMPHASIS $search[] = "/\\n^[*_-] ?[*_-] ?[*_-]$/Um"; @@ -95,12 +92,14 @@ class markdown return $res; } - /** Recursive function to translate the paragraphs + /** Translate the Markdown paragraph in HTML return the html */ private function paragraph ($mark) { - $debug = 1; $mark = str_replace ("\t", " ", $mark); + $mark = trim ($mark); + if ($mark === "") + return ""; $spacer = " "; $res = ""; // P, OL, UL (but not LI !) @@ -113,9 +112,9 @@ class markdown $lines = explode ("\n", $mark); foreach ($lines as $line) { - debug ( "DEBUT:$line"); + debugMKD ( "DEBUT:$line"); $type = $this->paragraphType ($line); - debug ( "DEBUT: Type='$type'"); + debugMKD ( "DEBUT: Type='$type'"); $matches = array (); switch ($type) { @@ -131,76 +130,60 @@ class markdown $lineTxt = $line; } $indent = strspn ($line, " "); - debug ( "DEBUT: Indent='$indent'"); + debugMKD ( "DEBUT: Indent='$indent'"); // Spacing if ($indent < end ($indentStack)) { - debug ( "DEB1 : Ending of block"); + debugMKD ( "DEB1 : Ending of block"); if (end ($htmlStack) === "li") { - debug ("Pending
  • : closing"); - debug ("
  • "); + debugMKD ("Pending
  • : closing"); + debugMKD ("
  • "); $res .= ""; array_pop ($htmlStack); } $oldType = array_pop ($typeStack); - debug (str_repeat (" ", end ($indentStack)).""); + debugMKD (str_repeat (" ", end ($indentStack)).""); $res .= "\n".str_repeat (" ", end ($indentStack)).""; array_pop ($htmlStack); + array_pop ($indentStack); if ($type === "ol" || $type === "ul") { - debug ("DEB2 : Pending
  • : closing"); - debug ("
  • "); + debugMKD ("DEB2 : Pending
  • : closing"); + debugMKD ("
  • "); $res .= "\n"; array_pop ($htmlStack); - debug ("DEB2 : Adding li"); - $htmlStack[] = "li"; - debug ( str_repeat (" ", $indent)."
  • "); - $res .= "\n".str_repeat (" ", $indent)."
  • "; } - debug (str_repeat (" ", $indent)."$lineTxt"); - $res .= str_repeat (" ", $indent)."$lineTxt"; - array_pop ($indentStack); - } - elseif ($indent > end ($indentStack)) - { - debug ( "DEB1 : Starting a new block"); - array_push ($indentStack, $indent); - array_push ($typeStack, $type); - debug ( str_repeat (" ", $indent)."<$type>"); - $res .= "\n".str_repeat (" ", $indent)."<$type>"; - $htmlStack[] = $type; - if ($type === "ol" || $type === "ul") + if ($type === "") { - debug ("DEB2 : Adding li"); - $htmlStack[] = "li"; - debug ( str_repeat (" ", $indent)."
  • "); - $res .= "\n".str_repeat (" ", $indent)."
  • "; + debugMKD ("DEB3 : End of block"); + array_pop ($indentStack); + $res .= "\n"; + continue; } - debug ( "$lineTxt"); - $res .= "$lineTxt"; } - else + + if ($indent == end ($indentStack)) { - debug ( "DEB1 : Continuous block $type/".end ($typeStack)); + debugMKD ( "DEB1 : Continuous block $type/".end ($typeStack)); if (end ($htmlStack) === "li") { - debug ("Pending
  • : closing"); - debug ("
  • "); + debugMKD ("Pending
  • : closing"); + debugMKD ("
  • "); $res .= ""; array_pop ($htmlStack); } - debug ( "DEB1 : Continuous block $type/".end ($typeStack)." SECOND"); + debugMKD ( "DEB1 : Continuous block $type/".end ($typeStack)." SECOND"); if ($type !== end ($typeStack)) { - debug ( "DEB2 : Continuous Block but type change"); + debugMKD ( "DEB2 : Continuous Block but type change"); $oldType = array_pop ($typeStack); - debug (str_repeat (" ", end ($indentStack)).""); + debugMKD (str_repeat (" ", end ($indentStack)).""); $res .= "\n".str_repeat (" ", end ($indentStack)).""; array_pop ($htmlStack); - debug (str_repeat (" ", end ($indentStack))."<$type>"); + debugMKD (str_repeat (" ", end ($indentStack))."<$type>"); $res .= "\n".str_repeat (" ", end ($indentStack))."<$type>"; $htmlStack[] = $type; array_push ($indentStack, $indent); @@ -209,26 +192,47 @@ class markdown if ($type === "ol" || $type === "ul") { - debug ("DEB2 : Adding li"); + debugMKD ("DEB2 : Adding li"); $htmlStack[] = "li"; - debug ( str_repeat (" ", $indent)."
  • "); + debugMKD ( str_repeat (" ", $indent)."
  • "); $res .= "\n".str_repeat (" ", $indent)."
  • "; } - debug ("$lineTxt"); - $res .= "$lineTxt"; } + + if ($indent > end ($indentStack)) + { + debugMKD ( "DEB1 : Starting a new block"); + if (end ($indentStack)) + array_pop ($indentStack); + array_push ($indentStack, $indent); + array_push ($typeStack, $type); + debugMKD ( str_repeat (" ", $indent)."<$type>"); + $res .= "\n".str_repeat (" ", $indent)."<$type>"; + $htmlStack[] = $type; + if ($type === "ol" || $type === "ul") + { + debugMKD ("DEB2 : Adding li"); + $htmlStack[] = "li"; + debugMKD ( str_repeat (" ", $indent)."
  • "); + $res .= "\n".str_repeat (" ", $indent)."
  • "; + } + } + + debugMKD ("$lineTxt"); + $res .= "$lineTxt"; + } - debug ( "DEB1 : End of loop"); + debugMKD ( "DEB1 : End of loop"); $htmlStack = array_reverse ($htmlStack); foreach ($htmlStack as $type) { - debug ( "FIN"); + debugMKD ( "FIN"); $res .= "\n"; } - debug ( "-----------\n"); + debugMKD ( "-----------\n"); return $res; }