In Files

Class Index [+]

Quicksearch

Haml::Exec::SassConvert

The `sass-convert` executable.

Public Class Methods

new(args) click to toggle source

@param args [Array] The command-line arguments

     # File lib/haml/exec.rb, line 630
630:       def initialize(args)
631:         super
632:         require 'sass'
633:         @options[:for_tree] = {}
634:         @options[:for_engine] = {:cache => false, :read_cache => true}
635:       end

Public Instance Methods

process_result() click to toggle source

Processes the options set by the command-line arguments, and runs the CSS compiler appropriately.

     # File lib/haml/exec.rb, line 709
709:       def process_result
710:         require 'sass'
711: 
712:         if @options[:recursive]
713:           process_directory
714:           return
715:         end
716: 
717:         super
718:         input = @options[:input]
719:         raise "Error: '#{input.path}' is a directory (did you mean to use --recursive?)" if File.directory?(input)
720:         output = @options[:output]
721:         output = input if @options[:in_place]
722:         process_file(input, output)
723:       end
set_opts(opts) click to toggle source

Tells optparse how to parse the arguments.

@param opts [OptionParser]

     # File lib/haml/exec.rb, line 640
640:       def set_opts(opts)
641:         opts.banner = Usage: sass-convert [options] [INPUT] [OUTPUT]Description:  Converts between CSS, Sass, and SCSS files.  E.g. converts from SCSS to Sass,  or converts from CSS to SCSS (adding appropriate nesting).Options:
642: 
643:         opts.on('-F', '--from FORMAT',
644:           'The format to convert from. Can be css, scss, sass, less, or sass2.',
645:           'sass2 is the same as sass, but updates more old syntax to new.',
646:           'By default, this is inferred from the input filename.',
647:           'If there is none, defaults to css.') do |name|
648:           @options[:from] = name.downcase.to_sym
649:           unless [:css, :scss, :sass, :less, :sass2].include?(@options[:from])
650:             raise "Unknown format for sass-convert --from: #{name}"
651:           end
652:           try_less_note if @options[:from] == :less
653:         end
654: 
655:         opts.on('-T', '--to FORMAT',
656:           'The format to convert to. Can be scss or sass.',
657:           'By default, this is inferred from the output filename.',
658:           'If there is none, defaults to sass.') do |name|
659:           @options[:to] = name.downcase.to_sym
660:           unless [:scss, :sass].include?(@options[:to])
661:             raise "Unknown format for sass-convert --to: #{name}"
662:           end
663:         end
664: 
665:         opts.on('-R', '--recursive',
666:           'Convert all the files in a directory. Requires --from and --to.') do
667:           @options[:recursive] = true
668:         end
669: 
670:         opts.on('-i', '--in-place',
671:           'Convert a file to its own syntax.',
672:           'This can be used to update some deprecated syntax.') do
673:           @options[:in_place] = true
674:         end
675: 
676:         opts.on('--dasherize', 'Convert underscores to dashes') do
677:           @options[:for_tree][:dasherize] = true
678:         end
679: 
680:         opts.on('--old', 'Output the old-style ":prop val" property syntax.',
681:                          'Only meaningful when generating Sass.') do
682:           @options[:for_tree][:old] = true
683:         end
684: 
685:         opts.on('-C', '--no-cache', "Don't cache to sassc files.") do
686:           @options[:for_engine][:read_cache] = false
687:         end
688: 
689:         unless ::Haml::Util.ruby1_8?
690:           opts.on('-E encoding', 'Specify the default encoding for Sass and CSS files.') do |encoding|
691:             Encoding.default_external = encoding
692:           end
693:         end
694: 
695:         super
696:       end

Private Instance Methods

process_directory() click to toggle source
     # File lib/haml/exec.rb, line 727
727:       def process_directory
728:         unless input = @options[:input] = @args.shift
729:           raise "Error: directory required when using --recursive."
730:         end
731: 
732:         output = @options[:output] = @args.shift
733:         raise "Error: --from required when using --recursive." unless @options[:from]
734:         raise "Error: --to required when using --recursive." unless @options[:to]
735:         raise "Error: '#{@options[:input]}' is not a directory" unless File.directory?(@options[:input])
736:         if @options[:output] && File.exists?(@options[:output]) && !File.directory?(@options[:output])
737:           raise "Error: '#{@options[:output]}' is not a directory"
738:         end
739:         @options[:output] ||= @options[:input]
740: 
741:         from = @options[:from]
742:         from = :sass if from == :sass2
743:         if @options[:to] == @options[:from] && !@options[:in_place]
744:           fmt = @options[:from]
745:           raise "Error: converting from #{fmt} to #{fmt} without --in-place"
746:         end
747: 
748:         ext = @options[:from]
749:         ext = :sass if ext == :sass2
750:         Dir.glob("#{@options[:input]}/**/*.#{ext}") do |f|
751:           output =
752:             if @options[:in_place]
753:               f
754:             elsif @options[:output]
755:               output_name = f.gsub(/\.(c|sa|sc|le)ss$/, ".#{@options[:to]}")
756:               output_name[0...@options[:input].size] = @options[:output]
757:               output_name
758:             else
759:               f.gsub(/\.(c|sa|sc|le)ss$/, ".#{@options[:to]}")
760:             end
761: 
762:           unless File.directory?(File.dirname(output))
763:             puts_action :directory, :green, File.dirname(output)
764:             FileUtils.mkdir_p(File.dirname(output))
765:           end
766:           puts_action :convert, :green, f
767:           if File.exists?(output)
768:             puts_action :overwrite, :yellow, output
769:           else
770:             puts_action :create, :green, output
771:           end
772: 
773:           input = open_file(f)
774:           output = @options[:in_place] ? input : open_file(output, "w")
775:           process_file(input, output)
776:         end
777:       end
process_file(input, output) click to toggle source
     # File lib/haml/exec.rb, line 779
779:       def process_file(input, output)
780:         if input.is_a?(File)
781:           @options[:from] ||=
782:             case input.path
783:             when /\.scss$/; :scss
784:             when /\.sass$/; :sass
785:             when /\.less$/; :less
786:             when /\.css$/; :css
787:             end
788:         elsif @options[:in_place]
789:           raise "Error: the --in-place option requires a filename."
790:         end
791: 
792:         if output.is_a?(File)
793:           @options[:to] ||=
794:             case output.path
795:             when /\.scss$/; :scss
796:             when /\.sass$/; :sass
797:             end
798:         end
799: 
800:         if @options[:from] == :sass2
801:           @options[:from] = :sass
802:           @options[:for_engine][:sass2] = true
803:         end
804: 
805:         @options[:from] ||= :css
806:         @options[:to] ||= :sass
807:         @options[:for_engine][:syntax] = @options[:from]
808: 
809:         out =
810:           ::Haml::Util.silence_haml_warnings do
811:             if @options[:from] == :css
812:               require 'sass/css'
813:               ::Sass::CSS.new(input.read, @options[:for_tree]).render(@options[:to])
814:             elsif @options[:from] == :less
815:               require 'sass/less'
816:               try_less_note
817:               input = input.read if input.is_a?(IO) && !input.is_a?(File) # Less is dumb
818:               Less::Engine.new(input).to_tree.to_sass_tree.send("to_#{@options[:to]}", @options[:for_tree])
819:             else
820:               if input.is_a?(File)
821:                 ::Sass::Files.tree_for(input.path, @options[:for_engine])
822:               else
823:                 ::Sass::Engine.new(input.read, @options[:for_engine]).to_tree
824:               end.send("to_#{@options[:to]}", @options[:for_tree])
825:             end
826:           end
827: 
828:         output = File.open(input.path, 'w') if @options[:in_place]
829:         output.write(out)
830:       rescue ::Sass::SyntaxError => e
831:         raise e if @options[:trace]
832:         file = " of #{e.sass_filename}" if e.sass_filename
833:         raise "Error on line #{e.sass_line}#{file}: #{e.message}\n  Use --trace for backtrace"
834:       rescue LoadError => err
835:         handle_load_error(err)
836:       end
try_less_note() click to toggle source
     # File lib/haml/exec.rb, line 839
839:       def try_less_note
840:         return if @@less_note_printed
841:         @@less_note_printed = true
842:         warn * NOTE: Sass and Less are different languages, and they work differently.* I'll do my best to translate, but some features -- especially mixins --* should be checked by hand.
843:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.