Module: Upkeep::Targeting::Extraction

Defined in:
lib/upkeep/targeting.rb

Class Method Summary collapse

Class Method Details

.css_escape(value) ⇒ Object



120
121
122
# File 'lib/upkeep/targeting.rb', line 120

def css_escape(value)
  value.to_s.gsub("\\", "\\\\\\").gsub('"', '\"')
end

.digest_html(html) ⇒ Object



112
113
114
# File 'lib/upkeep/targeting.rb', line 112

def digest_html(html)
  Digest::SHA256.hexdigest(normalize_html(html))
end

.extract_target_html(html, target) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/upkeep/targeting.rb', line 88

def extract_target_html(html, target)
  fragment = parse_html(html)
  node = node_for(fragment, target)

  raise "target not found in full rerender: #{target.inspect}" unless node

  node.to_html
end

.frame_id_for(target) ⇒ Object



116
117
118
# File 'lib/upkeep/targeting.rb', line 116

def frame_id_for(target)
  target.kind == "render_site" ? "site:#{target.id}" : target.id
end

.node_for(fragment, target) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/upkeep/targeting.rb', line 97

def node_for(fragment, target)
  case target.kind
  when "page"
    fragment.at_css(%([data-upkeep-page-frame="#{css_escape(target.id)}"]))
  when "fragment"
    fragment.at_css(%([data-upkeep-frame="#{css_escape(target.id)}"]))
  when "render_site"
    fragment.at_css(%(upkeep-render-site[data-upkeep-render-site="#{css_escape(target.id)}"]))
  end
end

.normalize_html(html) ⇒ Object



108
109
110
# File 'lib/upkeep/targeting.rb', line 108

def normalize_html(html)
  parse_html(html).to_html
end

.parse_html(html) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/upkeep/targeting.rb', line 124

def parse_html(html)
  source = html.to_s

  if source.match?(/\A\s*(?:<!doctype\b[^>]*>\s*)?<html[\s>]/i)
    Nokogiri::HTML5.parse(source)
  else
    Nokogiri::HTML5.fragment(source)
  end
end

.patches_from_full_rerender(full_html, targets) ⇒ Object



84
85
86
# File 'lib/upkeep/targeting.rb', line 84

def patches_from_full_rerender(full_html, targets)
  targets.map { |target| Patch.new(target, extract_target_html(full_html, target)) }
end