2006-12-04 02:47:37 +00:00
|
|
|
require 'sass/engine'
|
2007-01-07 04:08:48 +00:00
|
|
|
|
2006-12-04 02:47:37 +00:00
|
|
|
module Sass
|
2007-11-17 19:55:26 +00:00
|
|
|
# This module contains methods to aid in using Sass
|
|
|
|
# as a stylesheet-rendering plugin for various systems.
|
|
|
|
# Currently Rails/ActionController and Merb are supported out of the box.
|
2006-12-04 02:47:37 +00:00
|
|
|
module Plugin
|
|
|
|
class << self
|
|
|
|
@@options = {
|
2007-11-17 19:55:26 +00:00
|
|
|
:template_location => './public/stylesheets/sass',
|
|
|
|
:css_location => './public/stylesheets',
|
2006-12-04 02:47:37 +00:00
|
|
|
:always_update => false,
|
2007-11-17 19:55:26 +00:00
|
|
|
:always_check => true,
|
|
|
|
:full_exception => true
|
2006-12-04 02:47:37 +00:00
|
|
|
}
|
|
|
|
|
2006-12-18 01:31:11 +00:00
|
|
|
# Gets various options for Sass. See README for details.
|
2006-12-04 02:47:37 +00:00
|
|
|
#--
|
|
|
|
# TODO: *DOCUMENT OPTIONS*
|
|
|
|
#++
|
|
|
|
def options
|
|
|
|
@@options
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets various options for Sass.
|
|
|
|
def options=(value)
|
|
|
|
@@options.merge!(value)
|
|
|
|
end
|
2007-08-28 05:10:48 +00:00
|
|
|
|
2006-12-17 16:45:07 +00:00
|
|
|
# Checks each stylesheet in <tt>options[:css_location]</tt>
|
|
|
|
# to see if it needs updating,
|
|
|
|
# and updates it using the corresponding template
|
|
|
|
# from <tt>options[:templates]</tt>
|
|
|
|
# if it does.
|
2006-12-04 02:47:37 +00:00
|
|
|
def update_stylesheets
|
2007-09-02 01:48:25 +00:00
|
|
|
return if options[:never_update]
|
|
|
|
|
2007-05-03 09:04:20 +00:00
|
|
|
Dir.glob(File.join(options[:template_location], "**", "*.sass")).entries.each do |file|
|
2007-08-28 05:10:48 +00:00
|
|
|
|
2007-05-03 09:04:20 +00:00
|
|
|
# Get the relative path to the file with no extension
|
|
|
|
name = file.sub(options[:template_location] + "/", "")[0...-5]
|
2007-08-28 05:10:48 +00:00
|
|
|
|
2007-08-28 05:33:57 +00:00
|
|
|
if !forbid_update?(name) && (options[:always_update] || stylesheet_needs_update?(name))
|
2006-12-04 02:47:37 +00:00
|
|
|
css = css_filename(name)
|
|
|
|
File.delete(css) if File.exists?(css)
|
2007-08-28 05:10:48 +00:00
|
|
|
|
2007-02-02 06:15:28 +00:00
|
|
|
filename = template_filename(name)
|
2007-03-01 05:06:54 +00:00
|
|
|
l_options = @@options.dup
|
|
|
|
l_options[:filename] = filename
|
2007-09-02 01:48:25 +00:00
|
|
|
l_options[:load_paths] = load_paths
|
2007-03-01 05:06:54 +00:00
|
|
|
engine = Engine.new(File.read(filename), l_options)
|
2007-08-28 05:10:48 +00:00
|
|
|
result = begin
|
|
|
|
engine.render
|
|
|
|
rescue Exception => e
|
|
|
|
exception_string(e)
|
|
|
|
end
|
|
|
|
|
2007-05-03 09:04:20 +00:00
|
|
|
# Create any directories that might be necessary
|
|
|
|
dirs = [l_options[:css_location]]
|
|
|
|
name.split("/")[0...-1].each { |dir| dirs << "#{dirs[-1]}/#{dir}" }
|
|
|
|
dirs.each { |dir| Dir.mkdir(dir) unless File.exist?(dir) }
|
2007-08-28 05:10:48 +00:00
|
|
|
|
2007-05-03 09:04:20 +00:00
|
|
|
# Finally, write the file
|
2006-12-04 02:47:37 +00:00
|
|
|
File.open(css, 'w') do |file|
|
|
|
|
file.print(result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-08-28 05:10:48 +00:00
|
|
|
|
2006-12-04 02:47:37 +00:00
|
|
|
private
|
2007-08-28 05:10:48 +00:00
|
|
|
|
2007-09-02 01:48:25 +00:00
|
|
|
def load_paths
|
|
|
|
(options[:load_paths] || []) + [options[:template_location]]
|
|
|
|
end
|
|
|
|
|
2007-08-28 05:10:48 +00:00
|
|
|
def exception_string(e)
|
2007-11-17 19:55:26 +00:00
|
|
|
if options[:full_exception]
|
2007-08-28 05:10:48 +00:00
|
|
|
e_string = "#{e.class}: #{e.message}"
|
|
|
|
|
|
|
|
if e.is_a? Sass::SyntaxError
|
|
|
|
e_string << "\non line #{e.sass_line}"
|
|
|
|
|
|
|
|
if e.sass_filename
|
|
|
|
e_string << " of #{e.sass_filename}"
|
|
|
|
|
|
|
|
if File.exists?(e.sass_filename)
|
|
|
|
e_string << "\n\n"
|
|
|
|
|
|
|
|
min = [e.sass_line - 5, 0].max
|
|
|
|
File.read(e.sass_filename).rstrip.split("\n")[
|
|
|
|
min .. e.sass_line + 5
|
|
|
|
].each_with_index do |line, i|
|
|
|
|
e_string << "#{min + i + 1}: #{line}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
"/*\n#{e_string}\n\nBacktrace:\n#{e.backtrace.join("\n")}\n*/"
|
|
|
|
else
|
|
|
|
"/* Internal stylesheet error */"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-12-04 02:47:37 +00:00
|
|
|
def template_filename(name)
|
2007-09-02 01:48:25 +00:00
|
|
|
"#{options[:template_location]}/#{name}.sass"
|
2006-12-04 02:47:37 +00:00
|
|
|
end
|
2007-08-28 05:10:48 +00:00
|
|
|
|
2006-12-04 02:47:37 +00:00
|
|
|
def css_filename(name)
|
2007-09-02 01:48:25 +00:00
|
|
|
"#{options[:css_location]}/#{name}.css"
|
2006-12-04 02:47:37 +00:00
|
|
|
end
|
2007-08-28 05:10:48 +00:00
|
|
|
|
2007-08-28 05:33:57 +00:00
|
|
|
def forbid_update?(name)
|
|
|
|
name[0] == ?_
|
|
|
|
end
|
|
|
|
|
2006-12-04 02:47:37 +00:00
|
|
|
def stylesheet_needs_update?(name)
|
2007-09-02 01:48:25 +00:00
|
|
|
if !File.exists?(css_filename(name))
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
css_mtime = File.mtime(css_filename(name))
|
|
|
|
File.mtime(template_filename(name)) > css_mtime ||
|
|
|
|
dependencies(template_filename(name)).any?(&dependency_updated?(css_mtime))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def dependency_updated?(css_mtime)
|
|
|
|
lambda do |dep|
|
|
|
|
File.mtime(dep) > css_mtime ||
|
|
|
|
dependencies(dep).any?(&dependency_updated?(css_mtime))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def dependencies(filename)
|
|
|
|
File.readlines(filename).grep(/^@import /).map do |line|
|
|
|
|
line[8..-1].split(',').map do |inc|
|
|
|
|
Sass::Engine.find_file_to_import(inc.strip, load_paths)
|
|
|
|
end
|
|
|
|
end.flatten.grep(/\.sass$/)
|
2006-12-04 02:47:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-17 19:55:26 +00:00
|
|
|
require 'sass/plugin/rails' if defined?(ActionController)
|
|
|
|
require 'sass/plugin/merb' if defined?(Merb::Plugins)
|