Implement our own Redcarpet Template to support renderer-level options. Closes #442
This commit is contained in:
parent
045f25fa4e
commit
f0d781cce3
6 changed files with 61 additions and 14 deletions
|
@ -9,7 +9,7 @@ Feature: Markdown support
|
|||
Scenario: Markdown extensions (Maruku)
|
||||
Given the Server is running at "markdown-app"
|
||||
When I go to "/smarty_pants.html"
|
||||
Then I should see "“"
|
||||
Then I should see """
|
||||
When I go to "/no_intra_emphasis.html"
|
||||
Then I should not see "<em>"
|
||||
When I go to "/tables.html"
|
||||
|
|
|
@ -14,12 +14,15 @@ Feature: Markdown support
|
|||
:strikethrough => true,
|
||||
:lax_html_blocks => true,
|
||||
:space_after_headers => true,
|
||||
:superscript => true#, :smartypants => true
|
||||
:with_toc_data => true,
|
||||
:superscript => true,
|
||||
:smartypants => true,
|
||||
:hard_wrap => true
|
||||
|
||||
"""
|
||||
Given the Server is running at "markdown-app"
|
||||
# When I go to "/smarty_pants.html"
|
||||
# Then I should see "“"
|
||||
When I go to "/smarty_pants.html"
|
||||
Then I should see "“"
|
||||
When I go to "/no_intra_emphasis.html"
|
||||
Then I should not see "<em>"
|
||||
When I go to "/tables.html"
|
||||
|
@ -34,4 +37,7 @@ Feature: Markdown support
|
|||
Then I should not see "<h1>"
|
||||
When I go to "/superscript.html"
|
||||
Then I should see "<sup>"
|
||||
|
||||
When I go to "/with_toc_data.html"
|
||||
Then I should see "toc_0"
|
||||
When I go to "/hard_wrap.html"
|
||||
Then I should see "br"
|
|
@ -0,0 +1,2 @@
|
|||
hard
|
||||
wrap
|
|
@ -0,0 +1,3 @@
|
|||
# Header 1
|
||||
|
||||
## Header 2
|
|
@ -26,7 +26,10 @@ module Middleman
|
|||
app.after_configuration do
|
||||
|
||||
# Look for the user's preferred engine
|
||||
unless markdown_engine.nil?
|
||||
if markdown_engine == :redcarpet
|
||||
require "middleman-more/renderers/redcarpet"
|
||||
::Tilt.prefer(::Middleman::Renderers::RedcarpetTemplate)
|
||||
elsif markdown_engine.nil?
|
||||
|
||||
# Map symbols to classes
|
||||
markdown_engine_klass = if markdown_engine.is_a? Symbol
|
||||
|
@ -37,14 +40,6 @@ module Middleman
|
|||
markdown_engine_prefix
|
||||
end
|
||||
|
||||
if markdown_engine == :redcarpet
|
||||
# Forcably disable Redcarpet1 support.
|
||||
# Tilt defaults to this if available, but the compat
|
||||
# layer disables extensions.
|
||||
require "redcarpet"
|
||||
Object.send(:remove_const, :RedcarpetCompat) if defined? ::RedcarpetCompat
|
||||
end
|
||||
|
||||
# Tell tilt to use that engine
|
||||
::Tilt.prefer(markdown_engine_klass)
|
||||
end
|
||||
|
@ -54,5 +49,6 @@ module Middleman
|
|||
alias :included :registered
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
40
middleman-more/lib/middleman-more/renderers/redcarpet.rb
Normal file
40
middleman-more/lib/middleman-more/renderers/redcarpet.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
require "redcarpet"
|
||||
|
||||
module Middleman
|
||||
module Renderers
|
||||
|
||||
class RedcarpetTemplate < ::Tilt::RedcarpetTemplate::Redcarpet2
|
||||
|
||||
# Overwrite built-in Tilt version.
|
||||
# Don't overload :renderer option with smartypants
|
||||
# Supper renderer-level options
|
||||
def generate_renderer
|
||||
return options.delete(:renderer) if options.has_key?(:renderer)
|
||||
|
||||
# Pick a renderer
|
||||
renderer = ::Redcarpet::Render::HTML
|
||||
|
||||
# Support SmartyPants
|
||||
if options.delete(:smartypants)
|
||||
renderer = Class.new(renderer) do
|
||||
include ::Redcarpet::Render::SmartyPants
|
||||
end
|
||||
end
|
||||
|
||||
# Renderer Options
|
||||
possible_render_opts = [:filter_html, :no_images, :no_links, :no_styles, :safe_links_only, :with_toc_data, :hard_wrap, :xhtml]
|
||||
|
||||
render_options = possible_render_opts.inject({}) do |sum, opt|
|
||||
sum[opt] = options.delete(opt) if options.has_key?(opt)
|
||||
sum
|
||||
end
|
||||
|
||||
renderer.new(render_options)
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
::Tilt.register RedcarpetTemplate, 'markdown', 'mkd', 'md'
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue