Class: Middleman::Renderers::Sass::SassPlusCSSFilenameTemplate

Inherits:
Tilt::SassTemplate
  • Object
show all
Defined in:
middleman-core/lib/middleman-core/renderers/sass.rb

Overview

A SassTemplate for Tilt which outputs debug messages

Direct Known Subclasses

ScssPlusCSSFilenameTemplate

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ SassPlusCSSFilenameTemplate

Returns a new instance of SassPlusCSSFilenameTemplate



38
39
40
41
42
# File 'middleman-core/lib/middleman-core/renderers/sass.rb', line 38

def initialize(*args, &block)
  super

  @context = @options[:context] if @options.key?(:context)
end

Instance Method Details

#evaluate(context, _) ⇒ String

Add exception messaging

Parameters:

  • context (Class)

Returns:

  • (String)


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'middleman-core/lib/middleman-core/renderers/sass.rb', line 55

def evaluate(context, _)
  @context ||= context

  @engine = ::SassC::Engine.new(data, sass_options)

  begin
    @engine.render
  rescue ::SassC::SyntaxError => e
    raise e if @context.app.build?

    exception_to_css(e)
  end
end

#exception_to_css(e) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'middleman-core/lib/middleman-core/renderers/sass.rb', line 69

def exception_to_css(e)
  header = "#{e.class}: #{e.message}"

  <<~END
    /*
    #{header.gsub('*/', '*\\/')}

    Backtrace:\n#{e.backtrace.join("\n").gsub('*/', '*\\/')}
    */
    body:before {
      white-space: pre;
      font-family: monospace;
      content: "#{header.gsub('"', '\"').gsub("\n", '\\A ')}"; }
  END
end

#prepareObject



50
# File 'middleman-core/lib/middleman-core/renderers/sass.rb', line 50

def prepare; end

#sass_optionsHash

Change Sass path, for url functions, to the build folder if we're building

Returns:

  • (Hash)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'middleman-core/lib/middleman-core/renderers/sass.rb', line 95

def sass_options
  ctx = @context

  preexisting_load_paths = begin
    ::Sass.load_paths
                           rescue StandardError
                             []
  end

  more_opts = {
    load_paths: preexisting_load_paths + ctx.app.config[:sass_assets_paths],
    filename: eval_file,
    line: line,
    syntax: syntax,
    cache_location: ctx.app.config[:sass_cache_location],
    custom: {}.merge!(options[:custom] || {}).merge!(
      middleman_context: ctx.app,
      current_resource: ctx.current_resource
    )
  }

  if ctx.app.config[:sass_source_maps] || (ctx.app.config[:sass_source_maps].nil? && ctx.app.development?)
    more_opts[:source_map_file] = '.'
    more_opts[:source_map_embed] = true
    more_opts[:source_map_contents] = true
  end

  more_opts[:css_filename] = file.sub(/\.s[ac]ss$/, '') if ctx.is_a?(::Middleman::TemplateContext) && file

  {}.merge!(options).merge!(more_opts)
end

#syntaxSymbol

Define the expected syntax for the template

Returns:

  • (Symbol)


46
47
48
# File 'middleman-core/lib/middleman-core/renderers/sass.rb', line 46

def syntax
  :sass
end

#verticesObject



85
86
87
88
89
90
91
# File 'middleman-core/lib/middleman-core/renderers/sass.rb', line 85

def vertices
  @engine.dependencies.reduce(::Hamster::Set.empty) do |sum, d|
    sum << ::Middleman::Dependencies::FileVertex.new(@context.app.root_path, d.filename.to_sym)
  end
rescue ::SassC::NotRenderedError
  ::Hamster::Set.empty
end