From f3abbcd01b7db412ff8801a533ec59010e3d557d Mon Sep 17 00:00:00 2001
From: Gisle Hannemyr <gisle@hannemyr.no>
Date: Tue, 24 Mar 2015 08:08:46 +0100
Subject: [PATCH] Issue #1405734 by gisle: Patch to output READMEs as parsed
 markdown

---
 advanced_help.module          | 21 +++++++++++++++++++--
 help.css                      |  1 -
 help/using-advanced-help.html |  4 ++--
 3 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/advanced_help.module b/advanced_help.module
index 1f32306..e2d7abf 100644
--- a/advanced_help.module
+++ b/advanced_help.module
@@ -630,10 +630,27 @@ function advanced_help_view_topic($module, $topic, $popup = FALSE) {
     $info = advanced_help_get_topic($module, $topic);
     $file = "./$file_info[path]/$file_info[file]";
 
+    $ext = pathinfo($file, PATHINFO_EXTENSION);
     $output = file_get_contents($file);
+
     if (isset($info['readme file']) && $info['readme file']) {
       // Readme files are treated as plain text: filter accordingly.
-      $output = check_plain($output);
+
+      if ('md' == $ext && module_exists('markdown')) {
+        $filters = module_invoke('markdown', 'filter_info');
+        $md_info = $filters['filter_markdown'];
+        if (function_exists($md_info['process callback'])) {
+          $function = $md_info['process callback'];
+          $output = filter_xss_admin($function($output, NULL));
+        }
+        else {
+          $output = '<pre>' . check_plain($output) . '</pre>';
+        }
+      }
+      else {
+        // Nicer output if we wrap it in <pre>?
+        $output = check_plain($output);
+      }
     }
 
     // Make some exchanges. The strtr is because url() translates $ into %24
@@ -797,7 +814,7 @@ function _advanced_help_parse_ini() {
       elseif (!file_exists("$module_path/help")) {
         // Look for one or more README files.
         $files = file_scan_directory("./$module_path",
-          '/^(README|readme).*\.(txt|TXT)$/', array('recurse' => FALSE));
+          '/^(readme).*\.(txt|md)$/i', array('recurse' => FALSE));
         $path = "./$module_path";
         foreach ($files as $name => $fileinfo) {
           $info[$fileinfo->filename] = array(
diff --git a/help.css b/help.css
index b4a8087..4f6ad85 100644
--- a/help.css
+++ b/help.css
@@ -1,5 +1,4 @@
 
-.advanced-help-topic code,
 .advanced-help-topic pre {
   background: #f1f1f1;
   border: 1px solid #444;
diff --git a/help/using-advanced-help.html b/help/using-advanced-help.html
index ad85eb6..8b20115 100644
--- a/help/using-advanced-help.html
+++ b/help/using-advanced-help.html
@@ -23,9 +23,9 @@ parent = The optional topic parent to use in the breadcrumb,
 All topics are addressed by the module providing the topic, and by the topic
 id. To embed links, use the following format:
 </p>
-<code>
+<pre>
 $output .= theme('advanced_help_topic', $module, $topic);
-</code>
+</pre>
 
 <p>Inside your help file, link to other topics using the format <strong>&lt;a href="&amp;topic:module/topic&amp;"&gt;</strong>. This
 format will ensure the popup status remains consistent when switching between links.</p>
-- 
1.9.2

