Commit 390183a4 authored by Robert Schilling's avatar Robert Schilling

Factor out Emoji parsing using html-pipeline-gitlab

parent 9cd5ff04
...@@ -79,6 +79,9 @@ gem "six" ...@@ -79,6 +79,9 @@ gem "six"
# Seed data # Seed data
gem "seed-fu" gem "seed-fu"
# Markup pipeline for GitLab
gem 'html-pipeline-gitlab', '~> 0.1.0'
# Markdown to HTML # Markdown to HTML
gem "github-markup" gem "github-markup"
......
...@@ -239,6 +239,12 @@ GEM ...@@ -239,6 +239,12 @@ GEM
hipchat (0.14.0) hipchat (0.14.0)
httparty httparty
httparty httparty
html-pipeline (1.11.0)
activesupport (>= 2)
nokogiri (~> 1.4)
html-pipeline-gitlab (0.1.0)
gitlab_emoji (~> 0.0.1.1)
html-pipeline (~> 1.11.0)
http_parser.rb (0.5.3) http_parser.rb (0.5.3)
httparty (0.13.0) httparty (0.13.0)
json (~> 1.8) json (~> 1.8)
...@@ -629,6 +635,7 @@ DEPENDENCIES ...@@ -629,6 +635,7 @@ DEPENDENCIES
guard-spinach guard-spinach
haml-rails haml-rails
hipchat (~> 0.14.0) hipchat (~> 0.14.0)
html-pipeline-gitlab (~> 0.1.0)
httparty httparty
jasmine (= 2.0.2) jasmine (= 2.0.2)
jquery-atwho-rails (~> 0.3.3) jquery-atwho-rails (~> 0.3.3)
......
...@@ -151,12 +151,6 @@ module ApplicationHelper ...@@ -151,12 +151,6 @@ module ApplicationHelper
sanitize(str, tags: %w(a span)) sanitize(str, tags: %w(a span))
end end
def image_url(source)
# prevent relative_root_path being added twice (it's part of root_url and path_to_image)
root_url.sub(/#{root_path}$/, path_to_image(source))
end
alias_method :url_to_image, :image_url
def body_data_page def body_data_page
path = controller.controller_path.split('/') path = controller.controller_path.split('/')
......
require 'html/pipeline'
require 'html/pipeline/gitlab'
module Gitlab module Gitlab
# Custom parser for GitLab-flavored Markdown # Custom parser for GitLab-flavored Markdown
# #
...@@ -62,6 +65,16 @@ module Gitlab ...@@ -62,6 +65,16 @@ module Gitlab
insert_piece($1) insert_piece($1)
end end
# Context passed to the markdoqwn pipeline
markdown_context = {
asset_root: File.join(root_url,
Gitlab::Application.config.assets.prefix)
}
result = HTML::Pipeline::Gitlab::MarkdownPipeline.call(text,
markdown_context)
text = result[:output].to_html(save_with: 0)
allowed_attributes = ActionView::Base.sanitized_allowed_attributes allowed_attributes = ActionView::Base.sanitized_allowed_attributes
allowed_tags = ActionView::Base.sanitized_allowed_tags allowed_tags = ActionView::Base.sanitized_allowed_tags
...@@ -91,7 +104,6 @@ module Gitlab ...@@ -91,7 +104,6 @@ module Gitlab
# Returns parsed text # Returns parsed text
def parse(text, project = @project) def parse(text, project = @project)
parse_references(text, project) if project parse_references(text, project) if project
parse_emoji(text)
text text
end end
...@@ -136,28 +148,6 @@ module Gitlab ...@@ -136,28 +148,6 @@ module Gitlab
end end
end end
EMOJI_PATTERN = %r{(:(\S+):)}.freeze
def parse_emoji(text)
# parse emoji
text.gsub!(EMOJI_PATTERN) do |match|
if valid_emoji?($2)
image_tag(url_to_image("emoji/#{$2}.png"), class: 'emoji', title: $1, alt: $1, size: "20x20")
else
match
end
end
end
# Private: Checks if an emoji icon exists in the image asset directory
#
# emoji - Identifier of the emoji as a string (e.g., "+1", "heart")
#
# Returns boolean
def valid_emoji?(emoji)
Emoji.find_by_name(emoji)
end
# Private: Dispatches to a dedicated processing method based on reference # Private: Dispatches to a dedicated processing method based on reference
# #
# reference - Object reference ("@1234", "!567", etc.) # reference - Object reference ("@1234", "!567", etc.)
......
...@@ -14,6 +14,10 @@ describe GitlabMarkdownHelper do ...@@ -14,6 +14,10 @@ describe GitlabMarkdownHelper do
let(:snippet) { create(:project_snippet, project: project) } let(:snippet) { create(:project_snippet, project: project) }
let(:member) { project.project_members.where(user_id: user).first } let(:member) { project.project_members.where(user_id: user).first }
def url_helper(image_name)
File.join(root_url, 'assets', image_name)
end
before do before do
# Helper expects a @project instance variable # Helper expects a @project instance variable
@project = project @project = project
...@@ -38,8 +42,8 @@ describe GitlabMarkdownHelper do ...@@ -38,8 +42,8 @@ describe GitlabMarkdownHelper do
it "should not touch HTML entities" do it "should not touch HTML entities" do
@project.issues.stub(:where).with(id: '39').and_return([issue]) @project.issues.stub(:where).with(id: '39').and_return([issue])
actual = expected = "We'll accept good pull requests." actual = 'We'll accept good pull requests.'
gfm(actual).should == expected gfm(actual).should == "We'll accept good pull requests."
end end
it "should forward HTML options to links" do it "should forward HTML options to links" do
...@@ -330,7 +334,8 @@ describe GitlabMarkdownHelper do ...@@ -330,7 +334,8 @@ describe GitlabMarkdownHelper do
end end
it "keeps whitespace intact" do it "keeps whitespace intact" do
gfm("This deserves a :+1: big time.").should match(/deserves a <img.+\/> big time/) gfm('This deserves a :+1: big time.').
should match(/deserves a <img.+> big time/)
end end
it "ignores invalid emoji" do it "ignores invalid emoji" do
...@@ -448,7 +453,8 @@ describe GitlabMarkdownHelper do ...@@ -448,7 +453,8 @@ describe GitlabMarkdownHelper do
end end
it "should leave inline code untouched" do it "should leave inline code untouched" do
markdown("\nDon't use `$#{snippet.id}` here.\n").should == "<p>Don&#39;t use <code>$#{snippet.id}</code> here.</p>\n" markdown("\nDon't use `$#{snippet.id}` here.\n").should ==
"<p>Don't use <code>$#{snippet.id}</code> here.</p>\n"
end end
it "should leave ref-like autolinks untouched" do it "should leave ref-like autolinks untouched" do
...@@ -468,7 +474,7 @@ describe GitlabMarkdownHelper do ...@@ -468,7 +474,7 @@ describe GitlabMarkdownHelper do
end end
it "should generate absolute urls for emoji" do it "should generate absolute urls for emoji" do
markdown(":smile:").should include("src=\"#{url_to_image("emoji/smile")}") markdown(":smile:").should include("src=\"#{url_helper('emoji/smile')}")
end end
it "should handle relative urls for a file in master" do it "should handle relative urls for a file in master" do
......
Markdown is supported
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