class Pdfmult::Application

The main program. It’s run! method is called if the script is run from the command line. It parses the command line arguments and does the job.

Constants

ERRORCODE

Public Class Methods

new() click to toggle source
# File lib/pdfmult.rb, line 289
def initialize
  begin
    options = Optionparser.parse!(ARGV)
  rescue => e
    usage_fail(e.message)
  end
  @infile = options[:infile]
  @outfile = options[:outfile]
  @use_stdout = (@outfile == '-')
  @silent = options[:silent]
  @force = options[:force]
  @latex = options[:latex]
  @number = options[:number]
  @pages = options[:pages] || PDFInfo.new(@infile).page_count || 1
end

Public Instance Methods

run!() click to toggle source

The main program.

# File lib/pdfmult.rb, line 306
def run!

  # test for pdflatex installation
  unless @latex
    message = 'seems not to be installed (you might try using the -l option)'
    general_fail("`#{PDFLATEX}' #{message}")  unless self.class.command_available?("#{PDFLATEX} --version")
    general_fail("`pdfpages.sty' #{message}")  unless self.class.command_available?("#{KPSEWHICH} pdfpages.sty")
  end

  # test input file
  usage_fail("no such file: `#{@infile}'")  unless File.exist?(@infile)
  usage_fail("specified input not of the type `file'")  unless File.ftype(@infile) == 'file'

  # test for existing output file
  if !@use_stdout && !@force && File.exist?(@outfile)
    overwrite_ok = confirm("File `#{@outfile}' already exists. Overwrite?")
    exit  unless overwrite_ok
  end

  # create LaTeX document
  args = {
    :pdffile    => @infile,
    :layout     => Layout.new(@number),
    :page_count => @pages
  }
  document = LaTeXDocument.new(args)

  output = nil
  if @latex
    output = document.to_s
  else
    Dir.mktmpdir('pdfmult') do |dir|
      texfile = 'pdfmult.tex'
      pdffile = 'pdfmult.pdf'
      open("#{dir}/#{texfile}", 'w') {|f| f.write(document.to_s) }
      command = "#{PDFLATEX} -output-directory #{dir} #{texfile}"
      Open3.popen3(command) do |stdin, stdout, stderr|
        stdout.each_line {|line| warn line.chomp }  unless @silent # redirect progress messages to stderr
        stderr.read  # make sure all streams are read (and command has finished)
      end
      output = File.read("#{dir}/#{pdffile}")
    end
  end

  # redirect stdout to output file
  $stdout.reopen(@outfile, 'w')  unless @use_stdout

  warn "Writing on #{@outfile}."  unless (@use_stdout || @silent)
  puts output
end

Private Instance Methods

confirm(question) click to toggle source

Asks for yes or no (y/n).

question - string to be printed

Returns true if the answer is yes.

# File lib/pdfmult.rb, line 364
def confirm(question)
  loop do
    $stderr.print "#{question} [y/n] "
    reply = $stdin.gets.chomp.downcase  # $stdin avoids gets/ARGV problem
    return reply == 'y'  if /\A[yn]\Z/ =~ reply
    warn "Please answer `y' or `n'."
  end
end
general_fail(message) click to toggle source

Prints an error message and exits.

# File lib/pdfmult.rb, line 374
def general_fail(message)
  warn "#{PROGNAME}: #{message}"
  exit ERRORCODE[:general]
end
usage_fail(message) click to toggle source

Prints an error message and a short help information, then exits.

# File lib/pdfmult.rb, line 380
def usage_fail(message)
  warn "#{PROGNAME}: #{message}"
  warn "Use `#{PROGNAME} --help' for valid options."
  exit ERRORCODE[:usage]
end