Module: Html2rss::SST::Text
- Defined in:
- lib/html2rss/sst/text.rb
Overview
Visible-text extraction for SST nodes (port of Navigator::TextExtractor).
Class Method Summary collapse
Class Method Details
.extract(node, separator: ' ', exclude: nil) ⇒ String?
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/html2rss/sst/text.rb', line 15 def extract(node, separator: ' ', exclude: nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength raise ArgumentError, 'node is required' unless node excluded = exclude&.each_with_object({}.compare_by_identity) { |n, h| h[n] = true } if node.children.empty? text = node.own_text.to_s.gsub(/\s+/, ' ').strip return text.empty? ? nil : text end parts = collect_parts(node, separator, excluded) own = node.own_text.to_s.gsub(/\s+/, ' ').strip parts = [own, *parts] unless own.empty? return if parts.empty? parts.join.squeeze(' ').strip end |