Class: Html2rss::Html::ArticleExtractor::ImageExtractor
- Inherits:
-
Object
- Object
- Html2rss::Html::ArticleExtractor::ImageExtractor
- Defined in:
- lib/html2rss/html/article_extractor/image_extractor.rb
Overview
Image is responsible for extracting image URLs the article_tag.
Class Method Summary collapse
-
.call(article_tag, base_url:) ⇒ Html2rss::Url?
Best candidate image URL.
-
.from_img(article_tag) ⇒ String?
Src attribute from first matching image tag.
-
.from_source(article_tag) ⇒ String?
Extracts the largest image source from the srcset attribute of an img tag or a source tag inside a picture tag.
-
.from_style(article_tag) ⇒ String?
Best style-based background image URL.
Class Method Details
.call(article_tag, base_url:) ⇒ Html2rss::Url?
Returns best candidate image URL.
12 13 14 15 16 17 18 |
# File 'lib/html2rss/html/article_extractor/image_extractor.rb', line 12 def self.call(article_tag, base_url:) img_src = from_source(article_tag) || from_img(article_tag) || from_style(article_tag) Url.from_relative(img_src, base_url) if img_src end |
.from_img(article_tag) ⇒ String?
Returns src attribute from first matching image tag.
22 23 24 |
# File 'lib/html2rss/html/article_extractor/image_extractor.rb', line 22 def self.from_img(article_tag) article_tag.at_css('img[src]:not([src^="data"])')&.[]('src') end |
.from_source(article_tag) ⇒ String?
Extracts the largest image source from the srcset attribute of an img tag or a source tag inside a picture tag.
35 36 37 38 |
# File 'lib/html2rss/html/article_extractor/image_extractor.rb', line 35 def self.from_source(article_tag) srcsets = article_tag.css('img[srcset], picture > source[srcset]').map { |source| source['srcset'] } ArticleRules::Image.largest_from_srcsets(srcsets) end |
.from_style(article_tag) ⇒ String?
Returns best style-based background image URL.
42 43 44 45 |
# File 'lib/html2rss/html/article_extractor/image_extractor.rb', line 42 def self.from_style(article_tag) styles = article_tag.css('[style*="url"]').map { |tag| tag['style'] } ArticleRules::Image.best_from_styles(styles) end |