From b7ba05f7c1c018ff318888eaea1cf7d21216c48c Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Sun, 16 Aug 2009 18:12:59 -0700 Subject: [PATCH] [Extensions] Compass extensions can now process the content they deliver through ERB. --- lib/compass/actions.rb | 6 ++++ lib/compass/installers/base.rb | 7 ++++- lib/compass/installers/template_context.rb | 34 ++++++++++++++++++---- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/lib/compass/actions.rb b/lib/compass/actions.rb index 3959f7a2..0c7c2cae 100644 --- a/lib/compass/actions.rb +++ b/lib/compass/actions.rb @@ -32,6 +32,7 @@ module Compass def write_file(file_name, contents, options = nil, binary = false) options ||= self.options if self.respond_to?(:options) skip_write = options[:dry_run] + contents = process_erb(contents, options[:erb]) if options[:erb] if File.exists?(file_name) existing_contents = IO.read(file_name) if existing_contents == contents @@ -57,6 +58,11 @@ module Compass 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 def compile(sass_filename, css_filename, options) if options[:force] || Sass::Plugin.exact_stylesheet_needs_update?(css_filename, sass_filename) diff --git a/lib/compass/installers/base.rb b/lib/compass/installers/base.rb index eab60e54..6d9b6295 100644 --- a/lib/compass/installers/base.rb +++ b/lib/compass/installers/base.rb @@ -133,11 +133,16 @@ module Compass def install_html(from, to, options) if to =~ /\.haml$/ require 'haml' - html = Haml::Engine.new(File.read(templatize(from)), :filename => templatize(from)).render to = to[0..-(".haml".length+1)] if respond_to?(:install_location_for_html) to = install_location_for_html(to, options) 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) else install_html_without_haml(from, to, options) diff --git a/lib/compass/installers/template_context.rb b/lib/compass/installers/template_context.rb index a86d9142..4df933fa 100644 --- a/lib/compass/installers/template_context.rb +++ b/lib/compass/installers/template_context.rb @@ -1,22 +1,44 @@ module Compass module Installers class TemplateContext + def self.ctx(*arguments) - new(*arguments).get_binding + new(*arguments).send(:get_binding) end - def initialize(locals = {}) + + def initialize(template, locals = {}) + @template = template @locals = locals 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 @locals.each do |k, v| eval("#{k} = v") end binding end - def config - Compass.configuration - end - alias configuration config end end end \ No newline at end of file