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)
echo "CALL searchReplace ($line)\n";
// REMEMBER : THE $line is already in HTML ENTITIES !
// Quotes : "
$search = array ();
$replace = array ();
// Titles short
// == TITRE1
$search[] = "/^==+ (.+)( ==+)?$/Um";
$replace[] = "</p>\n<h1>\\1</h1>\n<p>";
$search[] = '~^([^\\\\]|^)(==+ (.+)( ==+)?)$~Um';
$replace[] = '</p>'."\n".'<h1>\3</h1>'."\n".'<p>';
// -- TITRE2
$search[] = "/^--+ (.+)( --+)?$/Um";
$replace[] = "</p>\n<h2>\\1</h2>\n<p>";
$search[] = '~^([^\\\\]|^)(--+ (.+)( --+)?)$~Um';
$replace[] = '</p>\n<h2>\3</h2>\n<p>';
// EMPHASIS : _ or * for normal, __ or ** for strong
$search[] = "/__(.+)__/U"; $replace[] = "<strong>\\1</strong>";
$search[] = "/_(.+)_/U"; $replace[] = "<em>\\1</em>";
$search[] = "/\*\*(.+)\*\*/U"; $replace[] = "<strong>\\1</strong>";
$search[] = "/\*(.+)\*/U"; $replace[] = "<em>\\1</em>";
$search[] = '~([^\\\\]|^)(__(.+)__)~U';
$replace[] = '\1<strong>\3</strong>';
$search[] = '~([^\\\\_]|^)(_(.+)_)~U';
$replace[] = '\1<em>\3</em>';
$search[] = '~([^\\\\]|^)(\*\*(.+)\*\*)~U';
$replace[] = '\1<strong>\3</strong>';
$search[] = '~([^\\\\*]|^)(\*(.+)\*)~U';
$replace[] = '\1<em>\3</em>';
// CODE : `code` -> <code>
$search[] = "/\\n?\`((\\n|.)+)\`/Um";
$replace[] = "<code>\\1</code>";
$search[] = "~\\n?([^\\\\]|^)(\`((\\n|.)+)\`)~Um";
$replace[] = '\1<code>\3</code>';
// LINKS (can be relative)
// images
$search[] = "(!\[(.+)\]\((.+)\))";
$replace[] = "<img src='\\2' alt='\\1'/>";
$search[] = '~([^\\\\]|^)(!\[(.+)\]\((.+)\))~';
$replace[] = '<img src=\'\4\' alt=\'\3\'/>';
// [Google Site](http://google.fr/ "With help bubble")
$search[] = "(\[(.+)\]\((.+) \"(.+)\"\))";
$replace[] = "<a href='\\2' title='\\3'>\\1</a>";
$search[] = '~([^\\\\!]|^)(\[(.+)\]\((.+) &quot;(.+)&quot;\))~';
$replace[] = '\1<a href=\'\4\' title=\'\5\'>\3</a>';
// [Google Site](http://google.fr/)
$search[] = "(\[(.+)\]\((.+)\))"; $replace[] = "<a href='\\2'>\\1</a>";
$search[] = '~([^\\\\!]|^)(\[(.+)\]\((.+)\))~U';
$replace[] = '\1<a href=\'\4\'>\3</a>';
// Automatics links :
// <http://dominique.fournier38.fr>
// <dominique@fournier38.fr>
$search[] = "#&lt;(https?://.+)&gt;#U";
$replace[] = "<a href='\\1'>\\1</a>";
$search[] = "#&lt;(.+@.+)&gt;#U";
$replace[] = "<a href='mailto:\\1'>\\1</a>";
$search[] = '~([^\\\\]|^)(&lt;(https?://.+)&gt;)~U';
$replace[] = '\1<a href=\'\3\'>\3</a>';
$search[] = '~([^\\\\]|^)(&lt;(.+@.+)&gt;)~U';
$replace[] = '\1<a href=\'mailto:\3\'>\3</a>';
// The links must not allow the <em> : redo the conversion
$search[] = "#(<a href='.*)<em>(.*)</em>(.*'>.*)<em>(.*)</em>(.*</a>)#";
$replace[] = "\\1_\\2_\\3_\\4_\\5";
$search[] = '~(<a href=\'.*)<em>(.*)</em>(.*\'>.*)<em>(.*)</em>(.*</a>)~';
$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[] = "</p><h6>\\1</h6><p>";
$search[] = '~^([^\\\\]|^)?(###### (.+)( +#+)?)$~Um';
$replace[] = '</p><h6>\3</h6><p>';
// ##### Title5
$search[] = "/^##### (.+)( +#+)?$/Um";
$replace[] = "</p><h5>\\1</h5><p>";
$search[] = '~^([^\\\\]|^)?(##### (.+)( +#+)?)$~Um';
$replace[] = '</p><h5>\3</h5><p>';
// #### Title4
$search[] = "/^#### (.+)( +#+)?$/Um";
$replace[] = "</p><h4>\\1</h4><p>";
$search[] = '~^([^\\\\]|^)?(#### (.+)( +#+)?)$~Um';
$replace[] = '</p><h4>\3</h4><p>';
// ### Title3
$search[] = "/^### (.+)( +#+)?$/Um";
$replace[] = "</p><h3>\\1</h3><p>";
$search[] = '~^([^\\\\]|^)?(### (.+)( +#+)?)$~Um';
$replace[] = '</p><h3>\3</h3><p>';
// ## Title2
$search[] = "/^## (.+)( +#+)?$/Um";
$replace[] = "</p><h2>\\1</h2><p>";
$search[] = '~^([^\\\\]|^)?(## (.+)( +#+)?)$~Um';
$replace[] = '</p><h2>\3</h2><p>';
// # Title1
$search[] = "/^# (.+)( +#+)?$/Um";
$replace[] = "</p><h1>\\1</h1><p>";
return preg_replace ($search, $replace, $line);
$search[] = '~^([^\\\\]|^)?(# (.+)( +#+)?)$~Um';
$replace[] = '</p><h1>\3</h1><p>';
// 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