Class: Pdfmult::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfmult.rb

Overview

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.

Constant Summary collapse

ERRORCODE =
{:general => 1, :usage => 2}

Class Method Summary collapse

Class Method Details

.ask(question) ⇒ Object

Asks for yes or no (y/n).

question - string to be printed

Returns true if the answer is yes.



354
355
356
357
358
359
360
361
362
# File 'lib/pdfmult.rb', line 354

def self.ask(question) # :nodoc:
  loop do
    $stderr.print "#{question} [y/n] "
    reply = $stdin.gets.chomp.downcase  # $stdin: avoids gets / ARGV problem
    return true   if reply == 'y'
    return false  if reply == 'n'
    warn "Please answer `y' or `n'."
  end
end

.command_available?(command) ⇒ Boolean

Tests silently whether the given system command is available.

command - command to test

Returns:

  • (Boolean)


380
381
382
# File 'lib/pdfmult.rb', line 380

def self.command_available?(command) # :nodoc:
  !!system("#{command} >/dev/null 2>&1")
end

.general_fail(message) ⇒ Object

Prints an error message and exits.



365
366
367
368
# File 'lib/pdfmult.rb', line 365

def self.general_fail(message) # :nodoc:
  warn "#{PROGNAME}: #{message}"
  exit ERRORCODE[:general]
end

.run!Object

The main program.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/pdfmult.rb', line 283

def self.run!

  # parse options
  begin
    options = Optionparser.parse!(ARGV)
  rescue => e
    usage_fail(e.message)
  end

  infile = options[:infile]
  outfile = options[:outfile]
  use_stdout = (outfile == '-')
  silent = options[:silent]

  # test for pdflatex installation
  unless options[:latex]
    message = 'seems not to be installed (you might try using the -l option)'
    general_fail("`#{PDFLATEX}' #{message}")  unless command_available?("#{PDFLATEX} --version")
    general_fail("`pdfpages.sty' #{message}")  unless 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 and !options[:force] and File.exist?(outfile)
    overwrite_ok = ask("File `#{outfile}' already exists. Overwrite?")
    exit  unless overwrite_ok
  end

  # set page number (get PDF info if necessary)
  pages = options[:pages] || PDFInfo.new(infile).page_count || 1

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

  output = nil
  if options[: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

.usage_fail(message) ⇒ Object

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



371
372
373
374
375
# File 'lib/pdfmult.rb', line 371

def self.usage_fail(message) # :nodoc:
  warn "#{PROGNAME}: #{message}"
  warn "Use `#{PROGNAME} --help' for valid options."
  exit ERRORCODE[:usage]
end