Permalink
Browse files

Merged pull request #79 from rkh/redcarpet.

add redcarpet support
  • Loading branch information...
2 parents 2736908 + 47f6eeb commit 9f4dd62fd98fd00e485ea1fca2ce8be02dbbbae6 @rtomayko committed Apr 26, 2011
Showing with 93 additions and 2 deletions.
  1. +1 −0 lib/tilt.rb
  2. +20 −0 lib/tilt/markdown.rb
  3. +17 −2 test/tilt_markdown_test.rb
  4. +55 −0 test/tilt_redcarpettemplate_test.rb
View
@@ -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'
View
@@ -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/
View
@@ -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
@@ -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'
@@ -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.