<commit_before># wkhtml2pdf Ruby interface
# http://code.google.com/p/wkhtmltopdf/

require 'logger'
require 'digest/md5'
require 'rbconfig'

if (RbConfig::CONFIG['target_os'] =~ /mswin|mingw/) && (RUBY_VERSION < '1.9')
  require 'win32/open3'
else
  require 'open3'
end

begin
  require 'active_support/core_ext/module/attribute_accessors'
rescue LoadError
  require 'active_support/core_ext/class/attribute_accessors'
end

begin
  require 'active_support/core_ext/object/blank'
rescue LoadError
  require 'active_support/core_ext/blank'
end

require 'wicked_pdf/version'
require 'wicked_pdf/railtie'
require 'wicked_pdf/tempfile'
require 'wicked_pdf/middleware'

class WickedPdf
  DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9')
  EXE_NAME = "wkhtmltopdf"
  @@config = {}
  cattr_accessor :config

  def initialize(wkhtmltopdf_binary_path = nil)
    @exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path
    raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
    raise "Bad #{EXE_NAME}'s path: #{@exe_path}" unless File.exists?(@exe_path)
    raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)

    @binary_version = DEFAULT_BINARY_VERSION
  end

  def retreive_binary_version
    begin
      stdin, stdout, stderr = Open3.popen3(@exe_path + ' -V')
      @binary_version = parse_version(stdout.gets(nil))
    rescue StandardError
    end
  end

  def pdf_from_html_file(filepath, options={})
    if WickedPdf.config[:retreive_version]
      retreive_binary_version
    end

    temp_path = options.delete(:temp_path)
    generated_pdf_file = WickedPdfTempfile.new("wicked_pdf_generated_file.pdf", temp_path)
    command = [@exe_path]
    command << '-q' unless on_windows? # suppress errors on stdout
    command += parse_options(options)
    command << "file://#{filepath}"
    command << generated_pdf_file.path.to_s

    print_command(command.inspect) if in_development_mode?

    err = Open3.popen3(*command) do |stdin, stdout, stderr|
      stderr.read
    end
    if return_file = options.delete(:return_file)
      return generated_pdf_file
    end
    generated_pdf_file.rewind
    generated_pdf_file.binmode
    pdf = generated_pdf_file.read
    raise "PDF could not be generated!\n Command Error: #{err}" if pdf and pdf.rstrip.length == 0
    pdf
  rescue Exception => e
    raise "Failed to execute:\n#{command}\nError: #{e}"
  ensure
    generated_pdf_file.close! if generated_pdf_file && !return_file
  end

  def pdf_from_string(string, options={})
    temp_path = options.delete(:temp_path)
    string_file = WickedPdfTempfile.new("wicked_pdf.html", temp_path)
    string_file.binmode
    string_file.write(string)<commit_msg>fixed problem with generated html file path on windows<commit_after># wkhtml2pdf Ruby interface
# http://code.google.com/p/wkhtmltopdf/

require 'logger'
require 'digest/md5'
require 'rbconfig'

if (RbConfig::CONFIG['target_os'] =~ /mswin|mingw/) && (RUBY_VERSION < '1.9')
  require 'win32/open3'
else
  require 'open3'
end

begin
  require 'active_support/core_ext/module/attribute_accessors'
rescue LoadError
  require 'active_support/core_ext/class/attribute_accessors'
end

begin
  require 'active_support/core_ext/object/blank'
rescue LoadError
  require 'active_support/core_ext/blank'
end

require 'wicked_pdf/version'
require 'wicked_pdf/railtie'
require 'wicked_pdf/tempfile'
require 'wicked_pdf/middleware'

class WickedPdf
  DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9')
  EXE_NAME = "wkhtmltopdf"
  @@config = {}
  cattr_accessor :config

  def initialize(wkhtmltopdf_binary_path = nil)
    @exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path
    raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
    raise "Bad #{EXE_NAME}'s path: #{@exe_path}" unless File.exists?(@exe_path)
    raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)

    @binary_version = DEFAULT_BINARY_VERSION
  end

  def retreive_binary_version
    begin
      stdin, stdout, stderr = Open3.popen3(@exe_path + ' -V')
      @binary_version = parse_version(stdout.gets(nil))
    rescue StandardError
    end
  end

  def pdf_from_html_file(filepath, options={})
    if WickedPdf.config[:retreive_version]
      retreive_binary_version
    end

    if os_windows?
      filepath = "file:///#{filepath}"
    else
      filepath = "file://#{filepath}"
    end

    temp_path = options.delete(:temp_path)
    generated_pdf_file = WickedPdfTempfile.new("wicked_pdf_generated_file.pdf", temp_path)
    command = [@exe_path]
    command << '-q' unless on_windows? # suppress errors on stdout
    command += parse_options(options)
    command << filepath
    command << generated_pdf_file.path.to_s

    print_command(command.inspect) if in_development_mode?

    err = Open3.popen3(*command) do |stdin, stdout, stderr|
      stderr.read
    end
    if return_file = options.delete(:return_file)
      return generated_pdf_file
    end
    generated_pdf_file.rewind
    generated_pdf_file.binmode
    pdf = generated_pdf_file.read
    raise "PDF could not be generated!\n Command Error: #{err}" if pdf and pdf.rstrip.length == 0
    pdf
  rescue Exception => e
    raise "Failed to execute:\n#{command}\nError: #{e}"
  ensure
    generated_pdf_file.close! if generated_pdf_file && !return_file
  end

  def pdf_from_string(string, options={})
    temp_path = options.delete(:temp_path)
    string_file = WickedPdfTempfile.new("wicked_pdf.html", temp_path)
    string_file.binmode
    string_file.write(string)
{'fraction_non_alphanumeric': 0.10570135746606335, 'fraction_numerical': 0.005791855203619909, 'mean_word_length': 4.328833172613308, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5677443', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1874, 'n_words': 389}