Class: Html2rss::Rendering::DescriptionBuilder
- Inherits:
-
Object
- Object
- Html2rss::Rendering::DescriptionBuilder
- 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.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#call ⇒ String?
Generates the complete description with media and sanitized text.
-
#initialize(base:, title:, url:, enclosures:, image:) ⇒ DescriptionBuilder
constructor
A new instance of DescriptionBuilder.
Constructor Details
#initialize(base:, title:, url:, enclosures:, image:) ⇒ DescriptionBuilder
Returns a new instance of DescriptionBuilder.
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.
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
#call ⇒ String?
Generates the complete description with media and sanitized text.
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 |