Sha256: 9747df5822679824f7c5fd09f379f4a78cea1d811079b10c2e64b0bee5221f2b

Contents?: true

Size: 818 Bytes

Versions: 7

Compression:

Stored size: 818 Bytes

Contents

class PDFKit
  module HTMLPreprocessor

    # Change relative paths to absolute, and relative protocols to absolute protocols
    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

    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

    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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pdfkit-0.8.5 lib/pdfkit/html_preprocessor.rb
pdfkit-0.8.4.3.2 lib/pdfkit/html_preprocessor.rb
pdfkit-0.8.4.3.1 lib/pdfkit/html_preprocessor.rb
pdfkit-0.8.4.2 lib/pdfkit/html_preprocessor.rb
pdfkit-0.8.4.1 lib/pdfkit/html_preprocessor.rb
pdfkit-0.8.4 lib/pdfkit/html_preprocessor.rb
pdfkit-0.8.3 lib/pdfkit/html_preprocessor.rb