Skip to content

Commit

Permalink
Began implementing filters using Tilt, WIP
Browse files Browse the repository at this point in the history
Note that this is a work in progress on issue haml#469 and will be rebased
before it's merged into master.
  • Loading branch information
norman committed May 21, 2012
1 parent 54d4789 commit e1201cc
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 233 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
Gemfile.lock
.rvmrc
.rbx
tmp
.sass-cache
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ gemspec
platform :mri do
gem "ruby-prof"
end

gem "therubyracer"
gem "less"
gem "coffee-script"
gem "RedCloth"
2 changes: 2 additions & 0 deletions haml.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Gem::Specification.new do |spec|
spec.has_rdoc = false
spec.test_files = Dir["test/**/*_test.rb"]

spec.add_dependency "tilt"

spec.add_development_dependency 'yard', '>= 0.5.3'
spec.add_development_dependency 'maruku', '>= 0.5.9'
spec.add_development_dependency 'hpricot'
Expand Down
95 changes: 14 additions & 81 deletions lib/haml/filters.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "tilt"

module Haml
# The module containing the default Haml filters,
Expand Down Expand Up @@ -185,6 +186,19 @@ 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 @@ -302,28 +316,6 @@ def render(text)
end
end

# Parses the filtered text with {Sass} to produce CSS output.
module Sass
include Base
lazy_require 'sass/plugin'

# @see Base#render
def render(text)
::Sass::Engine.new(text, ::Sass::Plugin.engine_options).render
end
end

# Parses the filtered text with {Sass} to produce CSS output using SCSS syntax.
module Scss
include Base
lazy_require 'sass/plugin'

# @see Base#render
def render(text)
::Sass::Engine.new(text, ::Sass::Plugin.engine_options.merge(:syntax => :scss)).render
end
end

# Parses the filtered text with ERB.
# Not available if the {file:REFERENCE.md#suppress_eval-option `:suppress_eval`} option is set to true.
# Embedded Ruby code is evaluated in the same context as the Haml template.
Expand All @@ -340,65 +332,6 @@ def compile(compiler, text)
end
end

# Parses the filtered text with [Textile](http://www.textism.com/tools/textile).
# Only works if [RedCloth](http://redcloth.org) is installed.
module Textile
include Base
lazy_require 'redcloth'

# @see Base#render
def render(text)
::RedCloth.new(text).to_html(:textile)
end
end
# An alias for the Textile filter,
# since the only available Textile parser is RedCloth.
# @api public
RedCloth = Textile
Filters.defined['redcloth'] = RedCloth

# Parses the filtered text with [Markdown](http://daringfireball.net/projects/markdown).
# Only works if [RDiscount](https://github.com/rtomayko/rdiscount),
# [RPeg-Markdown](https://github.com/rtomayko/rpeg-markdown),
# [Maruku](http://maruku.rubyforge.org),
# [Redcarpet](https://github.com/tanoku/redcarpet),
# or [Kramdown](https://github.com/gettalong/kramdown) are installed.
module Markdown
include Base
lazy_require 'rdiscount', 'peg_markdown', 'maruku', 'bluecloth', 'redcarpet', 'kramdown'

# @see Base#render
def render(text)
engine = case @required
when 'rdiscount'
::RDiscount
when 'peg_markdown'
::PEGMarkdown
when 'maruku'
::Maruku
when 'bluecloth'
::BlueCloth
when 'redcarpet'
::Redcarpet
when 'kramdown'
::Kramdown::Document
end
engine.new(text).to_html
end
end

# Parses the filtered text with [Maruku](http://maruku.rubyforge.org),
# which has some non-standard extensions to Markdown.
module Maruku
include Base
lazy_require 'maruku'

# @see Base#render
def render(text)
::Maruku.new(text).to_html
end
end

# Parses the filtered text with [Redcarpet](https://github.com/tanoku/redcarpet)
module Redcarpet
include Base
Expand Down
34 changes: 34 additions & 0 deletions test/filters_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
require 'test_helper'

class FiltersTest < MiniTest::Unit::TestCase

def render(text, options = {}, &block)
scope = options.delete(:scope) || Object.new
locals = options.delete(:locals) || {}
Haml::Engine.new(text, options).to_html(scope, locals, &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 }"],
: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"],
}

TESTS.each do |key, value|
library, pattern, haml = value

define_method("test_#{key}_filter") do
begin
Haml::Util.silence_warnings do
require library
assert_match(pattern, render(haml))
end
rescue LoadError
warn "Could not load #{key} filter's dependencies"
end
end
end
end
69 changes: 0 additions & 69 deletions test/results/filters.xhtml

This file was deleted.

8 changes: 4 additions & 4 deletions test/template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def controller_path

class TemplateTest < MiniTest::Unit::TestCase
TEMPLATE_PATH = File.join(File.dirname(__FILE__), "templates")
TEMPLATES = %w{ very_basic standard helpers
whitespace_handling original_engine list helpful
silent_script tag_parsing just_stuff partials
filters nuke_outer_whitespace nuke_inner_whitespace
TEMPLATES = %w{ very_basic standard helpers
whitespace_handling original_engine list helpful
silent_script tag_parsing just_stuff partials
nuke_outer_whitespace nuke_inner_whitespace
render_layout }
# partial layouts were introduced in 2.0.0
TEMPLATES << 'partial_layout' unless ActionPack::VERSION::MAJOR < 2
Expand Down
79 changes: 0 additions & 79 deletions test/templates/filters.haml

This file was deleted.

0 comments on commit e1201cc

Please sign in to comment.