Class: HTMLPipeline::NodeFilter::AbsoluteSourceFilter

Inherits:
HTMLPipeline::NodeFilter show all
Defined in:
lib/html_pipeline/node_filter/absolute_source_filter.rb

Overview

HTML Filter for replacing relative and root relative image URLs with fully qualified URLs

This is useful if an image is root relative but should really be going through a cdn, or if the content for the page assumes the host is known i.e. scraped webpages and some RSS feeds.

Context options:

:image_base_url - Base URL for image host for root relative src.
:image_subpage_url - For relative src.

This filter does not write additional information to the context. Note: This filter would need to be run before AssetProxyFilter.

Constant Summary collapse

SELECTOR =
Selma::Selector.new(match_element: "img")

Instance Attribute Summary

Attributes inherited from HTMLPipeline::NodeFilter

#context

Attributes inherited from Filter

#context, #result

Instance Method Summary collapse

Methods inherited from HTMLPipeline::NodeFilter

call, #html, #initialize, #reset!

Methods inherited from Filter

#base_url, #call, call, #has_ancestor?, #initialize, #needs, #validate

Constructor Details

This class inherits a constructor from HTMLPipeline::NodeFilter

Instance Method Details

#handle_element(element) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/html_pipeline/node_filter/absolute_source_filter.rb', line 27

def handle_element(element)
  src = element["src"]
  return if src.nil? || src.empty?

  src = src.strip
  return if src.start_with?("http")

  base = if src.start_with?("/")
    image_base_url
  else
    image_subpage_url
  end

  element["src"] = URI.join(base, src).to_s
end

#image_base_urlObject

Private: the base url you want to use



44
45
46
# File 'lib/html_pipeline/node_filter/absolute_source_filter.rb', line 44

def image_base_url
  context[:image_base_url] || raise("Missing context :image_base_url for #{self.class.name}")
end

#image_subpage_urlObject

Private: the relative url you want to use



49
50
51
# File 'lib/html_pipeline/node_filter/absolute_source_filter.rb', line 49

def image_subpage_url
  context[:image_subpage_url] || raise("Missing context :image_subpage_url for #{self.class.name}")
end

#selectorObject



23
24
25
# File 'lib/html_pipeline/node_filter/absolute_source_filter.rb', line 23

def selector
  SELECTOR
end