Index: marksmarty.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/marksmarty/marksmarty.module,v
retrieving revision 1.21
diff -u -p -r1.21 marksmarty.module
--- marksmarty.module	22 Jan 2008 22:25:27 -0000	1.21
+++ marksmarty.module	14 Feb 2008 13:51:54 -0000
@@ -114,17 +114,59 @@ function marksmarty_help($path = 'admin/
 function _marksmarty_process($text, $format) {
   require_once(dirname(__FILE__) .'/markdown.php');
   require_once(dirname(__FILE__) .'/smartypants.php');
+
+  $filters = filter_list_format($format);
+  if (isset($filters['geshifilter/0'])) {
+    // geshi is activated!
+    // when geshi is called _after_ marksmarty we have to do some compliance stuff
+    $geshi_compliant = $filters['geshifilter/0']->weight > $filters['marksmarty/0']->weight;
+  }
+
   if (variable_get("marksmarty_is_markdown_on_$format", 1) == 1) {
+    if ($geshi_compliant) {
+      // geshi prepares itself and replaces tags with a square bracket notation
+      // this can produce unwanted results with markdown
+      // revert it temporarily
+      $text = preg_replace('#\[geshifilter-(code(?: [^\]]*)?)\](.+)\[/geshifilter-code\]#Us',
+                            '<code>geshi:$1|$2|code</code>', $text);
+      $text = preg_replace('#\[geshifilter-(([a-z]+)(?: [^\]]*)?)\](.+)\[/geshifilter-\2\]#Us',
+                            '<pre>geshi:$1|$3|$2</pre>', $text);
+    }
     $text = Markdown($text);
+
+    if ($geshi_compliant) {
+      // redo what we reverted above so that the GeSHi module can do its work
+      $text = preg_replace('#<code>geshi:(code(?: [^|]*)?)\|(.+)\|code</code>#Us',
+                            '[geshifilter-$1]$2[/geshifilter-code]', $text);
+      $text = preg_replace('#<pre>geshi:(([a-z]+)(?: [^|]*)?)\|(.+)\|\2</pre>#Us',
+                            '[geshifilter-$1]$3[/geshifilter-$2]', $text);
+      // enable plaintext style for generic code blocks & spans
+      $text = str_replace(
+                array('<pre><code>', '</code></pre>',
+                '<code>', '</code>'),
+                array('[geshifilter-blockcode]', '[/geshifilter-blockcode]',
+                '[geshifilter-code]', '[/geshifilter-code]'),
+              $text);
+    }
   }
   if (variable_get("marksmarty_is_smarty_on_$format", 1) == 1) {
     global $smartypants_attr;
     $smartypants_attr = variable_get("marksmarty_smarty_hyphens_$format", 0) + 1;
     $text = SmartyPants($text);
+
+    if ($geshi_compliant) {
+      // &#8221; -> "
+      $text = preg_replace_callback('#\[geshifilter-[a-z]+ [^\]]+\]#U', '_marksmarty_geshipants', $text);
+    }
   }
   return $text;
 }
 
+// callback to revert &#8221; entities to double quotes
+function _marksmarty_geshipants($matches) {
+  return str_replace('&#8221;' , '"', $matches[0]);
+}
+
 function _marksmarty_settings($format) {
   require_once(dirname(__FILE__) .'/markdown.php');
   require_once(dirname(__FILE__) .'/smartypants.php');
