class ErbEscape::Encoder

Responsible for escaping erb templates.

Constants

REGEXP

The same Regexp used by Erubi, the default erb engine in Rails

Public Class Methods

new(str) click to toggle source
# File lib/erb_escape/encoder.rb, line 11
def initialize(str)
  @str = str
end

Public Instance Methods

encode() click to toggle source
# File lib/erb_escape/encoder.rb, line 15
def encode
  @str.gsub(REGEXP) do
    encode_tag(*Regexp.last_match.to_a)
  end
end

Private Instance Methods

encode_tag(_match, indicator, code, _tailch, rspace) click to toggle source
# File lib/erb_escape/encoder.rb, line 23
def encode_tag(_match, indicator, code, _tailch, rspace)
  start_tag = START_TAGS[indicator.to_s]
  end_tag = END_TAGS[indicator.to_s]
  escaped_code = CGI.escapeHTML(code)
  "#{start_tag}#{escaped_code}#{end_tag}#{rspace}"
end