| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
-- |
| 4 |
-- This is file `pfarrei.tlu', |
| 5 |
-- generated with the docstrip utility. |
| 6 |
-- |
| 7 |
-- The original source files were: |
| 8 |
-- |
| 9 |
-- pfarrei.dtx (with options: `pfarrei,lua') |
| 10 |
-- |
| 11 |
-- Copyright (c) 2013 Markus Kohm |
| 12 |
-- komascript at gmx info |
| 13 |
-- |
| 14 |
-- This file was generated from file(s) of the work `pfarrei'. |
| 15 |
-- ------------------------------------------------------------------ |
| 16 |
-- |
| 17 |
-- It may be distributed under the conditions of the |
| 18 |
-- LaTeX Project Public License in the version distributed together |
| 19 |
-- with the work `pfarrei'. You may however distribute the work |
| 20 |
-- `pfarrei' without all such generated files. See also |
| 21 |
-- <http://www.latex-project.org/lppl.txt> for additional |
| 22 |
-- information. |
| 23 |
-- |
| 24 |
-- This work has the LPPL maintenance status `unmaintained'. |
| 25 |
-- |
| 26 |
-- |
| 27 |
-- This package is currently unmaintained. |
| 28 |
-- |
| 29 |
-- The list of files belonging to the work `pfarrei' is given in |
| 30 |
-- the file `pfarrei.dtx'. |
| 31 |
-- |
| 32 |
local version_number = string.sub( '$Revision: 37 $', 12, -2 ) |
| 33 |
local action_version = ' r' .. version_number .. '\n' .. [[ |
| 34 |
|
| 35 |
Copyright (c) 2013 Markus Kohm. |
| 36 |
License: lppl 1.3c or later. See <http://www.latex-project.org/lppl.txt>. |
| 37 |
]] |
| 38 |
local action_help = [[ |
| 39 |
action options: |
| 40 |
|
| 41 |
-h, --help Print this help message. |
| 42 |
-V, --version Print the version information. |
| 43 |
|
| 44 |
processing options: |
| 45 |
-b, --booklet Generate a booklet instead of only two pages side by |
| 46 |
side onto one page. The whole booklet will be one |
| 47 |
signature. |
| 48 |
-s, --sidebyside Generate only two pages side by side onto one page |
| 49 |
instead of a booklet. |
| 50 |
-o, --overwrite Write the output to the <PDF file> instead of appending |
| 51 |
"-sidebyside.pdf" or "--booklet.pdf" to the basename |
| 52 |
of <PDF file> |
| 53 |
]] |
| 54 |
local action_opts = { |
| 55 |
['-h'] = 'help', |
| 56 |
['--help'] = 'help', |
| 57 |
['-V'] = 'version', |
| 58 |
['--version'] = 'version', |
| 59 |
} |
| 60 |
local processing_opts = { |
| 61 |
['-b'] = 'booklet', |
| 62 |
['--booklet'] = 'booklet', |
| 63 |
['-s'] = 'sidebyside', |
| 64 |
['--sidebyside'] = 'sidebyside', |
| 65 |
['-o'] = 'overwrite', |
| 66 |
['--overwrite'] = 'overwrite', |
| 67 |
['-d'] = 'debug', |
| 68 |
['--debug'] = 'debug', |
| 69 |
} |
| 70 |
|
| 71 |
-- detect action options and do action |
| 72 |
local i = 1 |
| 73 |
local action |
| 74 |
while arg[i] do |
| 75 |
action = action_opts[arg[i]] |
| 76 |
i = i+1 |
| 77 |
if action == 'help' then |
| 78 |
print( arg[0]..action_version ); |
| 79 |
print( 'Usage: ' .. arg[0] .. ' <action option>' ) |
| 80 |
print( ' ' .. arg[0] .. ' [<processing options>] <PDF file> ...\n' ) |
| 81 |
print( action_help ); |
| 82 |
os.exit( 0 ); |
| 83 |
elseif action == 'version' then |
| 84 |
print( arg[0] .. action_version ); |
| 85 |
os.exit( 0 ); |
| 86 |
end |
| 87 |
end |
| 88 |
|
| 89 |
-- process options and parameters |
| 90 |
local booklet = false |
| 91 |
local overwrite = false |
| 92 |
local debug = false |
| 93 |
i = 1 |
| 94 |
while arg[i] do |
| 95 |
action = processing_opts[arg[i]] |
| 96 |
if action == 'booklet' then booklet = true |
| 97 |
elseif action == 'sidebyside' then booklet = false |
| 98 |
elseif action == 'overwrite' then overwrite = true |
| 99 |
elseif action == 'debug' then debug = true |
| 100 |
elseif action == nil then |
| 101 |
-- build the temporary tex file |
| 102 |
local tmpdir = os.tmpdir("pfarrei.XXXXXX" ) |
| 103 |
local tmpfile = string.match( arg[i], '.*/(.*)$') or arg[i] |
| 104 |
-- pdflatex's -output-directory search for source pdf works with path specification but fails |
| 105 |
-- when simple file name in the current working directory is provided, we need to provide '../' then |
| 106 |
local local_source='' |
| 107 |
if tmpfile == arg[i] then local_source = '../' end |
| 108 |
local basename = string.match( tmpfile,'(.*)%.[^.]*$') or tmpfile |
| 109 |
tmpfile = tmpdir..'/'..basename..'.tex' |
| 110 |
local file = assert( io.open( tmpfile, 'w' ) ) |
| 111 |
if booklet then assert( file:write("\\PassOptionsToPackage{booklet}{pfarrei}\n") ) end |
| 112 |
assert( file:write("\\def\\OriginalFile{"..local_source,arg[i],"}\n") ) |
| 113 |
assert( file:write("\\input{a5toa4.tex}\n") ) |
| 114 |
assert( file:flush() ) |
| 115 |
file:close() |
| 116 |
-- call pdflatex |
| 117 |
assert( os.execute( 'pdflatex -interaction=batchmode -output-directory='..tmpdir..' '..tmpfile ) ) |
| 118 |
-- copy the resulting pdf file |
| 119 |
local srcfile = assert( io.open( tmpdir..'/'..basename..'.pdf', 'rb' ) ) |
| 120 |
if overwrite |
| 121 |
then |
| 122 |
tmpfile = arg[i] |
| 123 |
else |
| 124 |
tmpfile = string.match( arg[i], '(.*)%.[^.]*$' ) or arg[i] |
| 125 |
if booklet |
| 126 |
then |
| 127 |
tmpfile = tmpfile..'-booklet.pdf' |
| 128 |
else |
| 129 |
tmpfile = tmpfile..'-sidebyside.pdf' |
| 130 |
end |
| 131 |
end |
| 132 |
local destfile = assert( io.open( tmpfile, 'wb' ) ) |
| 133 |
local buffer |
| 134 |
while true do |
| 135 |
buffer = srcfile:read(8388608) |
| 136 |
if buffer==nil then break end |
| 137 |
assert( destfile:write(buffer) ) |
| 138 |
end |
| 139 |
assert( destfile:close() ) |
| 140 |
srcfile:close() |
| 141 |
if debug |
| 142 |
then |
| 143 |
print('DEBUG: Temporary files in: '..tmpdir); |
| 144 |
else |
| 145 |
tmpfile=tmpdir..'/'..basename |
| 146 |
os.remove( tmpfile..'.aux' ) |
| 147 |
os.remove( tmpfile..'.tex' ) |
| 148 |
os.remove( tmpfile..'.log' ) |
| 149 |
os.remove( tmpfile..'.pdf' ) |
| 150 |
os.remove( tmpdir ) |
| 151 |
end |
| 152 |
overwrite = false |
| 153 |
end |
| 154 |
i=i+1 |
| 155 |
end |
| 156 |
-- |
| 157 |
-- |
| 158 |
-- End of file `pfarrei.tlu'. |