lib/grover.rb in grover-0.4.1 vs lib/grover.rb in grover-0.4.2

- old
+ new

@@ -4,10 +4,11 @@ require 'grover/html_preprocessor' require 'grover/middleware' require 'grover/configuration' require 'schmooze' +require 'nokogiri' # # Grover interface for converting HTML to PDF # class Grover @@ -110,12 +111,18 @@ def processor Processor.new(root_path) end - def options_with_template_fix + def base_options options = @options.dup + options.merge! meta_options unless url_source? + options + end + + def options_with_template_fix + options = base_options display_url = options.delete :display_url if display_url options[:footer_template] ||= DEFAULT_FOOTER_TEMPLATE %i[header_template footer_template].each do |key| @@ -126,28 +133,47 @@ options end def normalized_options(path) options = Utils.normalize_object options_with_template_fix - options.merge! meta_options unless url_source? + fix_boolean_options! options + fix_numeric_options! options options['path'] = path if path options end + def fix_boolean_options!(options) + %w[displayHeaderFooter printBackground landscape preferCSSPageSize].each do |opt| + next unless options.key? opt + options[opt] = !FALSE_VALUES.include?(options[opt]) + end + end + + FALSE_VALUES = [nil, false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].freeze + + def fix_numeric_options!(options) + return unless options.key? 'scale' + options['scale'] = options['scale'].to_f + end + # # Extract out options from meta tags in the source - based on code from PDFKit project # def meta_options meta_opts = {} - @url.scan(/<meta [^>]*>/) do |meta| - tag_name = meta[/name=["']#{Grover.configuration.meta_tag_prefix}([a-z_-]+)["']/, 1] - next if tag_name.nil? + meta_tags.each do |meta| + tag_name = meta['name'] && meta['name'][/#{Grover.configuration.meta_tag_prefix}([a-z_-]+)/, 1] + next unless tag_name - Utils.deep_assign meta_opts, tag_name.split('-'), meta[/content=["']([^"']+)["']/, 1] + Utils.deep_assign meta_opts, tag_name.split('-'), meta['content'] end meta_opts + end + + def meta_tags + Nokogiri::HTML(@url).xpath('//meta') end def url_source? @url.match(/^http/i) end