diff --git a/markdown.php b/markdown.php new file mode 100644 index 0000000..2d69220 --- /dev/null +++ b/markdown.php @@ -0,0 +1,248 @@ + */ + +function debug ($msg) +{ + $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 +{ + /** Convert the markdown language to HTML + Return the HTML string + @param string $mark Message in markdown syntax to display */ + public function html ($mark) + { + $res = ""; + $search = array (); + $replace = array (); + + // VALIDATION +// if (substr ($mark, -1) !== "\n") +// $mark .= "\n"; + + // SEPARATORS : *** --- ___ * * * - - - _ _ _ + // Must be placed before EMPHASIS + $search[] = "/\\n^[*_-] ?[*_-] ?[*_-]$/Um"; + $replace[] = "
\n";
+
+ // EMPHASIS : _ or * for normal, __ or ** for strong
+ $search[] = "/__(.+)__/"; $replace[] = "\\1";
+ $search[] = "/_(.+)_/"; $replace[] = "\\1";
+ $search[] = "/\*\*(.+)\*\*/"; $replace[] = "\\1";
+ $search[] = "/\*(.+)\*/"; $replace[] = "\\1";
+
+ // CODE : `code` ->
+ $search[] = "/\\n?\`((\\n|.)+)\`/Um";
+ $replace[] = " \\1";
+
+ // LINKS
+ // [Google Site](http://google.fr/ "With help bubble")
+ $search[] = "(\[(.+)\]\((https?://.+) \"(.+)\"\))";
+ $replace[] = "\\1";
+ // [Google Site](http://google.fr/)
+ $search[] = "(\[(.+)\]\((http.+)\))"; $replace[] = "\\1";
+ // Automatics links :
+ //
"; + // ##### Title5 + $search[] = "/\\n^##### ([^#]+)(#*)$\\n/Um"; + $replace[] = "
\n"; + // #### Title4 + $search[] = "/\\n^#### ([^#]+)(#*)$\\n/Um"; + $replace[] = "
\n"; + // ### Title3 + $search[] = "/\\n^### ([^#]+)(#*)$\\n/Um"; + $replace[] = "
\n"; + // ## Title2 + $search[] = "/\\n^## ([^#]+)(#*)$\\n/Um"; + $replace[] = "
\n"; + // # Title1 + $search[] = "/\\n^# ([^#]+)(#*)$\\n/Um"; + $replace[] = "
\n"; + // Titles with underline (SeText) + // Titre1 + // ====== + $search[] = "/\\n^(.+)\\n==+$\\n/Um"; + $replace[] = "
\n"; + // Titre2 + // ------ + $search[] = "/\\n^(.+)\\n--+$\\n/Um"; + $replace[] = "
\n";
+
+ // End of line with double space :
+ $search[] = "/( )$/Um"; $replace[] = "
";
+
+ $res = preg_replace ($search, $replace, $mark);
+
+ $res = $this->paragraph ($res);
+ return $res;
+ }
+
+ /** Recursive function to translate the paragraphs
+ return the html */
+ private function paragraph ($mark)
+ {
+ $debug = 1;
+ $mark = str_replace ("\t", " ", $mark);
+ $spacer = " ";
+ $res = "";
+ // P, OL, UL (but not LI !)
+ // Use to found the changing of types
+ $typeStack = array ();
+ // Number of spaces
+ $indentStack = array (-1);
+ // All the HTML stack (with LI)
+ $htmlStack = array ();
+ $lines = explode ("\n", $mark);
+ foreach ($lines as $line)
+ {
+ debug ( "DEBUT:$line");
+ $type = $this->paragraphType ($line);
+ debug ( "DEBUT: Type='$type'");
+ $matches = array ();
+ switch ($type)
+ {
+ case "ol" :
+ preg_match ("/^( *)[0-9]+\. +(.*)/", $line, $matches);
+ $lineTxt = $matches[2];
+ break ;
+ case "ul" :
+ preg_match ("/^( *)[-+*] +(.*)/", $line, $matches);
+ $lineTxt = $matches[2];
+ break ;
+ default:
+ $lineTxt = $line;
+ }
+ $indent = strspn ($line, " ");
+ debug ( "DEBUT: Indent='$indent'");
+
+ // Spacing
+ if ($indent < end ($indentStack))
+ {
+ debug ( "DEB1 : Ending of block");
+ if (end ($htmlStack) === "li")
+ {
+ debug ("Pending