Module: Barista::HamlFilter::CoffeeScript

Defined in:
lib/barista/haml_filter.rb

Instance Method Summary collapse

Instance Method Details

#content_type(render_type) ⇒ Object



30
31
32
# File 'lib/barista/haml_filter.rb', line 30

def content_type(render_type)
  Barista::Server::CONTENT_TYPE_MAPPING[render_type]
end

#render_typeObject



26
27
28
# File 'lib/barista/haml_filter.rb', line 26

def render_type
  Barista.embedded_interpreter? ? :coffeescript : :javascript
end

#render_with_options(text, options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/barista/haml_filter.rb', line 5

def render_with_options(text, options)
  type = render_type
  case type
  when :coffeescript
    content_type  = 'text/coffeescript'
    cdata_wrapper = '#%s'
    inner         = text
  when :javascript
    content_type  = 'text/javascript'
    cdata_wrapper = '//%s'
    inner         = Barista::Compiler.compile(text)
  end
  output = []
  output << "<script type=#{options[:attr_wrapper]}#{content_type(type)}#{options[:attr_wrapper]}>"
  output << "  #{cdata_wrapper % '<![CDATA['}"
  output << "  #{inner}".rstrip.gsub("\n", "\n  ")
  output << "  #{cdata_wrapper % ']]>'}"
  output << "</script>"
  output.join("\n")
end