Class: WHTMLSprockets::Processor

Inherits:
Tilt::Template
  • Object
show all
Defined in:
lib/whtml_sprockets/processor.rb

Constant Summary collapse

JS_ESCAPE_MAP =
{
  '\\'    => '\\\\',
  '</'    => '<\/',
  "\r\n"  => '\n',
  "\n"    => '\n',
  "\r"    => '\n',
  '"'     => '\\"',
  "'"     => "\\'"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



25
26
27
# File 'lib/whtml_sprockets/processor.rb', line 25

def namespace
  @namespace
end

Class Method Details

.default_mime_typeObject



13
14
15
# File 'lib/whtml_sprockets/processor.rb', line 13

def self.default_mime_type
  'application/javascript'
end

.default_namespaceObject



17
18
19
# File 'lib/whtml_sprockets/processor.rb', line 17

def self.default_namespace
  'this.Wingman.View.templateSources'
end

Instance Method Details

#evaluate(scope, locals, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/whtml_sprockets/processor.rb', line 27

def evaluate(scope, locals, &block)
  escaped = data.gsub(/(\\|<\/|\r\n|\342\200\250|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] }
  
  <<-JS
    (function() {
      #{namespace} || (#{namespace} = {});
      #{namespace}[#{scope.logical_path.inspect}] = '#{escaped}';
    }).call(this);
  JS
end

#prepareObject



21
22
23
# File 'lib/whtml_sprockets/processor.rb', line 21

def prepare
  @namespace = self.class.default_namespace
end