Class: Sass::Engine

Inherits:
Object
  • Object
show all
Includes:
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.

?:
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.

?/
SASS_LOUD_COMMENT_CHAR =

The character that indicates that a comment allows interpolation and should be preserved even in :compressed mode.

?!
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_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,
  :filesystem_importer => Sass::Importers::Filesystem
}.freeze

Constants included from Util

Util::CHARSET_REGEXPS, Util::ENCODINGS_TO_CHECK, Util::RUBY_ENGINE, Util::RUBY_VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#abstract, #ap_geq?, #ap_geq_3?, #av_template_class, #caller_info, #check_encoding, #check_sass_encoding, #enum_cons, #enum_slice, #enum_with_index, #extract_values, #flatten, #has?, #inject_values, #inspect_obj, #intersperse, #ironruby?, #lcs, #map_hash, #map_keys, #map_vals, #merge_adjacent_strings, #ord, #paths, #powerset, #rails_env, #rails_root, #restrict, #ruby1_8?, #ruby1_8_6?, #sass_warn, #scope, #set_eql?, #set_hash, #silence_sass_warnings, #silence_warnings, #strip_string_array, #substitute, #to_hash, #version_geq, #version_gt, #windows?, #with_extracted_values

Constructor Details

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

Creates a new Engine. Note that Engine should only be used directly when compiling in-memory Sass code. If you're compiling a single Sass file from the filesystem, use for_file. If you're compiling multiple files from the filesystem, use {Sass::Plugin.

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.

See Also:

  • Sass::Engine.{Sass{Sass::Engine{Sass::Engine.for_file}
  • Sass::Engine.{Sass{Sass::Plugin}


234
235
236
237
# File 'lib/sass/engine.rb', line 234

def initialize(template, options={})
  @options = self.class.normalize_options(options)
  @template = template
end

Instance Attribute Details

#options{Symbol => Object} (readonly)

The options for the Sass engine. See the Sass options documentation.

Returns:

  • ({Symbol => Object})


215
216
217
# File 'lib/sass/engine.rb', line 215

def options
  @options
end

Class Method Details

.for_file(filename, options) ⇒ Sass::Engine

Returns the Sass::Engine for the given file. This is preferable to Sass::Engine.new when reading from a file because it properly sets up the Engine's metadata, enables parse-tree caching, and infers the syntax from the filename.

Parameters:

Returns:

  • (Sass::Engine)

    The Engine for the given Sass or SCSS file.

Raises:



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/sass/engine.rb', line 197

def self.for_file(filename, options)
  had_syntax = options[:syntax]

  if had_syntax
    # Use what was explicitly specificed
  elsif filename =~ /\.scss$/
    options.merge!(:syntax => :scss)
  elsif filename =~ /\.sass$/
    options.merge!(:syntax => :sass)
  end

  Sass::Engine.new(File.read(filename), options.merge(:filename => filename))
end

Instance Method Details

#dependencies[Sass::Engine]

Gets a set of all the documents that are (transitive) dependencies of this document, not including the document itself.

Returns:



279
280
281
282
# File 'lib/sass/engine.rb', line 279

def dependencies
  _dependencies(Set.new, engines = Set.new)
  engines - [self]
end

#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



246
247
248
249
# File 'lib/sass/engine.rb', line 246

def render
  return _render unless @options[:quiet]
  Sass::Util.silence_sass_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



269
270
271
272
# File 'lib/sass/engine.rb', line 269

def source_encoding
  check_encoding!
  @original_encoding
end

#to_treeSass::Tree::Node

Parses the document into its parse tree. Memoized.

Returns:

Raises:



256
257
258
259
260
# File 'lib/sass/engine.rb', line 256

def to_tree
  @tree ||= @options[:quiet] ?
    Sass::Util.silence_sass_warnings {_to_tree} :
    _to_tree
end