class SassC::Engine

Constants

OUTPUT_STYLES

Attributes

options[R]
template[R]

Public Class Methods

new(template, options = {}) click to toggle source
# File lib/sassc/engine.rb, line 16
def initialize(template, options = {})
  @template = template
  @options = options
  @functions = options.fetch(:functions, Script::Functions)
end

Public Instance Methods

dependencies() click to toggle source
# File lib/sassc/engine.rb, line 66
def dependencies
  raise NotRenderedError unless @dependencies
  Dependency.from_filenames(@dependencies)
end
filename() click to toggle source
# File lib/sassc/engine.rb, line 76
def filename
  @options[:filename]
end
render() click to toggle source
# File lib/sassc/engine.rb, line 22
def render
  return @template.dup if @template.empty?

  data_context = Native.make_data_context(@template)
  context = Native.data_context_get_context(data_context)
  native_options = Native.context_get_options(context)

  Native.option_set_is_indented_syntax_src(native_options, true) if sass?
  Native.option_set_input_path(native_options, filename) if filename
  Native.option_set_precision(native_options, precision) if precision
  Native.option_set_include_path(native_options, load_paths)
  Native.option_set_output_style(native_options, output_style_enum)
  Native.option_set_source_comments(native_options, true) if line_comments?
  Native.option_set_source_map_file(native_options, source_map_file) if source_map_file
  Native.option_set_source_map_embed(native_options, true) if source_map_embed?
  Native.option_set_source_map_contents(native_options, true) if source_map_contents?
  Native.option_set_omit_source_map_url(native_options, true) if omit_source_map_url?

  import_handler.setup(native_options)
  functions_handler.setup(native_options, functions: @functions)

  status = Native.compile_data_context(data_context)

  if status != 0
    message = Native.context_get_error_message(context)
    filename = Native.context_get_error_file(context)
    line = Native.context_get_error_line(context)

    raise SyntaxError.new(message, filename: filename, line: line)
  end

  css = Native.context_get_output_string(context)

  @dependencies = Native.context_get_included_files(context)
  @source_map   = Native.context_get_source_map_string(context)

  css.force_encoding(@template.encoding)
  @source_map.force_encoding(@template.encoding) if @source_map.is_a?(String)

  return css unless quiet?
ensure
  Native.delete_data_context(data_context) if data_context
end
source_map() click to toggle source
# File lib/sassc/engine.rb, line 71
def source_map
  raise NotRenderedError unless @source_map
  @source_map
end

Private Instance Methods

functions_handler() click to toggle source
# File lib/sassc/engine.rb, line 118
def functions_handler
  @functions_handler = FunctionsHandler.new(@options)
end
import_handler() click to toggle source
# File lib/sassc/engine.rb, line 114
def import_handler
  @import_handler ||= ImportHandler.new(@options)
end
line_comments?() click to toggle source
# File lib/sassc/engine.rb, line 94
def line_comments?
  @options[:line_comments]
end
load_paths() click to toggle source
# File lib/sassc/engine.rb, line 136
def load_paths
  paths = (@options[:load_paths] || []) + SassC.load_paths
  paths.join(File::PATH_SEPARATOR) unless paths.empty?
end
omit_source_map_url?() click to toggle source
# File lib/sassc/engine.rb, line 106
def omit_source_map_url?
  @options[:omit_source_map_url]
end
output_style() click to toggle source
# File lib/sassc/engine.rb, line 126
def output_style
  @output_style ||= begin
    style = @options.fetch(:style, :sass_style_nested).to_s
    style = "sass_style_#{style}" unless style.include?("sass_style_")
    style = style.to_sym
    raise InvalidStyleError unless Native::SassOutputStyle.symbols.include?(style)
    style
  end
end
output_style_enum() click to toggle source
# File lib/sassc/engine.rb, line 122
def output_style_enum
  @output_style_enum ||= Native::SassOutputStyle[output_style]
end
precision() click to toggle source
# File lib/sassc/engine.rb, line 86
def precision
  @options[:precision]
end
quiet?() click to toggle source
# File lib/sassc/engine.rb, line 82
def quiet?
  @options[:quiet]
end
sass?() click to toggle source
# File lib/sassc/engine.rb, line 90
def sass?
  @options[:syntax] && @options[:syntax].to_sym == :sass
end
source_map_contents?() click to toggle source
# File lib/sassc/engine.rb, line 102
def source_map_contents?
  @options[:source_map_contents]
end
source_map_embed?() click to toggle source
# File lib/sassc/engine.rb, line 98
def source_map_embed?
  @options[:source_map_embed]
end
source_map_file() click to toggle source
# File lib/sassc/engine.rb, line 110
def source_map_file
  @options[:source_map_file]
end