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