| Class | Haml::Exec::SassConvert |
| In: |
lib/haml/exec.rb
|
| Parent: | Generic |
The `sass-convert` executable.
@param args [Array<String>] The command-line arguments
# File lib/haml/exec.rb, line 609
609: def initialize(args)
610: super
611: require 'sass'
612: @options[:for_tree] = {}
613: @options[:for_engine] = {:cache => false, :read_cache => true}
614: end
Processes the options set by the command-line arguments, and runs the CSS compiler appropriately.
# File lib/haml/exec.rb, line 689
689: def process_result
690: require 'sass'
691:
692: if @options[:recursive]
693: process_directory
694: return
695: end
696:
697: super
698: input = @options[:input]
699: raise "Error: '#{input.path}' is a directory (did you mean to use --recursive?)" if File.directory?(input)
700: output = @options[:output]
701: output = input if @options[:in_place]
702: process_file(input, output)
703: end
Tells optparse how to parse the arguments.
@param opts [OptionParser]
# File lib/haml/exec.rb, line 619
619: def set_opts(opts)
620: opts.banner = "Usage: sass-convert [options] [INPUT] [OUTPUT]\n\nDescription:\n Converts between CSS, Sass, and SCSS files.\n E.g. converts from SCSS to Sass,\n or converts from CSS to SCSS (adding appropriate nesting).\n\nOptions:\n"
621:
622: opts.on('-F', '--from FORMAT',
623: 'The format to convert from. Can be css, scss, sass, less, or sass2.',
624: 'sass2 is the same as sass, but updates more old syntax to new.',
625: 'By default, this is inferred from the input filename.',
626: 'If there is none, defaults to css.') do |name|
627: @options[:from] = name.downcase.to_sym
628: unless [:css, :scss, :sass, :less, :sass2].include?(@options[:from])
629: raise "Unknown format for sass-convert --from: #{name}"
630: end
631: try_less_note if @options[:from] == :less
632: end
633:
634: opts.on('-T', '--to FORMAT',
635: 'The format to convert to. Can be scss or sass.',
636: 'By default, this is inferred from the output filename.',
637: 'If there is none, defaults to sass.') do |name|
638: @options[:to] = name.downcase.to_sym
639: unless [:scss, :sass].include?(@options[:to])
640: raise "Unknown format for sass-convert --to: #{name}"
641: end
642: end
643:
644: opts.on('-R', '--recursive',
645: 'Convert all the files in a directory. Requires --from and --to.') do
646: @options[:recursive] = true
647: end
648:
649: opts.on('-i', '--in-place',
650: 'Convert a file to its own syntax.',
651: 'This can be used to update some deprecated syntax.') do
652: @options[:in_place] = true
653: end
654:
655: opts.on('--dasherize', 'Convert underscores to dashes') do
656: @options[:for_tree][:dasherize] = true
657: end
658:
659: opts.on('--old', 'Output the old-style ":prop val" property syntax.',
660: 'Only meaningful when generating Sass.') do
661: @options[:for_tree][:old] = true
662: end
663:
664: opts.on('-C', '--no-cache', "Don't cache to sassc files.") do
665: @options[:for_engine][:read_cache] = false
666: end
667:
668: unless ::Haml::Util.ruby1_8?
669: opts.on('-E encoding', 'Specify the default encoding for Sass and CSS files.') do |encoding|
670: Encoding.default_external = encoding
671: end
672: end
673:
674: super
675: end
# File lib/haml/exec.rb, line 707
707: def process_directory
708: input = @options[:input] = @args.shift
709: output = @options[:output] = @args.shift
710: raise "Error: --from required when using --recursive." unless @options[:from]
711: raise "Error: --to required when using --recursive." unless @options[:to]
712: raise "Error: '#{@options[:input]}' is not a directory" unless File.directory?(@options[:input])
713: if @options[:output] && File.exists?(@options[:output]) && !File.directory?(@options[:output])
714: raise "Error: '#{@options[:output]}' is not a directory"
715: end
716: @options[:output] ||= @options[:input]
717:
718: if @options[:to] == @options[:from] && !@options[:in_place]
719: fmt = @options[:from]
720: raise "Error: converting from #{fmt} to #{fmt} without --in-place"
721: end
722:
723: ext = @options[:from]
724: ext = :sass if ext == :sass2
725: Dir.glob("#{@options[:input]}/**/*.#{ext}") do |f|
726: output =
727: if @options[:in_place]
728: f
729: elsif @options[:output]
730: output_name = f.gsub(/\.(c|sa|sc|le)ss$/, ".#{@options[:to]}")
731: output_name[0...@options[:input].size] = @options[:output]
732: output_name
733: else
734: f.gsub(/\.(c|sa|sc|le)ss$/, ".#{@options[:to]}")
735: end
736:
737: unless File.directory?(File.dirname(output))
738: puts_action :directory, :green, File.dirname(output)
739: FileUtils.mkdir_p(File.dirname(output))
740: end
741: puts_action :convert, :green, f
742: if File.exists?(output)
743: puts_action :overwrite, :yellow, output
744: else
745: puts_action :create, :green, output
746: end
747:
748: input = open_file(f)
749: output = @options[:in_place] ? input : open_file(output, "w")
750: process_file(input, output)
751: end
752: end
# File lib/haml/exec.rb, line 754
754: def process_file(input, output)
755: if input.is_a?(File)
756: @options[:from] ||=
757: case input.path
758: when /\.scss$/; :scss
759: when /\.sass$/; :sass
760: when /\.less$/; :less
761: when /\.css$/; :css
762: end
763: elsif @options[:in_place]
764: raise "Error: the --in-place option requires a filename."
765: end
766:
767: if output.is_a?(File)
768: @options[:to] ||=
769: case output.path
770: when /\.scss$/; :scss
771: when /\.sass$/; :sass
772: end
773: end
774:
775: if @options[:from] == :sass2
776: @options[:from] = :sass
777: @options[:for_engine][:sass2] = true
778: end
779:
780: @options[:from] ||= :css
781: @options[:to] ||= :sass
782: @options[:for_engine][:syntax] = @options[:from]
783:
784: out =
785: ::Haml::Util.silence_haml_warnings do
786: if @options[:from] == :css
787: require 'sass/css'
788: ::Sass::CSS.new(input.read, @options[:for_tree]).render(@options[:to])
789: elsif @options[:from] == :less
790: require 'sass/less'
791: try_less_note
792: input = input.read if input.is_a?(IO) && !input.is_a?(File) # Less is dumb
793: Less::Engine.new(input).to_tree.to_sass_tree.send("to_#{@options[:to]}", @options[:for_tree])
794: else
795: if input.is_a?(File)
796: ::Sass::Files.tree_for(input.path, @options[:for_engine])
797: else
798: ::Sass::Engine.new(input.read, @options[:for_engine]).to_tree
799: end.send("to_#{@options[:to]}", @options[:for_tree])
800: end
801: end
802:
803: output = File.open(input.path, 'w') if @options[:in_place]
804: output.write(out)
805: rescue ::Sass::SyntaxError => e
806: raise e if @options[:trace]
807: file = " of #{e.sass_filename}" if e.sass_filename
808: raise "Error on line #{e.sass_line}#{file}: #{e.message}\n Use --trace for backtrace"
809: rescue LoadError => err
810: handle_load_error(err)
811: end
# File lib/haml/exec.rb, line 814
814: def try_less_note
815: return if @@less_note_printed
816: @@less_note_printed = true
817: warn "* NOTE: Sass and Less are different languages, and they work differently.\n* I'll do my best to translate, but some features -- especially mixins --\n* should be checked by hand.\n"
818: end