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 stylesheet. This stylesheet can be encoded using any encoding that can be converted to Unicode. If the stylesheet contains an @charset declaration, that overrides the Ruby encoding (see the encoding documentation)

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

  • :indent (String) — default: " "

    The string to use for indenting each line. Defaults to two spaces.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sass/css.rb', line 29

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

  @options = options.merge(:_convert => true)
  # Backwards compatibility
  @options[:old] = true if @options[:alternate] == false
  @template = template
  @checked_encoding = false
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:



46
47
48
49
50
51
52
# File 'lib/sass/css.rb', line 46

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

#source_encodingEncoding?

Returns the original encoding of the document.

Returns:

  • (Encoding, nil)

Raises:

  • (Encoding::UndefinedConversionError)

    if the source encoding cannot be converted to UTF-8

  • (ArgumentError)

    if the document uses an unknown encoding with @charset



60
61
62
63
# File 'lib/sass/css.rb', line 60

def source_encoding
  check_encoding!
  @original_encoding
end