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 }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/pdfmult.rb', line 294

def initialize
  begin
    options = Optionparser.parse!(ARGV)
  rescue StandardError => 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

Class Method Details

.command_available?(command) ⇒ Boolean

Tests silently whether the given system command is available.

command - command to test

Returns:

  • (Boolean)


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

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

Instance Method Details

#run!Object

The main program.



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
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/pdfmult.rb', line 311

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"
      File.write("#{dir}/#{texfile}", 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