Skip to content

Commit

Permalink
Make option escape_html work
Browse files Browse the repository at this point in the history
  • Loading branch information
jmuheim committed May 12, 2016
1 parent 6c7c96a commit 02076e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
27 changes: 18 additions & 9 deletions lib/tilt/pandoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,33 @@ module Tilt
# http://pandoc.org/
class PandocTemplate < Template
def prepare
@output = PandocRuby.convert(data, options_hash.merge(
{:to => :html}
), *options_array
).strip
@output = PandocRuby.convert(data, options_hash, *options_array).strip
end

def tilt_to_pandoc_mapping
{ :smartypants => :smart }
def pandoc_optimised_options
options.inject({}) do |hash, option|
if option[0] == :smartypants
hash[:smart] = true if option[1] === true
elsif option[0] == :escape_html
hash[:f] = 'markdown-raw_html' if option[1] === true
else
hash[option[0]] = option[1]
end

hash
end.merge({:to => :html})
end

def options_array
options.map do |option|
tilt_to_pandoc_mapping[option[0]] || option[0] if option[1] === true
pandoc_optimised_options.map do |option|
option[0] if option[1] === true
end.compact
end

def options_hash
options.inject({}) do |hash, option|
pandoc_optimised_options.inject({}) do |hash, option|
# next if option[1] === true
# next if option[1] === false
hash[option[0]] = option[1] unless option[1] === true or option[1] === false
hash
end
Expand Down
9 changes: 7 additions & 2 deletions test/tilt_pandoctemplate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ class PandocTemplateTest < Minitest::Test
assert_equal "<p>OKAY – ‘Smarty Pants’</p>", template.render
end

# Pandoc has tons of additional markdown features.
# The test for footnotes should be see as a general representation for all of them.
test "stripping HTML when :escape_html is set" do
template = Tilt::PandocTemplate.new(:escape_html => true) { |t| "HELLO <blink>WORLD</blink>" }
assert_equal "<p>HELLO &lt;blink&gt;WORLD&lt;/blink&gt;</p>", template.render
end

# Pandoc has tons of additional markdown features (see http://pandoc.org/README.html#pandocs-markdown).
# The test for footnotes should be seen as a general representation for all of them.
test "generates footnotes" do
template = Tilt::PandocTemplate.new { |t| "Here is an inline note.^[Inlines notes are cool!]" }
assert_equal "<p>Here is an inline note.<a href=\"#fn1\" class=\"footnoteRef\" id=\"fnref1\"><sup>1</sup></a></p>\n<div class=\"footnotes\">\n<hr />\n<ol>\n<li id=\"fn1\"><p>Inlines notes are cool!<a href=\"#fnref1\">↩</a></p></li>\n</ol>\n</div>", template.render.strip
Expand Down

0 comments on commit 02076e0

Please sign in to comment.