lib/pdfmult.rb in pdfmult-1.0.0 vs lib/pdfmult.rb in pdfmult-1.1.0
- old
+ new
@@ -18,20 +18,24 @@
# if not, only the first page is processed
# (unless the page count was specified via command line option).
#
# +pdfmult+ uses +pdflatex+ with the +pdfpages+ package,
# so both have to be installed on the system.
+# If the --latex option is used, though, +pdflatex+ is not run
+# and a LaTeX file is created instead of a PDF.
#
# == Options
#
# -n, --number:: Number of copies to put on one page: 2 (default), 4, 8, 9, 16.
#
# -o, --output:: Output file (default: infile_NUMBER.pdf).
#
# -p, --pages:: Number of pages to convert.
# If given, +pdfmult+ does not try to obtain the page count from the source PDF.
#
+# -l, --latex:: Create a LaTeX file instead of a PDF file (default: infile_NUMBER.tex).
+#
# -h, --help:: Prints a brief help message and exits.
#
# -v, --version:: Prints a brief version information and exits.
#
# == Examples
@@ -55,12 +59,12 @@
# This module contains the classes for the +pdfmult+ tool
module Pdfmult
PROGNAME = 'pdfmult'
- VERSION = '1.0.0'
- DATE = '2012-03-15'
+ VERSION = '1.1.0'
+ DATE = '2012-03-16'
COPYRIGHT = "Copyright (C) 2011-2012 Marcus Stollsteimer.\n" +
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n" +
"This is free software: you are free to change and redistribute it.\n" +
"There is NO WARRANTY, to the extent permitted by law."
@@ -81,12 +85,13 @@
def self.parse!(argv)
options = {
:number => 2,
:infile => nil,
+ :latex => false,
:outfile => nil,
- :pages => nil
+ :pages => nil
}
opt_parser = OptionParser.new do |opt|
opt.banner = "Usage: #{PROGNAME} [options] file"
opt.separator ''
@@ -98,10 +103,15 @@
opt.separator 'The input PDF file may consist of several pages.'
opt.separator 'If pdfmult succeeds in obtaining the page count it will rearrange all pages,'
opt.separator 'if not, only the first page is processed'
opt.separator '(unless the page count was specified via command line option).'
opt.separator ''
+ opt.separator 'pdfmult uses pdflatex with the pdfpages package,'
+ opt.separator 'so both have to be installed on the system.'
+ opt.separator 'If the --latex option is used, though, pdflatex is not run'
+ opt.separator 'and a LaTeX file is created instead of a PDF.'
+ opt.separator ''
opt.separator 'Options'
opt.separator ''
# process --version and --help first,
# exit successfully (GNU Coding Standards)
@@ -133,21 +143,26 @@
"If given, #{PROGNAME} does not try to obtain the page count from the source PDF.") do |p|
raise(OptionParser::InvalidArgument, p) unless p > 0
options[:pages] = p
end
+ opt.on('-l', '--latex', 'Create a LaTeX file instead of a PDF file (default: file_2.tex).') do
+ options[:latex] = true
+ end
+
opt.separator ''
end
opt_parser.parse!(argv)
# only input file should be left in argv
raise(ArgumentError, 'wrong number of arguments') if (argv.size != 1 || argv[0] == '')
options[:infile] = argv.pop
# set output file unless set by option
- options[:outfile] ||= options[:infile].gsub(/(.pdf)$/, '') + "_#{options[:number].to_s}.pdf"
+ ext = options[:latex] ? 'tex' : 'pdf'
+ options[:outfile] ||= options[:infile].gsub(/(.pdf)$/, '') + "_#{options[:number].to_s}.#{ext}"
options
end
end
@@ -270,39 +285,67 @@
options = Optionparser.parse!(ARGV)
rescue => e
usage_fail(e.message)
end
- # tests
- general_fail("`#{PDFLATEX}' seems not to be installed") unless command_available?("#{PDFLATEX} --version")
- general_fail("`pdfpages.sty' seems not to be installed") unless command_available?("#{KPSEWHICH} pdfpages.sty")
-
- # main body #
-
infile = options[:infile]
outfile = options[:outfile]
+ # 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 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]
pages ||= PDFInfo.new(infile).page_count
pages ||= 1
# create LaTeX document
document = LaTeXDocument.new(infile, options[:number], pages)
- Dir.mktmpdir('pdfmult') do |dir|
- open("#{dir}/pdfmult.tex", 'w') do |f|
- pdfpath = "#{dir}/pdfmult.pdf"
+ if options[:latex]
+ open(outfile, 'w') do |f|
f.write(document.to_s)
- f.flush
- system("/usr/bin/pdflatex -output-directory #{dir} pdfmult.tex")
- puts "Writing on #{outfile}."
- FileUtils::mv(pdfpath, outfile)
end
+ else
+ Dir.mktmpdir('pdfmult') do |dir|
+ open("#{dir}/pdfmult.tex", 'w') do |f|
+ pdfpath = "#{dir}/pdfmult.pdf"
+ f.write(document.to_s)
+ f.flush
+ system("#{PDFLATEX} -output-directory #{dir} pdfmult.tex")
+ puts "Writing on #{outfile}."
+ FileUtils::mv(pdfpath, outfile)
+ end
+ end
+ end
+ end
+
+ # Asks for yes or no (y/n).
+ #
+ # +question+ - string to be printed
+ #
+ # Returns +true+ if the answer is yes.
+ def self.ask(question) # :nodoc:
+ while true
+ print question + ' [y/n] '
+ reply = $stdin.gets.chomp.downcase # $stdin: avoids gets / ARGV problem
+ return true if reply == 'y'
+ return false if reply == 'n'
+ puts "Please answer `y' or `n'."
end
end
# Prints an error message and exits.
def self.general_fail(message) # :nodoc: