Commit 41fb7d12 authored by Robert Speicher's avatar Robert Speicher
Browse files

Merge pull request #9289 from jirutka/asciidoc-2

Refactor, simplify and unify helpers for rendering markup
......@@ -222,7 +222,9 @@
end
def render_markup(file_name, file_content)
if asciidoc?(file_name)
if gitlab_markdown?(file_name)
Haml::Helpers.preserve(markdown(file_content))
elsif asciidoc?(file_name)
asciidoc(file_content)
else
GitHub::Markup.render(file_name, file_content).
......@@ -233,7 +235,7 @@
end
def markup?(filename)
Gitlab::MarkdownHelper.markup?(filename)
Gitlab::MarkupHelper.markup?(filename)
end
def gitlab_markdown?(filename)
......@@ -237,7 +239,7 @@
end
def gitlab_markdown?(filename)
Gitlab::MarkdownHelper.gitlab_markdown?(filename)
Gitlab::MarkupHelper.gitlab_markdown?(filename)
end
def asciidoc?(filename)
......@@ -241,7 +243,7 @@
end
def asciidoc?(filename)
Gitlab::MarkdownHelper.asciidoc?(filename)
Gitlab::MarkupHelper.asciidoc?(filename)
end
# Overrides ActionView::Helpers::UrlHelper#link_to to add `rel="nofollow"` to
......
......@@ -55,7 +55,7 @@
end
def editing_preview_title(filename)
if Gitlab::MarkdownHelper.previewable?(filename)
if Gitlab::MarkupHelper.previewable?(filename)
'Preview'
else
'Preview changes'
......
......@@ -25,15 +25,7 @@
end
def render_readme(readme)
if gitlab_markdown?(readme.name)
preserve(markdown(readme.data))
elsif asciidoc?(readme.name)
asciidoc(readme.data)
elsif markup?(readme.name)
render_markup(readme.name, readme.data)
else
simple_format(readme.data)
end
render_markup(readme.name, readme.data)
end
# Return an image icon depending on the file type and mode
......
class Tree
include Gitlab::MarkdownHelper
include Gitlab::MarkupHelper
attr_accessor :repository, :sha, :path, :entries
def initialize(repository, sha, path = '/')
path = '/' if path.blank?
......@@ -3,9 +3,9 @@
attr_accessor :repository, :sha, :path, :entries
def initialize(repository, sha, path = '/')
path = '/' if path.blank?
@repository = repository
@sha = sha
@path = path
......@@ -20,7 +20,7 @@
available_readmes = blobs.select(&:readme?)
if available_readmes.count == 0
return @readme = nil
return @readme = nil
end
# Take the first previewable readme, or the first available readme, if we
......
- if gitlab_markdown?(blob.name)
.file-content.wiki
= preserve do
= markdown(blob.data)
- elsif markup?(blob.name)
- if markup?(blob.name)
.file-content.wiki
= render_markup(blob.name, blob.data)
- else
......
......@@ -13,16 +13,7 @@
.file-title
%i.fa.fa-file
%strong= snippet_blob[:snippet_object].file_name
- if gitlab_markdown?(snippet_blob[:snippet_object].file_name)
.file-content.wiki
- snippet_blob[:snippet_chunks].each do |snippet|
- unless snippet[:data].empty?
= preserve do
= markdown(snippet[:data])
- else
.file-content.code
.nothing-here-block Empty file
- elsif markup?(snippet_blob[:snippet_object].file_name)
- if markup?(snippet_blob[:snippet_object].file_name)
.file-content.wiki
- snippet_blob[:snippet_chunks].each do |snippet|
- unless snippet[:data].empty?
......
- unless @snippet.content.empty?
- if gitlab_markdown?(@snippet.file_name)
.file-content.wiki
= preserve do
= markdown(@snippet.data)
- elsif markup?(@snippet.file_name)
- if markup?(@snippet.file_name)
.file-content.wiki
= render_markup(@snippet.file_name, @snippet.data)
- else
......
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 .ad .asciidoc))
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
# Public: Determines if the given filename has AsciiDoc extension.
#
# filename - Filename string to check
#
# Returns boolean
def asciidoc?(filename)
filename.downcase.end_with?(*%w(.adoc .ad .asciidoc))
end
def previewable?(filename)
gitlab_markdown?(filename) || markup?(filename)
end
end
end
module Gitlab
module MarkupHelper
module_function
# Public: Determines if a given filename is compatible with GitHub::Markup.
#
# filename - Filename string to check
#
# Returns boolean
def markup?(filename)
gitlab_markdown?(filename) ||
asciidoc?(filename) ||
filename.downcase.end_with?(*%w(.textile .rdoc .org .creole .wiki
.mediawiki .rst))
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
# Public: Determines if the given filename has AsciiDoc extension.
#
# filename - Filename string to check
#
# Returns boolean
def asciidoc?(filename)
filename.downcase.end_with?(*%w(.adoc .ad .asciidoc))
end
def previewable?(filename)
markup?(filename)
end
end
end
......@@ -269,6 +269,13 @@
expect(render_markup('foo.rst', content).encoding.name).to eq('UTF-8')
end
it "should delegate to #markdown when file name corresponds to Markdown" do
expect(self).to receive(:gitlab_markdown?).with('foo.md').and_return(true)
expect(self).to receive(:markdown).and_return('NOEL')
expect(render_markup('foo.md', content)).to eq('NOEL')
end
it "should delegate to #asciidoc when file name corresponds to AsciiDoc" do
expect(self).to receive(:asciidoc?).with('foo.adoc').and_return(true)
expect(self).to receive(:asciidoc).and_return('NOEL')
......
require 'spec_helper'
describe Gitlab::MarkdownHelper do
describe '#markup?' do
%w(textile rdoc org creole wiki
mediawiki rst adoc ad asciidoc).each do |type|
it "returns true for #{type} files" do
expect(Gitlab::MarkdownHelper.markup?("README.#{type}")).to be_truthy
end
end
it 'returns false when given a non-markup filename' do
expect(Gitlab::MarkdownHelper.markup?('README.rb')).not_to be_truthy
end
end
describe '#gitlab_markdown?' do
%w(mdown md markdown).each do |type|
it "returns true for #{type} files" do
expect(Gitlab::MarkdownHelper.gitlab_markdown?("README.#{type}")).to be_truthy
end
end
it 'returns false when given a non-markdown filename' do
expect(Gitlab::MarkdownHelper.gitlab_markdown?('README.rb')).not_to be_truthy
end
end
describe '#asciidoc?' do
%w(adoc ad asciidoc ADOC).each do |type|
it "returns true for #{type} files" do
expect(Gitlab::MarkdownHelper.asciidoc?("README.#{type}")).to be_truthy
end
end
it 'returns false when given a non-asciidoc filename' do
expect(Gitlab::MarkdownHelper.asciidoc?('README.rb')).not_to be_truthy
end
end
end
require 'spec_helper'
describe Gitlab::MarkupHelper do
describe '#markup?' do
%w(textile rdoc org creole wiki
mediawiki rst adoc ad asciidoc mdown md markdown).each do |type|
it "returns true for #{type} files" do
expect(Gitlab::MarkupHelper.markup?("README.#{type}")).to be_truthy
end
end
it 'returns false when given a non-markup filename' do
expect(Gitlab::MarkupHelper.markup?('README.rb')).not_to be_truthy
end
end
describe '#gitlab_markdown?' do
%w(mdown md markdown).each do |type|
it "returns true for #{type} files" do
expect(Gitlab::MarkupHelper.gitlab_markdown?("README.#{type}")).to be_truthy
end
end
it 'returns false when given a non-markdown filename' do
expect(Gitlab::MarkupHelper.gitlab_markdown?('README.rb')).not_to be_truthy
end
end
describe '#asciidoc?' do
%w(adoc ad asciidoc ADOC).each do |type|
it "returns true for #{type} files" do
expect(Gitlab::MarkupHelper.asciidoc?("README.#{type}")).to be_truthy
end
end
it 'returns false when given a non-asciidoc filename' do
expect(Gitlab::MarkupHelper.asciidoc?('README.rb')).not_to be_truthy
end
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment