Class | Sass::CSS |
In: |
lib/sass/css.rb
|
Parent: | Object |
This class converts CSS documents into Sass or SCSS templates. It works by parsing the CSS document into a {Sass::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; }"
@param template [String] The CSS code @option options :old [Boolean] (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.
# File lib/sass/css.rb, line 23 23: def initialize(template, options = {}) 24: if template.is_a? IO 25: template = template.read 26: end 27: 28: @options = options.dup 29: # Backwards compatibility 30: @options[:old] = true if @options[:alternate] == false 31: @template = template 32: end
Converts the CSS template into Sass or SCSS code.
@param fmt [Symbol] `:sass` or `:scss`, designating the format to return. @return [String] The resulting Sass or SCSS code @raise [Sass::SyntaxError] if there‘s an error parsing the CSS template
# File lib/sass/css.rb, line 39 39: def render(fmt = :sass) 40: Haml::Util.check_encoding(@template) do |msg, line| 41: raise Sass::SyntaxError.new(msg, :line => line) 42: end 43: 44: build_tree.send("to_#{fmt}", @options).strip + "\n" 45: rescue Sass::SyntaxError => err 46: err.modify_backtrace(:filename => @options[:filename] || '(css)') 47: raise err 48: end