Skip to content

Commit

Permalink
Add Kramdown support (thanks Postmodern)
Browse files Browse the repository at this point in the history
  • Loading branch information
judofyr committed Mar 26, 2011
1 parent ddff065 commit d763bfa
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/tilt.rb
Expand Up @@ -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'
Expand Down
21 changes: 21 additions & 0 deletions lib/tilt/markdown.rb
Expand Up @@ -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

42 changes: 42 additions & 0 deletions 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 "<h1 id='hello_world'>Hello World!</h1>", template.render
end

test "can be rendered more than once" do
template = Tilt::KramdownTemplate.new { |t| "# Hello World!" }
3.times { assert_equal "<h1 id='hello_world'>Hello World!</h1>", template.render }
end
end
rescue LoadError => boom
warn "Tilt::MarukuTemplate (disabled)\n"
end

0 comments on commit d763bfa

Please sign in to comment.