From aeb54fd6d5ad0893aaff631e33e0411e120eb291 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Mon, 7 May 2012 11:38:59 -0300 Subject: [PATCH] WIP --- lib/haml/filters.rb | 73 ++++++++++++++++++++++++++++------ test/filters/coffeescript.haml | 2 + test/filters/markdown.haml | 2 + test/filters/sass.haml | 4 ++ test/filters/scss.haml | 5 +++ test/filters_test.rb | 6 +-- 6 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 test/filters/coffeescript.haml create mode 100644 test/filters/markdown.haml create mode 100644 test/filters/sass.haml create mode 100644 test/filters/scss.haml diff --git a/lib/haml/filters.rb b/lib/haml/filters.rb index 287b6de633..dbbaa46338 100644 --- a/lib/haml/filters.rb +++ b/lib/haml/filters.rb @@ -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 `-` @@ -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 diff --git a/test/filters/coffeescript.haml b/test/filters/coffeescript.haml new file mode 100644 index 0000000000..024176e877 --- /dev/null +++ b/test/filters/coffeescript.haml @@ -0,0 +1,2 @@ +:coffeescript + a = "b" \ No newline at end of file diff --git a/test/filters/markdown.haml b/test/filters/markdown.haml new file mode 100644 index 0000000000..842c76cdca --- /dev/null +++ b/test/filters/markdown.haml @@ -0,0 +1,2 @@ +:markdown + # foo \ No newline at end of file diff --git a/test/filters/sass.haml b/test/filters/sass.haml new file mode 100644 index 0000000000..512951e3b6 --- /dev/null +++ b/test/filters/sass.haml @@ -0,0 +1,4 @@ +:sass + $width: 100 px + p + width: $width \ No newline at end of file diff --git a/test/filters/scss.haml b/test/filters/scss.haml new file mode 100644 index 0000000000..c5f581da68 --- /dev/null +++ b/test/filters/scss.haml @@ -0,0 +1,5 @@ +:scss + $width: 100 px; + p { + width: $width; + } \ No newline at end of file diff --git a/test/filters_test.rb b/test/filters_test.rb index 226dd6d7eb..4e76965f60 100644 --- a/test/filters_test.rb +++ b/test/filters_test.rb @@ -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"], @@ -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