Class: Html2rss::SST::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/sst/normalizer.rb

Overview

Sole Nokogiri consumer on the heuristic auto-source path. Builds an immutable SST::Document with parent/depth/chrome indices.

Defined Under Namespace

Classes: EmptyTree

Constant Summary collapse

MAX_NODES =

Hard ceiling for SST node allocations; beyond this we degrade.

5_000
STRIPPED_TAGS =

Tags stripped entirely from the SST.

Set['script', 'style', 'noscript', 'iframe', 'svg', 'template'].freeze
SEMANTIC_DEGRADE_TAGS =

Kept when MAX_NODES forces semantic-tag-only degrade.

Set['html', 'body', 'article', 'section', 'li', 'tr', 'div', 'a',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'time', 'img', 'p',
'ul', 'ol', 'main'].freeze
RAW_ATTR_KEEP =

Attribute names preserved in Attrs#raw for extraction.

/
  \A(
    data- |
    aria- |
    class |
    id |
    href |
    src |
    srcset |
    style |
    datetime |
    itemprop |
    type |
    category |
    categories |
    tag |
    tags |
    topic |
    topics |
    section |
    sections |
    label |
    labels |
    theme |
    themes |
    subject |
    subjects
  )
/xi
TYPED_ATTR_NAMES =

Typed Attrs fields excluded from the leftover raw hash.

%w[href src id class datetime itemprop style srcset type].to_set.freeze
IGNORED_CONTAINER_TAGS =

String form of Tags::IGNORED_CONTAINER_NAMES for chrome checks without to_sym.

Tags::IGNORED_CONTAINER_NAMES.to_set(&:to_s).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed_body) ⇒ Normalizer

Returns a new instance of Normalizer.

Parameters:

  • parsed_body (Nokogiri::HTML::Document, Nokogiri::XML::Node)


89
90
91
92
93
94
95
96
# File 'lib/html2rss/sst/normalizer.rb', line 89

def initialize(parsed_body)
  @parsed_body = parsed_body
  @node_count = 0
  @degraded = false
  @parents = {}.compare_by_identity
  @depths = {}.compare_by_identity
  @ignored_chrome = {}.compare_by_identity
end

Class Method Details

.call(input) ⇒ Document

Parameters:

  • input (String, Nokogiri::HTML::Document, Nokogiri::XML::Node)

    HTML string or already-parsed node (String is parsed once here)

Returns:

Raises:

  • (ArgumentError)

    when input is nil or unsupported



68
69
70
71
72
# File 'lib/html2rss/sst/normalizer.rb', line 68

def call(input)
  raise ArgumentError, 'input is required' if input.nil?

  new(coerce(input)).call
end

Instance Method Details

#callDocument

Returns:

Raises:



100
101
102
103
104
105
106
107
# File 'lib/html2rss/sst/normalizer.rb', line 100

def call
  root_nk = resolve_root_nk
  root = normalize_element(root_nk, parent: nil, depth: 0, path: '', chrome: false)
  raise EmptyTree, 'SST Normalizer produced an empty tree' unless root

  index = Index.new(root:, parents: @parents, depths: @depths, ignored_chrome: @ignored_chrome)
  Document.build(root:, index:, degraded: @degraded, node_count: @node_count)
end