class WickedPdf::OptionParser

Constants

BINARY_VERSION_WITHOUT_DASHES

Attributes

binary_version[R]
hf_tempfiles[R]

Public Class Methods

new(binary_version = WickedPdf::DEFAULT_BINARY_VERSION) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 7
def initialize(binary_version = WickedPdf::DEFAULT_BINARY_VERSION)
  @binary_version = binary_version
end

Public Instance Methods

parse(options) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 11
def parse(options)
  [
    parse_extra(options),
    parse_others(options),
    parse_global(options),
    parse_outline(options.delete(:outline)),
    parse_header_footer(:header => options.delete(:header),
                        :footer => options.delete(:footer),
                        :layout => options[:layout]),
    parse_cover(options.delete(:cover)),
    parse_toc(options.delete(:toc)),
    parse_basic_auth(options)
  ].flatten
end
valid_option(name) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 26
def valid_option(name)
  if binary_version < BINARY_VERSION_WITHOUT_DASHES
    "--#{name}"
  else
    name
  end
end

Private Instance Methods

make_option(name, value, type = :string) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 213
def make_option(name, value, type = :string)
  return value.collect { |v| make_option(name, v, type) } if value.is_a?(Array)

  if type == :name_value
    parts = value.to_s.split(' ')
    ["--#{name.tr('_', '-')}", *parts]
  elsif type == :boolean
    if value
      ["--#{name.tr('_', '-')}"]
    else
      []
    end
  else
    ["--#{name.tr('_', '-')}", value.to_s]
  end
end
make_options(options, names, prefix = '', type = :string) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 199
def make_options(options, names, prefix = '', type = :string)
  return [] if options.nil?

  names.collect do |o|
    if options[o].blank?
      []
    else
      make_option("#{prefix.blank? ? '' : prefix + '-'}#{o}",
                  options[o],
                  type)
    end
  end
end
parse_basic_auth(options) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 43
def parse_basic_auth(options)
  if options[:basic_auth]
    user, passwd = Base64.decode64(options[:basic_auth]).split(':')
    ['--username', user, '--password', passwd]
  else
    []
  end
end
parse_cover(argument) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 78
def parse_cover(argument)
  arg = argument.to_s
  return [] if arg.blank?

  # Filesystem path or URL - hand off to wkhtmltopdf
  if argument.is_a?(Pathname) || (arg[0, 4] == 'http')
    [valid_option('cover'), arg]
  else # HTML content
    @hf_tempfiles ||= []
    @hf_tempfiles << tf = WickedPdf::Tempfile.new('wicked_cover_pdf.html')
    tf.write arg
    tf.flush
    [valid_option('cover'), tf.path]
  end
end
parse_extra(options) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 36
def parse_extra(options)
  return [] if options[:extra].nil?
  return options[:extra].split if options[:extra].respond_to?(:split)

  options[:extra]
end
parse_global(options) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 141
def parse_global(options)
  r = []
  unless options.blank?
    r += make_options(options, %i[orientation
                                  dpi
                                  page_size
                                  page_width
                                  title
                                  log_level])
    r += make_options(options, %i[lowquality
                                  grayscale
                                  no_pdf_compression
                                  quiet], '', :boolean)
    r += make_options(options, %i[image_dpi
                                  image_quality
                                  page_height], '', :numeric)
    r += parse_margins(options.delete(:margin))
  end
  r
end
parse_margins(options) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 137
def parse_margins(options)
  make_options(options, %i[top bottom left right], 'margin', :numeric)
end
parse_others(options) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 162
def parse_others(options)
  r = []
  unless options.blank?
    r += make_options(options, %i[proxy
                                  username
                                  password
                                  encoding
                                  user_style_sheet
                                  viewport_size
                                  window_status
                                  allow])
    r += make_options(options, %i[cookie
                                  post], '', :name_value)
    r += make_options(options, %i[redirect_delay
                                  zoom
                                  page_offset
                                  javascript_delay], '', :numeric)
    r += make_options(options, %i[book
                                  default_header
                                  disable_javascript
                                  enable_plugins
                                  disable_internal_links
                                  disable_external_links
                                  keep_relative_links
                                  print_media_type
                                  disable_local_file_access
                                  enable_local_file_access
                                  disable_smart_shrinking
                                  use_xserver
                                  no_background
                                  images
                                  no_images
                                  no_stop_slow_scripts], '', :boolean)
  end
  r
end
parse_outline(options) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 128
def parse_outline(options)
  r = []
  unless options.blank?
    r = make_options(options, [:outline], '', :boolean)
    r += make_options(options, [:outline_depth], '', :numeric)
  end
  r
end
parse_toc(options) click to toggle source
# File lib/wicked_pdf/option_parser.rb, line 94
def parse_toc(options)
  return [] if options.nil?

  r = [valid_option('toc')]
  unless options.blank?
    r += make_options(options, %i[font_name header_text], 'toc')
    r += make_options(options, [:xsl_style_sheet])
    r += make_options(options, %i[depth
                                  header_fs
                                  text_size_shrink
                                  l1_font_size
                                  l2_font_size
                                  l3_font_size
                                  l4_font_size
                                  l5_font_size
                                  l6_font_size
                                  l7_font_size
                                  level_indentation
                                  l1_indentation
                                  l2_indentation
                                  l3_indentation
                                  l4_indentation
                                  l5_indentation
                                  l6_indentation
                                  l7_indentation], 'toc', :numeric)
    r += make_options(options, %i[no_dots
                                  disable_links
                                  disable_back_links], 'toc', :boolean)
    r += make_options(options, %i[disable_dotted_lines
                                  disable_toc_links], nil, :boolean)
  end
  r
end