--- module.orig	2008-02-11 01:09:47.000000000 +0100
+++ marksmarty.module	2008-02-11 01:20:44.000000000 +0100
@@ -1,5 +1,5 @@
 <?php
-// $Id: marksmarty.module,v 1.20 2008/01/22 21:57:25 weitzman Exp $
+// $Id: marksmarty.module,v 1.21 2008/01/22 22:25:27 weitzman Exp $
 
 /********************************************************************
  * Drupal Hooks
@@ -112,19 +112,65 @@ function marksmarty_help($path = 'admin/
  ********************************************************************/
 
 function _marksmarty_process($text, $format) {
-  require_once(dirname(__FILE__) .'/markdown.php');
-  require_once(dirname(__FILE__) .'/smartypants.php');
   if (variable_get("marksmarty_is_markdown_on_$format", 1) == 1) {
-    $text = Markdown($text);
+    require_once(dirname(__FILE__) .'/markdown.php');
+
+    $geshi_compliant = variable_get("marksmarty_geshi_compliant_$format", 0);
+    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-(([a-z]+)(?: [^\]]*)?)\](.+)\[/geshifilter-\2\]#Us',
+                            '<pre><geshi:$1>$3</geshi:$2></pre>', $text);
+    }
+
+    if (variable_get("marksmarty_markdown_no_markup_$format", 0) == 1) {
+      // use special markdown parser to escape unwanted html tags
+      require_once(dirname(__FILE__).'/no-markup_markdown.php');
+
+      $text = MarkdownWithoutMarkup($text, variable_get("marksmarty_markdown_allowed_html_$format", ''));
+    } else {
+      // default markdown parser
+      $text = Markdown($text);
+    }
+
+    if ($geshi_compliant) {
+      // redo what we reverted above so that the GeSHi module can do its work
+      $text = preg_replace('#<pre><geshi:(([a-z]+)(?: [^>]*)?)>(.+)</geshi:\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) {
+    require_once(dirname(__FILE__) .'/smartypants.php');
+
     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');
@@ -144,6 +190,25 @@ function _marksmarty_settings($format) {
     '#default_value' => variable_get("marksmarty_is_markdown_on_$format", 1),
     '#options' => array(0 => t('No'), 1 => t('Yes')),
   );
+  $form['markdown_settings']["marksmarty_markdown_no_markup_$format"] = array(
+    '#type' => 'select',
+    '#title' => t('Disable HTML markup in Markdown?'),
+    '#default_value' => variable_get("marksmarty_markdown_no_markup_$format", 0),
+    '#options' => array(0 => t('No'), 1 => t('Yes')),
+  );
+  $form['markdown_settings']["marksmarty_markdown_allowed_html_$format"] = array(
+    '#type' => 'textfield',
+    '#title' => t('HTML tag whitelist'),
+    '#description' => t('only applies when markup is disabled (see above)'),
+    '#default_value' => variable_get("marksmarty_markdown_allowed_html_$format", ''),
+  );
+  $form['markdown_settings']["marksmarty_geshi_compliant_$format"] = array(
+    '#type' => 'select',
+    '#title' => t('Enable GeSHi support mode'),
+    '#description' => t('When you use GeSHi rearrange the filters so that marksmarty comes before GeSHi and enable this option. When you disable HTML markup, add pre to the HTML tag whitelist.'),
+    '#default_value' => variable_get("marksmarty_geshi_compliant_$format", 0),
+    '#options' => array(0 => t('No'), 1 => t('Yes')),
+  );
   $form['markdown_settings']["marksmarty_is_smarty_on_$format"] = array(
     '#type' => 'select',
     '#title' => t('Enable SmartyPants?'),
