Class: HTML::Pipeline::AbsoluteSourceFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/html/pipeline/absolute_source_filter.rb

Instance Attribute Summary

Attributes inherited from Filter

#context, #result

Instance Method Summary collapse

Methods inherited from Filter

#base_url, call, #current_user, #doc, #has_ancestor?, #html, #initialize, #needs, #parse_html, #repository, #search_text_nodes, to_document, to_html, #validate

Constructor Details

This class inherits a constructor from HTML::Pipeline::Filter

Instance Method Details

#callObject

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. This filter would need to be run before CamoFilter.

[View source]

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/html/pipeline/absolute_source_filter.rb', line 20

def call
  doc.search("img").each do |element| 
    next if element['src'].nil? || element['src'].empty?
    src = element['src'].strip
    unless src.start_with? 'http'
      if src.start_with? '/'
        base = image_base_url
      else
        base = image_subpage_url
      end
      element["src"] = URI.join(base, src).to_s
    end
  end
  doc
end

#image_base_urlObject

Private: the base url you want to use

[View source]

37
38
39
# File 'lib/html/pipeline/absolute_source_filter.rb', line 37

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

#image_subpage_urlObject

Private: the relative url you want to use

[View source]

42
43
44
# File 'lib/html/pipeline/absolute_source_filter.rb', line 42

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