class Octodown::Support::RelativeRootFilter

Attributes

root[RW]
server[RW]

Public Instance Methods

call() click to toggle source
# File lib/octodown/support/relative_root_filter.rb, line 10
def call
  @root = context[:original_document_root]
  @server = context[:server]

  filter_images doc.search('img')
  filter_links doc.search('a[href]')
end

Private Instance Methods

filter_images(images) click to toggle source

TODO: These two methods are highly similar and can be refactored, but

I'm can't find the right abstraction at the moment that isn't a total
hack involving bizarre object references and mutation
# File lib/octodown/support/relative_root_filter.rb, line 38
def filter_images(images)
  images.each do |img|
    src = img['src']

    next if src.nil?

    src.strip!

    unless http_uri? src
      path = relative_path_from_document_root root, src
      img['src'] = path
    end
  end

  doc
end
http_uri?(src) click to toggle source
# File lib/octodown/support/relative_root_filter.rb, line 24
def http_uri?(src)
  parsed_uri = begin
    URI.parse src
               rescue URI::InvalidURIError
                 src
  end

  parsed_uri.is_a? URI::HTTP
end
relative_path_from_document_root(root, src) click to toggle source
# File lib/octodown/support/relative_root_filter.rb, line 20
def relative_path_from_document_root(root, src)
  server ? src : File.join(root, src).to_s
end