Class: Html2rss::AutoSource::Discovery::SemanticContainers

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/auto_source/discovery/semantic_containers.rb

Overview

Collects semantic content containers from a parsed HTML document.

Constant Summary collapse

SELECTORS =

Candidate selectors used to locate extractable semantic content blocks.

[
  'article:not(:has(article))',
  'section:not(:has(section))',
  'li:not(:has(li))',
  'tr:not(:has(tr))',
  'div:not(:has(div))'
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed_body) ⇒ SemanticContainers

Returns a new instance of SemanticContainers.

Parameters:

  • parsed_body (Nokogiri::HTML::Document)

    parsed document



25
26
27
# File 'lib/html2rss/auto_source/discovery/semantic_containers.rb', line 25

def initialize(parsed_body)
  @parsed_body = parsed_body
end

Class Method Details

.call(parsed_body) ⇒ Array<Nokogiri::XML::Node>

Returns candidate semantic containers.

Parameters:

  • parsed_body (Nokogiri::HTML::Document)

    parsed document

Returns:

  • (Array<Nokogiri::XML::Node>)

    candidate semantic containers



20
21
22
# File 'lib/html2rss/auto_source/discovery/semantic_containers.rb', line 20

def self.call(parsed_body)
  new(parsed_body).call
end

Instance Method Details

#callArray<Nokogiri::XML::Node>

Returns candidate semantic containers.

Returns:

  • (Array<Nokogiri::XML::Node>)

    candidate semantic containers



30
31
32
33
34
35
36
37
38
# File 'lib/html2rss/auto_source/discovery/semantic_containers.rb', line 30

def call
  cache = {}.compare_by_identity
  candidates = @parsed_body.css(SELECTORS.join(',')).reject do |node|
    Html2rss::Html::Navigator.ignored_container_path?(node, cache)
  end

  candidates = filter_nested_containers(candidates)
  sort_by_depth(candidates)
end