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

A little refactoring in sass/plugin.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@595 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2007-08-28 05:10:48 +00:00
parent b7cf2063b4
commit 6e2c784c6a

View file

@ -30,7 +30,7 @@ module Sass
def options=(value) def options=(value)
@@options.merge!(value) @@options.merge!(value)
end end
# Checks each stylesheet in <tt>options[:css_location]</tt> # Checks each stylesheet in <tt>options[:css_location]</tt>
# to see if it needs updating, # to see if it needs updating,
# and updates it using the corresponding template # and updates it using the corresponding template
@ -38,54 +38,30 @@ module Sass
# if it does. # if it does.
def update_stylesheets def update_stylesheets
Dir.glob(File.join(options[:template_location], "**", "*.sass")).entries.each do |file| Dir.glob(File.join(options[:template_location], "**", "*.sass")).entries.each do |file|
# Get the relative path to the file with no extension # Get the relative path to the file with no extension
name = file.sub(options[:template_location] + "/", "")[0...-5] name = file.sub(options[:template_location] + "/", "")[0...-5]
if options[:always_update] || stylesheet_needs_update?(name) if options[:always_update] || stylesheet_needs_update?(name)
css = css_filename(name) css = css_filename(name)
File.delete(css) if File.exists?(css) File.delete(css) if File.exists?(css)
filename = template_filename(name) filename = template_filename(name)
l_options = @@options.dup l_options = @@options.dup
l_options[:filename] = filename l_options[:filename] = filename
l_options[:load_paths] = (l_options[:load_paths] || []) + [l_options[:template_location]] l_options[:load_paths] = (l_options[:load_paths] || []) + [l_options[:template_location]]
engine = Engine.new(File.read(filename), l_options) engine = Engine.new(File.read(filename), l_options)
begin result = begin
result = engine.render engine.render
rescue Exception => e rescue Exception => e
if RAILS_ENV != "production" exception_string(e)
e_string = "#{e.class}: #{e.message}" end
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
result = "/*\n#{e_string}\n\nBacktrace:\n#{e.backtrace.join("\n")}\n*/"
else
result = "/* Internal stylesheet error */"
end
end
# Create any directories that might be necessary # Create any directories that might be necessary
dirs = [l_options[:css_location]] dirs = [l_options[:css_location]]
name.split("/")[0...-1].each { |dir| dirs << "#{dirs[-1]}/#{dir}" } name.split("/")[0...-1].each { |dir| dirs << "#{dirs[-1]}/#{dir}" }
dirs.each { |dir| Dir.mkdir(dir) unless File.exist?(dir) } dirs.each { |dir| Dir.mkdir(dir) unless File.exist?(dir) }
# Finally, write the file # Finally, write the file
File.open(css, 'w') do |file| File.open(css, 'w') do |file|
file.print(result) file.print(result)
@ -93,17 +69,45 @@ module Sass
end end
end end
end end
private private
def exception_string(e)
if RAILS_ENV != "production"
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
def template_filename(name) def template_filename(name)
"#{@@options[:template_location]}/#{name}.sass" "#{@@options[:template_location]}/#{name}.sass"
end end
def css_filename(name) def css_filename(name)
"#{@@options[:css_location]}/#{name}.css" "#{@@options[:css_location]}/#{name}.css"
end end
def stylesheet_needs_update?(name) def stylesheet_needs_update?(name)
!File.exists?(css_filename(name)) || (File.mtime(template_filename(name)) - 2) > File.mtime(css_filename(name)) !File.exists?(css_filename(name)) || (File.mtime(template_filename(name)) - 2) > File.mtime(css_filename(name))
end end