Class: Synvert::Core::Engine::Erubis

Inherits:
Erubis::Eruby
  • Object
show all
Defined in:
lib/synvert/core/engine/erb.rb

Overview

borrowed from rails

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#add_expr(src, code, indicator) ⇒ Object

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



87
88
89
90
91
92
93
94
# File 'lib/synvert/core/engine/erb.rb', line 87

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

#add_expr_escaped(src, code) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/synvert/core/engine/erb.rb', line 107

def add_expr_escaped(src, code)
  flush_newline_if_pending(src)
  if code =~ BLOCK_EXPR
    src << '@output_buffer.safe_append= ' << code << ERUBY_EXPR_SPLITTER
  else
    src << '@output_buffer.safe_append=(' << code << ');' << ERUBY_EXPR_SPLITTER
  end
end

#add_expr_literal(src, code) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/synvert/core/engine/erb.rb', line 98

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

#add_postamble(src) ⇒ Object



134
135
136
137
# File 'lib/synvert/core/engine/erb.rb', line 134

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

#add_preamble(src) ⇒ Object



65
66
67
68
# File 'lib/synvert/core/engine/erb.rb', line 65

def add_preamble(src)
  @newline_pending = 0
  src << '@output_buffer = output_buffer || ActionView::OutputBuffer.new;'
end

#add_stmt(src, code) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/synvert/core/engine/erb.rb', line 116

def add_stmt(src, code)
  flush_newline_if_pending(src)
  if code != "\n" && code != ''
    index =
      case code
      when /\A(\s*)\r?\n/
        $1.length
      when /\A(\s+)/
        $1.end_with?(' ') ? $1.length - 1 : $1.length
      else
        0
      end
    code.insert(index, ERUBY_STMT_SPLITTER)
    code.insert(-1, ERUBY_STMT_SPLITTER[0...-1])
  end
  super
end

#add_text(src, text) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/synvert/core/engine/erb.rb', line 70

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

#flush_newline_if_pending(src) ⇒ Object



139
140
141
142
143
144
# File 'lib/synvert/core/engine/erb.rb', line 139

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