Skip to content
Snippets Groups Projects
Commit 99eb2831 authored by Robert Schilling's avatar Robert Schilling
Browse files

Use readme we support to render if there are multiple readmes

parent 0e3f8ea2
No related branches found
No related tags found
1 merge request!7466Improve readme markup, fixes #7455
Loading
Loading
@@ -21,6 +21,16 @@ module TreeHelper
tree.html_safe
end
 
def render_readme(readme)
if Gitlab::MarkdownHelper.gitlab_markdown?(readme.name)
preserve(markdown(readme.data))
elsif Gitlab::MarkdownHelper.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'
Loading
Loading
@@ -38,20 +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
# Simple shortcut to File.join
def tree_join(*args)
File.join(*args)
Loading
Loading
@@ -94,7 +90,8 @@ module TreeHelper
end
 
def editing_preview_title(filename)
if gitlab_markdown?(filename) || markup?(filename)
if Gitlab::MarkdownHelper.gitlab_markdown?(filename) ||
Gitlab::MarkdownHelper.markup?(filename)
'Preview'
else
'Diff'
Loading
Loading
Loading
Loading
@@ -6,7 +6,24 @@ 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::MarkdownHelper.gitlab_markdown?(readme.name) ||
Gitlab::MarkdownHelper.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
Loading
Loading
- if gitlab_markdown?(blob.name)
- if Gitlab::MarkdownHelper.gitlab_markdown?(blob.name)
.file-content.wiki
= preserve do
= markdown(blob.data)
- elsif markup?(blob.name)
- elsif Gitlab::MarkdownHelper.markup?(blob.name)
.file-content.wiki
= render_markup(blob.name, blob.data)
- else
Loading
Loading
.diff-file
.diff-content
- if gitlab_markdown?(@blob.name)
- if Gitlab::MarkdownHelper.gitlab_markdown?(@blob.name)
.file-content.wiki
= preserve do
= markdown(@content)
- elsif markup?(@blob.name)
- elsif Gitlab::MarkdownHelper.markup?(@blob.name)
.file-content.wiki
= raw GitHub::Markup.render(@blob.name, @content)
- else
Loading
Loading
Loading
Loading
@@ -3,10 +3,4 @@
%i.icon-file
= readme.name
.wiki
- if gitlab_markdown?(readme.name)
= preserve do
= markdown(readme.data)
- elsif markup?(readme.name)
= render_markup(readme.name, readme.data)
- else
= simple_format(readme.data)
= render_readme(readme)
- unless @snippet.content.empty?
- if gitlab_markdown?(@snippet.file_name)
- if Gitlab::MarkdownHelper.gitlab_markdown?(@snippet.file_name)
.file-content.wiki
= preserve do
= markdown(@snippet.data)
- elsif markup?(@snippet.file_name)
- elsif Gitlab::MarkdownHelper.markup?(@snippet.file_name)
.file-content.wiki
= render_markup(@snippet.file_name, @snippet.data)
- else
Loading
Loading
module Gitlab
module MarkdownHelper
module_function
# 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
# Public: Determines if a given filename is compatible with
# GitLab-flavored Markdown.
#
# filename - Filename string to check
#
# Returns boolean
def gitlab_markdown?(filename)
filename.downcase.end_with?(*%w(.mdown .md .markdown))
end
end
end
require 'spec_helper'
describe TreeHelper do
describe '#markup?' do
%w(textile rdoc org creole wiki mediawiki
rst adoc asciidoc asc).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
require 'spec_helper'
describe Gitlab::MarkdownHelper do
describe '#markup?' do
%w(textile rdoc org creole wiki
mediawiki rst adoc asciidoc asc).each do |type|
it "returns true for #{type} files" do
Gitlab::MarkdownHelper.markup?("README.#{type}").should be_true
end
end
it 'returns false when given a non-markup filename' do
Gitlab::MarkdownHelper.markup?('README.rb').should_not be_true
end
end
describe '#gitlab_markdown?' do
%w(mdown md markdown).each do |type|
it "returns true for #{type} files" do
Gitlab::MarkdownHelper.gitlab_markdown?("README.#{type}").should be_true
end
end
it 'returns false when given a non-markdown filename' do
Gitlab::MarkdownHelper.gitlab_markdown?('README.rb').should_not be_true
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment