Class: Nyara::View::Erubis

Inherits:
Erubis::Eruby
  • Object
show all
Defined in:
lib/nyara/view_handlers/erubis.rb

Overview

mostly same as actionpack/action_view/template/handlers/erb.rb

Constant Summary collapse

BLOCK_EXPR =
/\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.src(template) ⇒ Object


7
8
9
# File 'lib/nyara/view_handlers/erubis.rb', line 7

def self.src template
  new(template).src
end

Instance Method Details

#add_expr(src, code, indicator) ⇒ Object

Erubis toggles <%= and <%== behavior when escaping is enabled. We override to always treat <%== as escaped.


34
35
36
37
38
39
40
41
# File 'lib/nyara/view_handlers/erubis.rb', line 34

def add_expr(src, code, indicator)
  case indicator
  when '=='
    add_expr_escaped(src, code)
  else
    super
  end
end

#add_expr_escaped(src, code) ⇒ Object


54
55
56
57
58
59
60
61
# File 'lib/nyara/view_handlers/erubis.rb', line 54

def add_expr_escaped(src, code)
  flush_newline_if_pending(src)
  if code =~ BLOCK_EXPR
    src << "_erbout.safe_append= " << code
  else
    src << "_erbout.safe_append=(" << code << ");"
  end
end

#add_expr_literal(src, code) ⇒ Object


45
46
47
48
49
50
51
52
# File 'lib/nyara/view_handlers/erubis.rb', line 45

def add_expr_literal(src, code)
  flush_newline_if_pending(src)
  if code =~ BLOCK_EXPR
    src << '_erbout.append= ' << code
  else
    src << '_erbout.append=(' << code << ');'
  end
end

#add_postamble(src) ⇒ Object


68
69
70
71
# File 'lib/nyara/view_handlers/erubis.rb', line 68

def add_postamble(src)
  flush_newline_if_pending(src)
  src << '_erbout.join'
end

#add_preamble(src) ⇒ Object


11
12
13
14
# File 'lib/nyara/view_handlers/erubis.rb', line 11

def add_preamble(src)
  @newline_pending = 0
  src << "_erbout = @_nyara_view.out;"
end

#add_stmt(src, code) ⇒ Object


63
64
65
66
# File 'lib/nyara/view_handlers/erubis.rb', line 63

def add_stmt(src, code)
  flush_newline_if_pending(src)
  super
end

#add_text(src, text) ⇒ Object

ensure 1:1 mapping of "\n" between template and output


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/nyara/view_handlers/erubis.rb', line 17

def add_text(src, text)
  return if text.empty?

  if text == "\n"
    @newline_pending += 1
  else
    src << "_erbout.safe_append='"
    src << "\n" * @newline_pending if @newline_pending > 0
    src << escape_text(text)
    src << "';"

    @newline_pending = 0
  end
end

#flush_newline_if_pending(src) ⇒ Object


73
74
75
76
77
78
# File 'lib/nyara/view_handlers/erubis.rb', line 73

def flush_newline_if_pending(src)
  if @newline_pending > 0
    src << "_erbout.safe_append='#{"\n" * @newline_pending}';"
    @newline_pending = 0
  end
end