First fonctionnal version with code protection, titles

Adding time statistics in debug (32ms to parse a 21ko markdown file)


git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1285 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-05-10 21:44:43 +00:00
parent 6ed9dc84a9
commit cde5e524d5

View File

@@ -20,11 +20,37 @@ class markdown
public function html ($mark) public function html ($mark)
{ {
$res = ""; $res = "";
$search = array ();
$replace = array ();
$mark = htmlentities ($mark, ENT_QUOTES); $mark = htmlentities ($mark, ENT_QUOTES);
// Here are the regexp on multilines
$search = array ();
$replace = array ();
// Titles with underline (SeText)
// Titre1
// ======
$search[] = "/^(.+)\\n==+$\\n/Um";
$replace[] = "\n<h1>\\1</h1>\n";
// Titre2
// ------
$search[] = "/^(.+)\\n--+$\\n/Um";
$replace[] = "\n<h2>\\1</h2>\n";
$mark = preg_replace ($search, $replace, $mark);
$res = $this->paragraph ($mark);
return $res;
}
/** Translate the Markdown paragraph in HTML
return the html */
private function paragraph ($mark)
{
$timeStart = microtime (TRUE);
// Initialization of convertions
$search = array ();
$replace = array ();
// SEPARATORS : *** --- ___ * * * - - - _ _ _ // SEPARATORS : *** --- ___ * * * - - - _ _ _
// Must be placed before EMPHASIS // Must be placed before EMPHASIS
$search[] = "/\\n^[*_-] ?[*_-] ?[*_-]$/Um"; $search[] = "/\\n^[*_-] ?[*_-] ?[*_-]$/Um";
@@ -75,31 +101,13 @@ class markdown
// # Title1 // # Title1
$search[] = "/\\n^# ([^#]+)(#*)$\\n/Um"; $search[] = "/\\n^# ([^#]+)(#*)$\\n/Um";
$replace[] = "</p>\n<h1>\\1</h1>\n<p>"; $replace[] = "</p>\n<h1>\\1</h1>\n<p>";
// Titles with underline (SeText)
// Titre1
// ======
$search[] = "/\\n^(.+)\\n==+$\\n/Um";
$replace[] = "</p>\n<h1>\\1</h1>\n<p>";
// Titre2
// ------
$search[] = "/^(.+)\\n--+$\\n/Um";
$replace[] = "\n<h2>\\1</h2>\n";
// End of line with double space : <br/> // End of line with double space : <br/>
$search[] = "/( )$/Um"; $replace[] = "<br/>"; $search[] = "/( )$/Um"; $replace[] = "<br/>";
// End of line with continuous on second line : add blank // End of line with continuous on second line : add blank
// $search[] = "/(.)\\n([A-Za-z0-9])/Um"; $replace[] = "\\1 \\2"; // $search[] = "/(.)\\n([A-Za-z0-9])/Um"; $replace[] = "\\1 \\2";
$res = preg_replace ($search, $replace, $mark);
$res = $this->paragraph ($res);
return $res; // Cleanning the markdown text
}
/** Translate the Markdown paragraph in HTML
return the html */
private function paragraph ($mark)
{
$mark = str_replace ("\t", " ", $mark); $mark = str_replace ("\t", " ", $mark);
$mark = trim ($mark); $mark = trim ($mark);
if ($mark === "") if ($mark === "")
@@ -115,6 +123,7 @@ class markdown
// All the HTML stack (with LI) // All the HTML stack (with LI)
$htmlStack = array (); $htmlStack = array ();
$lines = explode ("\n", $mark); $lines = explode ("\n", $mark);
$timeInit = microtime (TRUE) - $timeStart;
foreach ($lines as $nb=>$line) foreach ($lines as $nb=>$line)
{ {
debugMKD ("DEBUT:$line"); debugMKD ("DEBUT:$line");
@@ -253,19 +262,20 @@ class markdown
$res .= str_repeat (" ", end ($indentStack))."</$oldType>"; $res .= str_repeat (" ", end ($indentStack))."</$oldType>";
array_pop ($htmlStack); array_pop ($htmlStack);
} }
$type = "pre><code"; $typetmp = "pre><code";
$htmlStack[] = "pre"; $htmlStack[] = "pre";
$htmlStack[] = "code"; $htmlStack[] = "code";
array_push ($typeStack, "code"); array_push ($typeStack, "code");
} }
else else
{ {
$typetmp = $type;
$htmlStack[] = $type; $htmlStack[] = $type;
array_push ($typeStack, $type); array_push ($typeStack, $type);
} }
array_push ($indentStack, $indent); array_push ($indentStack, $indent);
debugMKD (str_repeat (" ", $indent)."<$type>"); debugMKD (str_repeat (" ", $indent)."<$typetmp>");
$res .= "\n".str_repeat (" ", $indent)."<$type>"; $res .= "\n".str_repeat (" ", $indent)."<$typetmp>";
if ($type === "ol" || $type === "ul") if ($type === "ol" || $type === "ul")
{ {
debugMKD ("DEB2 : Adding li"); debugMKD ("DEB2 : Adding li");
@@ -284,6 +294,14 @@ class markdown
array_pop ($htmlStack); array_pop ($htmlStack);
} }
// If code, there is no emphasis, email, and other convertions
if ($type !== "code")
{
$timetmp = microtime (TRUE);
$lineTxt = preg_replace ($search, $replace, $lineTxt);
$timeregex += (microtime (TRUE) - $timetmp);
}
debugMKD ("$lineTxt"); debugMKD ("$lineTxt");
$res .= "$lineTxt\n"; $res .= "$lineTxt\n";
} }
@@ -296,6 +314,9 @@ class markdown
$res .= "</$type>\n"; $res .= "</$type>\n";
} }
debugMKD ("TimeInit=".($timeInit*1000)."ms");
debugMKD ("TimeRegex=".($timeregex*1000)."ms");
debugMKD ("TimeAll=".((microtime (TRUE) - $timeStart)*1000)."ms");
debugMKD ("-----------\n"); debugMKD ("-----------\n");
return $res; return $res;
} }