diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-04-21 18:17:19 +0300 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2017-04-27 14:22:17 +0300 |
commit | 4f2d6b3e21911ab29ae18dff1be909d94f3127ed (patch) | |
tree | ea7186401f4b0fa02970c55f788b1dabeb2a247a /app/helpers/markup_helper.rb | |
parent | 05e0f504530a162d4bcb886adf504c12cffd5934 (diff) |
Refactor MarkupHelper
Diffstat (limited to 'app/helpers/markup_helper.rb')
-rw-r--r-- | app/helpers/markup_helper.rb | 116 |
1 files changed, 48 insertions, 68 deletions
diff --git a/app/helpers/markup_helper.rb b/app/helpers/markup_helper.rb index c39b630aec6..95036ccf57d 100644 --- a/app/helpers/markup_helper.rb +++ b/app/helpers/markup_helper.rb @@ -59,18 +59,22 @@ module MarkupHelper fragment.to_html.html_safe end - def markdown(text, context = {}) - html = markdown_render(text, context) + # Return the first line of +text+, up to +max_chars+, after parsing the line + # as Markdown. HTML tags in the parsed output are not counted toward the + # +max_chars+ limit. If the length limit falls within a tag's contents, then + # the tag contents are truncated without removing the closing tag. + def first_line_in_markdown(text, max_chars = nil, options = {}) + md = markdown(text, options).strip - markup_postprocess(html, context) + truncate_visible(md, max_chars || md.length) if md.present? end - def markdown_render(text, context = {}) + def markdown(text, context = {}) return "" unless text.present? context[:project] ||= @project - - Banzai.render(text, context) + html = context.delete(:rendered) || markdown_unsafe(text, context) + banzai_postprocess(html, context) end def markdown_field(object, field) @@ -81,85 +85,59 @@ module MarkupHelper banzai_postprocess(html, object.banzai_render_context(field)) end - def asciidoc_render(text) - Gitlab::Asciidoc.render( - text, - project: @project, - current_user: (current_user if defined?(current_user)), - - # RelativeLinkFilter - project_wiki: @project_wiki, - requested_path: @path, - ref: @ref, - commit: @commit - ) - end - - def other_markup_render(file_name, text) - Gitlab::OtherMarkup.render( - file_name, - text, - project: @project, - current_user: (current_user if defined?(current_user)), - - # RelativeLinkFilter - project_wiki: @project_wiki, - requested_path: @path, - ref: @ref, - commit: @commit - ) - end - - def markup_postprocess(html, context = {}) - return "" unless html.present? - + def markup(file_name, text, context = {}) context[:project] ||= @project - + html = context.delete(:rendered) || markup_unsafe(file_name, text, context) banzai_postprocess(html, context) end - # Return the first line of +text+, up to +max_chars+, after parsing the line - # as Markdown. HTML tags in the parsed output are not counted toward the - # +max_chars+ limit. If the length limit falls within a tag's contents, then - # the tag contents are truncated without removing the closing tag. - def first_line_in_markdown(text, max_chars = nil, options = {}) - md = markdown(text, options).strip + def render_wiki_content(wiki_page) + text = wiki_page.content + return "" unless text.present? - truncate_visible(md, max_chars || md.length) if md.present? - end + context = { pipeline: :wiki, project: @project, project_wiki: @project_wiki, page_slug: wiki_page.slug } - def render_wiki_content(wiki_page) - context = { pipeline: :wiki, project_wiki: @project_wiki, page_slug: wiki_page.slug } - case wiki_page.format - when :markdown - html = markdown_render(wiki_page.content, context) - when :asciidoc - html = asciidoc_render(wiki_page.content) - else - return wiki_page.formatted_content.html_safe - end - markup_postprocess(html, context) - end + html = + case wiki_page.format + when :markdown + markdown_unsafe(text, context) + when :asciidoc + asciidoc_unsafe(text) + else + wiki_page.formatted_content.html_safe + end - def render_markup(file_name, file_content) - html = markup_render(file_name, file_content) - markup_postprocess(html) + banzai_postprocess(html, context) end - def markup_render(file_name, file_content) + def markup_unsafe(file_name, text, context = {}) + return "" unless text.present? + if gitlab_markdown?(file_name) - Hamlit::RailsHelpers.preserve(markdown_render(file_content)) + Hamlit::RailsHelpers.preserve(markdown_unsafe(text, context)) elsif asciidoc?(file_name) - asciidoc_render(file_content) + asciidoc_unsafe(text) elsif plain?(file_name) content_tag :pre, class: 'plain-readme' do - file_content + text end else - other_markup_render(file_name, file_content) + other_markup_unsafe(file_name, text) end rescue RuntimeError - simple_format(file_content) + simple_format(text) + end + + def markdown_unsafe(text, context = {}) + Banzai.render(text, context) + end + + def asciidoc_unsafe(text) + Gitlab::Asciidoc.render(text) + end + + def other_markup_unsafe(file_name, text) + Gitlab::OtherMarkup.render(file_name, text) end # Returns the text necessary to reference `entity` across projects @@ -249,6 +227,8 @@ module MarkupHelper # Calls Banzai.post_process with some common context options def banzai_postprocess(html, context = {}) + return "" unless html.present? + context.merge!( current_user: (current_user if defined?(current_user)), |