| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
ß |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
runtexfile = runtexfile or { } |
| 13 |
local version = 0.06 |
| 14 |
runtexfile.version = version |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
kpse.set_program_name("texlua") |
| 32 |
|
| 33 |
local f = kpse.find_file("lualibs.lua") |
| 34 |
|
| 35 |
|
| 36 |
require("lualibs") |
| 37 |
|
| 38 |
|
| 39 |
local function isInArray(value,array) |
| 40 |
for _,v in pairs(array) do |
| 41 |
if v == value then |
| 42 |
return true |
| 43 |
end |
| 44 |
end |
| 45 |
return false |
| 46 |
end |
| 47 |
|
| 48 |
args = {} |
| 49 |
for i=1, #arg do |
| 50 |
args[#args+1] = arg[i] |
| 51 |
|
| 52 |
end |
| 53 |
|
| 54 |
local verbose = isInArray("-V",args) or isInArray("--verbose",args) |
| 55 |
local usefilename = isInArray("-f",args) or isInArray("--usefilename",args) |
| 56 |
|
| 57 |
local function flog(s) |
| 58 |
if verbose then tmpfile:write(s.."\n") end |
| 59 |
end |
| 60 |
|
| 61 |
if verbose then |
| 62 |
tmpfile = io.open("runtexfile.log","w") |
| 63 |
tmpfile:write("Start logfile (verbose)\n") |
| 64 |
end |
| 65 |
|
| 66 |
if isInArray("-v",args) or isInArray("--version",args) then |
| 67 |
print("runtexfile version "..runtexfile.version) |
| 68 |
os.exit() |
| 69 |
end |
| 70 |
|
| 71 |
if isInArray("-h",args) or isInArray("--help",args) then |
| 72 |
print([[ |
| 73 |
parameter handling |
| 74 |
-h,--help |
| 75 |
-V,--verbose |
| 76 |
-v,--version |
| 77 |
<file> (string) tex file name with or without extension]]) |
| 78 |
os.exit() |
| 79 |
end |
| 80 |
|
| 81 |
local luaVersion = _VERSION |
| 82 |
if verbose then |
| 83 |
flog("Check Lua version: "..luaVersion) |
| 84 |
end |
| 85 |
if (luaVersion < "Lua 5.3") then |
| 86 |
print("=========================================") |
| 87 |
print("Sorry. but we need at least LuaTeX 1.09") |
| 88 |
print("Leaving program runtexfile") |
| 89 |
print("=========================================") |
| 90 |
os.exit() |
| 91 |
end |
| 92 |
|
| 93 |
print("runtexfile version "..runtexfile.version) |
| 94 |
local current_dir = "." |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
local commands = {"lualatex", "luatex", "luahbtex", "latex", "pdflatex", "xelatex", "xetex" } |
| 103 |
|
| 104 |
local function removeTeXFileExtension(fname) |
| 105 |
return fname:gsub("%.tex", "") |
| 106 |
end |
| 107 |
|
| 108 |
local function getFileName(s) |
| 109 |
|
| 110 |
local r = s:match "(%b<>)$" |
| 111 |
return r and r:sub(2,-2) |
| 112 |
|
| 113 |
end |
| 114 |
|
| 115 |
local LTXfile = removeTeXFileExtension(args[#args]) |
| 116 |
local mainLTXfile = LTXfile |
| 117 |
print("Main file: "..mainLTXfile) |
| 118 |
flog("Main file: "..mainLTXfile) |
| 119 |
|
| 120 |
local specialFileName |
| 121 |
|
| 122 |
file = io.open(LTXfile,"r") |
| 123 |
if not file then |
| 124 |
file = io.open(LTXfile..".tex","r") |
| 125 |
if not file then |
| 126 |
print("Fatal error: no file "..LTXfile.." or "..LTXfile..".tex") |
| 127 |
print("I will exit ...") |
| 128 |
flog("Fatal error: no file "..LTXfile.." or "..LTXfile..".tex") |
| 129 |
if verbose then |
| 130 |
tmpfile:close() |
| 131 |
os.exit() |
| 132 |
end |
| 133 |
end |
| 134 |
end |
| 135 |
|
| 136 |
|
| 137 |
local step = 1 |
| 138 |
local saveLTXfile = LTXfile |
| 139 |
local commandLineFound = false |
| 140 |
local para = "" |
| 141 |
|
| 142 |
for line in file:lines() do |
| 143 |
if line ~= "" then |
| 144 |
words = {} |
| 145 |
flog(step..": "..line) |
| 146 |
for word in line:gmatch("%S+") do table.insert(words, word) end |
| 147 |
if words[1] == "%!" and words[2] == "HV" then |
| 148 |
commandLineFound = true |
| 149 |
specialFileName = getFileName(line) |
| 150 |
|
| 151 |
if specialFileName then |
| 152 |
flog("Use special filename: "..specialFileName) |
| 153 |
specialFileName = specialFileName:gsub("!!file!!",mainLTXfile) |
| 154 |
flog("geändert zu filename: "..specialFileName) |
| 155 |
else |
| 156 |
flog("Kein specialFileName") |
| 157 |
end |
| 158 |
command = words[3] |
| 159 |
if isInArray(command,commands) then |
| 160 |
para = "--interaction=nonstopmode" |
| 161 |
else |
| 162 |
para = "" |
| 163 |
end |
| 164 |
flog("Initial parameter: "..para) |
| 165 |
if #words > 3 then |
| 166 |
if specialFileName then |
| 167 |
noPara = #words - 1 |
| 168 |
else |
| 169 |
noPara = #words |
| 170 |
end |
| 171 |
for i = 4,noPara do para = para .. " " .. words[i] end |
| 172 |
end |
| 173 |
flog("Scanned parameter: "..para) |
| 174 |
if specialFileName then LTXfile = specialFileName end |
| 175 |
flog("Real filename: "..LTXfile) |
| 176 |
flog("Will run: ".. command.." "..para.." "..LTXfile) |
| 177 |
flog("and log into file "..current_dir.."/runtexfile-"..step..".log") |
| 178 |
print("running: " .. command.." "..para.." "..LTXfile) |
| 179 |
|
| 180 |
local handle = io.popen(command.." "..para.." "..LTXfile.." > "..current_dir.."/runtexfile-"..step..".log") |
| 181 |
local result = handle:read("*a") |
| 182 |
handle:close() |
| 183 |
if result ~= "" then print("cmd output: " .. result) end |
| 184 |
logfile = io.open(current_dir.."/runtexfile-"..step..".log","r") |
| 185 |
if not logfile then |
| 186 |
for line in logfile:lines() do |
| 187 |
if string.find(line, "! LaTeX Error:") > 0 then |
| 188 |
print("There is at least one error!") |
| 189 |
end |
| 190 |
end |
| 191 |
end |
| 192 |
else |
| 193 |
break |
| 194 |
end |
| 195 |
LTXfile = saveLTXfile |
| 196 |
step = step + 1 |
| 197 |
end |
| 198 |
end |
| 199 |
|
| 200 |
if commandLineFound == false then |
| 201 |
print("No command line with %! HV <command> defined! running: " .. commands[1]) |
| 202 |
|
| 203 |
local handle = io.popen(commands[1].." "..para.." "..LTXfile.." > "..current_dir.."/runtexfile-"..step..".log") |
| 204 |
local result = handle:read("*a") |
| 205 |
handle:close() |
| 206 |
if result ~= "" then print("cmd output: " .. result) end |
| 207 |
logfile = io.open(current_dir.."/runtexfile-"..step..".log","r") |
| 208 |
if not logfile then |
| 209 |
for line in logfile:lines() do |
| 210 |
if string.find(line, "! LaTeX Error:") > 0 then |
| 211 |
print("There is at least one error!") |
| 212 |
end |
| 213 |
end |
| 214 |
end |
| 215 |
end |
| 216 |
file:close() |
| 217 |
if verbose then |
| 218 |
tmpfile:write("Close logfile (verbose)\n") |
| 219 |
tmpfile:close() |
| 220 |
end |
| 221 |
|