Class: Detergent::ContentLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/detergent/content_locator.rb

Overview

Locates the main content node within a document's body: finds the highest-scoring node in the tree, then walks up its ancestors to find the best root container for the content.

Constant Summary collapse

SEMANTIC_CONTAINERS =
%w[article main section].freeze
PARENT_PROMOTION_RATIO =

A parent must beat the best root found so far by this ratio to be promoted — not just score marginally higher from including the child.

1.3

Instance Method Summary collapse

Constructor Details

#initialize(scorer) ⇒ ContentLocator

Returns a new instance of ContentLocator.



14
15
16
# File 'lib/detergent/content_locator.rb', line 14

def initialize(scorer)
  @scorer = scorer
end

Instance Method Details

#locate(body) ⇒ Object



18
19
20
21
22
23
# File 'lib/detergent/content_locator.rb', line 18

def locate(body)
  highest_scoring = find_highest_scoring_node(body)
  return nil unless highest_scoring

  find_content_root(highest_scoring)
end