Sha256: 3a97749079cde79da2214b2920c0887030866eeeb516d4b8b56dc89b6683c975

Contents?: true

Size: 1.06 KB

Versions: 13

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

class Grover
  #
  # Helper module for preparing HTML for conversion
  #
  # Sourced from the PDFKit project
  # @see https://github.com/pdfkit/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

    def self.translate_relative_paths(html, root_url)
      # Try out this regexp using rubular http://rubular.com/r/hiAxBNX7KE
      html.gsub(%r{(href|src)=(['"])/([^/"']([^\"']*|[^"']*))?['"]}, "\\1=\\2#{root_url}\\3\\2")
    end
    private_class_method :translate_relative_paths

    def self.translate_relative_protocols(body, protocol)
      # Try out this regexp using rubular http://rubular.com/r/0Ohk0wFYxV
      body.gsub(%r{(href|src)=(['"])//([^\"']*|[^"']*)['"]}, "\\1=\\2#{protocol}://\\3\\2")
    end
    private_class_method :translate_relative_protocols
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
grover-0.12.1 lib/grover/html_preprocessor.rb
grover-0.11.4 lib/grover/html_preprocessor.rb
grover-0.11.3 lib/grover/html_preprocessor.rb
grover-0.11.2 lib/grover/html_preprocessor.rb
grover-0.11.1 lib/grover/html_preprocessor.rb
grover-0.10.1 lib/grover/html_preprocessor.rb
grover-0.9.2 lib/grover/html_preprocessor.rb
grover-0.9.1 lib/grover/html_preprocessor.rb
grover-0.8.3 lib/grover/html_preprocessor.rb
grover-0.8.2 lib/grover/html_preprocessor.rb
grover-0.8.1 lib/grover/html_preprocessor.rb
grover-0.7.4 lib/grover/html_preprocessor.rb
grover-0.7.3 lib/grover/html_preprocessor.rb