Class: Html2rss::SST::Node
- Inherits:
-
Data
- Object
- Data
- Html2rss::SST::Node
- Defined in:
- lib/html2rss/sst/node.rb
Overview
Immutable simplified semantic tree node. Predicates and traversal live on the type so Scoring/Segmenter never touch Nokogiri.
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#own_text ⇒ Object
readonly
Returns the value of attribute own_text.
-
#tag_path ⇒ Object
readonly
Returns the value of attribute tag_path.
Class Method Summary collapse
Instance Method Summary collapse
-
#descendants ⇒ Array<Node>
All descendants excluding self.
-
#each_node {|node| ... } ⇒ Enumerator, Array<Node>
Depth-first enumeration of self and descendants.
- #eligible_href? ⇒ Boolean
- #find(&predicate) ⇒ Node?
- #find_all(&predicate) ⇒ Array<Node>
- #heading? ⇒ Boolean
- #ignored_container_name? ⇒ Boolean
- #image? ⇒ Boolean
- #link? ⇒ Boolean
-
#text_density ⇒ Float
Words per descendant link.
- #utility_landmark? ⇒ Boolean
-
#visible_text(separator: ' ', exclude: nil) ⇒ String?
Visible text with block newlines, excluding optional subtrees.
-
#word_count ⇒ Integer
Alphanumeric word count of visible text.
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs
8 9 10 |
# File 'lib/html2rss/sst/node.rb', line 8 def attrs @attrs end |
#children ⇒ Object (readonly)
Returns the value of attribute children
8 9 10 |
# File 'lib/html2rss/sst/node.rb', line 8 def children @children end |
#name ⇒ Object (readonly)
Returns the value of attribute name
8 9 10 |
# File 'lib/html2rss/sst/node.rb', line 8 def name @name end |
#own_text ⇒ Object (readonly)
Returns the value of attribute own_text
8 9 10 |
# File 'lib/html2rss/sst/node.rb', line 8 def own_text @own_text end |
#tag_path ⇒ Object (readonly)
Returns the value of attribute tag_path
8 9 10 |
# File 'lib/html2rss/sst/node.rb', line 8 def tag_path @tag_path end |
Class Method Details
.build(name:, attrs: nil, own_text: nil, children: nil, tag_path: nil) ⇒ Node
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/html2rss/sst/node.rb', line 17 def self.build(name:, attrs: nil, own_text: nil, children: nil, tag_path: nil) attrs_value = coerce_attrs(attrs) kids = coerce_children(children) new( name: coerce_name(name), attrs: attrs_value, own_text: own_text.to_s.freeze, children: kids, tag_path: tag_path.to_s.freeze ) end |
Instance Method Details
#descendants ⇒ Array<Node>
Returns all descendants excluding self.
98 99 100 |
# File 'lib/html2rss/sst/node.rb', line 98 def descendants children.flat_map { |child| [child, *child.descendants] } end |
#each_node {|node| ... } ⇒ Enumerator, Array<Node>
Depth-first enumeration of self and descendants.
89 90 91 92 93 94 |
# File 'lib/html2rss/sst/node.rb', line 89 def each_node(&block) return enum_for(:each_node) unless block yield self children.each { |child| child.each_node(&block) } end |
#eligible_href? ⇒ Boolean
77 78 79 80 81 82 |
# File 'lib/html2rss/sst/node.rb', line 77 def eligible_href? href = attrs.href return false if href.nil? || href.empty? Tags::SKIP_HREF_PREFIXES.none? { |prefix| href.start_with?(prefix) } end |
#find(&predicate) ⇒ Node?
110 111 112 |
# File 'lib/html2rss/sst/node.rb', line 110 def find(&predicate) each_node.find(&predicate) end |
#find_all(&predicate) ⇒ Array<Node>
104 105 106 |
# File 'lib/html2rss/sst/node.rb', line 104 def find_all(&predicate) each_node.select(&predicate) end |
#heading? ⇒ Boolean
65 |
# File 'lib/html2rss/sst/node.rb', line 65 def heading? = Tags::HEADING_NAMES.include?(name) |
#ignored_container_name? ⇒ Boolean
73 |
# File 'lib/html2rss/sst/node.rb', line 73 def ignored_container_name? = Tags::IGNORED_CONTAINER_NAMES.include?(name) |
#image? ⇒ Boolean
61 |
# File 'lib/html2rss/sst/node.rb', line 61 def image? = name == :img |
#link? ⇒ Boolean
55 56 57 |
# File 'lib/html2rss/sst/node.rb', line 55 def link? name == :a && eligible_href? end |
#text_density ⇒ Float
Returns words per descendant link.
140 141 142 143 144 |
# File 'lib/html2rss/sst/node.rb', line 140 def text_density words = word_count links = descendants.count(&:link?) links.zero? ? words.to_f : words.to_f / links end |
#utility_landmark? ⇒ Boolean
69 |
# File 'lib/html2rss/sst/node.rb', line 69 def utility_landmark? = Tags::UTILITY_LANDMARK_NAMES.include?(name) |
#visible_text(separator: ' ', exclude: nil) ⇒ String?
Visible text with block newlines, excluding optional subtrees.
120 121 122 123 124 125 126 |
# File 'lib/html2rss/sst/node.rb', line 120 def visible_text(separator: ' ', exclude: nil) if exclude.nil? && separator == ' ' && (index = Index.for_node(self)) return index.memo_visible_text(self) end Text.extract(self, separator:, exclude:) end |