From 52c521ffe84dac5dc72a299ee1ecf4736a10f07f Mon Sep 17 00:00:00 2001
From: Robert Speicher <rspeicher@gmail.com>
Date: Wed, 5 Sep 2012 16:52:49 -0400
Subject: [PATCH] Use GitHub::Markup to parse markup files

Closes #1382
---
 app/helpers/tree_helper.rb          | 10 ++++++++++
 app/views/refs/_tree.html.haml      |  6 +-----
 app/views/refs/_tree_file.html.haml |  5 ++---
 spec/helpers/tree_helper_spec.rb    | 15 +++++++++++++++
 4 files changed, 28 insertions(+), 8 deletions(-)
 create mode 100644 spec/helpers/tree_helper_spec.rb

diff --git a/app/helpers/tree_helper.rb b/app/helpers/tree_helper.rb
index ed3053d8af59d..c51ee84a25ef1 100644
--- a/app/helpers/tree_helper.rb
+++ b/app/helpers/tree_helper.rb
@@ -24,4 +24,14 @@ def tree_full_path(content)
       content.name
     end
   end
+
+  # Public: Determines if a given filename is compatible with GitHub::Markup.
+  #
+  # filename - Filename string to check
+  #
+  # Returns boolean
+  def markup?(filename)
+    filename.end_with?(*%w(.mdown .md .markdown .textile .rdoc .org .creole
+                          .mediawiki .rst .asciidoc .pod))
+  end
 end
diff --git a/app/views/refs/_tree.html.haml b/app/views/refs/_tree.html.haml
index a4765c1087ac1..297a3b5f60ad2 100644
--- a/app/views/refs/_tree.html.haml
+++ b/app/views/refs/_tree.html.haml
@@ -43,11 +43,7 @@
           %i.icon-file
           = content.name
         .file_content.wiki
-          - if content.name =~ /\.(md|markdown)$/i
-            = preserve do
-              = markdown(content.data)
-          - else
-            = simple_format(content.data)
+          = raw GitHub::Markup.render(content.name, content.data)
 
 :javascript
   $(function(){
diff --git a/app/views/refs/_tree_file.html.haml b/app/views/refs/_tree_file.html.haml
index b5ed61bb45a28..765f271a1bf50 100644
--- a/app/views/refs/_tree_file.html.haml
+++ b/app/views/refs/_tree_file.html.haml
@@ -9,10 +9,9 @@
       = link_to "history", project_commits_path(@project, path: params[:path], ref: @ref), class: "btn very_small"
       = link_to "blame", blame_file_project_ref_path(@project, @ref, path: params[:path]), class: "btn very_small"
   - if file.text?
-    - if name =~ /\.(md|markdown)$/i
+    - if markup?(name)
       .file_content.wiki
-        = preserve do
-          = markdown(file.data)
+        = raw GitHub::Markup.render(name, file.data)
     - else
       .file_content.code
         - unless file.empty?
diff --git a/spec/helpers/tree_helper_spec.rb b/spec/helpers/tree_helper_spec.rb
new file mode 100644
index 0000000000000..bb124d8b303d8
--- /dev/null
+++ b/spec/helpers/tree_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe TreeHelper do
+  describe '#markup?' do
+    %w(mdown md markdown textile rdoc org creole mediawiki rst asciidoc pod).each do |type|
+      it "returns true for #{type} files" do
+        markup?("README.#{type}").should be_true
+      end
+    end
+
+    it "returns false when given a non-markup filename" do
+      markup?('README.rb').should_not be_true
+    end
+  end
+end
-- 
GitLab