Class: BootstrapEmail::Converter::SupportUrlTokens

Inherits:
Base
  • Object
show all
Defined in:
lib/bootstrap-email/converters/support_url_tokens.rb

Constant Summary collapse

OPEN_BRACKETS =
[CGI.escape('{{'), '{{', "#{CGI.escape('{')}%", '{%'].freeze
CLOSE_BRACKETS =
[CGI.escape('}}'), '}}', "%#{CGI.escape('}')}", '%}'].freeze

Instance Attribute Summary

Attributes inherited from Base

#doc

Class Method Summary collapse

Methods inherited from Base

build, #initialize

Constructor Details

This class inherits a constructor from BootstrapEmail::Converter::Base

Class Method Details

.closing_regexObject



30
31
32
# File 'lib/bootstrap-email/converters/support_url_tokens.rb', line 30

def self.closing_regex
  closing_regex = CLOSE_BRACKETS.map { |bracket| Regexp.quote(bracket) }.join('|')
end

.opening_regexObject



26
27
28
# File 'lib/bootstrap-email/converters/support_url_tokens.rb', line 26

def self.opening_regex
  opening_regex = OPEN_BRACKETS.map { |bracket| Regexp.quote(bracket) }.join('|')
end

.replace(html) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bootstrap-email/converters/support_url_tokens.rb', line 9

def self.replace(html)
  regex = /((href|src)=("|'))(.*?((#{opening_regex}).*?(#{closing_regex})).*?)("|')/
  return unless regex.match?(html)

  inner_regex = /((#{opening_regex}).*?(#{closing_regex}))/

  html.gsub!(regex) do |_match|
    start_text = Regexp.last_match(1)
    middle_text = Regexp.last_match(4)
    end_text = Regexp.last_match(8)
    middle_text.gsub!(inner_regex) do |match|
      CGI.unescape(match)
    end
    "#{start_text}#{middle_text}#{end_text}"
  end
end