Merge pull request #7853 from Razer6/feature/html_pipeline

Factor out Emoji parsing using html-pipeline-gitlab
This commit is contained in:
Dmitriy Zaporozhets 2014-10-02 11:18:40 +03:00
commit 6c3ad0dab6
5 changed files with 34 additions and 34 deletions

View file

@ -79,6 +79,9 @@ gem "six"
# Seed data
gem "seed-fu"
# Markup pipeline for GitLab
gem 'html-pipeline-gitlab', '~> 0.1.0'
# Markdown to HTML
gem "github-markup"

View file

@ -239,6 +239,12 @@ GEM
hipchat (0.14.0)
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)
httparty (0.13.0)
json (~> 1.8)
@ -630,6 +636,7 @@ DEPENDENCIES
guard-spinach
haml-rails
hipchat (~> 0.14.0)
html-pipeline-gitlab (~> 0.1.0)
httparty
jasmine (= 2.0.2)
jquery-atwho-rails (~> 0.3.3)

View file

@ -151,12 +151,6 @@ module ApplicationHelper
sanitize(str, tags: %w(a span))
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
path = controller.controller_path.split('/')

View file

@ -1,3 +1,6 @@
require 'html/pipeline'
require 'html/pipeline/gitlab'
module Gitlab
# Custom parser for GitLab-flavored Markdown
#
@ -62,6 +65,16 @@ module Gitlab
insert_piece($1)
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_tags = ActionView::Base.sanitized_allowed_tags
@ -91,7 +104,6 @@ module Gitlab
# Returns parsed text
def parse(text, project = @project)
parse_references(text, project) if project
parse_emoji(text)
text
end
@ -136,28 +148,6 @@ module Gitlab
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
#
# reference - Object reference ("@1234", "!567", etc.)

View file

@ -14,6 +14,10 @@ describe GitlabMarkdownHelper do
let(:snippet) { create(:project_snippet, project: project) }
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
# Helper expects a @project instance variable
@project = project
@ -38,8 +42,8 @@ describe GitlabMarkdownHelper do
it "should not touch HTML entities" do
@project.issues.stub(:where).with(id: '39').and_return([issue])
actual = expected = "We'll accept good pull requests."
gfm(actual).should == expected
actual = 'We'll accept good pull requests.'
gfm(actual).should == "We'll accept good pull requests."
end
it "should forward HTML options to links" do
@ -330,7 +334,8 @@ describe GitlabMarkdownHelper do
end
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
it "ignores invalid emoji" do
@ -448,7 +453,8 @@ describe GitlabMarkdownHelper do
end
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
it "should leave ref-like autolinks untouched" do
@ -468,7 +474,7 @@ describe GitlabMarkdownHelper do
end
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
it "should handle relative urls for a file in master" do