Sha256: f16a5b0907e06b9a68f269eb7f08e81567320ef2724414382b7775ac0c7e0e62

Contents?: true

Size: 911 Bytes

Versions: 2

Compression:

Stored size: 911 Bytes

Contents

class SiteDiff
class Sanitizer
class Regexp
  def initialize(rule)
    @rule = rule
  end

  def selector?
    false
  end

  def applies?(html, node)
    applies_to_string?(html)
  end

  def apply(html)
    gsub!(html)
  end

  def self.create(rule)
    rule['selector'] ? WithSelector.new(rule) : new(rule)
  end

  class WithSelector < Regexp
    def selector?
      true
    end

    def contexts(node)
      sels = @rule['selector']
      node.css(sels).each { |e| yield(e) }
    end

    def applies?(html, node)
      enum_for(:contexts, node).any? { |e| applies_to_string?(e.to_html) }
    end

    def apply(node)
      contexts(node) { |e| e.replace(gsub!(e.to_html)) }
    end
  end

protected
  def gsub!(str)
    re = ::Regexp.new(@rule['pattern'])
    sub = @rule['substitute'] || ''
    str.gsub!(re, sub)
    str
  end

  def applies_to_string?(str)
    gsub!(str.dup) != str
  end
end
end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sitediff-0.0.3 lib/sitediff/sanitize/regexp.rb
sitediff-0.0.2 lib/sitediff/sanitize/regexp.rb