Class: Staticfy::Handlers::HTML

Inherits:
Base
  • Object
show all
Defined in:
lib/staticfy/handlers/html.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize, #local_uri, #method_missing

Constructor Details

This class inherits a constructor from Staticfy::Handlers::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Staticfy::Handlers::Base

Instance Method Details

#attribute_iterator(document, tag, attr) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/staticfy/handlers/html.rb', line 71

def attribute_iterator(document, tag, attr)
  document.search("//#{tag}[@#{attr}]").each do |tag|
    a = tag[attr]
    next if a.nil? or a.empty?
    abs = to_absolute(URI(a)) rescue next
    if in_domain?(abs)
      yield abs, tag
    end
  end
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/staticfy/handlers/html.rb', line 24

def fetch_links
  return [] unless doc

  links = []

  # follow links
  attribute_iterator(doc, "a", "href") do |abs, tag|
    links << abs
  end

  # fetch scripts
  attribute_iterator(doc, "script", "src") do |abs, tag|
    links << abs
  end

  # follow styles
  attribute_iterator(doc, "link", "href") do |abs, tag|
    links << abs
  end

  # images
  attribute_iterator(doc, "img", "src") do |abs, tag|
    links << abs
  end

  attribute_iterator(doc, "input", "src") do |abs, tag|
    links << abs
  end

  links.uniq!
  links
end

#local_bodyObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/staticfy/handlers/html.rb', line 57

def local_body
  return body unless doc

  html = doc.dup

  update_links(html, "a", "href")
  update_links(html, "script", "src")
  update_links(html, "link", "href")
  update_links(html, "img", "src")
  update_links(html, "input", "src")

  html.to_s
end


82
83
84
85
86
# File 'lib/staticfy/handlers/html.rb', line 82

def update_links(document, tag, attr)
  attribute_iterator(document, tag, attr) do |abs, tag|
    tag[attr] = Staticfy::Handlers.local_uri(abs)
  end
end