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.


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

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)

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

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


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

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

  error_msg = <<~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
  error_msg = error_msg.dup if error_msg.frozen? # slim will fail, if we pass frozen string as render result
  error_msg
end

#prepareObject


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

def prepare; end

#sass_optionsHash

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

Returns:

  • (Hash)

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
126
127
128
129
# File 'middleman-core/lib/middleman-core/renderers/sass.rb', line 99

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)

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

def syntax
  :sass
end

#verticesObject


89
90
91
92
93
94
95
# File 'middleman-core/lib/middleman-core/renderers/sass.rb', line 89

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