Class: Sass::Engine

Inherits:
Object
  • Object
show all
Defined in:
/Users/jbolton/Projects/sass-site/.sass/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

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

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Engine) initialize(template, options = {})

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 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}


261
262
263
264
# File '/Users/jbolton/Projects/sass-site/.sass/lib/sass/engine.rb', line 261

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

Instance Attribute Details

- ({Symbol => Object}) options (readonly)

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

Returns:

  • ({Symbol => Object})


242
243
244
# File '/Users/jbolton/Projects/sass-site/.sass/lib/sass/engine.rb', line 242

def options
  @options
end

Class Method Details

+ (Sass::Engine) for_file(filename, options)

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:



224
225
226
227
228
229
230
231
232
233
234
235
236
# File '/Users/jbolton/Projects/sass-site/.sass/lib/sass/engine.rb', line 224

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

- ([Sass::Engine]) dependencies

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

Returns:



326
327
328
329
# File '/Users/jbolton/Projects/sass-site/.sass/lib/sass/engine.rb', line 326

def dependencies
  _dependencies(Set.new, engines = Set.new)
  Sass::Util.array_minus(engines, [self])
end

- (String) render 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



273
274
275
276
# File '/Users/jbolton/Projects/sass-site/.sass/lib/sass/engine.rb', line 273

def render
  return encode_and_set_charset(_to_tree.render) unless @options[:quiet]
  Sass::Util.silence_sass_warnings {encode_and_set_charset(_to_tree.render)}
end

- ((String, Sass::Source::Map)) render_with_sourcemap(sourcemap_uri)

Render the template to CSS and return the source map.

Parameters:

  • sourcemap_uri (String)

    The sourcemap URI to use in the @sourceMappingURL comment. If this is relative, it should be relative to the location of the CSS file.

Returns:

Raises:

  • (Sass::SyntaxError)

    if there’s an error in the document, or if the public URL for this document couldn’t be determined.

  • (Encoding::UndefinedConversionError)

    if the source encoding cannot be converted to UTF-8

  • (ArgumentError)

    if the document uses an unknown encoding with @charset



290
291
292
293
# File '/Users/jbolton/Projects/sass-site/.sass/lib/sass/engine.rb', line 290

def render_with_sourcemap(sourcemap_uri)
  return _render_with_sourcemap(sourcemap_uri) unless @options[:quiet]
  Sass::Util.silence_sass_warnings {_render_with_sourcemap(sourcemap_uri)}
end

- (Encoding?) source_encoding

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



316
317
318
319
# File '/Users/jbolton/Projects/sass-site/.sass/lib/sass/engine.rb', line 316

def source_encoding
  check_encoding!
  @original_encoding
end

- (Sass::Tree::Node) to_tree

Parses the document into its parse tree. Memoized.

Returns:

Raises:



301
302
303
304
305
306
307
# File '/Users/jbolton/Projects/sass-site/.sass/lib/sass/engine.rb', line 301

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