module PDFKit::HTMLPreprocessor

Public Class Methods

process(html, root_url, protocol) click to toggle source

Change relative paths to absolute, and relative protocols to absolute protocols

# File lib/pdfkit/html_preprocessor.rb, line 7
def self.process(html, root_url, protocol)
  html = translate_relative_paths(html, root_url) if root_url
  html = translate_relative_protocols(html, protocol) if protocol
  html
end

Private Class Methods

translate_relative_paths(html, root_url) click to toggle source
# File lib/pdfkit/html_preprocessor.rb, line 15
def self.translate_relative_paths(html, root_url)
  # Try out this regexp using rubular http://rubular.com/r/hiAxBNX7KE
  html.gsub(/(href|src)=(['"])\/([^\/"']([^\"']*|[^"']*))?['"]/, "\\1=\\2#{root_url}\\3\\2")
end
translate_relative_protocols(body, protocol) click to toggle source
# File lib/pdfkit/html_preprocessor.rb, line 20
def self.translate_relative_protocols(body, protocol)
  # Try out this regexp using rubular http://rubular.com/r/0Ohk0wFYxV
  body.gsub(/(href|src)=(['"])\/\/([^\"']*|[^"']*)['"]/, "\\1=\\2#{protocol}://\\3\\2")
end