diff options
author | Dmitriy Zaporozhets <dzaporozhets@gitlab.com> | 2015-04-22 09:06:03 +0000 |
---|---|---|
committer | Dmitriy Zaporozhets <dzaporozhets@gitlab.com> | 2015-04-22 09:06:03 +0000 |
commit | 2c4eef589923ca6228c941f32feec7b56302c0ee (patch) | |
tree | b48d8486879da2c3b83f4bd2d3d08c601c2a7ce9 /lib | |
parent | d7f61affaf845f44b4cc995e34eb1606c47c8eff (diff) | |
parent | 870969a1b28e129f5ba73539528e81273b7e7b40 (diff) | |
download | gitlab-ce-2c4eef589923ca6228c941f32feec7b56302c0ee.tar.gz |
Merge branch 'rs-remove-gitlab-pipeline' into 'master'
Remove html-pipeline-gitlab
Ports the custom emoji filter to the app itself.
See merge request !1781
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/markdown.rb | 28 | ||||
-rw-r--r-- | lib/gitlab/markdown/emoji_filter.rb | 79 |
2 files changed, 95 insertions, 12 deletions
diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index f2302015437..37b250d353e 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -1,5 +1,4 @@ require 'html/pipeline' -require 'html/pipeline/gitlab' module Gitlab # Custom parser for GitLab-flavored Markdown @@ -61,19 +60,24 @@ module Gitlab reference_only_path: true ) - markdown_context = { + pipeline = HTML::Pipeline.new(filters) + + context = { + # SanitizationFilter + whitelist: sanitization_whitelist, + + # EmojiFilter asset_root: Gitlab.config.gitlab.url, asset_host: Gitlab::Application.config.asset_host, - whitelist: sanitization_whitelist, - reference_class: html_options[:class], - only_path: options[:reference_only_path], + + # ReferenceFilter current_user: current_user, - project: project + only_path: options[:reference_only_path], + project: project, + reference_class: html_options[:class] } - markdown_pipeline = HTML::Pipeline::Gitlab.new(filters).pipeline - - result = markdown_pipeline.call(text, markdown_context) + result = pipeline.call(text, context) save_options = 0 if options[:xhtml] @@ -91,7 +95,7 @@ module Gitlab private - # Custom filters for html-pipeline: + # Filters used in our pipeline # # SanitizationFilter should come first so that all generated reference HTML # goes through untouched. @@ -101,6 +105,8 @@ module Gitlab [ HTML::Pipeline::SanitizationFilter, + Gitlab::Markdown::EmojiFilter, + Gitlab::Markdown::UserReferenceFilter, Gitlab::Markdown::IssueReferenceFilter, Gitlab::Markdown::ExternalIssueReferenceFilter, @@ -109,8 +115,6 @@ module Gitlab Gitlab::Markdown::CommitRangeReferenceFilter, Gitlab::Markdown::CommitReferenceFilter, Gitlab::Markdown::LabelReferenceFilter, - - HTML::Pipeline::Gitlab::GitlabEmojiFilter ] end diff --git a/lib/gitlab/markdown/emoji_filter.rb b/lib/gitlab/markdown/emoji_filter.rb new file mode 100644 index 00000000000..e239f766844 --- /dev/null +++ b/lib/gitlab/markdown/emoji_filter.rb @@ -0,0 +1,79 @@ +require 'gitlab_emoji' +require 'html/pipeline/filter' +require 'action_controller' + +module Gitlab + module Markdown + # HTML filter that replaces :emoji: with images. + # + # Based on HTML::Pipeline::EmojiFilter + # + # Context options: + # :asset_root + # :asset_host + class EmojiFilter < HTML::Pipeline::Filter + IGNORED_ANCESTOR_TAGS = %w(pre code tt).to_set + + def call + doc.search('text()').each do |node| + content = node.to_html + next unless content.include?(':') + next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS) + + html = emoji_image_filter(content) + + next if html == content + + node.replace(html) + end + + doc + end + + # Replace :emoji: with corresponding images. + # + # text - String text to replace :emoji: in. + # + # Returns a String with :emoji: replaced with images. + def emoji_image_filter(text) + text.gsub(emoji_pattern) do |match| + name = $1 + "<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{emoji_url(name)}' height='20' width='20' align='absmiddle' />" + end + end + + private + + def emoji_url(name) + emoji_path = "emoji/#{emoji_filename(name)}" + if context[:asset_host] + # Asset host is specified. + url_to_image(emoji_path) + elsif context[:asset_root] + # Gitlab url is specified + File.join(context[:asset_root], url_to_image(emoji_path)) + else + # All other cases + url_to_image(emoji_path) + end + end + + def url_to_image(image) + ActionController::Base.helpers.url_to_image(image) + end + + # Build a regexp that matches all valid :emoji: names. + def self.emoji_pattern + @emoji_pattern ||= /:(#{Emoji.emojis_names.map { |name| Regexp.escape(name) }.join('|')}):/ + end + + def emoji_pattern + self.class.emoji_pattern + end + + def emoji_filename(name) + "#{Emoji.emoji_filename(name)}.png" + end + end + end +end |