diff --git a/markdown.php b/markdown.php index daf80b2..b0bceca 100644 --- a/markdown.php +++ b/markdown.php @@ -57,44 +57,54 @@ class markdown { if ($this->debug) echo "CALL searchReplace ($line)\n"; + // REMEMBER : THE $line is already in HTML ENTITIES ! + // Quotes : " $search = array (); $replace = array (); // Titles short // == TITRE1 - $search[] = "/^==+ (.+)( ==+)?$/Um"; - $replace[] = "

\n

\\1

\n

"; + $search[] = '~^([^\\\\]|^)(==+ (.+)( ==+)?)$~Um'; + $replace[] = '

'."\n".'

\3

'."\n".'

'; // -- TITRE2 - $search[] = "/^--+ (.+)( --+)?$/Um"; - $replace[] = "

\n

\\1

\n

"; + $search[] = '~^([^\\\\]|^)(--+ (.+)( --+)?)$~Um'; + $replace[] = '

\n

\3

\n

'; + // EMPHASIS : _ or * for normal, __ or ** for strong - $search[] = "/__(.+)__/U"; $replace[] = "\\1"; - $search[] = "/_(.+)_/U"; $replace[] = "\\1"; - $search[] = "/\*\*(.+)\*\*/U"; $replace[] = "\\1"; - $search[] = "/\*(.+)\*/U"; $replace[] = "\\1"; + $search[] = '~([^\\\\]|^)(__(.+)__)~U'; + $replace[] = '\1\3'; + $search[] = '~([^\\\\_]|^)(_(.+)_)~U'; + $replace[] = '\1\3'; + + $search[] = '~([^\\\\]|^)(\*\*(.+)\*\*)~U'; + $replace[] = '\1\3'; + $search[] = '~([^\\\\*]|^)(\*(.+)\*)~U'; + $replace[] = '\1\3'; // CODE : `code` -> - $search[] = "/\\n?\`((\\n|.)+)\`/Um"; - $replace[] = "\\1"; + $search[] = "~\\n?([^\\\\]|^)(\`((\\n|.)+)\`)~Um"; + $replace[] = '\1\3'; // LINKS (can be relative) // images - $search[] = "(!\[(.+)\]\((.+)\))"; - $replace[] = "\\1"; + $search[] = '~([^\\\\]|^)(!\[(.+)\]\((.+)\))~'; + $replace[] = '\'\3\'/'; // [Google Site](http://google.fr/ "With help bubble") - $search[] = "(\[(.+)\]\((.+) \"(.+)\"\))"; - $replace[] = "\\1"; + $search[] = '~([^\\\\!]|^)(\[(.+)\]\((.+) "(.+)"\))~'; + $replace[] = '\1\3'; // [Google Site](http://google.fr/) - $search[] = "(\[(.+)\]\((.+)\))"; $replace[] = "\\1"; + $search[] = '~([^\\\\!]|^)(\[(.+)\]\((.+)\))~U'; + $replace[] = '\1\3'; + // Automatics links : // // - $search[] = "#<(https?://.+)>#U"; - $replace[] = "\\1"; - $search[] = "#<(.+@.+)>#U"; - $replace[] = "\\1"; + $search[] = '~([^\\\\]|^)(<(https?://.+)>)~U'; + $replace[] = '\1\3'; + $search[] = '~([^\\\\]|^)(<(.+@.+)>)~U'; + $replace[] = '\1\3'; // The links must not allow the : redo the conversion - $search[] = "#(.*)(.*)(.*)#"; - $replace[] = "\\1_\\2_\\3_\\4_\\5"; + $search[] = '~((.*)(.*\'>.*)(.*)(.*)~'; + $replace[] = '\1_\2_\3_\4_\5'; // TODO : Links by reference : // Voici un petit texte écrit par [Michel Fortin][mf]. // [mf]: http://michelf.ca/ "Mon site web" @@ -102,24 +112,36 @@ class markdown // TITLES // Titles ATX (Optionnal sharp at the end) // ###### Title6 - $search[] = "/^###### (.+)( +#+)?$/Um"; - $replace[] = "

\\1

"; + $search[] = '~^([^\\\\]|^)?(###### (.+)( +#+)?)$~Um'; + $replace[] = '

\3

'; // ##### Title5 - $search[] = "/^##### (.+)( +#+)?$/Um"; - $replace[] = "

\\1

"; + $search[] = '~^([^\\\\]|^)?(##### (.+)( +#+)?)$~Um'; + $replace[] = '

\3

'; // #### Title4 - $search[] = "/^#### (.+)( +#+)?$/Um"; - $replace[] = "

\\1

"; + $search[] = '~^([^\\\\]|^)?(#### (.+)( +#+)?)$~Um'; + $replace[] = '

\3

'; // ### Title3 - $search[] = "/^### (.+)( +#+)?$/Um"; - $replace[] = "

\\1

"; + $search[] = '~^([^\\\\]|^)?(### (.+)( +#+)?)$~Um'; + $replace[] = '

\3

'; // ## Title2 - $search[] = "/^## (.+)( +#+)?$/Um"; - $replace[] = "

\\1

"; + $search[] = '~^([^\\\\]|^)?(## (.+)( +#+)?)$~Um'; + $replace[] = '

\3

'; // # Title1 - $search[] = "/^# (.+)( +#+)?$/Um"; - $replace[] = "

\\1

"; - return preg_replace ($search, $replace, $line); + $search[] = '~^([^\\\\]|^)?(# (.+)( +#+)?)$~Um'; + $replace[] = '

\3

'; + // Remove the backslashes on the existing regex + foreach ($search as $s) + { + $s = str_replace ('([^\\\\]|^)?', '([\\\\])', $s); + $s = str_replace ('([^\\\\]|^)', '([\\\\])', $s); + $s = str_replace ('([^\\\\!]|^)', '([\\\\])', $s); + $s = str_replace ('([^\\\\*]|^)', '([\\\\])', $s); + $s = str_replace ('([^\\\\_]|^)', '([\\\\])', $s); + $search[] = $s; + $replace[] = '\2'; + } + $res = preg_replace ($search, $replace, $line); + return $res; } /** Return HTML code corresponding to the code block