summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-08-11 13:02:24 +0300
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-08-11 13:02:24 +0300
commit505203e7d5360500a40cc2a6894f87ad68d44dfd (patch)
treeec3cf5737bacb3ae399372d43ba3528039b2935c /app
parentddd238eff87cb2df799b17dd3e8701780cc4d5b4 (diff)
parentd7701a263f71d48ba284ad325fd7c5a48742fe7d (diff)
downloadgitlab-ce-505203e7d5360500a40cc2a6894f87ad68d44dfd.tar.gz
Merge pull request #7466 from Razer6/improve_markup_handling
Improve readme markup, fixes #7455
Diffstat (limited to 'app')
-rw-r--r--app/helpers/application_helper.rb10
-rw-r--r--app/helpers/tree_helper.rb28
-rw-r--r--app/models/tree.rb20
-rw-r--r--app/views/projects/edit_tree/preview.html.haml2
-rw-r--r--app/views/projects/tree/_readme.html.haml9
5 files changed, 41 insertions, 28 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index c3d89eb1b82..cc49b89191b 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -222,6 +222,16 @@ module ApplicationHelper
def render_markup(file_name, file_content)
GitHub::Markup.render(file_name, file_content).html_safe
+ rescue RuntimeError
+ simple_format(file_content)
+ end
+
+ def markup?(filename)
+ Gitlab::MarkdownHelper.markup?(filename)
+ end
+
+ def gitlab_markdown?(filename)
+ Gitlab::MarkdownHelper.gitlab_markdown?(filename)
end
def spinner(text = nil, visible = false)
diff --git a/app/helpers/tree_helper.rb b/app/helpers/tree_helper.rb
index bde2a60faa9..d815257a4e3 100644
--- a/app/helpers/tree_helper.rb
+++ b/app/helpers/tree_helper.rb
@@ -21,6 +21,16 @@ module TreeHelper
tree.html_safe
end
+ def render_readme(readme)
+ if gitlab_markdown?(readme.name)
+ preserve(markdown(readme.data))
+ elsif markup?(readme.name)
+ render_markup(readme.name, readme.data)
+ else
+ simple_format(readme.data)
+ end
+ end
+
# Return an image icon depending on the file type
#
# type - String type of the tree item; either 'folder' or 'file'
@@ -38,24 +48,6 @@ module TreeHelper
"file_#{hexdigest(content.name)}"
end
- # Public: Determines if a given filename is compatible with GitHub::Markup.
- #
- # filename - Filename string to check
- #
- # Returns boolean
- def markup?(filename)
- filename.downcase.end_with?(*%w(.textile .rdoc .org .creole .wiki .mediawiki
- .rst .adoc .asciidoc .asc))
- end
-
- def gitlab_markdown?(filename)
- filename.downcase.end_with?(*%w(.mdown .md .markdown))
- end
-
- def plain_text_readme? filename
- filename =~ /^README(.txt)?$/i
- end
-
# Simple shortcut to File.join
def tree_join(*args)
File.join(*args)
diff --git a/app/models/tree.rb b/app/models/tree.rb
index ac2183be44b..07c9a825e24 100644
--- a/app/models/tree.rb
+++ b/app/models/tree.rb
@@ -1,4 +1,6 @@
class Tree
+ include Gitlab::MarkdownHelper
+
attr_accessor :entries, :readme, :contribution_guide
def initialize(repository, sha, path = '/')
@@ -6,7 +8,23 @@ class Tree
git_repo = repository.raw_repository
@entries = Gitlab::Git::Tree.where(git_repo, sha, path)
- if readme_tree = @entries.find(&:readme?)
+ available_readmes = @entries.select(&:readme?)
+
+ if available_readmes.count > 0
+ # If there is more than 1 readme in tree, find readme which is supported
+ # by markup renderer.
+ if available_readmes.length > 1
+ supported_readmes = available_readmes.select do |readme|
+ gitlab_markdown?(readme.name) || markup?(readme.name)
+ end
+
+ # Take the first supported readme, or the first available readme, if we
+ # don't support any of them
+ readme_tree = supported_readmes.first || available_readmes.first
+ else
+ readme_tree = available_readmes.first
+ end
+
readme_path = path == '/' ? readme_tree.name : File.join(path, readme_tree.name)
@readme = Gitlab::Git::Blob.find(git_repo, sha, readme_path)
end
diff --git a/app/views/projects/edit_tree/preview.html.haml b/app/views/projects/edit_tree/preview.html.haml
index 340f68cc05c..87ce5dc31d3 100644
--- a/app/views/projects/edit_tree/preview.html.haml
+++ b/app/views/projects/edit_tree/preview.html.haml
@@ -6,7 +6,7 @@
= markdown(@content)
- elsif markup?(@blob.name)
.file-content.wiki
- = raw GitHub::Markup.render(@blob.name, @content)
+ = raw render_markup(@blob.name, @content)
- else
.file-content.code
- unless @diff.empty?
diff --git a/app/views/projects/tree/_readme.html.haml b/app/views/projects/tree/_readme.html.haml
index c066b7102fd..9d0292059d6 100644
--- a/app/views/projects/tree/_readme.html.haml
+++ b/app/views/projects/tree/_readme.html.haml
@@ -3,11 +3,4 @@
%i.icon-file
= readme.name
.wiki
- - if gitlab_markdown?(readme.name)
- = preserve do
- = markdown(readme.data)
- - elsif plain_text_readme?(readme.name)
- %pre.clean
- = readme.data
- - elsif markup?(readme.name)
- = render_markup(readme.name, readme.data)
+ = render_readme(readme)