Sha256: f78ef8777b4d50612d2565b2111002e8803422d06dc3b76786535ebefc385785

Contents?: true

Size: 1.03 KB

Versions: 17

Compression:

Stored size: 1.03 KB

Contents

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

17 entries across 17 versions & 1 rubygems

Version Path
grover-0.7.2 lib/grover/html_preprocessor.rb
grover-0.7.1 lib/grover/html_preprocessor.rb
grover-0.6.2 lib/grover/html_preprocessor.rb
grover-0.6.1 lib/grover/html_preprocessor.rb
grover-0.5.5 lib/grover/html_preprocessor.rb
grover-0.5.4 lib/grover/html_preprocessor.rb
grover-0.5.3 lib/grover/html_preprocessor.rb
grover-0.5.2 lib/grover/html_preprocessor.rb
grover-0.5.1 lib/grover/html_preprocessor.rb
grover-0.4.4 lib/grover/html_preprocessor.rb
grover-0.4.3 lib/grover/html_preprocessor.rb
grover-0.4.2 lib/grover/html_preprocessor.rb
grover-0.4.1 lib/grover/html_preprocessor.rb
grover-0.3.1 lib/grover/html_preprocessor.rb
grover-0.3.0 lib/grover/html_preprocessor.rb
grover-0.2.2 lib/grover/html_preprocessor.rb
grover-0.2.1 lib/grover/html_preprocessor.rb