Better support of imbricated lists
Better support of empty texts git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1283 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
112
markdown.php
112
markdown.php
@@ -3,8 +3,9 @@
|
||||
@package domframework
|
||||
@author Dominique Fournier <dominique@fournier38.fr> */
|
||||
|
||||
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 <Li> : closing");
|
||||
debug ("</li>");
|
||||
debugMKD ("Pending <Li> : closing");
|
||||
debugMKD ("</li>");
|
||||
$res .= "</li>";
|
||||
array_pop ($htmlStack);
|
||||
}
|
||||
$oldType = array_pop ($typeStack);
|
||||
debug (str_repeat (" ", end ($indentStack))."</$oldType>");
|
||||
debugMKD (str_repeat (" ", end ($indentStack))."</$oldType>");
|
||||
$res .= "\n".str_repeat (" ", end ($indentStack))."</$oldType>";
|
||||
array_pop ($htmlStack);
|
||||
array_pop ($indentStack);
|
||||
if ($type === "ol" || $type === "ul")
|
||||
{
|
||||
debug ("DEB2 : Pending <Li> : closing");
|
||||
debug ("</li>");
|
||||
debugMKD ("DEB2 : Pending <Li> : closing");
|
||||
debugMKD ("</li>");
|
||||
$res .= "\n</li>";
|
||||
array_pop ($htmlStack);
|
||||
debug ("DEB2 : Adding li");
|
||||
$htmlStack[] = "li";
|
||||
debug ( str_repeat (" ", $indent)."<li>");
|
||||
$res .= "\n".str_repeat (" ", $indent)."<li>";
|
||||
}
|
||||
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)."<li>");
|
||||
$res .= "\n".str_repeat (" ", $indent)."<li>";
|
||||
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 <Li> : closing");
|
||||
debug ("</li>");
|
||||
debugMKD ("Pending <Li> : closing");
|
||||
debugMKD ("</li>");
|
||||
$res .= "</li>";
|
||||
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))."</$oldType>");
|
||||
debugMKD (str_repeat (" ", end ($indentStack))."</$oldType>");
|
||||
$res .= "\n".str_repeat (" ", end ($indentStack))."</$oldType>";
|
||||
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)."<li>");
|
||||
debugMKD ( str_repeat (" ", $indent)."<li>");
|
||||
$res .= "\n".str_repeat (" ", $indent)."<li>";
|
||||
}
|
||||
|
||||
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)."<li>");
|
||||
$res .= "\n".str_repeat (" ", $indent)."<li>";
|
||||
}
|
||||
}
|
||||
|
||||
debugMKD ("$lineTxt");
|
||||
$res .= "$lineTxt";
|
||||
|
||||
}
|
||||
|
||||
debug ( "DEB1 : End of loop");
|
||||
debugMKD ( "DEB1 : End of loop");
|
||||
$htmlStack = array_reverse ($htmlStack);
|
||||
foreach ($htmlStack as $type)
|
||||
{
|
||||
debug ( "FIN</$type>");
|
||||
debugMKD ( "FIN</$type>");
|
||||
$res .= "</$type>\n";
|
||||
}
|
||||
|
||||
debug ( "-----------\n");
|
||||
debugMKD ( "-----------\n");
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user