1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
This commit is contained in:
Norman Clarke 2012-05-07 11:38:59 -03:00
parent e1201cc6b1
commit aeb54fd6d5
6 changed files with 77 additions and 15 deletions

View file

@ -187,18 +187,6 @@ rescue LoadError; end
module Haml
module Filters
# Filters for other template engines are provided by Tilt.
["Sass", "Scss", "Less", "CoffeeScript", "Maruku"].each do |name|
module_eval(<<-END)
module #{name}
include Base
def render(text)
Tilt::#{name}Template.new {text}.render
end
end
END
end
# Does not parse the filtered text.
# This is useful for large blocks of text without HTML tags,
# when you don't want lines starting with `.` or `-`
@ -256,6 +244,67 @@ END
end
end
module TiltFilter
def template_class=(val)
@template_class = val
end
def template_class
@template_class
end
def self.extended(base)
base.instance_eval do
def render(text)
template_class.new {text}.render
end
end
end
end
module Sass
include Base
extend Css
extend TiltFilter
def self.render_with_options(text, options)
text = template_class.new {text}.render
super
end
end
module Scss
include Base
extend Css
extend TiltFilter
def self.render_with_options(text, options)
text = template_class.new {text}.render
super
end
end
module Markdown
include Base
extend TiltFilter
end
module Coffeescript
include Base
extend Javascript
extend TiltFilter
def self.render_with_options(text, options)
text = template_class.new {text}.render
super
end
end
Sass.template_class = Tilt["template.sass"]
Scss.template_class = Tilt["template.scss"]
Markdown.template_class = Tilt["template.markdown"]
Coffeescript.template_class = Tilt["template.coffee"]
# Surrounds the filtered text with CDATA tags.
module Cdata
include Base

View file

@ -0,0 +1,2 @@
:coffeescript
a = "b"

View file

@ -0,0 +1,2 @@
:markdown
# foo

4
test/filters/sass.haml Normal file
View file

@ -0,0 +1,4 @@
:sass
$width: 100 px
p
width: $width

5
test/filters/scss.haml Normal file
View file

@ -0,0 +1,5 @@
:scss
$width: 100 px;
p {
width: $width;
}

View file

@ -10,8 +10,8 @@ class FiltersTest < MiniTest::Unit::TestCase
end
TESTS = {
:sass => ["sass", /width: 100;/, ":sass\n p\n width: 100"],
:scss => ["sass", /width: 100;/, ":scss\n $width: 100;\n p {\n width: $width;\n }"],
:sass => ["sass/plugin", /width: 100;/, ":sass\n p\n width: 100"],
:scss => ["sass/plugin", /width: 100;/, ":scss\n $width: 100;\n p {\n width: $width;\n }"],
:less => ["less", /width: 100;/, ":less\n @width: 100;\n p {\n width: @width;\n }"],
:coffeescript => ["coffee_script", /var foo;/, ":coffeescript\n foo = 'bar'"],
:maruku => ["maruku", /h1/, ":maruku\n # foo"],
@ -24,8 +24,8 @@ class FiltersTest < MiniTest::Unit::TestCase
begin
Haml::Util.silence_warnings do
require library
assert_match(pattern, render(haml))
end
assert_match(pattern, render(haml))
rescue LoadError
warn "Could not load #{key} filter's dependencies"
end