Class: Haml::Exec::SassConvert

Inherits:
Generic show all
Defined in:
lib/haml/exec.rb

Overview

The sass-convert executable.

Instance Method Summary collapse

Methods inherited from Generic

#color, #get_line, #parse, #parse!, #puts_action, #to_s

Constructor Details

#initialize(args) ⇒ SassConvert

Returns a new instance of SassConvert.

Parameters:

  • args (Array<String>)

    The command-line arguments



570
571
572
573
574
# File 'lib/haml/exec.rb', line 570

def initialize(args)
  super
  @options[:for_tree] = {}
  @options[:for_engine] = {}
end

Instance Method Details

#process_directory



657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/haml/exec.rb', line 657

def process_directory
  input = @options[:input] = @args.shift
  output = @options[:output] = @args.shift
  raise "Error: --from required when using --recursive." unless @options[:from]
  raise "Error: --to required when using --recursive." unless @options[:to]
  raise "Error: '#{@options[:input]}' is not a directory" unless File.directory?(@options[:input])
  if @options[:output] && File.exists?(@options[:output]) && !File.directory?(@options[:output])
    raise "Error: '#{@options[:output]}' is not a directory"
  end
  @options[:output] ||= @options[:input]

  ext = @options[:from]
  ext = :sass if ext == :sass2
  Dir.glob("#{@options[:input]}/**/*.#{ext}") do |f|
    output =
      if @options[:in_place]
        f
      elsif @options[:output]
        output_name = f.gsub(/\.(c|sa|sc)ss$/, ".#{@options[:to]}")
        output_name[0...@options[:input].size] = @options[:output]
        output_name
      else
        f.gsub(/\.(c|sa|sc)ss$/, ".#{@options[:to]}")
      end

    unless File.directory?(File.dirname(output))
      puts_action :directory, :green, File.dirname(output)
      FileUtils.mkdir_p(File.dirname(output))
    end
    puts_action :convert, :green, f
    if File.exists?(output)
      puts_action :overwrite, :yellow, output
    else
      puts_action :create, :green, output
    end

    input = open_file(f)
    output = @options[:in_place] ? input : open_file(output, "w")
    process_file(input, output)
  end
end

#process_file(input, output)



699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# File 'lib/haml/exec.rb', line 699

def process_file(input, output)
  if input.is_a?(File)
    @options[:from] ||=
      case input.path
      when /\.scss$/; :scss
      when /\.sass$/; :sass
      when /\.css$/; :css
      end
  elsif @options[:in_place]
    raise "Error: the --in-place option requires a filename."
  end

  if output.is_a?(File)
    @options[:to] ||=
      case output.path
      when /\.scss$/; :scss
      when /\.sass$/; :sass
      end
  end

  if @options[:from] == :sass2
    @options[:from] = :sass
    @options[:for_engine][:sass2] = true
  end

  @options[:from] ||= :css
  @options[:to] ||= :sass
  @options[:for_engine][:syntax] = @options[:from]

  out =
    ::Haml::Util.silence_haml_warnings do
      if @options[:from] == :css
        require 'sass/css'
        ::Sass::CSS.new(input.read, @options[:for_tree]).render(@options[:to])
      else
        if input.is_a?(File)
          ::Sass::Files.tree_for(input.path, @options[:for_engine])
        else
          ::Sass::Engine.new(input.read, @options[:for_engine]).to_tree
        end.send("to_#{@options[:to]}", @options[:for_tree])
      end
    end

  output = File.open(input.path, 'w') if @options[:in_place]
  output.write(out)
rescue ::Sass::SyntaxError => e
  raise e if @options[:trace]
  raise "Syntax error on line #{get_line e}: #{e.message}\n  Use --trace for backtrace"
end

#process_result

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



641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/haml/exec.rb', line 641

def process_result
  require 'sass'

  if @options[:recursive]
    process_directory
    return
  end

  super
  input = @options[:input]
  raise "Error: '#{input}' is a directory (did you mean to use --recursive?)" if File.directory?(input)
  output = @options[:output]
  output = input if @options[:in_place]
  process_file(input, output)
end

#set_opts(opts)

Tells optparse how to parse the arguments.

Parameters:

  • opts (OptionParser)


579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/haml/exec.rb', line 579

def set_opts(opts)
  opts.banner = <<END
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:
END

  opts.on('-F', '--from FORMAT',
    'The format to convert from. Can be css, scss, sass, or sass2.',
    'sass2 is the same as sass, but updates more old syntax to new.',
    'By default, this is inferred from the input filename.',
    'If there is none, defaults to css.') do |name|
    @options[:from] = name.downcase.to_sym
    unless [:css, :scss, :sass].include?(@options[:from])
      raise "Unknown format for sass-convert --from: #{name}"
    end
  end

  opts.on('-T', '--to FORMAT',
    'The format to convert to. Can be scss or sass.',
    'By default, this is inferred from the output filename.',
    'If there is none, defaults to sass.') do |name|
    @options[:to] = name.downcase.to_sym
    unless [:scss, :sass].include?(@options[:to])
      raise "Unknown format for sass-convert --to: #{name}"
    end
  end

  opts.on('-R', '--recursive',
    'Convert all the files in a directory. Requires --from and --to.') do
    @options[:recursive] = true
  end

  opts.on('-i', '--in-place',
    'Convert a file to its own syntax.',
    'This can be used to update some deprecated syntax.') do
    @options[:in_place] = true
  end

  opts.on('--dasherize', 'Convert underscores to dashes') do
    @options[:for_tree][:dasherize] = true
  end

  opts.on('--old', 'Output the old-style ":prop val" property syntax.',
                   'Only meaningful when generating Sass.') do
    @options[:for_tree][:old] = true
  end

  opts.on('-C', '--no-cache', "Don't cache to sassc files.") do
    @options[:for_engine][:cache] = false
  end

  super
end