Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTML::Pipeline to core #1760

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions jekyll.gemspec
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('liquid', "~> 2.5.2")
s.add_runtime_dependency('classifier', "~> 1.3")
s.add_runtime_dependency('listen', "~> 1.3")
s.add_runtime_dependency('html-pipeline', "~> 1.0.0")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not a default, I'd prefer to keep it as a specify-it-if-you-want-it gem.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Changing!

s.add_runtime_dependency('maruku', "~> 0.6.0")
s.add_runtime_dependency('pygments.rb', "~> 0.5.0")
s.add_runtime_dependency('commander', "~> 4.1.3")
Expand All @@ -41,8 +42,11 @@ Gem::Specification.new do |s|
s.add_development_dependency('cucumber', "~> 1.3")
s.add_development_dependency('RedCloth', "~> 4.2")
s.add_development_dependency('kramdown', "~> 1.2")
s.add_development_dependency('github-markdown', "~> 0.6.3")
s.add_development_dependency('rdiscount', "~> 1.6")
s.add_development_dependency('launchy', "~> 2.3")
s.add_development_dependency('sanitize', "~> 2.0.6")
s.add_development_dependency('gemoji', "~> 1.5.0")
s.add_development_dependency('simplecov', "~> 0.7")
s.add_development_dependency('simplecov-gem-adapter', "~> 1.0.1")
s.add_development_dependency('coveralls', "~> 0.7.0")
Expand Down
4 changes: 3 additions & 1 deletion lib/jekyll/converters/markdown.rb
Expand Up @@ -9,6 +9,8 @@ class Markdown < Converter
def setup
return if @setup
@parser = case @config['markdown']
when 'html_pipeline'
HTMLPipelineParser.new @config
when 'redcarpet'
RedcarpetParser.new @config
when 'kramdown'
Expand All @@ -19,7 +21,7 @@ def setup
MarukuParser.new @config
else
STDERR.puts "Invalid Markdown processor: #{@config['markdown']}"
STDERR.puts " Valid options are [ maruku | rdiscount | kramdown | redcarpet ]"
STDERR.puts " Valid options are [ maruku | rdiscount | kramdown | redcarpet | html_pipeline ]"
raise FatalException.new("Invalid Markdown process: #{@config['markdown']}")
end
@setup = true
Expand Down
58 changes: 58 additions & 0 deletions lib/jekyll/converters/markdown/html_pipeline_parser.rb
@@ -0,0 +1,58 @@
module Jekyll
module Converters
class Markdown
class HTMLPipelineParser
def initialize(config)
require 'html/pipeline'
@config = config
@errors = []
end

def filter_key(s)
s.to_s.downcase.to_sym
end

def is_filter?(f)
f < HTML::Pipeline::Filter
rescue LoadError, ArgumentError
false
end

def ensure_default_opts
@config['html_pipeline']['filters'] ||= ['markdownfilter']
@config['html_pipeline']['context'] ||= {'gfm' => true}
# symbolize strings as keys, which is what HTML::Pipeline wants
@config['html_pipeline']['context'] = @config['html_pipeline']['context'].symbolize_keys
end

def setup
unless @setup
ensure_default_opts

filters = @config['html_pipeline']['filters'].map do |f|
if is_filter?(f)
f
else
key = filter_key(f)
begin
filter = HTML::Pipeline.constants.find { |c| c.downcase == key }
HTML::Pipeline.const_get(filter)
rescue Exception => e
raise FatalException.new(e)
end
end
end

@parser = HTML::Pipeline.new(filters, @config['html_pipeline']['context'])
@setup = true
end
end

def convert(content)
setup
@parser.to_html(content)
end
end
end
end
end
47 changes: 47 additions & 0 deletions test/test_html_pipeline.rb
@@ -0,0 +1,47 @@
# encoding: UTF-8

require 'helper'

class TestHTMLPipeline < Test::Unit::TestCase
context "HTMLPipeline" do
setup do
@config = {
'markdown' => 'html_pipeline',
'html_pipeline' => {
'filters' => ['markdownfilter', 'sanitizationfilter', 'emojifilter', 'mentionfilter'],
'context' => {
'asset_root' => "http://foo.com/icons",
'base_url' => "https://github.com/",
}
}
}

@config = Jekyll.configuration(@config)
@markdown = Converters::Markdown.new(@config)
end

should "pass regular options" do
assert_equal "<h1>Some Header</h1>", @markdown.convert('# Some Header #').strip
end

should "pass rendering emoji" do
assert_equal "<p><img class=\"emoji\" title=\":trollface:\" alt=\":trollface:\" src=\"http://foo.com/icons/emoji/trollface.png\" height=\"20\" width=\"20\" align=\"absmiddle\"></p>", @markdown.convert(':trollface:').strip
end

should "pass rendering mentions" do
assert_equal "<p><strong>Hey, <a href=\"https://github.com/mojombo\" class=\"user-mention\">@mojombo</a></strong>!</p>", @markdown.convert('**Hey, @mojombo**!').strip
end

should "fail when a library dependency is not met" do
override = { 'html_pipeline' => { 'filters' => ['markdownfilter, AutolinkFilter'] } }
markdown = Converters::Markdown.new(@config.deep_merge(override))
# assert_fail markdown.convert('http://www.github.com')
end

should "fail when a context dependency is not met" do
override = { 'html_pipeline' => { 'context' => {} } }
markdown = Converters::Markdown.new(@config.deep_merge(override))
# assert_fail markdown.convert(':trollface:')
end
end
end