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).



65
66
67
68
69
70
71
72
73
74
# File 'lib/sass/css.rb', line 65

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

  @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



79
80
81
82
83
84
85
86
87
88
# File 'lib/sass/css.rb', line 79

def render
  begin
    build_tree.to_sass(0, @options).strip + "\n"
  rescue Exception => err
    line = @template.string[[email protected]].split("\n").size

    err.backtrace.unshift "(css):#{line}"
    raise err
  end
end