Class: Dang::View

Inherits:
Object
  • Object
show all
Defined in:
lib/dang/rails.rb

Constant Summary collapse

ENCODING_TAG =
Regexp.new("\\A(<-\\s*#{ActionView::ENCODING_FLAG}\\s*->)[ \\t]*")

Instance Method Summary collapse

Instance Method Details

#call(template) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dang/rails.rb', line 24

def call(template)
  template_source = template.source.dup.force_encoding(Encoding::ASCII_8BIT)

  dang = template_source.gsub(ENCODING_TAG, '')
  encoding = $2

  dang.force_encoding valid_encoding(template.source.dup, encoding)

  # Always make sure we return a String in the default_internal
  dang.encode!

  parser = Dang::Parser.new(dang, true)
  unless parser.parse
    io = StringIO.new
    parser.show_error(io)
    raise io.string
  end

  parser.compile
end

#handles_encoding?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/dang/rails.rb', line 3

def handles_encoding?
  true
end

#valid_encoding(string, encoding) ⇒ Object

Raises:

  • (ActionView::WrongEncodingError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dang/rails.rb', line 7

def valid_encoding(string, encoding)
  # If a magic encoding comment was found, tag the
  # String with this encoding. This is for a case
  # where the original String was assumed to be,
  # for instance, UTF-8, but a magic comment
  # proved otherwise
  string.force_encoding(encoding) if encoding

  # If the String is valid, return the encoding we found
  return string.encoding if string.valid_encoding?

  # Otherwise, raise an exception
  raise ActionView::WrongEncodingError.new(string, string.encoding)
end