Class: Sass::Engine

Inherits:
Object
  • Object
show all
Includes:
Haml::Util
Defined in:
lib/sass/engine.rb

Overview

This class handles the parsing and compilation of the Sass template. Example usage:

template = File.load('stylesheets/sassy.sass')
sass_engine = Sass::Engine.new(template)
output = sass_engine.render
puts output

Defined Under Namespace

Classes: Line

Constant Summary collapse

PROPERTY_CHAR =

The character that begins a CSS property.

?:
SCRIPT_CHAR =

The character that designates that a property should be assigned to a SassScript expression.

?=
COMMENT_CHAR =

The character that designates the beginning of a comment, either Sass or CSS.

?/
SASS_COMMENT_CHAR =

The character that follows the general COMMENT_CHAR and designates a Sass comment, which is not output as a CSS comment.

?/
CSS_COMMENT_CHAR =

The character that follows the general COMMENT_CHAR and designates a CSS comment, which is embedded in the CSS document.

?*
DIRECTIVE_CHAR =

The character used to denote a compiler directive.

?@
ESCAPE_CHAR =

Designates a non-parsed rule.

?\\
MIXIN_DEFINITION_CHAR =

Designates block as mixin definition rather than CSS rules to output

?=
MIXIN_INCLUDE_CHAR =

Includes named mixin declared using MIXIN_DEFINITION_CHAR

?+
PROPERTY_NEW_MATCHER =

The regex that matches properties of the form name: prop.

/^[^\s:"\[]+\s*[=:](\s|$)/
PROPERTY_NEW =

The regex that matches and extracts data from properties of the form name: prop.

/^([^\s=:"]+)\s*(=|:)(?:\s+|$)(.*)/
PROPERTY_OLD =

The regex that matches and extracts data from properties of the form :name prop.

/^:([^\s=:"]+)\s*(=?)(?:\s+|$)(.*)/
DEFAULT_OPTIONS =

The default options for Sass::Engine.

{
  :style => :nested,
  :load_paths => ['.'],
  :cache => true,
  :cache_location => './.sass-cache',
  :syntax => :sass,
}.freeze

Constants included from Haml::Util

Haml::Util::CHARSET_REGEXPS, Haml::Util::ENCODINGS_TO_CHECK, Haml::Util::RUBY_VERSION

Instance Method Summary collapse

Methods included from Haml::Util

#ap_geq?, #ap_geq_3?, #assert_html_safe!, #av_template_class, #caller_info, #check_encoding, #check_haml_encoding, #check_sass_encoding, #def_static_method, #enum_cons, #enum_slice, #enum_with_index, #flatten, #haml_warn, #has?, #html_safe, #intersperse, #lcs, #map_hash, #map_keys, #map_vals, #merge_adjacent_strings, #ord, #paths, #powerset, #rails_env, #rails_root, #rails_safe_buffer_class, #rails_xss_safe?, #restrict, #ruby1_8?, #ruby1_8_6?, #scope, #set_eql?, #set_hash, #silence_haml_warnings, #silence_warnings, #static_method_name, #strip_string_array, #substitute, #to_hash, #windows?

Constructor Details

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

Returns a new instance of Engine.

Parameters:

  • template (String)

    The Sass template. This template can be encoded using any encoding that can be converted to Unicode. If the template contains an @charset declaration, that overrides the Ruby encoding (see the encoding documentation)

  • options ({Symbol => Object}) (defaults to: {})

    An options hash; see the Sass options documentation



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/sass/engine.rb', line 143

def initialize(template, options={})
  @options = DEFAULT_OPTIONS.merge(options.reject {|k, v| v.nil?})
  @template = template

  # Support both, because the docs said one and the other actually worked
  # for quite a long time.
  @options[:line_comments] ||= @options[:line_numbers]

  # Backwards compatibility
  @options[:property_syntax] ||= @options[:attribute_syntax]
  case @options[:property_syntax]
  when :alternate; @options[:property_syntax] = :new
  when :normal; @options[:property_syntax] = :old
  end
end

Instance Method Details

#renderString Also known as: to_css

Render the template to CSS.

Returns:

  • (String)

    The CSS

Raises:

  • (Sass::SyntaxError)

    if there's an error in the document

  • (Encoding::UndefinedConversionError)

    if the source encoding cannot be converted to UTF-8

  • (ArgumentError)

    if the document uses an unknown encoding with @charset



166
167
168
169
# File 'lib/sass/engine.rb', line 166

def render
  return _render unless @options[:quiet]
  Haml::Util.silence_haml_warnings {_render}
end

#source_encodingEncoding?

Returns the original encoding of the document, or nil under Ruby 1.8.

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



188
189
190
191
# File 'lib/sass/engine.rb', line 188

def source_encoding
  check_encoding!
  @original_encoding
end

#to_treeSass::Tree::Node

Parses the document into its parse tree.

Returns:

Raises:



176
177
178
179
# File 'lib/sass/engine.rb', line 176

def to_tree
  return _to_tree unless @options[:quiet]
  Haml::Util.silence_haml_warnings {_to_tree}
end