Skip to content

Commit

Permalink
add support for mediawiki templates
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed Aug 9, 2011
1 parent ebf4883 commit 2a1545b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 26 deletions.
49 changes: 25 additions & 24 deletions README.md
Expand Up @@ -21,30 +21,31 @@ template engines included in the distribution.

Support for these template engines is included with the package:

ENGINE FILE EXTENSIONS REQUIRED LIBRARIES
-------------------------- ---------------------- ----------------------------
ERB .erb, .rhtml none (included ruby stdlib)
Interpolated String .str none (included ruby core)
Erubis .erb, .rhtml, .erubis erubis
Haml .haml haml
Sass .sass haml (< 3.1) or sass (>= 3.1)
Scss .scss haml (< 3.1) or sass (>= 3.1)
Less CSS .less less
Builder .builder builder
Liquid .liquid liquid
RDiscount .markdown, .mkd, .md rdiscount
Redcarpet .markdown, .mkd, .md redcarpet
BlueCloth .markdown, .mkd, .md bluecloth
Kramdown .markdown, .mkd, .md kramdown
Maruku .markdown, .mkd, .md maruku
RedCloth .textile redcloth
RDoc .rdoc rdoc
Radius .radius radius
Markaby .mab markaby
Nokogiri .nokogiri nokogiri
CoffeeScript .coffee coffee-script (+ javascript)
Creole (Wiki markup) .creole creole
Yajl .yajl yajl-ruby
ENGINE FILE EXTENSIONS REQUIRED LIBRARIES
-------------------------- ----------------------- ----------------------------
ERB .erb, .rhtml none (included ruby stdlib)
Interpolated String .str none (included ruby core)
Erubis .erb, .rhtml, .erubis erubis
Haml .haml haml
Sass .sass haml (< 3.1) or sass (>= 3.1)
Scss .scss haml (< 3.1) or sass (>= 3.1)
Less CSS .less less
Builder .builder builder
Liquid .liquid liquid
RDiscount .markdown, .mkd, .md rdiscount
Redcarpet .markdown, .mkd, .md redcarpet
BlueCloth .markdown, .mkd, .md bluecloth
Kramdown .markdown, .mkd, .md kramdown
Maruku .markdown, .mkd, .md maruku
RedCloth .textile redcloth
RDoc .rdoc rdoc
Radius .radius radius
Markaby .mab markaby
Nokogiri .nokogiri nokogiri
CoffeeScript .coffee coffee-script (+ javascript)
Creole (Wiki markup) .wiki, .creole creole
WikiCloth (Wiki markup) .wiki, .mediawiki, .mw wikicloth
Yajl .yajl yajl-ruby

These template engines ship with their own Tilt integration:

Expand Down
5 changes: 3 additions & 2 deletions lib/tilt.rb
Expand Up @@ -181,8 +181,9 @@ def clear
require 'tilt/rdoc'
register RDocTemplate, 'rdoc'

require 'tilt/creole'
register CreoleTemplate, 'creole'
require 'tilt/wiki'
register CreoleTemplate, 'wiki', 'creole'
register WikiClothTemplate, 'wiki', 'mediawiki', 'mw'

require 'tilt/yajl'
register YajlTemplate, 'yajl'
Expand Down
22 changes: 22 additions & 0 deletions lib/tilt/creole.rb → lib/tilt/wiki.rb
Expand Up @@ -25,4 +25,26 @@ def evaluate(scope, locals, &block)
@output ||= @engine.to_html
end
end

# WikiCloth implementation. See:
# http://redcloth.org/
class WikiClothTemplate < Template
def self.engine_initialized?
defined? ::WikiCloth::Parser
end

def initialize_engine
require_template_library 'wikicloth'
end

def prepare
@parser = options.delete(:parser) || WikiCloth::Parser
@engine = @parser.new options.merge(:data => data)
@output = nil
end

def evaluate(scope, locals, &block)
@output ||= @engine.to_html
end
end
end
4 changes: 4 additions & 0 deletions test/tilt_creoletemplate_test.rb
Expand Up @@ -9,6 +9,10 @@ class CreoleTemplateTest < Test::Unit::TestCase
assert_equal Tilt::CreoleTemplate, Tilt['test.creole']
end

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

test "compiles and evaluates the template on #render" do
template = Tilt::CreoleTemplate.new { |t| "= Hello World!" }
assert_equal "<h1>Hello World!</h1>", template.render
Expand Down
32 changes: 32 additions & 0 deletions test/tilt_wikiclothtemplate_test.rb
@@ -0,0 +1,32 @@
require 'contest'
require 'tilt'

begin
require 'wikicloth'

class WikiClothTemplateTest < Test::Unit::TestCase
test "is registered for '.mediawiki' files" do
assert_equal Tilt::WikiClothTemplate, Tilt['test.mediawiki']
end

test "is registered for '.mw' files" do
assert_equal Tilt::WikiClothTemplate, Tilt['test.mw']
end

test "is registered for '.wiki' files" do
assert_equal Tilt::WikiClothTemplate, Tilt['test.wiki']
end

test "compiles and evaluates the template on #render" do
template = Tilt::WikiClothTemplate.new { |t| "= Hello World! =" }
assert_match /<h1>.*Hello World!.*<\/h1>/, template.render
end

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

0 comments on commit 2a1545b

Please sign in to comment.