Skip to content

Commit

Permalink
Merged pull request rtomayko#79 from rkh/redcarpet.
Browse files Browse the repository at this point in the history
add redcarpet support
  • Loading branch information
rtomayko committed Apr 26, 2011
2 parents 2736908 + 47f6eeb commit 9f4dd62
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/tilt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def clear
register MarukuTemplate, 'markdown', 'mkd', 'md'
register KramdownTemplate, 'markdown', 'mkd', 'md'
register BlueClothTemplate, 'markdown', 'mkd', 'md'
register RedcarpetTemplate, 'markdown', 'mkd', 'md'
register RDiscountTemplate, 'markdown', 'mkd', 'md'

require 'tilt/textile'
Expand Down
20 changes: 20 additions & 0 deletions lib/tilt/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ def evaluate(scope, locals, &block)
end
end

# Upskirt Markdown implementation. See:
# https://github.com/tanoku/redcarpet
#
# Compatible to RDiscount
class RedcarpetTemplate < RDiscountTemplate
self.default_mime_type = 'text/html'

def self.engine_initialized?
defined? ::RedcarpetCompat
end

def initialize_engine
require_template_library 'redcarpet'
end

def prepare
@engine = RedcarpetCompat.new(data, *flags)
@output = nil
end
end

# BlueCloth Markdown implementation. See:
# http://deveiate.org/projects/BlueCloth/
Expand Down
19 changes: 17 additions & 2 deletions test/tilt_markdown_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ def test_smarty_pants_false
end

def test_smarty_pants_true
html = nrender "Hello ``World'' -- This is --- a test ...", :smartypants => true
assert_equal "<p>Hello “World” — This is —– a test …</p>", html
if self.class.template == Tilt::RedcarpetTemplate
warn "\nsmartypants not yet fully supported by redcarpet (#{__FILE__}:#{__LINE__})"
else
html = nrender "Hello ``World'' -- This is --- a test ...", :smartypants => true
assert_equal "<p>Hello “World” — This is —– a test …</p>", html
end
end
end

Expand All @@ -82,6 +86,17 @@ class MarkdownRDiscountTest < Test::Unit::TestCase
# It should already be warned in the main tests
end

begin
require 'redcarpet'

class MarkdownRedcarpetTest < Test::Unit::TestCase
include MarkdownTests
template Tilt::RedcarpetTemplate
end
rescue LoadError => boom
# It should already be warned in the main tests
end

begin
require 'bluecloth'

Expand Down
55 changes: 55 additions & 0 deletions test/tilt_redcarpettemplate_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'contest'
require 'tilt'

begin
require 'redcarpet'

class RedcarpetTemplateTest < Test::Unit::TestCase
test "registered for '.md' files" do
assert Tilt.mappings['md'].include?(Tilt::RedcarpetTemplate)
end

test "registered for '.mkd' files" do
assert Tilt.mappings['mkd'].include?(Tilt::RedcarpetTemplate)
end

test "registered for '.markdown' files" do
assert Tilt.mappings['markdown'].include?(Tilt::RedcarpetTemplate)
end

test "registered above BlueCloth" do
%w[md mkd markdown].each do |ext|
mappings = Tilt.mappings[ext]
blue_idx = mappings.index(Tilt::BlueClothTemplate)
rdis_idx = mappings.index(Tilt::RedcarpetTemplate)
assert rdis_idx < blue_idx,
"#{rdis_idx} should be lower than #{blue_idx}"
end
end

test "preparing and evaluating templates on #render" do
template = Tilt::RedcarpetTemplate.new { |t| "# Hello World!" }
assert_equal "<h1>Hello World!</h1>\n", template.render
end

test "can be rendered more than once" do
template = Tilt::RedcarpetTemplate.new { |t| "# Hello World!" }
3.times { assert_equal "<h1>Hello World!</h1>\n", template.render }
end

test "smartypants when :smart is set" do
template = Tilt::RedcarpetTemplate.new(:smart => true) { |t|
"OKAY -- 'Smarty Pants'" }
assert_equal "<p>OKAY &mdash; &lsquo;Smarty Pants&rsquo;</p>\n",
template.render
end

test "stripping HTML when :filter_html is set" do
template = Tilt::RedcarpetTemplate.new(:filter_html => true) { |t|
"HELLO <blink>WORLD</blink>" }
assert_equal "<p>HELLO &lt;blink&gt;WORLD&lt;/blink&gt;</p>\n", template.render
end
end
rescue LoadError => boom
warn "Tilt::RedcarpetTemplate (disabled)\n"
end

0 comments on commit 9f4dd62

Please sign in to comment.