Class: SassC::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/sassc/engine.rb

Constant Summary collapse

OUTPUT_STYLES =
%i[
  sass_style_nested
  sass_style_expanded
  sass_style_compact
  sass_style_compressed
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Engine.



16
17
18
19
20
# File 'lib/sassc/engine.rb', line 16

def initialize(template, options = {})
  @template = template
  @options = options
  @functions = options.fetch(:functions, Script::Functions)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/sassc/engine.rb', line 14

def options
  @options
end

#templateObject (readonly)

Returns the value of attribute template.



14
15
16
# File 'lib/sassc/engine.rb', line 14

def template
  @template
end

Instance Method Details

#dependenciesObject

Raises:



61
62
63
64
# File 'lib/sassc/engine.rb', line 61

def dependencies
  raise NotRenderedError unless @dependencies
  Dependency.from_filenames(@dependencies)
end

#filenameObject



71
72
73
# File 'lib/sassc/engine.rb', line 71

def filename
  @options[:filename]
end

#renderObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sassc/engine.rb', line 22

def render
  return @template.dup if @template.empty?

  result = ::Sass.compile_string(
    @template,
    importer: import_handler.setup(nil),
    load_paths: load_paths,
    syntax: syntax,
    url: file_url,

    charset: @options.fetch(:charset, true),
    source_map: source_map_embed? || !source_map_file.nil?,
    source_map_include_sources: source_map_contents?,
    style: output_style,

    functions: functions_handler.setup(nil, functions: @functions),
    importers: @options.fetch(:importers, []),

    alert_ascii: @options.fetch(:alert_ascii, false),
    alert_color: @options.fetch(:alert_color, nil),
    logger: @options.fetch(:logger, nil),
    quiet_deps: @options.fetch(:quiet_deps, false),
    verbose: @options.fetch(:verbose, false)
  )

  @dependencies = result.loaded_urls
                        .filter { |url| url.start_with?(Protocol::FILE) && url != file_url }
                        .map { |url| URL.file_url_to_path(url) }
  @source_map = post_process_source_map(result.source_map)

  return post_process_css(result.css) unless quiet?
rescue ::Sass::CompileError => e
  line = e.span&.start&.line
  line += 1 unless line.nil?
  url = e.span&.url
  path = (URL.parse(url).route_from(URL.path_to_file_url("#{Dir.pwd}/")) if url&.start_with?(Protocol::FILE))
  raise SyntaxError.new(e.full_message, filename: path, line: line)
end

#source_mapObject

Raises:



66
67
68
69
# File 'lib/sassc/engine.rb', line 66

def source_map
  raise NotRenderedError unless @source_map
  @source_map
end