From 0b7590e27fbb471f90a7bf3ebaf20439f6558853 Mon Sep 17 00:00:00 2001
From: Capi Etheriel <barraponto@gmail.com>
Date: Mon, 8 Apr 2013 11:59:19 -0300
Subject: Issue #1319492 by barraponto, fangel: Disable certain parts of Markdown

---
 markdown.module | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)

diff --git a/markdown.module b/markdown.module
index 22f1263..d860017 100644
--- a/markdown.module
+++ b/markdown.module
@@ -24,6 +24,12 @@ function markdown_filter_info() {
     'description' => t('Allows content to be submitted using Markdown, a simple plain-text syntax that is filtered into valid XHTML.'),
     'process callback' => '_filter_markdown',
     'settings callback' => '_filter_markdown_settings',
+    'default settings' => array(
+      'headings' => 1,
+      'tables' => 1,
+      'lists' => 1,
+      'images' => 1,
+    ),
     'tips callback'  => '_filter_markdown_tips',
   );
 
@@ -136,18 +142,41 @@ function _filter_markdown($text, $format) {
     $text = Markdown($text);
   }
 
+  static $parsers;
+  if (empty($parsers[$format->format])) {
+    $parser_class = MARKDOWN_PARSER_CLASS;
+    $parsers[$format->format] = new $parser_class;
+  }
+
+  if (empty($format->settings['headings'])) {
+    unset($parsers[$format->format]->block_gamut['doHeaders']);
+  }
+  if (empty($format->settings['tables'])) {
+    unset($parsers[$format->format]->block_gamut['doTables']);
+  }
+  if (empty($format->settings['lists'])) {
+    unset($parsers[$format->format]->block_gamut['doLists']);
+    unset($parsers[$format->format]->block_gamut['doDefLists']);
+  }
+  if (empty($format->settings['images'])) {
+    unset($parsers[$format->format]->span_gamut['doImages']);
+  }
+
+ $text = $parsers[$format->format]->transform($text);
   return $text;
 }
 
 /**
- * Filter settings callback. Just provides a version overview.
+ * Filter settings callback.
  */
 function _filter_markdown_settings($form, &$form_state, $filter, $format, $defaults) {
   libraries_load('php-markdown');
 
+  $filter->settings += $defaults;
   $settings['markdown_wrapper'] = array(
     '#type' => 'fieldset',
     '#title' => t('Markdown'),
+    '#parents' => array('filters', $filter->name, 'settings'),
   );
   $links = array(
     'Markdown PHP Version: <a href="http://michelf.com/projects/php-markdown/">' . MARKDOWN_VERSION . '</a>',
@@ -158,6 +187,30 @@ function _filter_markdown_settings($form, &$form_state, $filter, $format, $defau
     '#type' => 'item',
     '#markup' => theme('item_list', array('items' => $links)),
   );
+  $settings['markdown_wrapper']['headings'] = array(
+    '#type' => 'checkbox',
+    '#default_value' => $filter->settings['headings'],
+    '#title' => t('Enable headings'),
+    '#description' => t('If checked, headings will be parsed.'),
+  );
+  $settings['markdown_wrapper']['tables'] = array(
+    '#type' => 'checkbox',
+    '#default_value' => $filter->settings['tables'],
+    '#title' => t('Enable tables'),
+    '#description' => t('If checked, tables will be parsed.'),
+  );
+  $settings['markdown_wrapper']['lists'] = array(
+    '#type' => 'checkbox',
+    '#default_value' => $filter->settings['lists'],
+    '#title' => t('Enable lists'),
+    '#description' => t('If checked, lists (including definition lists) will be parsed.'),
+  );
+  $settings['markdown_wrapper']['images'] = array(
+    '#type' => 'checkbox',
+    '#default_value' => $filter->settings['images'],
+    '#title' => t('Enable images'),
+    '#description' => t('If checked, images will be enabled.'),
+  );
 
   return $settings;
 }
-- 
1.8.2

