Class: Html2rss::Html::SstArticleExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/html/sst_article_extractor.rb

Overview

Builds an Article from an SST segment / ranked segment. Port of Html::ArticleExtractor field logic onto SST::Node. rubocop:disable Metrics/ClassLength -- SST field extractors colocated for parity with ArticleExtractor

Constant Summary collapse

KICKER_CLASS_PATTERN =

CSS class tokens that mark kicker / eyebrow text (excluded from titles).

/kicker|eyebrow|pre-title|pretitle|overline/i
FALLBACK_HEADING_NAMES =

Inline emphasis tags used as title fallbacks when no heading exists.

%i[strong b].freeze
CATEGORY_CONTAINER_NAMES =

Nested blocks that mean a category node is actually a content container.

%i[p article section].to_set.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ranked_or_segment, base_url:, scraper: nil, fallback_anchorless: false, time_zone: 'UTC') ⇒ SstArticleExtractor

Returns a new instance of SstArticleExtractor.

Parameters:

  • ranked_or_segment (Scoring::RankedSegment, AutoSource::Segment)
  • base_url (String, Html2rss::Url)
  • scraper (Class, nil) (defaults to: nil)
  • fallback_anchorless (Boolean) (defaults to: false)
  • time_zone (String) (defaults to: 'UTC')

    channel time zone for naive leftover dates

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/html2rss/html/sst_article_extractor.rb', line 37

def initialize(ranked_or_segment, base_url:, scraper: nil, fallback_anchorless: false, time_zone: 'UTC')
  segment = ranked_or_segment.is_a?(Scoring::RankedSegment) ? ranked_or_segment.segment : ranked_or_segment
  raise ArgumentError, 'segment is required' unless segment.is_a?(AutoSource::Segment)

  @segment = segment
  @root = segment.root_node
  @selected_anchor = segment.primary_link
  @base_url = base_url
  @scraper = scraper
  @fallback_anchorless = fallback_anchorless
  @time_zone = time_zone
end

Class Method Details

.call(ranked_or_segment, base_url:, scraper: nil, fallback_anchorless: false, time_zone: 'UTC') ⇒ Html2rss::Article?

Parameters:

  • ranked_or_segment (Scoring::RankedSegment, AutoSource::Segment)
  • base_url (String, Html2rss::Url)
  • scraper (Class, nil) (defaults to: nil)
  • fallback_anchorless (Boolean) (defaults to: false)
  • time_zone (String) (defaults to: 'UTC')

    channel time zone for naive leftover dates

Returns:



27
28
29
# File 'lib/html2rss/html/sst_article_extractor.rb', line 27

def call(ranked_or_segment, base_url:, scraper: nil, fallback_anchorless: false, time_zone: 'UTC')
  new(ranked_or_segment, base_url:, scraper:, fallback_anchorless:, time_zone:).call
end

Instance Method Details

#callHtml2rss::Article?

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/html2rss/html/sst_article_extractor.rb', line 52

def call # rubocop:disable Metrics/MethodLength
  title = extract_title
  lines = leftover_lines
  published_at = extract_published_at(lines)
  root, lines, published_at = parent_card_fields(title, lines, published_at)
  attrs = {
    title:,
    url: extract_url,
    image: extract_image,
    description: ArticleRules::Description.from_lines(lines, title:),
    id: generate_id,
    published_at:,
    enclosures: extract_enclosures,
    categories: extract_categories(title, root:),
    scraper: @scraper
  }
  article = Article.new(**attrs)
  article.valid? ? article : nil
end