From cde5e524d51e5f504c65b4398d5485612df2f3a4 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Sat, 10 May 2014 21:44:43 +0000 Subject: [PATCH] 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 --- markdown.php | 73 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/markdown.php b/markdown.php index 6f50985..c69724a 100644 --- a/markdown.php +++ b/markdown.php @@ -20,11 +20,37 @@ class markdown public function html ($mark) { $res = ""; - $search = array (); - $replace = array (); - $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

\\1

\n"; + // Titre2 + // ------ + $search[] = "/^(.+)\\n--+$\\n/Um"; + $replace[] = "\n

\\1

\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 : *** --- ___ * * * - - - _ _ _ // Must be placed before EMPHASIS $search[] = "/\\n^[*_-] ?[*_-] ?[*_-]$/Um"; @@ -75,31 +101,13 @@ class markdown // # Title1 $search[] = "/\\n^# ([^#]+)(#*)$\\n/Um"; $replace[] = "

\n

\\1

\n

"; - // Titles with underline (SeText) - // Titre1 - // ====== - $search[] = "/\\n^(.+)\\n==+$\\n/Um"; - $replace[] = "

\n

\\1

\n

"; - // Titre2 - // ------ - $search[] = "/^(.+)\\n--+$\\n/Um"; - $replace[] = "\n

\\1

\n"; - // End of line with double space :
$search[] = "/( )$/Um"; $replace[] = "
"; // End of line with continuous on second line : add blank -// $search[] = "/(.)\\n([A-Za-z0-9])/Um"; $replace[] = "\\1 \\2"; - $res = preg_replace ($search, $replace, $mark); - $res = $this->paragraph ($res); + // $search[] = "/(.)\\n([A-Za-z0-9])/Um"; $replace[] = "\\1 \\2"; - return $res; - } - - /** Translate the Markdown paragraph in HTML - return the html */ - private function paragraph ($mark) - { + // Cleanning the markdown text $mark = str_replace ("\t", " ", $mark); $mark = trim ($mark); if ($mark === "") @@ -115,6 +123,7 @@ class markdown // All the HTML stack (with LI) $htmlStack = array (); $lines = explode ("\n", $mark); + $timeInit = microtime (TRUE) - $timeStart; foreach ($lines as $nb=>$line) { debugMKD ("DEBUT:$line"); @@ -253,19 +262,20 @@ class markdown $res .= str_repeat (" ", end ($indentStack)).""; array_pop ($htmlStack); } - $type = "pre>"); - $res .= "\n".str_repeat (" ", $indent)."<$type>"; + debugMKD (str_repeat (" ", $indent)."<$typetmp>"); + $res .= "\n".str_repeat (" ", $indent)."<$typetmp>"; if ($type === "ol" || $type === "ul") { debugMKD ("DEB2 : Adding li"); @@ -284,6 +294,14 @@ class markdown 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"); $res .= "$lineTxt\n"; } @@ -296,6 +314,9 @@ class markdown $res .= "\n"; } + debugMKD ("TimeInit=".($timeInit*1000)."ms"); + debugMKD ("TimeRegex=".($timeregex*1000)."ms"); + debugMKD ("TimeAll=".((microtime (TRUE) - $timeStart)*1000)."ms"); debugMKD ("-----------\n"); return $res; }