Sha256: 3cb4577c5b1bff87b4bd10ba23eb0fb951322c4a7872b13a77085c9796df9667

Contents?: true

Size: 1.52 KB

Versions: 37

Compression:

Stored size: 1.52 KB

Contents

require 'uri'

module HTML
  class Pipeline

    class AbsoluteSourceFilter < Filter
      # 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.
      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
      
      # Private: the base url you want to use
      def image_base_url
        context[:image_base_url] or raise "Missing context :image_base_url for #{self.class.name}"
      end

      # Private: the relative url you want to use
      def image_subpage_url
        context[:image_subpage_url] or raise "Missing context :image_subpage_url for #{self.class.name}"
      end
    
    end
  end
end

Version data entries

37 entries across 37 versions & 2 rubygems

Version Path
html-pipeline-1.6.0 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-1.5.0 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-1.4.0 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-1.3.0 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-1.1.0 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-1.0.0 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-0.3.1 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-0.3.0 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-0.2.1 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-0.2.0 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-0.1.0 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-0.0.14 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-0.0.13 lib/html/pipeline/absolute_source_filter.rb
geothird-html-pipeline-0.0.12 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-0.0.12 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-0.0.11 lib/html/pipeline/absolute_source_filter.rb
html-pipeline-0.0.10 lib/html/pipeline/absolute_source_filter.rb