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

Inherits:
Tilt::SassTemplate
  • Object
show all
Defined in:
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


32
33
34
35
36
# File 'lib/middleman-core/renderers/sass.rb', line 32

def initialize(*args, &block)
  super

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

Instance Method Details

#evaluate(context, _) ⇒ String

Add exception messaging


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/middleman-core/renderers/sass.rb', line 49

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


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/middleman-core/renderers/sass.rb', line 63

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


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

def prepare; end

#sass_optionsHash

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


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/middleman-core/renderers/sass.rb', line 81

def sass_options
  ctx = @context

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

  more_opts = {
    load_paths: preexisting_load_paths + ctx.app.config[:sass_assets_paths],
    filename: eval_file,
    line: line,
    syntax: syntax,
    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

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

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

#syntaxSymbol

Define the expected syntax for the template


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

def syntax
  :sass
end