1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Tilt-based filters now working, WIP

This commit is contained in:
Norman Clarke 2012-05-21 16:08:19 -03:00
parent aeb54fd6d5
commit 0aa897e1e5
2 changed files with 58 additions and 69 deletions

View file

@ -3,9 +3,4 @@ gemspec
platform :mri do platform :mri do
gem "ruby-prof" gem "ruby-prof"
end end
gem "therubyracer"
gem "less"
gem "coffee-script"
gem "RedCloth"

View file

@ -245,12 +245,15 @@ END
end end
module TiltFilter module TiltFilter
def template_class=(val) extend self
@template_class = val attr_accessor :template_class, :tilt_extension
end
def template_class def template_class
@template_class @template_class or begin
@template_class = Tilt["t.#{tilt_extension}"]
rescue LoadError
raise Error.new("Can't run #{self} filter; required dependencies not available")
end
end end
def self.extended(base) def self.extended(base)
@ -262,48 +265,67 @@ END
end end
end end
module Sass module PrecompiledFilter
include Base def precompiled(text)
extend Css template_class.new { text }.send(:precompiled, {}).first
extend TiltFilter end
def self.render_with_options(text, options) def compile(compiler, text)
text = template_class.new {text}.render return if compiler.options[:suppress_eval]
super compiler.send(:push_script, precompiled(text))
end end
end end
module Scss template_engines = {
include Base "Sass" => "sass",
extend Css "Scss" => "scss",
extend TiltFilter "Less" => "less",
"Markdown" => "markdown",
"Coffee" => "coffee",
"Nokogiri" => "nokogiri",
"Builder" => "builder",
"Markaby" => "mab",
"Erb" => "erb"
}
def self.render_with_options(text, options) template_engines.each do |name, extension|
text = template_class.new {text}.render module_eval(<<-END)
super module #{name}
include Base
extend TiltFilter
end
# Let Tilt figure out which library to use, since there are lot of engine
# for Markdown, and may eventually be more than one for Sass, Less, etc.
#{name}.tilt_extension = '#{extension}'
END
end
# Some filters precompile for performance
Nokogiri.extend PrecompiledFilter
Builder.extend PrecompiledFilter
Erb.extend PrecompiledFilter
# 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.
module Erb
class << self
def precompiled(text)
super.sub(/^#coding:.*?\n/, '')
end
end end
end end
module Markdown # Allow :coffee as a longhand for :coffee
Filters.defined["coffeescript"] = Coffee
# Maruku has no dedicated extension
module Maruku
include Base include Base
extend TiltFilter extend TiltFilter
end end
Maruku.template_class = Tilt::MarukuTemplate
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. # Surrounds the filtered text with CDATA tags.
module Cdata module Cdata
@ -364,33 +386,5 @@ END
Haml::Helpers.preserve text Haml::Helpers.preserve text
end end
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.
module ERB
include Base
lazy_require 'erb'
# @see Base#compile
def compile(compiler, text)
return if compiler.options[:suppress_eval]
src = ::ERB.new(text).src.sub(/^#coding:.*?\n/, '').
sub(/^_erbout = '';/, "")
compiler.send(:push_silent, src)
end
end
# Parses the filtered text with [Redcarpet](https://github.com/tanoku/redcarpet)
module Redcarpet
include Base
lazy_require 'redcarpet'
# @see Base#render
def render(text)
::Redcarpet.new(text).to_html
end
end
end end
end end