Class: Html2rss::Rendering::DescriptionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/rendering/description_builder.rb

Overview

Builds a sanitized article description from the base text, title, and optional media.

Combines media elements (images, audio, video, PDFs) with sanitized text content to create rich RSS descriptions that reveal more scraped information.

Examples:

Basic usage

builder = DescriptionBuilder.new(
  base: "Article content",
  title: "Article Title",
  url: "https://example.com",
  enclosures: [enclosure_object],
  image: "https://example.com/image.jpg"
)
description = builder.call

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base:, title:, url:, enclosures:, image:) ⇒ DescriptionBuilder

Returns a new instance of DescriptionBuilder.

Parameters:

  • base (String)

    The base text content for the description

  • title (String)

    The article title (used for alt text and title removal)

  • url (String, Html2rss::Url)

    The article URL (used for sanitization)

  • enclosures (Array<Html2rss::RssBuilder::Enclosure>, nil)

    Media enclosures

  • image (String, Html2rss::Url, nil)

    Fallback image URL



41
42
43
44
45
46
47
# File 'lib/html2rss/rendering/description_builder.rb', line 41

def initialize(base:, title:, url:, enclosures:, image:)
  @base = base.to_s
  @title = title
  @url = url
  @enclosures = Array(enclosures)
  @image = image
end

Class Method Details

.remove_pattern_from_start(text, pattern, end_of_range: (text.size * 0.5).to_i) ⇒ String

Removes the specified pattern from the beginning of the text within a given range if the pattern occurs before the range’s end.

Parameters:

  • text (String)
  • pattern (String)
  • end_of_range (Integer) (defaults to: (text.size * 0.5).to_i)

    Optional, defaults to half the text length

Returns:

  • (String)


27
28
29
30
31
32
33
34
# File 'lib/html2rss/rendering/description_builder.rb', line 27

def self.remove_pattern_from_start(text, pattern, end_of_range: (text.size * 0.5).to_i)
  return text unless text.is_a?(String) && pattern.is_a?(String)

  index = text.index(pattern)
  return text if index.nil? || index >= end_of_range

  text.gsub(/^(.{0,#{end_of_range}})#{Regexp.escape(pattern)}/, '\1')
end

Instance Method Details

#callString?

Generates the complete description with media and sanitized text.

Returns:

  • (String, nil)

    The complete description or nil if empty



52
53
54
55
56
57
58
59
# File 'lib/html2rss/rendering/description_builder.rb', line 52

def call
  fragments = []
  fragments.concat(Array(rendered_media))
  fragments << processed_base_description

  result = fragments.compact.join("\n\n").strip
  result.empty? ? nil : result
end