Class: Erbse::RubyGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/erbse/eruby.rb

Constant Summary collapse

BLOCK_EXPR =

def add_expr_literal(src, code)

src << " #{@bufvar} << (" << code << ').to_s;'

end

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

Instance Method Summary collapse

Instance Method Details

#____add_stmt(src, code) ⇒ Object

Erubis toggles <%= and <%== behavior when escaping is enabled. We override to always treat <%== as escaped. def add_expr(src, code, indicator)

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

end



54
55
56
57
# File 'lib/erbse/eruby.rb', line 54

def ____add_stmt(src, code)
  src << code
  src << ';' unless code[-1] == ?\n
end

#add_expr_debug(src, code) ⇒ Object

def add_expr_escaped(src, code)

src << " #{@bufvar} << " << escaped_expr(code) << ';'

end



91
92
93
94
95
# File 'lib/erbse/eruby.rb', line 91

def add_expr_debug(src, code)
  code.strip!
  s = (code.dump =~ /\A"(.*)"\z/) && $1
  src << ' $stderr.puts("*** debug: ' << s << '=#{(' << code << ').inspect}");'
end

#add_expr_escaped(src, code) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/erbse/eruby.rb', line 78

def add_expr_escaped(src, code)
  flush_newline_if_pending(src)
  if code =~ BLOCK_EXPR
    src << "@output_buffer.safe_expr_append= " << code
  else
    src << "@output_buffer.safe_expr_append=(" << code << ");"
  end
end

#add_expr_literal(src, code) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/erbse/eruby.rb', line 69

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

#add_postamble(src) ⇒ Object

– def add_postamble(src)

src << "\n#{@bufvar}.join\n"

end ++



102
103
104
105
# File 'lib/erbse/eruby.rb', line 102

def add_postamble(src)
  flush_newline_if_pending(src)
  src << '@output_buffer.to_s'
end

#add_preamble(src) ⇒ Object

– def add_preamble(src)

src << "#{@bufvar} = [];"

end ++



20
21
22
23
# File 'lib/erbse/eruby.rb', line 20

def add_preamble(src)
  @newline_pending = 0
  src << "@output_buffer = output_buffer;" # DISCUSS: i removed the output_buffer || ActionView::OB.new rubbish here.
end

#add_stmt(src, code) ⇒ Object



59
60
61
62
# File 'lib/erbse/eruby.rb', line 59

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

#add_text(src, text) ⇒ Object

def add_text(src, text)

src << " #{@bufvar} << '" << escape_text(text) << "';" unless text.empty?

end



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/erbse/eruby.rb', line 28

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

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

    @newline_pending = 0
  end
end

#escape_text(text) ⇒ Object



7
8
9
# File 'lib/erbse/eruby.rb', line 7

def escape_text(text)
  text.gsub(/['\\]/, '\\\\\&')   # "'" => "\\'",  '\\' => '\\\\'
end

#escaped_expr(code) ⇒ Object



11
12
13
# File 'lib/erbse/eruby.rb', line 11

def escaped_expr(code)
  return "#{@escapefunc}(#{code})"
end

#flush_newline_if_pending(src) ⇒ Object



107
108
109
110
111
112
# File 'lib/erbse/eruby.rb', line 107

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

#init_generator(properties = {}) ⇒ Object



3
4
5
# File 'lib/erbse/eruby.rb', line 3

def init_generator(properties={})
  @escapefunc ||= "Erubis::XmlHelper.escape_xml"
end