markdown: manage correctely the backslashes and the emphasis and the HR separators

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3744 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2017-06-01 20:46:48 +00:00
parent 3c6b323300
commit 26b3f53398

View File

@@ -59,6 +59,91 @@ class markdown
echo "CALL searchReplace ($line)\n";
// REMEMBER : THE $line is already in HTML ENTITIES !
// Quotes : "
$res = $line;
// Manage the <hr/> separators
$search = array ("***", "---", "___", "* * *", "- - -", "_ _ _");
foreach ($search as $key=>$pattern)
{
$start = 0;
while (1)
{
$start = strpos ($res, $pattern, $start);
if ($start === false)
break;
if ($res[$start+1] === $pattern)
{
// Pattern too long, not this test : skip it
$start += strlen ($pattern) + 1;
continue;
}
if ($start > 1 && $res[$start-1] === "\\")
{
// Search the ending pattern to skip it. Remove the backslash
$res = substr ($res, 0, $start - 1) . substr ($res, $start);
}
else
{
$res = substr ($res, 0, $start) . "<hr/>" .
substr ($res, $start+strlen ($pattern));
}
}
}
// Manage the emphasis and code correctely with the backslash
$search = array ();
$replace = array ();
$search[] = "__";
$replace[] = "<strong>\\1</strong>";
$search[] = "_";
$replace[] = "<em>\\1</em>";
$search[] = "**";
$replace[] = "<strong>\\1</strong>";
$search[] = "*";
$replace[] = "<em>\\1</em>";
$search[] = "`";
$replace[] = "<code>\\1</code>";
foreach ($search as $key=>$pattern)
{
$start = 0;
while (1)
{
$start = strpos ($res, $pattern, $start);
if ($start === false)
break;
$end = strpos ($res, $pattern, $start + strlen ($pattern));
if ($end === false)
break;
if ($res[$start+1] === $pattern)
{
// Pattern too long, not this test : skip it
$start += strlen ($pattern) + strspn ($res, $pattern, $start+1);
continue;
}
if ($start > 1 && $res[$start-1] === "\\")
{
// Search the ending pattern to skip it. Remove the backslash
$res = substr ($res, 0, $start - 1) . substr ($res, $start);
}
else
{
// It is the real pattern found, without backslash. Replace by the
// $replace value
$content = substr ($res, $start + strlen ($pattern),
$end - $start - strlen ($pattern));
if (trim ($content) !== "")
{
$first = substr ($replace[$key], 0, strpos ($replace[$key], "\\1"));
$second = substr ($replace[$key], strpos ($replace[$key], "\\1")+2);
$res = substr ($res, 0, $start).$first.$content.$second.
substr ($res, $end + strlen ($pattern));
}
}
$start = $end + strlen ($pattern);
}
}
// Manage the others cases
$search = array ();
$replace = array ();
// Titles short
@@ -69,25 +154,10 @@ class markdown
$search[] = '~^([^\\\\]|^)(--+ (.+)( --+)?)$~Um';
$replace[] = '</p>\n<h2>\3</h2>\n<p>';
// EMPHASIS : _ or * for normal, __ or ** for strong
$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[] = '\1<code>\3</code>';
// LINKS (can be relative)
// images
$search[] = '~([^\\\\]|^)(!\[(.+)\]\((.+)\))~';
$replace[] = '<img src=\'\4\' alt=\'\3\'/>';
$replace[] = '\1<img src=\'\4\' alt=\'\3\'/>';
// [Google Site](http://google.fr/ "With help bubble")
$search[] = '~([^\\\\!]|^)(\[(.+)\]\((.+) &quot;(.+)&quot;\))~';
$replace[] = '\1<a href=\'\4\' title=\'\5\'>\3</a>';
@@ -140,7 +210,13 @@ class markdown
$search[] = $s;
$replace[] = '\2';
}
$res = preg_replace ($search, $replace, $line);
/*foreach ($search as $key=>$s)
{
echo "$key => $s\n";
$res = preg_replace ($s, $replace[$key], $res);
echo "$res\n";
}*/
$res = preg_replace ($search, $replace, $res);
return $res;
}