Class: Foxy::Html
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Foxy::Html
show all
- Includes:
- Monads
- Defined in:
- lib/foxy/html.rb
Instance Method Summary
collapse
Methods included from Monads
#and_then, #dangerously, #many, #mapy, #normally, #optionaly, #safy, #then
Constructor Details
#initialize(html = nil) ⇒ Html
Returns a new instance of Html.
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/foxy/html.rb', line 14
def initialize(html = nil)
if html.nil?
super([])
elsif html.is_a? self.class
super(html.__getobj__)
elsif html.respond_to?(:to_str)
super(html.to_str.scan(RE_HTML).map { |args| Node.build(*args) })
elsif html.respond_to?(:read)
super(html.read.scan(RE_HTML).map { |args| Node.build(*args) })
else
super(html)
end
end
|
Instance Method Details
#attr(name) ⇒ Object
98
99
100
|
# File 'lib/foxy/html.rb', line 98
def attr(name)
first.attr(name)
end
|
#clean(**kws) ⇒ Object
28
29
30
|
# File 'lib/foxy/html.rb', line 28
def clean(**kws)
Html.new(map { |node| node.clean(**kws) })
end
|
90
91
92
|
# File 'lib/foxy/html.rb', line 90
def
each_with_object([]) { |node, acc| acc << node.content.sub(/^<!--/, "").sub(/-->$/, "") if node.type == :comment }
end
|
#css(query) ⇒ Object
73
74
75
|
# File 'lib/foxy/html.rb', line 73
def css(query)
Collection.new([self]).css(query)
end
|
#find(**kws) ⇒ Object
77
78
79
80
|
# File 'lib/foxy/html.rb', line 77
def find(**kws)
isearch(**kws) { |val| return val unless val.empty? }
nil
end
|
102
103
104
|
# File 'lib/foxy/html.rb', line 102
def id
first.id
end
|
#isearch(tagname: nil, id: nil, cls: nil, fun: nil, css: nil) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/foxy/html.rb', line 32
def isearch(tagname: nil, id: nil, cls: nil, fun: nil, css: nil)
cls = Array(cls)
tagname &&= tagname.downcase
y = 0
buff = []
close_tagname = nil
each do |node| if y.zero? && node.tag? && (!tagname || node.tagname! == tagname) &&
(!id || node.id! == id) && (cls - node.cls!).empty? &&
(!fun || fun.call(node))
close_tagname = node.tagname!
y += 1
elsif y && node.tag? && node.tagname! == close_tagname
y += 1
end
buff << node if y > 0
y -= 1 if y > 0 && node.closetag? && node.tagname! == close_tagname
next unless buff && y.zero?
yield Html.new(buff)
buff = []
close_tagname = nil
end
end
|
#joinedtexts ⇒ Object
94
95
96
|
# File 'lib/foxy/html.rb', line 94
def joinedtexts
texts.join.gsub(/[\r\n\s]+/, " ").strip
end
|
82
83
84
|
# File 'lib/foxy/html.rb', line 82
def rebuild
map(&:content).join
end
|
#search(**kws) ⇒ Object
66
67
68
69
70
71
|
# File 'lib/foxy/html.rb', line 66
def search(**kws)
return Collection.new([self]).search(kws) if kws[:css]
list = []
isearch(**kws) { |val| list << val unless val.empty? }
Collection.new(list)
end
|
86
87
88
|
# File 'lib/foxy/html.rb', line 86
def texts
each_with_object([]) { |node, acc| acc << node.content if node.type == :notag }
end
|
106
107
108
|
# File 'lib/foxy/html.rb', line 106
def to_s
rebuild
end
|
#try(m, *a, &b) ⇒ Object
10
11
12
|
# File 'lib/foxy/html.rb', line 10
def try(m, *a, &b)
public_send(m, *a, &b) if respond_to?(m)
end
|