Add short titles support (== TITRE -> h1, -- TITRE -> h2)

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1482 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-06-24 12:17:54 +00:00
parent 29bc34a07b
commit 197c9de161

View File

@@ -3,17 +3,14 @@
@package domframework
@author Dominique Fournier <dominique@fournier38.fr> */
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[] = "</p>\n<hr/>\n<p>";
// Titles short
// == TITRE1
$search[] = "/^==+ (.+)( ==+)?$/Um";
$replace[] = "</p>\n<h1>\\1</h1>\n<p>";
// -- TITRE2
$search[] = "/^--+ (.+)( --+)?$/Um";
$replace[] = "</p>\n<h2>\\1</h2>\n<p>";
// EMPHASIS : _ or * for normal, __ or ** for strong
$search[] = "/__(.+)__/"; $replace[] = "<strong>\\1</strong>";
$search[] = "/_(.+)_/"; $replace[] = "<em>\\1</em>";
@@ -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 <Li> : closing");
debugMKD ("</li>");
$this->debugMKD ("Pending <Li> : closing");
$this->debugMKD ("</li>");
$res .= "</li>";
array_pop ($htmlStack);
}
@@ -180,29 +184,29 @@ class markdown
$oldType = array_pop ($typeStack);
if ($oldType === "code")
{
debugMKD ("</code></pre>");
$this->debugMKD ("</code></pre>");
$res .= "</code></pre>\n";
array_pop ($htmlStack);
array_pop ($indentStack);
}
else
{
debugMKD (str_repeat (" ", end ($indentStack))."</$oldType>");
$this->debugMKD (str_repeat (" ", end ($indentStack))."</$oldType>");
$res .= str_repeat (" ", end ($indentStack))."</$oldType>\n";
}
array_pop ($indentStack);
array_pop ($htmlStack);
if ($type === "ol" || $type === "ul")
{
debugMKD ("DEB2 : Pending <Li> : closing");
debugMKD ("</li>");
$this->debugMKD ("DEB2 : Pending <Li> : closing");
$this->debugMKD ("</li>");
$res .= "\n</li>";
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 <Li> : closing");
debugMKD ("</li>");
$this->debugMKD ("Pending <Li> : closing");
$this->debugMKD ("</li>");
$res .= "</li>";
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))."</$oldType>");
$this->debugMKD (str_repeat (" ", end ($indentStack))."</$oldType>");
$res .= str_repeat (" ", end ($indentStack))."</$oldType>";
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)."<li>");
$this->debugMKD (str_repeat (" ", $indent)."<li>");
$res .= "\n".str_repeat (" ", $indent)."<li>";
}
@@ -258,10 +262,10 @@ class markdown
{
// The code indentation should not be parsed as a new code : the
// <code></code> 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))."</$oldType>");
$this->debugMKD (str_repeat (" ", end ($indentStack))."</$oldType>");
$res .= str_repeat (" ", end ($indentStack))."</$oldType>";
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)."<li>");
$this->debugMKD (str_repeat (" ", $indent)."<li>");
$res .= "\n".str_repeat (" ", $indent)."<li>";
}
}
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))."</$oldType>");
$this->debugMKD (str_repeat (" ", end ($indentStack))."</$oldType>");
$res .= "\n".str_repeat (" ", end ($indentStack))."</$oldType>";
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</$type>");
$this->debugMKD ("FIN</$type>");
$res .= "</$type>\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);
}
}