class Haml::Exec::Sass
The ‘sass` executable.
def initialize(args)
-
args
(Array
) -- The command-line arguments
def initialize(args) super @name = "Sass" @options[:for_engine][:load_paths] = ['.'] + (ENV['SASSPATH'] || '').split(File::PATH_SEPARATOR) end
def interactive
def interactive require 'sass' require 'sass/repl' ::Sass::Repl.new(@options).run end
def process_result
Processes the options set by the command-line arguments,
def process_result if !@options[:update] && !@options[:watch] && @args.first && @args.first.include?(':') if @args.size == 1 @args = @args.first.split(':', 2) else @options[:update] = true end end return interactive if @options[:interactive] return watch_or_update if @options[:watch] || @options[:update] super begin input = @options[:input] output = @options[:output] @options[:syntax] ||= :scss if input.is_a?(File) && input.path =~ /\.scss$/ tree = if input.is_a?(File) && !@options[:check_syntax] ::Sass::Files.tree_for(input.path, @options[:for_engine]) else # We don't need to do any special handling of @options[:check_syntax] here, # because the Sass syntax checking happens alongside evaluation # and evaluation doesn't actually evaluate any code anyway. ::Sass::Engine.new(input.read(), @options[:for_engine]).to_tree end input.close() if input.is_a?(File) output.write(tree.render) output.close() if output.is_a? File rescue ::Sass::SyntaxError => e raise e if @options[:trace] raise e.sass_backtrace_str("standard input") end end
def set_opts(opts)
-
opts
(OptionParser
) --
def set_opts(opts) super opts.on('--scss', 'Use the CSS-superset SCSS syntax.') do @options[:for_engine][:syntax] = :scss end opts.on('--watch', 'Watch files or directories for changes.', 'The location of the generated CSS can be set using a colon:', ' sass --watch input.sass:output.css', ' sass --watch input-dir:output-dir') do @options[:watch] = true end opts.on('--update', 'Compile files or directories to CSS.', 'Locations are set like --watch.') do @options[:update] = true end opts.on('-t', '--style NAME', 'Output style. Can be nested (default), compact, compressed, or expanded.') do |name| @options[:for_engine][:style] = name.to_sym end opts.on('-q', '--quiet', 'Silence warnings during compilation.') do @options[:for_engine][:quiet] = true end opts.on('-g', '--debug-info', 'Emit extra information in the generated CSS that can be used by the FireSass Firebug plugin.') do @options[:for_engine][:debug_info] = true end opts.on('-l', '--line-numbers', '--line-comments', 'Emit comments in the generated CSS indicating the corresponding sass line.') do @options[:for_engine][:line_numbers] = true end opts.on('-i', '--interactive', 'Run an interactive SassScript shell.') do @options[:interactive] = true end opts.on('-I', '--load-path PATH', 'Add a sass import path.') do |path| @options[:for_engine][:load_paths] << path end opts.on('--cache-location PATH', 'The path to put cached Sass files. Defaults to .sass-cache.') do |loc| @options[:for_engine][:cache_location] = loc end opts.on('-C', '--no-cache', "Don't cache to sassc files.") do @options[:for_engine][:cache] = false end unless ::Haml::Util.ruby1_8? opts.on('-E encoding', 'Specify the default encoding for Sass files.') do |encoding| Encoding.default_external = encoding end end end
def watch_or_update
def watch_or_update require 'sass' require 'sass/plugin' ::Sass::Plugin.options.merge! @options[:for_engine] ::Sass::Plugin.options[:unix_newlines] = @options[:unix_newlines] if @args[1] && !@args[0].include?(':') flag = @options[:update] ? "--update" : "--watch" err = if !File.exist?(@args[1]) "doesn't exist" elsif @args[1] =~ /\.css$/ "is a CSS file" end raise <<MSG if err {@args[1]} #{err}. you mean: sass #{flag} #{@args[0]}:#{@args[1]} end dirs, files = @args.map {|name| name.split(':', 2)}. partition {|i, _| File.directory? i} files.map! {|from, to| [from, to || from.gsub(/\..*?$/, '.css')]} dirs.map! {|from, to| [from, to || from]} ::Sass::Plugin.options[:template_location] = dirs ::Sass::Plugin.on_updating_stylesheet do |_, css| if File.exists? css puts_action :overwrite, :yellow, css else puts_action :create, :green, css end end ::Sass::Plugin.on_creating_directory {|dirname| puts_action :directory, :green, dirname} ::Sass::Plugin.on_deleting_css {|filename| puts_action :delete, :yellow, filename} ::Sass::Plugin.on_compilation_error do |error, _, _| raise error unless error.is_a?(::Sass::SyntaxError) puts_action :error, :red, "#{error.sass_filename} (Line #{error.sass_line}: #{error.message})" end if @options[:update] ::Sass::Plugin.update_stylesheets(files) return end puts ">>> Sass is watching for changes. Press Ctrl-C to stop." ::Sass::Plugin.on_template_modified {|template| puts ">>> Change detected to: #{template}"} ::Sass::Plugin.on_template_created {|template| puts ">>> New template detected: #{template}"} ::Sass::Plugin.on_template_deleted {|template| puts ">>> Deleted template detected: #{template}"} ::Sass::Plugin.watch(files) end