From 26b3f53398c9546e11b09f1ebc4cf576871f12eb Mon Sep 17 00:00:00 2001
From: Dominique Fournier
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) . "
" .
+ substr ($res, $start+strlen ($pattern));
+ }
+ }
+ }
+
+ // Manage the emphasis and code correctely with the backslash
+ $search = array ();
+ $replace = array ();
+ $search[] = "__";
+ $replace[] = "\\1";
+ $search[] = "_";
+ $replace[] = "\\1";
+ $search[] = "**";
+ $replace[] = "\\1";
+ $search[] = "*";
+ $replace[] = "\\1";
+ $search[] = "`";
+ $replace[] = "\\1";
+
+ 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[] = '
';
- // EMPHASIS : _ or * for normal, __ or ** for strong
- $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\3';
-
// LINKS (can be relative)
// images
$search[] = '~([^\\\\]|^)(!\[(.+)\]\((.+)\))~';
- $replace[] = '';
+ $replace[] = '\1
';
// [Google Site](http://google.fr/ "With help bubble")
$search[] = '~([^\\\\!]|^)(\[(.+)\]\((.+) "(.+)"\))~';
$replace[] = '\1\3';
@@ -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;
}