| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
NAME = "ptex2pdf[.lua]" |
| 4 |
VERSION = "20200520.0" |
| 5 |
AUTHOR = "Norbert Preining" |
| 6 |
AUTHOREMAIL = "[email protected]" |
| 7 |
SHORTDESC = "Convert Japanese TeX documents to pdf" |
| 8 |
LONGDESC = [[ |
| 9 |
Main purpose of the script is easy support of Japanese typesetting |
| 10 |
engines in TeXworks. As TeXworks typesetting setup does not allow |
| 11 |
for multistep processing, this script runs one of the ptex based |
| 12 |
programs (ptex, uptex, eptex, euptex, platex, uplatex) followed |
| 13 |
by dvipdfmx. |
| 14 |
]] |
| 15 |
USAGE = [[ |
| 16 |
[texlua] ptex2pdf[.lua] { option | basename[.tex] } ... |
| 17 |
options: -v version |
| 18 |
-h help |
| 19 |
-help print full help (installation, TeXworks setup) |
| 20 |
-e use eptex class of programs |
| 21 |
-u use uptex class of programs |
| 22 |
-l use latex based formats |
| 23 |
-ld use latex-dev based formats |
| 24 |
-s stop at dvi |
| 25 |
-i retain intermediate files |
| 26 |
-ot '<opts>' extra options for TeX |
| 27 |
-od '<opts>' extra options for dvipdfmx |
| 28 |
-output-directory '<dir>' directory for created files]] |
| 29 |
|
| 30 |
LICENSECOPYRIGHT = [[ |
| 31 |
Originally based on musixtex.lua from Bob Tennent. |
| 32 |
|
| 33 |
(c) Copyright 2016-2020 Japanese TeX Development Community |
| 34 |
(c) Copyright 2013-2020 Norbert Preining [email protected] |
| 35 |
(c) Copyright 2012 Bob Tennent [email protected] |
| 36 |
|
| 37 |
This program is free software; you can redistribute it and/or modify it |
| 38 |
under the terms of the GNU General Public License as published by the |
| 39 |
Free Software Foundation; either version 2 of the License, or (at your |
| 40 |
option) any later version. |
| 41 |
|
| 42 |
This program is distributed in the hope that it will be useful, |
| 43 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 44 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 45 |
Public License for more details. |
| 46 |
|
| 47 |
You should have received a copy of the GNU General Public License along |
| 48 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 49 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 50 |
]] |
| 51 |
|
| 52 |
INSTALLATION = [[ |
| 53 |
Copy the file ptex2pdf.lua into a directory where scripts are found, |
| 54 |
that is for example |
| 55 |
|
| 56 |
`TLROOT/texmf-dist/scripts/ptex2pdf/` |
| 57 |
|
| 58 |
(where `TLROOT` is for example the root of your TeX Live installation) |
| 59 |
|
| 60 |
### Unix ### |
| 61 |
|
| 62 |
create a link in one of the bin dirs to the above file, in the |
| 63 |
TeX Live case: |
| 64 |
|
| 65 |
`TLROOT/bin/ARCH/ptex2pdf -> ../../texmf-dist/scripts/ptex2pdf/ptex2pdf.lua` |
| 66 |
|
| 67 |
### Windows ### |
| 68 |
create a copy of runscript.exe as ptex2pdf.exe, in the TeX Live case: |
| 69 |
|
| 70 |
`copy TLROOT/bin/win32/runscript.exe TLROOT/bin/win32/ptex2pdf.exe` |
| 71 |
]] |
| 72 |
|
| 73 |
TEXWORKS = [[ |
| 74 |
Under Preferences > Typesetting add new entries, for example: |
| 75 |
|
| 76 |
for ptex files: |
| 77 |
|
| 78 |
| Setting | Value | |
| 79 |
|-------------|--------------------| |
| 80 |
| Name: | pTeX (ptex2pdf) | |
| 81 |
| Program: | ptex2pdf | |
| 82 |
| Arguments: | -ot | |
| 83 |
| | $synctexoption | |
| 84 |
| | $fullname | |
| 85 |
|
| 86 |
for platex files: |
| 87 |
|
| 88 |
| Setting | Value | |
| 89 |
|-------------|--------------------| |
| 90 |
| Name: | pLaTeX (ptex2pdf) | |
| 91 |
| Program: | ptex2pdf | |
| 92 |
| Arguments: | -l | |
| 93 |
| | -ot | |
| 94 |
| | $synctexoption | |
| 95 |
| | $fullname | |
| 96 |
|
| 97 |
for uptex files: |
| 98 |
|
| 99 |
| Setting | Value | |
| 100 |
|-------------|--------------------| |
| 101 |
| Name: | upTeX (ptex2pdf) | |
| 102 |
| Program: | ptex2pdf | |
| 103 |
| Arguments: | -u | |
| 104 |
| | -ot | |
| 105 |
| | $synctexoption | |
| 106 |
| | $fullname | |
| 107 |
|
| 108 |
for uplatex files: |
| 109 |
|
| 110 |
| Setting | Value | |
| 111 |
|-------------|--------------------| |
| 112 |
| Name: | upLaTeX (ptex2pdf) | |
| 113 |
| Program: | ptex2pdf | |
| 114 |
| Arguments: | -l | |
| 115 |
| | -u | |
| 116 |
| | -ot | |
| 117 |
| | $synctexoption | |
| 118 |
| | $fullname | |
| 119 |
|
| 120 |
If you need special kanji encodings for one of these programs, |
| 121 |
add the respective `-kanji` option with the `$synctexoption`. Example: |
| 122 |
|
| 123 |
for platex files in SJIS encoding: |
| 124 |
|
| 125 |
| Setting | Value | |
| 126 |
|-------------|----------------------------| |
| 127 |
| Name: | pLaTeX/SJIS (ptex2pdf) | |
| 128 |
| Program: | ptex2pdf | |
| 129 |
| Arguments: | -l | |
| 130 |
| | -ot | |
| 131 |
| | -kanji=sjis $synctexoption | |
| 132 |
| | $fullname | |
| 133 |
]] |
| 134 |
|
| 135 |
DEVELPLACE = "http://github.com/texjporg/ptex2pdf" |
| 136 |
|
| 137 |
|
| 138 |
CHANGELOG = [[ |
| 139 |
- version 0.1 2013-03-08 NP |
| 140 |
Initial release on blog |
| 141 |
- version 0.2 2013-03-10 NP |
| 142 |
import into git repository |
| 143 |
support passing options on to tex and dvipdfm |
| 144 |
add README with TeXworks config options |
| 145 |
- version 0.3 2013-05-01 NP |
| 146 |
include the readme in the lua code |
| 147 |
fix program name for -e -u |
| 148 |
- version 0.4 2013-05-07 NP |
| 149 |
quote the filename with ", so that special chars do survive |
| 150 |
add an example for TeXworks for files with different kanji encoding |
| 151 |
- version 0.5 2014-11-05 NP |
| 152 |
on Windows: set command_line_encoding to utf8 when running uptex |
| 153 |
(patch by Akira Kakuto) |
| 154 |
- version 0.6 2015-03-08 NP |
| 155 |
cygwin didn't like the (accidentally inserted) spaces after the |
| 156 |
texlua in the shebang line, and stopped working with |
| 157 |
"no such program: "texlua " ..." |
| 158 |
- version 0.7 2015-04-29 |
| 159 |
move to github as gitorious will be closed, adapt help output |
| 160 |
to generate github flavored markdown |
| 161 |
check for files using kpathsea instead of opening directly, to allow |
| 162 |
for input of files found by kpathsea (closes github issue 1) |
| 163 |
- version 0.8 2015-06-15 |
| 164 |
file name checks: first search for arg as is, then try .tex and .ltx |
| 165 |
(closes github issue: 3) |
| 166 |
- version 0.9 2016-12-12 |
| 167 |
allow for files in sub-directories |
| 168 |
add -output-directory option |
| 169 |
update copyright and development place (now in texjp) |
| 170 |
support 'flag=val' to specify option values |
| 171 |
only allow one (1) filename argument |
| 172 |
- version 20170603.0 |
| 173 |
start version number in the format YYYYMMDD.0 |
| 174 |
better support for cp932 windows filenames |
| 175 |
first replace all backslash chars to slash chars |
| 176 |
- version 20170622.0 |
| 177 |
pass all non-optional arguments before filename to TeX engine |
| 178 |
- version 20180514.0 |
| 179 |
Windows: for uptex use command_line_encoding=utf8, for all other turn |
| 180 |
it off (set to none) |
| 181 |
- version 20181212.0 |
| 182 |
support directories containing dots (thanks kn1cht) |
| 183 |
- version 20200119.0 |
| 184 |
check invalid PATH string beforehand on windows |
| 185 |
- version 20200307.0 |
| 186 |
add -ld option to run (u)platex-dev |
| 187 |
- version 20200520.0 |
| 188 |
Windows: lua mode - support non-ascii file name on recent luatex |
| 189 |
]] |
| 190 |
|
| 191 |
|
| 192 |
function usage() |
| 193 |
print(USAGE) |
| 194 |
end |
| 195 |
|
| 196 |
function makereadme() |
| 197 |
print("# " .. NAME .. " #") |
| 198 |
print() |
| 199 |
print("**Author:** " .. AUTHOR .. " ") |
| 200 |
print("**Website:** http://github.com/texjporg/ptex2pdf ") |
| 201 |
print("**License:** GPLv2") |
| 202 |
print() |
| 203 |
print(SHORTDESC) |
| 204 |
print() |
| 205 |
print("## Description ##") |
| 206 |
print() |
| 207 |
print(LONGDESC) |
| 208 |
print("## Usage ##") |
| 209 |
print() |
| 210 |
print("`````") |
| 211 |
print(USAGE) |
| 212 |
print("`````") |
| 213 |
print() |
| 214 |
print("## Installation ##") |
| 215 |
print() |
| 216 |
print(INSTALLATION) |
| 217 |
print("## TeXworks setup ##") |
| 218 |
print() |
| 219 |
print(TEXWORKS) |
| 220 |
print() |
| 221 |
print("## Development place ##") |
| 222 |
print() |
| 223 |
print(DEVELPLACE) |
| 224 |
print() |
| 225 |
print("## Changelog ##") |
| 226 |
print() |
| 227 |
print(CHANGELOG) |
| 228 |
print("## Copyright and License ##") |
| 229 |
print() |
| 230 |
print(LICENSECOPYRIGHT) |
| 231 |
end |
| 232 |
|
| 233 |
function help() |
| 234 |
print(NAME .. ": " .. SHORTDESC) |
| 235 |
print() |
| 236 |
print("Author: " .. AUTHOR) |
| 237 |
print() |
| 238 |
print(LONGDESC) |
| 239 |
print(USAGE) |
| 240 |
end |
| 241 |
|
| 242 |
function fullhelp() |
| 243 |
help() |
| 244 |
print("Installation") |
| 245 |
print("------------") |
| 246 |
print(INSTALLATION) |
| 247 |
print("TeXworks setup") |
| 248 |
print("--------------") |
| 249 |
print(TEXWORKS) |
| 250 |
print("Development place") |
| 251 |
print("-----------------") |
| 252 |
print(DEVELPLACE) |
| 253 |
print() |
| 254 |
print("Copyright and License") |
| 255 |
print("---------------------") |
| 256 |
print(LICENSECOPYRIGHT) |
| 257 |
end |
| 258 |
|
| 259 |
function whoami () |
| 260 |
print("This is " .. NAME .. " version ".. VERSION .. ".") |
| 261 |
end |
| 262 |
|
| 263 |
function print_ifdebug(message) |
| 264 |
|
| 265 |
end |
| 266 |
|
| 267 |
function slashify(str) |
| 268 |
return (tostring(str):gsub("[\x81-\x9f\xe0-\xfc]?.", { ["\\"] = "/" })) |
| 269 |
end |
| 270 |
|
| 271 |
if #arg == 0 then |
| 272 |
usage() |
| 273 |
os.exit(0) |
| 274 |
end |
| 275 |
|
| 276 |
|
| 277 |
tex = "ptex" |
| 278 |
texopts = "" |
| 279 |
dvipdf = "dvipdfmx" |
| 280 |
dvipdfopts = "" |
| 281 |
intermediate = 1 |
| 282 |
|
| 283 |
use_eptex = 0 |
| 284 |
use_uptex = 0 |
| 285 |
use_latex = 0 |
| 286 |
use_latexdev = 0 |
| 287 |
outputdir = "." |
| 288 |
prefilename = "" |
| 289 |
filename = "" |
| 290 |
bname = "" |
| 291 |
exit_code = 0 |
| 292 |
narg = 1 |
| 293 |
repeat |
| 294 |
this_arg = arg[narg] |
| 295 |
|
| 296 |
this_arg = string.gsub(this_arg, "^%-%-", "-") |
| 297 |
|
| 298 |
if this_arg == "-v" then |
| 299 |
whoami() |
| 300 |
os.exit(0) |
| 301 |
elseif this_arg == "-readme" then |
| 302 |
makereadme() |
| 303 |
os.exit(0) |
| 304 |
elseif this_arg == "-output-directory" then |
| 305 |
narg = narg+1 |
| 306 |
outputdir = arg[narg] |
| 307 |
elseif (string.sub(this_arg, 1, 18) == "-output-directory=") then |
| 308 |
outputdir = string.sub(this_arg, 19, -1) |
| 309 |
elseif this_arg == "-print-version" then |
| 310 |
print(VERSION) |
| 311 |
os.exit(0) |
| 312 |
elseif this_arg == "-h" then |
| 313 |
help() |
| 314 |
os.exit(0) |
| 315 |
elseif this_arg == "-help" then |
| 316 |
fullhelp() |
| 317 |
os.exit(0) |
| 318 |
elseif this_arg == "-e" then |
| 319 |
use_eptex = 1 |
| 320 |
elseif this_arg == "-u" then |
| 321 |
use_uptex = 1 |
| 322 |
elseif this_arg == "-l" then |
| 323 |
use_latex = 1 |
| 324 |
elseif this_arg == "-ld" then |
| 325 |
use_latex = 1 |
| 326 |
use_latexdev = 1 |
| 327 |
elseif this_arg == "-s" then |
| 328 |
dvipdf = "" |
| 329 |
elseif this_arg == "-i" then |
| 330 |
intermediate = 0 |
| 331 |
elseif this_arg == "-ot" then |
| 332 |
narg = narg+1 |
| 333 |
texopts = arg[narg] |
| 334 |
elseif (string.sub(this_arg, 1, 4) == "-ot=") then |
| 335 |
texopts = string.sub(this_arg, 5, -1) |
| 336 |
elseif this_arg == "-od" then |
| 337 |
narg = narg+1 |
| 338 |
dvipdfopts = arg[narg] |
| 339 |
elseif (string.sub(this_arg, 1, 4) == "-od=") then |
| 340 |
dvipdfopts = string.sub(this_arg, 5, -1) |
| 341 |
else |
| 342 |
if filename == "" then |
| 343 |
filename = this_arg |
| 344 |
else |
| 345 |
|
| 346 |
print("Multiple filename arguments? OK, I'll take the latter one.") |
| 347 |
prefilename = prefilename .. " \"" .. filename .. "\"" |
| 348 |
filename = this_arg |
| 349 |
end |
| 350 |
end |
| 351 |
narg = narg+1 |
| 352 |
until narg > #arg |
| 353 |
|
| 354 |
whoami() |
| 355 |
|
| 356 |
if use_latex == 1 then |
| 357 |
if use_uptex == 1 then |
| 358 |
if use_latexdev == 1 then |
| 359 |
tex = "uplatex-dev" |
| 360 |
else |
| 361 |
tex = "uplatex" |
| 362 |
end |
| 363 |
else |
| 364 |
if use_latexdev == 1 then |
| 365 |
tex = "platex-dev" |
| 366 |
else |
| 367 |
tex = "platex" |
| 368 |
end |
| 369 |
end |
| 370 |
else |
| 371 |
if use_eptex == 1 then |
| 372 |
if use_uptex == 1 then |
| 373 |
tex = "euptex" |
| 374 |
else |
| 375 |
tex = "eptex" |
| 376 |
end |
| 377 |
else |
| 378 |
if use_uptex == 1 then |
| 379 |
tex = "uptex" |
| 380 |
else |
| 381 |
tex = "ptex" |
| 382 |
end |
| 383 |
end |
| 384 |
end |
| 385 |
|
| 386 |
function is_texlivew32() |
| 387 |
if status.luatex_version < 112 then |
| 388 |
return false |
| 389 |
end |
| 390 |
if os.type == 'windows' |
| 391 |
and kpse.var_value('command_line_encoding') ~= nil then |
| 392 |
return true |
| 393 |
else |
| 394 |
return false |
| 395 |
end |
| 396 |
end |
| 397 |
|
| 398 |
|
| 399 |
kpse.set_program_name(tex) |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
if ( filename == "" ) then |
| 406 |
print("No filename argument given, exiting.") |
| 407 |
os.exit(1) |
| 408 |
else |
| 409 |
if is_texlivew32() then |
| 410 |
filename = chgstrcp.syscptoutf8(filename) |
| 411 |
end |
| 412 |
filename = slashify(filename) |
| 413 |
if ( kpse.find_file(filename) == nil ) then |
| 414 |
|
| 415 |
if ( kpse.find_file(filename .. ".tex") == nil ) then |
| 416 |
|
| 417 |
if ( kpse.find_file(filename .. ".ltx") == nil ) then |
| 418 |
print("File cannot be found with kpathsea: ", filename .. "[.tex, .ltx]") |
| 419 |
os.exit(1) |
| 420 |
else |
| 421 |
if is_texlivew32() then |
| 422 |
filename = chgstrcp.utf8tosyscp(filename) |
| 423 |
end |
| 424 |
bname = filename |
| 425 |
filename = filename .. ".ltx" |
| 426 |
end |
| 427 |
else |
| 428 |
if is_texlivew32() then |
| 429 |
filename = chgstrcp.utf8tosyscp(filename) |
| 430 |
end |
| 431 |
bname = filename |
| 432 |
filename = filename .. ".tex" |
| 433 |
end |
| 434 |
else |
| 435 |
|
| 436 |
if is_texlivew32() then |
| 437 |
filename = chgstrcp.utf8tosyscp(filename) |
| 438 |
end |
| 439 |
bname = string.gsub(filename, "^(.*)%.[^./]+$", "%1") |
| 440 |
end |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
bname = string.gsub(bname, "^.*/(.*)$", "%1") |
| 445 |
end |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
if os.type == 'windows' then |
| 450 |
if use_uptex == 1 then |
| 451 |
os.setenv('command_line_encoding', 'utf8') |
| 452 |
else |
| 453 |
os.setenv('command_line_encoding', 'none') |
| 454 |
end |
| 455 |
end |
| 456 |
if (outputdir ~= ".") then |
| 457 |
texopts = "-output-directory \"" .. outputdir .. "\" " .. texopts |
| 458 |
bname = outputdir .. "/" .. bname |
| 459 |
dvipdfopts = "-o \"" .. bname .. ".pdf\"" |
| 460 |
end |
| 461 |
print("Processing ".. filename) |
| 462 |
texcmd = tex .. " " .. texopts .. prefilename .. " \"" .. filename .. "\"" |
| 463 |
tex_return = os.execute(texcmd) |
| 464 |
|
| 465 |
|
| 466 |
if os.type == 'windows' and tex_return == -1 then |
| 467 |
print("Invalid PATH setting found. Please ensure that cmd.exe can be found.\n") |
| 468 |
end |
| 469 |
dvipdfcmd = dvipdf .. " " .. dvipdfopts .. " \"" .. bname .. ".dvi" .. "\"" |
| 470 |
if tex_return == 0 and |
| 471 |
(dvipdf == "" or (os.execute(dvipdfcmd) == 0)) then |
| 472 |
if dvipdf ~= "" then |
| 473 |
print(bname .. ".pdf generated by " .. dvipdf .. ".") |
| 474 |
end |
| 475 |
if intermediate == 1 then |
| 476 |
if dvipdf ~= "" then |
| 477 |
os.remove( bname .. ".dvi" ) |
| 478 |
end |
| 479 |
end |
| 480 |
else |
| 481 |
print("ptex2pdf processing of " .. filename .. " failed.\n") |
| 482 |
print_ifdebug("tex = " .. texcmd) |
| 483 |
print_ifdebug("dvipdf = " .. dvipdfcmd) |
| 484 |
os.exit(2) |
| 485 |
end |
| 486 |
|
| 487 |
|
| 488 |
os.exit( 0 ) |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|