Sha256: 91bac5f82c6b6ffc5538cc23def27c957f3473c31cb678ce73df30990786e633

Contents?: true

Size: 845 Bytes

Versions: 4

Compression:

Stored size: 845 Bytes

Contents

# frozen_string_literal: true

module BootstrapEmail
  module Converter
    class SupportUrlTokens < Base
      OPEN_BRACKETS = CGI.escape('{{').freeze
      CLOSE_BRACKETS = CGI.escape('}}').freeze

      def self.replace(html)
        regex = /((href|src)=("|'))(.*?((#{Regexp.quote(OPEN_BRACKETS)}).*?(#{Regexp.quote(CLOSE_BRACKETS)})).*?)("|')/
        return unless regex.match?(html)

        inner_regex = /((#{Regexp.quote(OPEN_BRACKETS)}).*?(#{Regexp.quote(CLOSE_BRACKETS)}))/

        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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bootstrap-email-1.3.1 lib/bootstrap-email/converters/support_url_tokens.rb
bootstrap-email-1.3.0 lib/bootstrap-email/converters/support_url_tokens.rb
bootstrap-email-1.2.0 lib/bootstrap-email/converters/support_url_tokens.rb
bootstrap-email-1.1.7 lib/bootstrap-email/converters/support_url_tokens.rb