Class: Sass::CSS

Inherits:
Object show all
Defined in:
lib/sass/css.rb

Overview

This class converts CSS documents into Sass templates. It works by parsing the CSS document into a Tree structure, and then applying various transformations to the structure to produce more concise and idiomatic Sass.

Example usage:

Sass::CSS.new("p { color: blue }").render #=> "p\n  color: blue"

Instance Method Summary collapse

Constructor Details

#initialize(template, options = {}) ⇒ CSS

Returns a new instance of CSS.

Parameters:

  • template (String)

    The CSS code

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :old (Boolean) — default: false

    Whether or not to output old property syntax (:color blue as opposed to color: blue).

  • :filename (String)

    The filename of the CSS file being processed. Used for error reporting



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sass/css.rb', line 68

def initialize(template, options = {})
  if template.is_a? IO
    template = template.read
  end

  @line = 1
  @options = options.dup
  # Backwards compatibility
  @options[:old] = true if @options[:alternate] == false
  @template = StringScanner.new(template)
end

Instance Method Details

#renderString

Converts the CSS template into Sass code.

Returns:

  • (String)

    The resulting Sass code

Raises:



84
85
86
87
88
89
90
91
92
93
# File 'lib/sass/css.rb', line 84

def render
  Haml::Util.check_encoding(@template.string) do |msg, line|
    raise Sass::SyntaxError.new(msg, :line => line)
  end

  build_tree.to_sass(0, @options).strip + "\n"
rescue Sass::SyntaxError => err
  err.modify_backtrace(:filename => @options[:filename] || '(css)', :line => @line)
  raise err
end