From d763bfad87bd98baaf80375e850c0c0a68d9678b Mon Sep 17 00:00:00 2001 From: Magnus Holm Date: Sat, 26 Mar 2011 12:09:35 +0100 Subject: [PATCH] Add Kramdown support (thanks Postmodern) --- lib/tilt.rb | 1 + lib/tilt/markdown.rb | 21 +++++++++++++++++++ test/tilt_kramdown_test.rb | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 test/tilt_kramdown_test.rb diff --git a/lib/tilt.rb b/lib/tilt.rb index 8fc0c04b..6b8a5039 100644 --- a/lib/tilt.rb +++ b/lib/tilt.rb @@ -477,6 +477,7 @@ def precompiled(locals) require 'tilt/markdown' register BlueClothTemplate, 'markdown', 'mkd', 'md' register MarukuTemplate, 'markdown', 'mkd', 'md' + register KramdownTemplate, 'markdown', 'mkd', 'md' register RDiscountTemplate, 'markdown', 'mkd', 'md' require 'tilt/textile' diff --git a/lib/tilt/markdown.rb b/lib/tilt/markdown.rb index 27c4a434..35143f78 100644 --- a/lib/tilt/markdown.rb +++ b/lib/tilt/markdown.rb @@ -78,5 +78,26 @@ def evaluate(scope, locals, &block) @output ||= @engine.to_html end end + + # Kramdown Markdown implementation. See: + # http://kramdown.rubyforge.org/ + class KramdownTemplate < Template + def self.engine_initialized? + defined? ::Kramdown + end + + def initialize_engine + require_template_library 'kramdown' + end + + def prepare + @engine = Kramdown::Document.new(data, options) + @output = nil + end + + def evaluate(scope, locals, &block) + @output ||= @engine.to_html + end + end end diff --git a/test/tilt_kramdown_test.rb b/test/tilt_kramdown_test.rb new file mode 100644 index 00000000..b6228359 --- /dev/null +++ b/test/tilt_kramdown_test.rb @@ -0,0 +1,42 @@ +require 'contest' +require 'tilt' + +begin + require 'kramdown' + + class MarukuTemplateTest < Test::Unit::TestCase + test "registered for '.md' files" do + assert Tilt.mappings['md'].include?(Tilt::KramdownTemplate) + end + + test "registered for '.mkd' files" do + assert Tilt.mappings['mkd'].include?(Tilt::KramdownTemplate) + end + + test "registered for '.markdown' files" do + assert Tilt.mappings['markdown'].include?(Tilt::KramdownTemplate) + end + + test "registered above MarukuTemplate" do + %w[md mkd markdown].each do |ext| + mappings = Tilt.mappings[ext] + kram_idx = mappings.index(Tilt::KramdownTemplate) + maru_idx = mappings.index(Tilt::MarukuTemplate) + assert kram_idx < maru_idx, + "#{kram_idx} should be lower than #{maru_idx}" + end + end + + test "preparing and evaluating templates on #render" do + template = Tilt::KramdownTemplate.new { |t| "# Hello World!" } + assert_equal "

Hello World!

", template.render + end + + test "can be rendered more than once" do + template = Tilt::KramdownTemplate.new { |t| "# Hello World!" } + 3.times { assert_equal "

Hello World!

", template.render } + end + end +rescue LoadError => boom + warn "Tilt::MarukuTemplate (disabled)\n" +end