Class: ActionView::Template::Handlers::ERB

Inherits:
Handler
  • Object
show all
Defined in:
lib/fredit/erb.rb

Instance Method Summary collapse

Instance Method Details

#compile(template) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fredit/erb.rb', line 6

def compile(template)

  # copied from original

  if template.source.encoding_aware?
    # First, convert to BINARY, so in case the encoding is
    # wrong, we can still find an encoding tag
    # (<%# encoding %>) inside the String using a regular
    # expression
    template_source = template.source.dup.force_encoding("BINARY")

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

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

    # Always make sure we return a String in the default_internal
    erb.encode!
  else
    erb = template.source.dup
  end

  # begin Fredit patch

  if Fredit.template_editable?(template)
    source_file = Fredit.rel_path template.identifier
    edit_link = "<div style='color:red'>#{Fredit.link(source_file)}</div> "
    if erb =~ /^\s*<!DOCTYPE/ && erb =~ /<body[^>]*>/
      erb = erb.sub(/<body[^>]*>/, '\&' + edit_link)
    else
      erb = edit_link + erb
    end
  end

  # end Fredit patch

  self.class.erb_implementation.new(
    erb,
    :trim => (self.class.erb_trim_mode == "-")
  ).src
end