[Extensions] Compass extensions can now process the content they deliver through ERB.

This commit is contained in:
Chris Eppstein 2009-08-16 18:12:59 -07:00
parent c9454190e7
commit b7ba05f7c1
3 changed files with 40 additions and 7 deletions

View File

@ -32,6 +32,7 @@ module Compass
def write_file(file_name, contents, options = nil, binary = false) def write_file(file_name, contents, options = nil, binary = false)
options ||= self.options if self.respond_to?(:options) options ||= self.options if self.respond_to?(:options)
skip_write = options[:dry_run] skip_write = options[:dry_run]
contents = process_erb(contents, options[:erb]) if options[:erb]
if File.exists?(file_name) if File.exists?(file_name)
existing_contents = IO.read(file_name) existing_contents = IO.read(file_name)
if existing_contents == contents if existing_contents == contents
@ -57,6 +58,11 @@ module Compass
end end
end end
def process_erb(contents, ctx = nil)
ctx = Object.new.instance_eval("binding") unless ctx.is_a? Binding
ERB.new(contents).result(ctx)
end
# Compile one Sass file # Compile one Sass file
def compile(sass_filename, css_filename, options) def compile(sass_filename, css_filename, options)
if options[:force] || Sass::Plugin.exact_stylesheet_needs_update?(css_filename, sass_filename) if options[:force] || Sass::Plugin.exact_stylesheet_needs_update?(css_filename, sass_filename)

View File

@ -133,11 +133,16 @@ module Compass
def install_html(from, to, options) def install_html(from, to, options)
if to =~ /\.haml$/ if to =~ /\.haml$/
require 'haml' require 'haml'
html = Haml::Engine.new(File.read(templatize(from)), :filename => templatize(from)).render
to = to[0..-(".haml".length+1)] to = to[0..-(".haml".length+1)]
if respond_to?(:install_location_for_html) if respond_to?(:install_location_for_html)
to = install_location_for_html(to, options) to = install_location_for_html(to, options)
end end
contents = File.read(templatize(from))
if options.delete(:erb)
ctx = TemplateContext.ctx(:to => to, :options => options)
contents = process_erb(contents, ctx)
end
html = Haml::Engine.new(contents, :filename => templatize(from)).render
write_file(targetize(to), html, options) write_file(targetize(to), html, options)
else else
install_html_without_haml(from, to, options) install_html_without_haml(from, to, options)

View File

@ -1,22 +1,44 @@
module Compass module Compass
module Installers module Installers
class TemplateContext class TemplateContext
def self.ctx(*arguments) def self.ctx(*arguments)
new(*arguments).get_binding new(*arguments).send(:get_binding)
end end
def initialize(locals = {})
def initialize(template, locals = {})
@template = template
@locals = locals @locals = locals
end end
def http_stylesheets_path
config.http_stylesheets_path ||
config.default_for(:http_stylesheets_path) ||
config.http_root_relative(config.css_dir)
end
Compass::Configuration::ATTRIBUTES.each do |attribute|
unless instance_methods.include?(attribute.to_s)
define_method attribute do
config.send(attribute) || config.default_for(attribute)
end
end
end
def config
Compass.configuration
end
alias configuration config
protected
def get_binding def get_binding
@locals.each do |k, v| @locals.each do |k, v|
eval("#{k} = v") eval("#{k} = v")
end end
binding binding
end end
def config
Compass.configuration
end
alias configuration config
end end
end end
end end