Class: Sass::CSS

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

Overview

This class converts CSS documents into Sass or SCSS 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/SCSS.

Example usage:

Sass::CSS.new("p { color: blue }").render(:sass) #=> "p\n  color: blue"
Sass::CSS.new("p { color: blue }").render(:scss) #=> "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). This is only meaningful when generating Sass code, rather than SCSS.



23
24
25
26
27
28
29
30
31
32
# File 'lib/sass/css.rb', line 23

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 = template
end

Instance Method Details

#render(fmt = :sass) ⇒ String

Converts the CSS template into Sass or SCSS code.

Parameters:

  • fmt (Symbol) (defaults to: :sass)

    :sass or :scss, designating the format to return.

Returns:

  • (String)

    The resulting Sass or SCSS code

Raises:



39
40
41
42
43
44
45
46
47
48
# File 'lib/sass/css.rb', line 39

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

  build_tree.send("to_#{fmt}", @options).strip + "\n"
rescue Sass::SyntaxError => err
  err.modify_backtrace(:filename => @options[:filename] || '(css)')
  raise err
end