Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
norman committed May 21, 2012
1 parent e1201cc commit aeb54fd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 15 deletions.
73 changes: 61 additions & 12 deletions lib/haml/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,6 @@ def resolve_lazy_requires
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 `-`
Expand Down Expand Up @@ -256,6 +244,67 @@ def render_with_options(text, options)
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
Expand Down
2 changes: 2 additions & 0 deletions test/filters/coffeescript.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:coffeescript
a = "b"
2 changes: 2 additions & 0 deletions test/filters/markdown.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:markdown
# foo
4 changes: 4 additions & 0 deletions test/filters/sass.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:sass
$width: 100 px
p
width: $width
5 changes: 5 additions & 0 deletions test/filters/scss.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:scss
$width: 100 px;
p {
width: $width;
}
6 changes: 3 additions & 3 deletions test/filters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def render(text, options = {}, &block)
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"],
Expand All @@ -24,8 +24,8 @@ def render(text, options = {}, &block)
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
Expand Down

0 comments on commit aeb54fd

Please sign in to comment.