markdown: allow to escape a part of string with backslash to not apply the markdown translation

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3743 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2017-06-01 14:38:49 +00:00
parent 42cfed50c1
commit 3c6b323300

View File

@@ -57,44 +57,54 @@ class markdown
{ {
if ($this->debug) if ($this->debug)
echo "CALL searchReplace ($line)\n"; echo "CALL searchReplace ($line)\n";
// REMEMBER : THE $line is already in HTML ENTITIES !
// Quotes : "
$search = array (); $search = array ();
$replace = array (); $replace = array ();
// Titles short // Titles short
// == TITRE1 // == TITRE1
$search[] = "/^==+ (.+)( ==+)?$/Um"; $search[] = '~^([^\\\\]|^)(==+ (.+)( ==+)?)$~Um';
$replace[] = "</p>\n<h1>\\1</h1>\n<p>"; $replace[] = '</p>'."\n".'<h1>\3</h1>'."\n".'<p>';
// -- TITRE2 // -- TITRE2
$search[] = "/^--+ (.+)( --+)?$/Um"; $search[] = '~^([^\\\\]|^)(--+ (.+)( --+)?)$~Um';
$replace[] = "</p>\n<h2>\\1</h2>\n<p>"; $replace[] = '</p>\n<h2>\3</h2>\n<p>';
// EMPHASIS : _ or * for normal, __ or ** for strong // EMPHASIS : _ or * for normal, __ or ** for strong
$search[] = "/__(.+)__/U"; $replace[] = "<strong>\\1</strong>"; $search[] = '~([^\\\\]|^)(__(.+)__)~U';
$search[] = "/_(.+)_/U"; $replace[] = "<em>\\1</em>"; $replace[] = '\1<strong>\3</strong>';
$search[] = "/\*\*(.+)\*\*/U"; $replace[] = "<strong>\\1</strong>"; $search[] = '~([^\\\\_]|^)(_(.+)_)~U';
$search[] = "/\*(.+)\*/U"; $replace[] = "<em>\\1</em>"; $replace[] = '\1<em>\3</em>';
$search[] = '~([^\\\\]|^)(\*\*(.+)\*\*)~U';
$replace[] = '\1<strong>\3</strong>';
$search[] = '~([^\\\\*]|^)(\*(.+)\*)~U';
$replace[] = '\1<em>\3</em>';
// CODE : `code` -> <code> // CODE : `code` -> <code>
$search[] = "/\\n?\`((\\n|.)+)\`/Um"; $search[] = "~\\n?([^\\\\]|^)(\`((\\n|.)+)\`)~Um";
$replace[] = "<code>\\1</code>"; $replace[] = '\1<code>\3</code>';
// LINKS (can be relative) // LINKS (can be relative)
// images // images
$search[] = "(!\[(.+)\]\((.+)\))"; $search[] = '~([^\\\\]|^)(!\[(.+)\]\((.+)\))~';
$replace[] = "<img src='\\2' alt='\\1'/>"; $replace[] = '<img src=\'\4\' alt=\'\3\'/>';
// [Google Site](http://google.fr/ "With help bubble") // [Google Site](http://google.fr/ "With help bubble")
$search[] = "(\[(.+)\]\((.+) \"(.+)\"\))"; $search[] = '~([^\\\\!]|^)(\[(.+)\]\((.+) &quot;(.+)&quot;\))~';
$replace[] = "<a href='\\2' title='\\3'>\\1</a>"; $replace[] = '\1<a href=\'\4\' title=\'\5\'>\3</a>';
// [Google Site](http://google.fr/) // [Google Site](http://google.fr/)
$search[] = "(\[(.+)\]\((.+)\))"; $replace[] = "<a href='\\2'>\\1</a>"; $search[] = '~([^\\\\!]|^)(\[(.+)\]\((.+)\))~U';
$replace[] = '\1<a href=\'\4\'>\3</a>';
// Automatics links : // Automatics links :
// <http://dominique.fournier38.fr> // <http://dominique.fournier38.fr>
// <dominique@fournier38.fr> // <dominique@fournier38.fr>
$search[] = "#&lt;(https?://.+)&gt;#U"; $search[] = '~([^\\\\]|^)(&lt;(https?://.+)&gt;)~U';
$replace[] = "<a href='\\1'>\\1</a>"; $replace[] = '\1<a href=\'\3\'>\3</a>';
$search[] = "#&lt;(.+@.+)&gt;#U"; $search[] = '~([^\\\\]|^)(&lt;(.+@.+)&gt;)~U';
$replace[] = "<a href='mailto:\\1'>\\1</a>"; $replace[] = '\1<a href=\'mailto:\3\'>\3</a>';
// The links must not allow the <em> : redo the conversion // The links must not allow the <em> : redo the conversion
$search[] = "#(<a href='.*)<em>(.*)</em>(.*'>.*)<em>(.*)</em>(.*</a>)#"; $search[] = '~(<a href=\'.*)<em>(.*)</em>(.*\'>.*)<em>(.*)</em>(.*</a>)~';
$replace[] = "\\1_\\2_\\3_\\4_\\5"; $replace[] = '\1_\2_\3_\4_\5';
// TODO : Links by reference : // TODO : Links by reference :
// Voici un petit texte écrit par [Michel Fortin][mf]. // Voici un petit texte écrit par [Michel Fortin][mf].
// [mf]: http://michelf.ca/ "Mon site web" // [mf]: http://michelf.ca/ "Mon site web"
@@ -102,24 +112,36 @@ class markdown
// TITLES // TITLES
// Titles ATX (Optionnal sharp at the end) // Titles ATX (Optionnal sharp at the end)
// ###### Title6 // ###### Title6
$search[] = "/^###### (.+)( +#+)?$/Um"; $search[] = '~^([^\\\\]|^)?(###### (.+)( +#+)?)$~Um';
$replace[] = "</p><h6>\\1</h6><p>"; $replace[] = '</p><h6>\3</h6><p>';
// ##### Title5 // ##### Title5
$search[] = "/^##### (.+)( +#+)?$/Um"; $search[] = '~^([^\\\\]|^)?(##### (.+)( +#+)?)$~Um';
$replace[] = "</p><h5>\\1</h5><p>"; $replace[] = '</p><h5>\3</h5><p>';
// #### Title4 // #### Title4
$search[] = "/^#### (.+)( +#+)?$/Um"; $search[] = '~^([^\\\\]|^)?(#### (.+)( +#+)?)$~Um';
$replace[] = "</p><h4>\\1</h4><p>"; $replace[] = '</p><h4>\3</h4><p>';
// ### Title3 // ### Title3
$search[] = "/^### (.+)( +#+)?$/Um"; $search[] = '~^([^\\\\]|^)?(### (.+)( +#+)?)$~Um';
$replace[] = "</p><h3>\\1</h3><p>"; $replace[] = '</p><h3>\3</h3><p>';
// ## Title2 // ## Title2
$search[] = "/^## (.+)( +#+)?$/Um"; $search[] = '~^([^\\\\]|^)?(## (.+)( +#+)?)$~Um';
$replace[] = "</p><h2>\\1</h2><p>"; $replace[] = '</p><h2>\3</h2><p>';
// # Title1 // # Title1
$search[] = "/^# (.+)( +#+)?$/Um"; $search[] = '~^([^\\\\]|^)?(# (.+)( +#+)?)$~Um';
$replace[] = "</p><h1>\\1</h1><p>"; $replace[] = '</p><h1>\3</h1><p>';
return preg_replace ($search, $replace, $line); // 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 /** Return HTML code corresponding to the code block