| 1 |
#!/usr/bin/env perl |
| 2 |
use v5.26; |
| 3 |
|
| 4 |
############################# LICENCE ################################## |
| 5 |
# v0.20 2020-08-24 simplify the use of PSTricks with pdf # |
| 6 |
# (c) Herbert Voss <[email protected]> # |
| 7 |
# Pablo González L <[email protected]> # |
| 8 |
# # |
| 9 |
# This program is free software; you can redistribute it and/or modify # |
| 10 |
# it under the terms of the GNU General Public License as published by # |
| 11 |
# the Free Software Foundation; either version 3 of the License, or # |
| 12 |
# (at your option) any later version. # |
| 13 |
# # |
| 14 |
# This program is distributed in the hope that it will be useful, but # |
| 15 |
# WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # |
| 17 |
# General Public License for more details. # |
| 18 |
######################################################################## |
| 19 |
|
| 20 |
use Getopt::Long qw(:config bundling_override no_ignore_case); # require_order |
| 21 |
use File::Spec::Functions qw(catfile devnull); |
| 22 |
use File::Basename; |
| 23 |
use Archive::Tar; |
| 24 |
use Data::Dumper; |
| 25 |
use FileHandle; |
| 26 |
use IO::Compress::Zip qw(:all); |
| 27 |
use File::Path qw(remove_tree); |
| 28 |
use File::Temp qw(tempdir); |
| 29 |
use POSIX qw(strftime); |
| 30 |
use File::Copy; |
| 31 |
use File::Find; |
| 32 |
use Env qw(PATH); |
| 33 |
use autodie; |
| 34 |
use Config; |
| 35 |
use Cwd; |
| 36 |
use if $^O eq 'MSWin32', 'Win32'; |
| 37 |
use if $^O eq 'MSWin32', 'Win32::Console::ANSI'; # need for color :) |
| 38 |
use Term::ANSIColor; |
| 39 |
|
| 40 |
### Internal vars |
| 41 |
my $tempDir = tempdir( CLEANUP => 1); # temporary directory |
| 42 |
my $workdir = cwd; # current working dir |
| 43 |
my $imgdir = 'images'; # where to save the images |
| 44 |
my $margins = '1'; # margins in pdfcrop |
| 45 |
my $clear = 0; # 0 or 1, clears all temporary files |
| 46 |
my $dpi = '300'; # very low value for the png's |
| 47 |
my $runs = '1'; # set runs for compliler |
| 48 |
my $nocorp = 0; # 1->no crop pdf files created |
| 49 |
my $norun = 0; # 1->no create images |
| 50 |
my $noprew = 0; # 1->create images in nopreview mode |
| 51 |
my $runbibtex = 0; # 1->runs bibtex |
| 52 |
my $runbiber = 0; # 1->runs biber and sets $runbibtex=0 |
| 53 |
my $xetex = 0; # 1->Using (Xe)LaTeX for compilation. |
| 54 |
my $luatex = 0; # 1->Using dvilualatex for compilation. |
| 55 |
my $latexmk = 0; # 1->Using latexmk for compiler output file. |
| 56 |
my $arara; # 1->Using arara for compiler output file. |
| 57 |
my $nosource = 0; # Delete TeX source for images |
| 58 |
my $srcenv; # write only source code of environments |
| 59 |
my $nopdf; # Don't create a pdf image files |
| 60 |
my $force; # try to capture \psset |
| 61 |
my $nocrop; # Don't crop image files |
| 62 |
my $zip; # compress files generated in .zip |
| 63 |
my $tar; # compress files generated in .tar.gz |
| 64 |
my $help; # help info |
| 65 |
my $version; # version info |
| 66 |
my $shell; # enable -shell-escape |
| 67 |
my $verbose = 0; # verbose info |
| 68 |
my @verb_env_tmp; # store verbatim environments |
| 69 |
my $tmpverbenv; # save verbatim environment |
| 70 |
my $myverb = 'myverb' ; # internal \myverb macro |
| 71 |
my $write18; # storing write18 for compiler in TeXLive and MikTeX |
| 72 |
my $gscmd; # ghostscript executable name |
| 73 |
my $gray; # gray scale ghostscript |
| 74 |
my $log = 0; # log file |
| 75 |
my $PSTexa = 0; # run extract PSTexample environments |
| 76 |
my $STDenv = 0; # run extract pspicture/psfrag environments |
| 77 |
my @currentopt; # storing current options for log file |
| 78 |
my %opts_cmd; # hash to store options for Getopt::Long and log |
| 79 |
|
| 80 |
### Script identification |
| 81 |
my $scriptname = 'pst2pdf'; |
| 82 |
my $nv = 'v0.20'; |
| 83 |
my $date = '2020-08-22'; |
| 84 |
my $ident = '$Id: pst2pdf.pl 119 2020-08-22 12:04:09Z herbert $'; |
| 85 |
|
| 86 |
### Log vars |
| 87 |
my $LogFile = "$scriptname.log"; |
| 88 |
my $LogWrite; |
| 89 |
my $LogTime = strftime("%y/%m/%d %H:%M:%S", localtime); |
| 90 |
|
| 91 |
### Error in command line |
| 92 |
sub errorUsage { |
| 93 |
my $msg = shift; |
| 94 |
die color('red').'* Error!!: '.color('reset').$msg. |
| 95 |
" (run $scriptname --help for more information)\n"; |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
### Extended error messages |
| 100 |
sub exterr { |
| 101 |
chomp(my $msg_errno = $!); |
| 102 |
chomp(my $msg_extended_os_error = $^E); |
| 103 |
if ($msg_errno eq $msg_extended_os_error) { |
| 104 |
$msg_errno; |
| 105 |
} |
| 106 |
else { |
| 107 |
"$msg_errno/$msg_extended_os_error"; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
### Funtion uniq |
| 112 |
sub uniq { |
| 113 |
my %seen; |
| 114 |
return grep !$seen{$_}++, @_; |
| 115 |
} |
| 116 |
|
| 117 |
### Funtion array_minus |
| 118 |
sub array_minus(\@\@) { |
| 119 |
my %e = map{ $_ => undef } @{$_[1]}; |
| 120 |
return grep !exists $e{$_}, @{$_[0]}; |
| 121 |
} |
| 122 |
|
| 123 |
### Funtion to create hash begin -> BEGIN, end -> END |
| 124 |
sub crearhash { |
| 125 |
my %cambios; |
| 126 |
for my $aentra(@_){ |
| 127 |
for my $initend (qw(begin end)) { |
| 128 |
$cambios{"\\$initend\{$aentra"} = "\\\U$initend\E\{$aentra"; |
| 129 |
} |
| 130 |
} |
| 131 |
return %cambios; |
| 132 |
} |
| 133 |
|
| 134 |
### Print colored info in screen |
| 135 |
sub Infocolor { |
| 136 |
my $type = shift; |
| 137 |
my $info = shift; |
| 138 |
if ($type eq 'Running') { |
| 139 |
print color('cyan'), '* ', color('reset'), color('green'), |
| 140 |
"$type: ", color('reset'), color('cyan'), "$info\r\n", color('reset'); |
| 141 |
} |
| 142 |
if ($type eq 'Warning') { |
| 143 |
print color('bold red'), "* $type: ", color('reset'), |
| 144 |
color('yellow'), "$info\r\n", color('reset'); |
| 145 |
} |
| 146 |
if ($type eq 'Finish') { |
| 147 |
print color('cyan'), '* ', color('reset'), color('magenta'), |
| 148 |
"$type!: ", color('reset'), color('green'), "$info\r\n",color('reset'); |
| 149 |
} |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
### Write Log line and print msg (common) |
| 154 |
sub Infoline { |
| 155 |
my $msg = shift; |
| 156 |
my $now = strftime("%y/%m/%d %H:%M:%S", localtime); |
| 157 |
if ($log) { $LogWrite->print(sprintf "[%s] * %s\n", $now, $msg); } |
| 158 |
say $msg; |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
### Write Log line (no print msg and time stamp) |
| 163 |
sub Logline { |
| 164 |
my $msg = shift; |
| 165 |
if ($log) { $LogWrite->print("$msg\n"); } |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
### Write Log line (time stamp) |
| 170 |
sub Log { |
| 171 |
my $msg = shift; |
| 172 |
my $now = strftime("%y/%m/%d %H:%M:%S", localtime); |
| 173 |
if ($log) { $LogWrite->print(sprintf "[%s] * %s\n", $now, $msg); } |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
### Write array env in Log |
| 178 |
sub Logarray { |
| 179 |
my ($env_ref) = @_; |
| 180 |
my @env_tmp = @{ $env_ref }; # dereferencing and copying each array |
| 181 |
if ($log) { |
| 182 |
if (@env_tmp) { |
| 183 |
my $tmp = join "\n", map { qq/* $_/ } @env_tmp; |
| 184 |
print {$LogWrite} "$tmp\n"; |
| 185 |
} |
| 186 |
else { |
| 187 |
print {$LogWrite} "Not found\n"; |
| 188 |
} |
| 189 |
} |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
### Extended print info for execute system commands using $ command |
| 194 |
sub Logrun { |
| 195 |
my $msg = shift; |
| 196 |
my $now = strftime("%y/%m/%d %H:%M:%S", localtime); |
| 197 |
if ($log) { $LogWrite->print(sprintf "[%s] \$ %s\n", $now, $msg); } |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
### Capture and execute system commands |
| 202 |
sub RUNOSCMD { |
| 203 |
my $cmdname = shift; |
| 204 |
my $argcmd = shift; |
| 205 |
my $showmsg = shift; |
| 206 |
my $captured = "$cmdname $argcmd"; |
| 207 |
Logrun($captured); |
| 208 |
if ($showmsg eq 'show') { |
| 209 |
if ($verbose) { |
| 210 |
Infocolor('Running', $captured); |
| 211 |
} |
| 212 |
else{ Infocolor('Running', $cmdname); } |
| 213 |
} |
| 214 |
if ($showmsg eq 'only' and $verbose) { |
| 215 |
Infocolor('Running', $captured); |
| 216 |
} |
| 217 |
# Run system system command |
| 218 |
$captured = qx{$captured}; |
| 219 |
if ($log) { $LogWrite->print($captured); } |
| 220 |
if ($? == -1) { |
| 221 |
my $errorlog = "* Error!!: ".$cmdname." failed to execute (%s)!\n"; |
| 222 |
my $errorprint = "* Error!!: ".color('reset').$cmdname." failed to execute (%s)!\n"; |
| 223 |
if ($log) { $LogWrite->print(sprintf $errorlog, exterr); } |
| 224 |
print STDERR color('red'); |
| 225 |
die sprintf $errorprint, exterr; |
| 226 |
} elsif ($? & 127) { |
| 227 |
my $errorlog = "* Error!!: ".$cmdname." died with signal %d!\n"; |
| 228 |
my $errorprint = "* Error!!: ".color('reset').$cmdname." died with signal %d!\n"; |
| 229 |
if ($log) { $LogWrite->print(sprintf $errorlog, ($? & 127)); } |
| 230 |
print STDERR color('red'); |
| 231 |
die sprintf $errorprint, ($? & 127); |
| 232 |
} elsif ($? != 0 ) { |
| 233 |
my $errorlog = "* Error!!: ".$cmdname." exited with error code %d!\n"; |
| 234 |
my $errorprint = "* Error!!: ".color('reset').$cmdname." exited with error code %d!\n"; |
| 235 |
if ($log) { $LogWrite->print(sprintf $errorlog, $? >> 8); } |
| 236 |
print STDERR color('red'); |
| 237 |
die sprintf $errorprint, $? >> 8; |
| 238 |
} |
| 239 |
if ($verbose) { print $captured; } |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
### General information |
| 244 |
my $copyright = <<"END_COPYRIGHT" ; |
| 245 |
[$date] (c) Herbert Voss <hvoss\@tug.org> and Pablo González L <pablgonz\@yahoo.com>. |
| 246 |
END_COPYRIGHT |
| 247 |
|
| 248 |
my $versiontxt= <<"END_VERSION" ; |
| 249 |
${scriptname} ${nv} ${ident} |
| 250 |
${copyright} |
| 251 |
END_VERSION |
| 252 |
|
| 253 |
### Standart info in terminal |
| 254 |
my $title = "$scriptname $nv - Running a PSTricks document with (pdf/xe/lua)latex [HV \& PG]\n"; |
| 255 |
|
| 256 |
### Usage of script |
| 257 |
sub usage { |
| 258 |
find_ghostscript(); |
| 259 |
|
| 260 |
my $usage = <<"END_OF_USAGE"; |
| 261 |
${title} |
| 262 |
pst2pdf is a Perl script which isolates all PostScript or PSTricks related |
| 263 |
parts of the TeX document, read all postscript, pspicture, psgraph and |
| 264 |
PSTexample environments, extract source code in standalone files and |
| 265 |
converting them into image format. Create new file with all extracted |
| 266 |
environments converted to \\includegraphics and runs (pdf/xe/lua)latex. |
| 267 |
|
| 268 |
Usage: $scriptname [<options>] <texfile>[.tex|.ltx] |
| 269 |
$scriptname <texfile>[.tex|.ltx] [options] |
| 270 |
|
| 271 |
If used without [<options>] the extracted environments are saved in |
| 272 |
standalone files and converted to pdf image format in the "./images" |
| 273 |
directory using "latex>dvips>ps2pdf" and "preview" package to process |
| 274 |
<texfile> and "pdflatex" for compiler <texfile-pdf>. |
| 275 |
|
| 276 |
Options: |
| 277 |
[default] |
| 278 |
-l, --log Write .log file with debug information [off] |
| 279 |
-h, --help Display command line help and exit [off] |
| 280 |
-v, --version Display current version ($nv) and exit [off] |
| 281 |
-V, --verbose Verbose printing information [off] |
| 282 |
-t, --tif Create .tif files using ghostscript [$gscmd] |
| 283 |
-b, --bmp Create .bmp files using ghostscript [$gscmd] |
| 284 |
-j, --jpg Create .jpg files using ghostscript [$gscmd] |
| 285 |
-p, --png Create .png files using ghostscript [$gscmd] |
| 286 |
-e, --eps Create .eps files using poppler-utils [pdftops] |
| 287 |
-s, --svg Create .svg files using poppler-utils [pdftocairo] |
| 288 |
-P, --ppm Create .ppm files using poppler-utils [pdftoppm] |
| 289 |
-g, --gray Gray scale for images using ghostscript [off] |
| 290 |
-f, --force Try to capture \\psset{...} to extract [off] |
| 291 |
-x, --xetex Using xelatex for compiler input and output [off] |
| 292 |
-ns,--nosource Do not create standalone files [off] |
| 293 |
-np, --noprew, --single |
| 294 |
Create images files without preview package [off] |
| 295 |
-ni,--norun, --noimages |
| 296 |
Generate file-pdf.tex, but not images [off] |
| 297 |
-d <integer>, --dpi <integer> |
| 298 |
Dots per inch resolution for images [150] |
| 299 |
-r <integer>, --runs <integer> |
| 300 |
Set the number of times the compiler will |
| 301 |
run on the input file for extraction [1] |
| 302 |
-m <integer>, --margins <integer> |
| 303 |
Set margins in bp for pdfcrop [0] |
| 304 |
--myverb <macro> Add "\\macroname" to verbatim inline search [myverb] |
| 305 |
--ignore <environment> |
| 306 |
Add "environment" to verbatim environments [empty] |
| 307 |
--srcenv Create files with only code environment [off] |
| 308 |
--shell Enable \\write18\{SHELL COMMAND\} [off] |
| 309 |
--nopdf Do not create images in pdf format [off] |
| 310 |
--nocrop Does not run pdfcrop [off] |
| 311 |
--imgdir <dirname> Set name of directory to save images/files [images] |
| 312 |
--luatex Using dvilualatex>dvips>ps2pdf for compiler |
| 313 |
input and lualatex for compiler output file [off] |
| 314 |
--arara Use arara for compiler output file [off] |
| 315 |
--latexmk Using latexmk for compiler output file [off] |
| 316 |
--zip Compress generated files in .zip format [off] |
| 317 |
--tar Compress generated files in .tar.gz format [off] |
| 318 |
--bibtex Run bibtex on the .aux file (if exists) [off] |
| 319 |
--biber Run biber on the .bcf file (if exists) [off] |
| 320 |
|
| 321 |
Example: |
| 322 |
\$ $scriptname -e -p --imgdir pics test.tex |
| 323 |
* Create a ./pics directory (if it doesn't exist) with all extracted |
| 324 |
* environments converted to individual files (.pdf, .eps, .png, .tex), |
| 325 |
* a file test-fig-all.tex with all extracted environments and the file |
| 326 |
* test-pdf.tex with all environments converted to \\includegraphics using |
| 327 |
* latex>dvips>ps2pdf and preview package for <test.tex> and pdflatex |
| 328 |
* for <test-pdf.tex>. |
| 329 |
|
| 330 |
See texdoc pst2pdf for full documentation. |
| 331 |
END_OF_USAGE |
| 332 |
print $usage; |
| 333 |
exit 0; |
| 334 |
} |
| 335 |
|
| 336 |
### Options in terminal |
| 337 |
my $result=GetOptions ( |
| 338 |
'h|help' => \$help, # flag |
| 339 |
'v|version' => \$version, # flag |
| 340 |
'l|log' => \$log, # flag |
| 341 |
'f|force' => \$force, # flag |
| 342 |
'd|dpi=i' => \$dpi, # numeric |
| 343 |
'runs=i' => \$runs, # numeric |
| 344 |
'm|margins=i' => \$margins, # numeric |
| 345 |
'imgdir=s' => \$imgdir, # string |
| 346 |
'myverb=s' => \$myverb, # string |
| 347 |
'ignore=s' => \$tmpverbenv, # string |
| 348 |
'c|clear' => \$clear, # flag (unused) |
| 349 |
'ni|norun|noimages' => \$norun, # flag |
| 350 |
'np|noprew|single' => \$noprew, # flag |
| 351 |
'bibtex' => \$runbibtex,# flag |
| 352 |
'biber' => \$runbiber, # flag |
| 353 |
'arara' => \$arara, # flag |
| 354 |
'latexmk' => \$latexmk, # flag |
| 355 |
'srcenv' => \$srcenv, # flag |
| 356 |
'nopdf' => \$nopdf, # flag |
| 357 |
'shell' => \$shell, # flag |
| 358 |
'nocrop' => \$nocrop, # flag |
| 359 |
'zip' => \$zip, # flag |
| 360 |
'tar' => \$tar, # flag |
| 361 |
'g|gray' => \$gray, # flag |
| 362 |
'b|bmp' => \$opts_cmd{image}{bmp}, # gs |
| 363 |
't|tif' => \$opts_cmd{image}{tif}, # gs |
| 364 |
'j|jpg' => \$opts_cmd{image}{jpg}, # gs |
| 365 |
'p|png' => \$opts_cmd{image}{png}, # gs |
| 366 |
's|svg' => \$opts_cmd{image}{svg}, # pdftocairo |
| 367 |
'e|eps' => \$opts_cmd{image}{eps}, # pdftops |
| 368 |
'P|ppm' => \$opts_cmd{image}{ppm}, # pdftoppm |
| 369 |
'x|xetex' => \$xetex, # flag |
| 370 |
'luatex' => \$luatex, # flag |
| 371 |
'ns|nosource' => \$nosource, # flag |
| 372 |
'V|Verbose' => \$verbose, # flag |
| 373 |
) or do { $log = 0 ; die usage(0); }; |
| 374 |
|
| 375 |
### Open pst2pdf.log file |
| 376 |
if ($log) { |
| 377 |
if (!defined $ARGV[0]) { errorUsage('Input filename missing'); } |
| 378 |
my $tempname = $ARGV[0]; |
| 379 |
$tempname =~ s/\.(tex|ltx)$//; |
| 380 |
if ($LogFile eq "$tempname.log") { $LogFile = "$scriptname-log.log"; } |
| 381 |
$LogWrite = FileHandle->new("> $LogFile"); |
| 382 |
} |
| 383 |
|
| 384 |
### Init pst2pdf.log file |
| 385 |
Log("The script $scriptname $nv was started in $workdir"); |
| 386 |
Log("Creating the temporary directory $tempDir"); |
| 387 |
|
| 388 |
### Check --arara and --latexmk |
| 389 |
if ($arara && $latexmk) { |
| 390 |
Log('Error!!: Options --arara and --latexmk are mutually exclusive'); |
| 391 |
errorUsage('Options --arara and --latexmk are mutually exclusive'); |
| 392 |
} |
| 393 |
|
| 394 |
### Check --biber and --bibtex |
| 395 |
if ($runbiber && $runbibtex) { |
| 396 |
Log('Error!!: Options --biber and --bibtex are mutually exclusive'); |
| 397 |
errorUsage('Options --biber and --bibtex are mutually exclusive'); |
| 398 |
} |
| 399 |
|
| 400 |
### Check --runs |
| 401 |
if( $runs <= 0 or $runs >= 3) { |
| 402 |
Log('Error!!: Invalid argument for --runs, argument out of range'); |
| 403 |
errorUsage('Invalid argument for --runs option'); |
| 404 |
} |
| 405 |
|
| 406 |
### Check --dpi |
| 407 |
if( $dpi <= 0 or $dpi >= 2500) { |
| 408 |
Log('Error!!: Invalid argument for --dpi, argument out of range'); |
| 409 |
errorUsage('Invalid argument for --dpi option'); |
| 410 |
} |
| 411 |
|
| 412 |
### Validate --myverb |
| 413 |
if ($myverb =~ /^(?:\\|\-).+?/) { |
| 414 |
Log('Error!!: Invalid argument for --myverb option, argument begin with \ or -'); |
| 415 |
errorUsage('Invalid argument for --myverb'); |
| 416 |
} |
| 417 |
|
| 418 |
### Validate --ignore |
| 419 |
if ($tmpverbenv =~ /^(?:\\|\-).+?/) { |
| 420 |
Log('Error!!: Invalid argument for --ignore option, argument begin with \ or -'); |
| 421 |
errorUsage('Invalid argument for --ignore'); |
| 422 |
} |
| 423 |
|
| 424 |
### If $tmpverbenv is valid pass to @verb_env_tmp |
| 425 |
if ($tmpverbenv) { @verb_env_tmp = $tmpverbenv; } |
| 426 |
|
| 427 |
### Make ENV safer, see perldoc perlsec |
| 428 |
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; |
| 429 |
|
| 430 |
### The next code it's part of pdfcrop (adapted from TexLive 2014) |
| 431 |
# Windows detection |
| 432 |
my $Win = 0; |
| 433 |
if ($^O =~ /mswin32/i) { $Win = 1; } |
| 434 |
|
| 435 |
my $archname = $Config{'archname'}; |
| 436 |
$archname = 'unknown' unless defined $Config{'archname'}; |
| 437 |
|
| 438 |
# Get ghostscript command name |
| 439 |
sub find_ghostscript () { |
| 440 |
if ($log) { |
| 441 |
Log('General information about the Perl instalation and operating system'); |
| 442 |
print {$LogWrite} "* Perl executable: $^X\n"; |
| 443 |
if ($] < 5.006) { |
| 444 |
print {$LogWrite} "* Perl version: $]\n"; |
| 445 |
} |
| 446 |
else { |
| 447 |
printf {$LogWrite} "* Perl version: v%vd\n", $^V; |
| 448 |
} |
| 449 |
if (defined &ActivePerl::BUILD) { |
| 450 |
printf {$LogWrite} "* Perl product: ActivePerl, build %s\n", ActivePerl::BUILD(); |
| 451 |
} |
| 452 |
printf {$LogWrite} "* Pointer size: $Config{'ptrsize'}\n"; |
| 453 |
printf {$LogWrite} "* Pipe support: %s\n", |
| 454 |
(defined $Config{'d_pipe'} ? 'yes' : 'no'); |
| 455 |
printf {$LogWrite} "* Fork support: %s\n", |
| 456 |
(defined $Config{'d_fork'} ? 'yes' : 'no'); |
| 457 |
} |
| 458 |
my $system = 'unix'; |
| 459 |
$system = 'win' if $^O =~ /mswin32/i; |
| 460 |
$system = 'msys' if $^O =~ /msys/i; |
| 461 |
$system = 'cygwin' if $^O =~ /cygwin/i; |
| 462 |
$system = 'miktex' if defined $ENV{'TEXSYSTEM'} and |
| 463 |
$ENV{'TEXSYSTEM'} =~ /miktex/i; |
| 464 |
if ($log) { |
| 465 |
print {$LogWrite} "* OS name: $^O\n"; |
| 466 |
print {$LogWrite} "* Arch name: $archname\n"; |
| 467 |
if ($^O eq 'MSWin32') { |
| 468 |
my $tmp = Win32::GetOSName(); |
| 469 |
print {$LogWrite} "* System: $tmp\n"; |
| 470 |
} |
| 471 |
else { print {$LogWrite} "* System: $system\n"; } |
| 472 |
} |
| 473 |
Log('Trying to locate the executable for Ghostscript'); |
| 474 |
my %candidates = ( |
| 475 |
'unix' => [qw|gs|], |
| 476 |
'win' => [qw|gswin32c|], |
| 477 |
'msys' => [qw|gswin64c gswin32c|], |
| 478 |
'cygwin' => [qw|gs|], |
| 479 |
'miktex' => [qw|mgs gswin32c|], |
| 480 |
); |
| 481 |
if ($system eq 'win' or $system eq 'miktex') { |
| 482 |
if ($archname =~ /mswin32-x64/i) { |
| 483 |
my @a = (); |
| 484 |
foreach my $name (@{$candidates{$system}}) { |
| 485 |
push @a, 'gswin64c' if $name eq 'gswin32c'; |
| 486 |
push @a, $name; |
| 487 |
} |
| 488 |
$candidates{$system} = \@a; |
| 489 |
} |
| 490 |
} |
| 491 |
my %exe = ( |
| 492 |
'unix' => q{}, |
| 493 |
'win' => '.exe', |
| 494 |
'msys' => '.exe', |
| 495 |
'cygwin' => '.exe', |
| 496 |
'miktex' => '.exe', |
| 497 |
); |
| 498 |
my $candidates_ref = $candidates{$system}; |
| 499 |
my $exe = $Config{'_exe'}; |
| 500 |
$exe = $exe{$system} unless defined $exe; |
| 501 |
my @path = File::Spec->path(); |
| 502 |
my $found = 0; |
| 503 |
foreach my $candidate (@$candidates_ref) { |
| 504 |
foreach my $dir (@path) { |
| 505 |
my $file = File::Spec->catfile($dir, "$candidate$exe"); |
| 506 |
if (-x $file) { |
| 507 |
$gscmd = $candidate; |
| 508 |
$found = 1; |
| 509 |
if ($log) { print {$LogWrite} "* Found ($candidate): $file\n"; } |
| 510 |
last; |
| 511 |
} |
| 512 |
if ($log) { print {$LogWrite} "* Not found ($candidate): $file\n"; } |
| 513 |
} |
| 514 |
last if $found; |
| 515 |
} |
| 516 |
if (not $found and $Win and $system ne 'msys') { |
| 517 |
$found = SearchRegistry(); |
| 518 |
} |
| 519 |
if (not $found and $system eq 'msys') { |
| 520 |
$found = Searchbyregquery(); |
| 521 |
} |
| 522 |
if ($found) { |
| 523 |
if ($log) { print {$LogWrite} "* Autodetected ghostscript command: $gscmd\n"; } |
| 524 |
} |
| 525 |
else { |
| 526 |
$gscmd = $$candidates_ref[0]; |
| 527 |
if ($log) { print {$LogWrite} "* Default ghostscript command: $gscmd\n"; } |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
sub SearchRegistry { |
| 532 |
my $found = 0; |
| 533 |
# The module Win32::TieRegistry not aviable in cygwin/msys |
| 534 |
eval 'use Win32::TieRegistry qw|KEY_READ REG_SZ|'; |
| 535 |
if ($@) { |
| 536 |
if ($log) { |
| 537 |
print {$LogWrite} "* Registry lookup for Ghostscript failed:\n"; |
| 538 |
my $msg = $@; |
| 539 |
$msg =~ s/\s+$//; |
| 540 |
foreach (split /\r?\n/, $msg) { |
| 541 |
print {$LogWrite} " $_\n"; |
| 542 |
} |
| 543 |
} |
| 544 |
return $found; |
| 545 |
} |
| 546 |
my $open_params = {Access => KEY_READ(), Delimiter => q{/}}; |
| 547 |
my $key_name_software = 'HKEY_LOCAL_MACHINE/SOFTWARE/'; |
| 548 |
my $current_key = $key_name_software; |
| 549 |
my $software = new Win32::TieRegistry $current_key, $open_params; |
| 550 |
if (not $software) { |
| 551 |
if ($log) { |
| 552 |
print {$LogWrite} "* Cannot find or access registry key `$current_key'!\n"; |
| 553 |
} |
| 554 |
return $found; |
| 555 |
} |
| 556 |
if ($log) { print {$LogWrite} "* Search registry at `$current_key'.\n"; } |
| 557 |
my %list; |
| 558 |
foreach my $key_name_gs (grep /Ghostscript/i, $software->SubKeyNames()) { |
| 559 |
$current_key = "$key_name_software$key_name_gs/"; |
| 560 |
if ($log) { print {$LogWrite} "* Registry entry found: $current_key\n"; } |
| 561 |
my $key_gs = $software->Open($key_name_gs, $open_params); |
| 562 |
if (not $key_gs) { |
| 563 |
if ($log) { print {$LogWrite} "* Cannot open registry key `$current_key'!\n"; } |
| 564 |
next; |
| 565 |
} |
| 566 |
foreach my $key_name_version ($key_gs->SubKeyNames()) { |
| 567 |
$current_key = "$key_name_software$key_name_gs/$key_name_version/"; |
| 568 |
if ($log) { print {$LogWrite} "* Registry entry found: $current_key\n"; } |
| 569 |
if (not $key_name_version =~ /^(\d+)\.(\d+)$/) { |
| 570 |
if ($log) { print {$LogWrite} " The sub key is not a version number!\n"; } |
| 571 |
next; |
| 572 |
} |
| 573 |
my $version_main = $1; |
| 574 |
my $version_sub = $2; |
| 575 |
$current_key = "$key_name_software$key_name_gs/$key_name_version/"; |
| 576 |
my $key_version = $key_gs->Open($key_name_version, $open_params); |
| 577 |
if (not $key_version) { |
| 578 |
if ($log) { print {$LogWrite} "* Cannot open registry key `$current_key'!\n"; } |
| 579 |
next; |
| 580 |
} |
| 581 |
$key_version->FixSzNulls(1); |
| 582 |
my ($value, $type) = $key_version->GetValue('GS_DLL'); |
| 583 |
if ($value and $type == REG_SZ()) { |
| 584 |
if ($log) { print {$LogWrite} " GS_DLL = $value\n"; } |
| 585 |
$value =~ s|([\\/])([^\\/]+\.dll)$|$1gswin32c.exe|i; |
| 586 |
my $value64 = $value; |
| 587 |
$value64 =~ s/gswin32c\.exe$/gswin64c.exe/; |
| 588 |
if ($archname =~ /mswin32-x64/i and -f $value64) { |
| 589 |
$value = $value64; |
| 590 |
} |
| 591 |
if (-f $value) { |
| 592 |
if ($log) { print {$LogWrite} "EXE found: $value\n"; } |
| 593 |
} |
| 594 |
else { |
| 595 |
if ($log) { print {$LogWrite} "EXE not found!\n"; } |
| 596 |
next; |
| 597 |
} |
| 598 |
my $sortkey = sprintf '%02d.%03d %s', |
| 599 |
$version_main, $version_sub, $key_name_gs; |
| 600 |
$list{$sortkey} = $value; |
| 601 |
} |
| 602 |
else { |
| 603 |
if ($log) { print {$LogWrite} "Missing key `GS_DLL' with type `REG_SZ'!\n"; } |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
foreach my $entry (reverse sort keys %list) { |
| 608 |
$gscmd = $list{$entry}; |
| 609 |
if ($log) { print {$LogWrite} "* Found (via registry): $gscmd\n"; } |
| 610 |
$found = 1; |
| 611 |
last; |
| 612 |
} |
| 613 |
return $found; |
| 614 |
} # end GS search registry |
| 615 |
|
| 616 |
### This part is only necessary if you're using Git on windows and don't |
| 617 |
### have gs configured in PATH. Git for windows don't have a Win32::TieRegistry |
| 618 |
### module for perl (is not supported in the current versions of msys). |
| 619 |
sub Searchbyregquery { |
| 620 |
my $found = 0; |
| 621 |
my $gs_regkey; |
| 622 |
my $opt_reg = '//s //v'; |
| 623 |
if ($log) { print {$LogWrite} "* Search Ghostscript in Windows registry under mingw/msys:\n";} |
| 624 |
$gs_regkey = qx{reg query "HKLM\\Software\\GPL Ghostscript" $opt_reg GS_DLL}; |
| 625 |
if ($? == 0) { |
| 626 |
if ($log) { print {$LogWrite} "* Registry entry found for GS_DLL (64 bits version)\n";} |
| 627 |
} |
| 628 |
else { |
| 629 |
$gs_regkey = qx{reg query "HKLM\\Software\\Wow6432Node\\GPL Ghostscript" $opt_reg GS_DLL}; |
| 630 |
if ($? == 0) { |
| 631 |
if ($log) { print {$LogWrite} "* Registry entry found for GS_DLL (32 bits version)\n";} |
| 632 |
} |
| 633 |
} |
| 634 |
my ($gs_find) = $gs_regkey =~ m/(?:\s* GS_DLL \s* REG_SZ \s*) (.+?)(?:\.dll.+?\R)/s; |
| 635 |
if ($gs_find) { |
| 636 |
my ($gs_vol, $gs_path, $gs_ver) = $gs_find =~ m/ |
| 637 |
(\w{1})(?:\:) # volumen |
| 638 |
(.+?) # path to executable |
| 639 |
(?:\\gsdll) # LIB |
| 640 |
(\d{2}) # Version |
| 641 |
/xs; |
| 642 |
# Adjust |
| 643 |
$gs_vol = lc($gs_vol); |
| 644 |
$gs_path = '/'.$gs_vol.$gs_path; |
| 645 |
$gs_path =~ s|\\|/|gmsxi; |
| 646 |
# Add to PATH |
| 647 |
if ($log) { print {$LogWrite} "* Add $gs_path to PATH for current session\n"; } |
| 648 |
$PATH .= ":$gs_path"; |
| 649 |
# Set executable |
| 650 |
$gscmd = 'gswin'.$gs_ver.'c'; |
| 651 |
if ($log) { print {$LogWrite} "* Found (via reg query): $gscmd\n"; } |
| 652 |
$found = 1; |
| 653 |
} |
| 654 |
if ($@) { |
| 655 |
if ($log) { |
| 656 |
print {$LogWrite} "* Registry lookup for Ghostscript by reg query failed:\n"; |
| 657 |
my $msg = $@; |
| 658 |
$msg =~ s/\s+$//; |
| 659 |
foreach (split /\r?\n/, $msg) { |
| 660 |
print {$LogWrite} " $_\n"; |
| 661 |
} |
| 662 |
} |
| 663 |
return $found; |
| 664 |
} |
| 665 |
return $found; |
| 666 |
} |
| 667 |
|
| 668 |
### Call GS |
| 669 |
find_ghostscript(); |
| 670 |
|
| 671 |
### Windows need suport space in path |
| 672 |
if ($Win and $gscmd =~ /\s/) { $gscmd = "\"$gscmd\"";} |
| 673 |
|
| 674 |
### Help |
| 675 |
if (defined $help) { |
| 676 |
usage(1); |
| 677 |
exit 0; |
| 678 |
} |
| 679 |
|
| 680 |
### Version |
| 681 |
if (defined $version) { |
| 682 |
print $versiontxt; |
| 683 |
exit 0; |
| 684 |
} |
| 685 |
|
| 686 |
### Set temp internal vars for <name-fig-tmp> and extraction |
| 687 |
my $tmp = int(rand(10000)); |
| 688 |
my $dtxverb = "verbatim$tmp"; |
| 689 |
my $wrapping = "$scriptname$tmp"; # wraped for environment extraction |
| 690 |
|
| 691 |
Log("Set up the environment [$wrapping] to encapsulate the extraction"); |
| 692 |
|
| 693 |
### Set vars for match/regex |
| 694 |
my $BP = "\\\\begin\{$wrapping\}"; |
| 695 |
my $EP = "\\\\end\{$wrapping\}"; |
| 696 |
my $BE = '\\\\begin\{PSTexample\}'; |
| 697 |
my $EE = '\\\\end\{PSTexample\}'; |
| 698 |
|
| 699 |
### Define key = pdf for image format |
| 700 |
if (!$nopdf) { |
| 701 |
$opts_cmd{image}{pdf} = 1; |
| 702 |
} |
| 703 |
|
| 704 |
### Store image formats in hash |
| 705 |
my $format = join q{, },grep { defined $opts_cmd{image}{$_} } keys %{$opts_cmd{image}}; |
| 706 |
if (!$norun) { |
| 707 |
Log("Defined image formats for creating: $format"); |
| 708 |
} |
| 709 |
|
| 710 |
### Check <input file> from command line |
| 711 |
@ARGV > 0 or errorUsage('Input filename missing'); |
| 712 |
@ARGV < 2 or errorUsage('Unknown option or too many input files'); |
| 713 |
|
| 714 |
### Check <input file> extention |
| 715 |
my @SuffixList = ('.tex', '', '.ltx'); # posibles |
| 716 |
my ($name, $path, $ext) = fileparse($ARGV[0], @SuffixList); |
| 717 |
$ext = '.tex' if not $ext; |
| 718 |
|
| 719 |
### Read <input file> in memory |
| 720 |
Log("Read input file $name$ext in memory"); |
| 721 |
open my $inputfile, '<:crlf', "$name$ext"; |
| 722 |
my $ltxfile; |
| 723 |
{ |
| 724 |
local $/; |
| 725 |
$ltxfile = <$inputfile>; |
| 726 |
} |
| 727 |
close $inputfile; |
| 728 |
|
| 729 |
### Identification message in terminal |
| 730 |
print $title; |
| 731 |
|
| 732 |
### Default environment to extract |
| 733 |
my @extractenv = qw ( |
| 734 |
postscript pspicture psgraph PSTexample |
| 735 |
); |
| 736 |
|
| 737 |
### Default verbatim environment |
| 738 |
my @iverb_env = qw ( |
| 739 |
Example CenterExample SideBySideExample PCenterExample PSideBySideExample |
| 740 |
verbatim Verbatim BVerbatim LVerbatim SaveVerbatim PSTcode |
| 741 |
LTXexample tcblisting spverbatim minted listing lstlisting |
| 742 |
alltt comment chklisting verbatimtab listingcont boxedverbatim |
| 743 |
demo sourcecode xcomment pygmented pyglist program programl |
| 744 |
programL programs programf programsc programt |
| 745 |
); |
| 746 |
push @verb_env_tmp, @iverb_env; |
| 747 |
|
| 748 |
### Default verbatim write environment |
| 749 |
my @verw_env_tmp; |
| 750 |
my @iverbw_env = qw ( |
| 751 |
scontents filecontents tcboutputlisting tcbexternal tcbwritetmp extcolorbox extikzpicture |
| 752 |
VerbatimOut verbatimwrite filecontentsdef filecontentshere filecontentsdefmacro |
| 753 |
filecontentsdefstarred filecontentsgdef filecontentsdefmacro filecontentsgdefmacro |
| 754 |
); |
| 755 |
push @verw_env_tmp, @iverbw_env; |
| 756 |
|
| 757 |
######################################################################## |
| 758 |
# One problem that can arise is the filecontents environment, this can # |
| 759 |
# contain a complete document and be anywhere, before dividing we will # |
| 760 |
# make some replacements for this and comment lines # |
| 761 |
######################################################################## |
| 762 |
|
| 763 |
### Create a Regex for verbatim write environment |
| 764 |
@verw_env_tmp = uniq(@verw_env_tmp); |
| 765 |
my $tmpverbw = join q{|}, map { quotemeta } sort { length $a <=> length $b } @verw_env_tmp; |
| 766 |
$tmpverbw = qr/$tmpverbw/x; |
| 767 |
my $tmp_verbw = qr { |
| 768 |
( |
| 769 |
(?: |
| 770 |
\\begin\{$tmpverbw\*?\} |
| 771 |
(?: |
| 772 |
(?>[^\\]+)| |
| 773 |
\\ |
| 774 |
(?!begin\{$tmpverbw\*?\}) |
| 775 |
(?!end\{$tmpverbw\*?\})| |
| 776 |
(?-1) |
| 777 |
)* |
| 778 |
\\end\{$tmpverbw\*?\} |
| 779 |
) |
| 780 |
) |
| 781 |
}x; |
| 782 |
|
| 783 |
### A pre-regex for comment lines |
| 784 |
my $tmpcomment = qr/^ \s* \%+ .+? $ /mx; |
| 785 |
|
| 786 |
### Hash for replace in verbatim's and comment lines |
| 787 |
my %document = ( |
| 788 |
'\begin{document}' => '\BEGIN{document}', |
| 789 |
'\end{document}' => '\END{document}', |
| 790 |
'\documentclass' => '\DOCUMENTCLASS', |
| 791 |
'\pagestyle{' => '\PAGESTYLE{', |
| 792 |
'\thispagestyle{' => '\THISPAGESTYLE{', |
| 793 |
); |
| 794 |
|
| 795 |
### Changes in input file for verbatim write and comment lines |
| 796 |
while ($ltxfile =~ / $tmp_verbw | $tmpcomment /pgmx) { |
| 797 |
my ($pos_inicial, $pos_final) = ($-[0], $+[0]); |
| 798 |
my $encontrado = ${^MATCH}; |
| 799 |
while (my($busco, $cambio) = each %document) { |
| 800 |
$encontrado =~ s/\Q$busco\E/$cambio/g; |
| 801 |
} |
| 802 |
substr $ltxfile, $pos_inicial, $pos_final-$pos_inicial, $encontrado; |
| 803 |
pos ($ltxfile) = $pos_inicial + length $encontrado; |
| 804 |
} |
| 805 |
|
| 806 |
### Now, split <input file> |
| 807 |
my ($atbegindoc, $document) = $ltxfile =~ m/\A (\s* .*? \s*) (\\documentclass.*)\z/msx; |
| 808 |
|
| 809 |
### Rules to capture for regex my $CORCHETES = qr/\[ [^]]*? \]/x; |
| 810 |
my $braces = qr/ (?:\{)(.+?)(?:\}) /msx; |
| 811 |
my $braquet = qr/ (?:\[)(.+?)(?:\]) /msx; |
| 812 |
my $no_corchete = qr/ (?:\[ .*? \])? /msx; |
| 813 |
|
| 814 |
### Array for capture new verbatim environments defined in input file |
| 815 |
my @cverb_env = qw ( |
| 816 |
newtcblisting DeclareTCBListing ProvideTCBListing NewTCBListing |
| 817 |
lstnewenvironment NewListingEnvironment NewProgram specialcomment |
| 818 |
includecomment DefineVerbatimEnvironment newverbatim newtabverbatim |
| 819 |
); |
| 820 |
|
| 821 |
### Regex to capture names for new verbatim environments from input file |
| 822 |
my $cverbenv = join q{|}, map { quotemeta} sort { length $a <=> length $b } @cverb_env; |
| 823 |
$cverbenv = qr/\b(?:$cverbenv) $no_corchete $braces/msx; |
| 824 |
|
| 825 |
### Array for capture new verbatim write environments defined in input file |
| 826 |
my @cverb_env_w = qw ( |
| 827 |
renewtcbexternalizetcolorbox renewtcbexternalizeenvironment |
| 828 |
newtcbexternalizeenvironment newtcbexternalizetcolorbox newenvsc |
| 829 |
); |
| 830 |
|
| 831 |
### Regex to capture names for new verbatim write environments from input file |
| 832 |
my $cverbenvw = join q{|}, map { quotemeta} sort { length $a <=> length $b } @cverb_env_w; |
| 833 |
$cverbenvw = qr/\b(?:$cverbenvw) $no_corchete $braces/msx; |
| 834 |
|
| 835 |
### Regex to capture MINTED related environments |
| 836 |
my $mintdenv = qr/\\ newminted $braces (?:\{.+?\}) /x; |
| 837 |
my $mintcenv = qr/\\ newminted $braquet (?:\{.+?\}) /x; |
| 838 |
my $mintdshrt = qr/\\ newmint $braces (?:\{.+?\}) /x; |
| 839 |
my $mintcshrt = qr/\\ newmint $braquet (?:\{.+?\}) /x; |
| 840 |
my $mintdline = qr/\\ newmintinline $braces (?:\{.+?\}) /x; |
| 841 |
my $mintcline = qr/\\ newmintinline $braquet (?:\{.+?\}) /x; |
| 842 |
|
| 843 |
### Filter input file, now $ltxfile is pass to $filecheck |
| 844 |
Log("Filter $name$ext \(remove % and comments\)"); |
| 845 |
my @filecheck = $ltxfile; |
| 846 |
s/%.*\n//mg foreach @filecheck; # del comments |
| 847 |
s/^\s*|\s*//mg foreach @filecheck; # del white space |
| 848 |
my $filecheck = join q{}, @filecheck; |
| 849 |
|
| 850 |
### Search verbatim and verbatim write environments <input file> |
| 851 |
Log("Search verbatim and verbatim write environments in $name$ext"); |
| 852 |
|
| 853 |
### Search new verbatim write names in <input file> |
| 854 |
my @new_write_env = $filecheck =~ m/$cverbenvw/xg; |
| 855 |
if (@new_write_env) { |
| 856 |
Log("Found new verbatim write environments in $name$ext"); |
| 857 |
Logarray(\@new_write_env); |
| 858 |
push @verw_env_tmp, @new_write_env; |
| 859 |
} |
| 860 |
|
| 861 |
### Search new verbatim environments in <input file> |
| 862 |
my @verb_input = $filecheck =~ m/$cverbenv/xg; |
| 863 |
if (@verb_input) { |
| 864 |
Log("Found new verbatim environments in $name$ext"); |
| 865 |
Logarray(\@verb_input); |
| 866 |
push @verb_env_tmp, @verb_input; |
| 867 |
} |
| 868 |
|
| 869 |
### Search \newminted{$mintdenv}{options} in <input file>, need add "code" |
| 870 |
my @mint_denv = $filecheck =~ m/$mintdenv/xg; |
| 871 |
if (@mint_denv) { |
| 872 |
Log("Found \\newminted\{envname\} in $name$ext"); |
| 873 |
# Append "code" |
| 874 |
$mintdenv = join "\n", map { qq/$_\Qcode\E/ } @mint_denv; |
| 875 |
@mint_denv = split /\n/, $mintdenv; |
| 876 |
Logarray(\@mint_denv); |
| 877 |
push @verb_env_tmp, @mint_denv; |
| 878 |
} |
| 879 |
|
| 880 |
### Search \newminted[$mintcenv]{lang} in <input file> |
| 881 |
my @mint_cenv = $filecheck =~ m/$mintcenv/xg; |
| 882 |
if (@mint_cenv) { |
| 883 |
Log("Found \\newminted\[envname\] in $name$ext"); |
| 884 |
Logarray(\@mint_cenv); |
| 885 |
push @verb_env_tmp, @mint_cenv; |
| 886 |
} |
| 887 |
|
| 888 |
### Remove repetead again :) |
| 889 |
@verb_env_tmp = uniq(@verb_env_tmp); |
| 890 |
|
| 891 |
### Capture verbatim inline macros in <input file> |
| 892 |
Log("Search verbatim macros in $name$ext"); |
| 893 |
|
| 894 |
### Store all minted inline/short in @mintline |
| 895 |
my @mintline; |
| 896 |
|
| 897 |
### Search \newmint{$mintdshrt}{options} in <input file> (while) |
| 898 |
my @mint_dshrt = $filecheck =~ m/$mintdshrt/xg; |
| 899 |
if (@mint_dshrt) { |
| 900 |
Log("Found \\newmint\{macroname\} (short) in $name$ext"); |
| 901 |
Logarray(\@mint_dshrt); |
| 902 |
push @mintline, @mint_dshrt; |
| 903 |
} |
| 904 |
|
| 905 |
### Search \newmint[$mintcshrt]{lang}{options} in <input file> (while) |
| 906 |
my @mint_cshrt = $filecheck =~ m/$mintcshrt/xg; |
| 907 |
if (@mint_cshrt) { |
| 908 |
Log("Found \\newmint\[macroname\] (short) in $name$ext"); |
| 909 |
Logarray(\@mint_cshrt); |
| 910 |
push @mintline, @mint_cshrt; |
| 911 |
} |
| 912 |
|
| 913 |
### Search \newmintinline{$mintdline}{options} in <input file> (while) |
| 914 |
my @mint_dline = $filecheck =~ m/$mintdline/xg; |
| 915 |
if (@mint_dline) { |
| 916 |
Log("Found \\newmintinline\{macroname\} in $name$ext"); |
| 917 |
# Append "inline" |
| 918 |
$mintdline = join "\n", map { qq/$_\Qinline\E/ } @mint_dline; |
| 919 |
@mint_dline = split /\n/, $mintdline; |
| 920 |
Logarray(\@mint_dline); |
| 921 |
push @mintline, @mint_dline; |
| 922 |
} |
| 923 |
|
| 924 |
### Search \newmintinline[$mintcline]{lang}{options} in <input file> (while) |
| 925 |
my @mint_cline = $filecheck =~ m/$mintcline/xg; |
| 926 |
if (@mint_cline) { |
| 927 |
Log("Found \\newmintinline\[macroname\] in $name$ext"); |
| 928 |
Logarray(\@mint_cline); |
| 929 |
push @mintline, @mint_cline; |
| 930 |
} |
| 931 |
|
| 932 |
### Add standart mint, mintinline and lstinline |
| 933 |
my @mint_tmp = qw(mint mintinline lstinline); |
| 934 |
|
| 935 |
### Join all inline verbatim macros captured |
| 936 |
push @mintline, @mint_tmp; |
| 937 |
@mintline = uniq(@mintline); |
| 938 |
|
| 939 |
### Create a regex using @mintline |
| 940 |
my $mintline = join q{|}, map { quotemeta } sort { length $a <=> length $b } @mintline; |
| 941 |
$mintline = qr/\b(?:$mintline)/x; |
| 942 |
|
| 943 |
### Reserved words in verbatim inline (while) |
| 944 |
my %changes_in = ( |
| 945 |
'%CleanPST' => '%PSTCLEAN', |
| 946 |
'\psset' => '\PSSET', |
| 947 |
'\pspicture' => '\TRICKS', |
| 948 |
'\endpspicture' => '\ENDTRICKS', |
| 949 |
'\psgraph' => '\PSGRAPHTRICKS', |
| 950 |
'\endpsgraph' => '\ENDPSGRAPHTRICKS', |
| 951 |
'\usepackage' => '\USEPACKAGE', |
| 952 |
'{graphicx}' => '{GRAPHICX}', |
| 953 |
'\graphicspath{' => '\GRAPHICSPATH{', |
| 954 |
); |
| 955 |
|
| 956 |
### Hash to replace \begin and \end in verbatim inline |
| 957 |
my %init_end = ( |
| 958 |
'\begin{' => '\BEGIN{', |
| 959 |
'\end{' => '\END{', |
| 960 |
); |
| 961 |
|
| 962 |
### Join changes in new hash (while) for verbatim inline |
| 963 |
my %cambios = (%changes_in,%init_end); |
| 964 |
|
| 965 |
### Variables and constantes |
| 966 |
my $no_del = "\0"; |
| 967 |
my $del = $no_del; |
| 968 |
|
| 969 |
### Rules |
| 970 |
my $llaves = qr/\{ .+? \} /x; |
| 971 |
my $no_llaves = qr/(?: $llaves )? /x; |
| 972 |
my $corchetes = qr/\[ .+? \] /x; |
| 973 |
my $delimitador = qr/\{ (?<del>.+?) \} /x; |
| 974 |
my $scontents = qr/Scontents [*]? $no_corchete /ix; |
| 975 |
my $verb = qr/(?:((spv|(?:q|f)?v|V)erb|$myverb)[*]?) /ix; |
| 976 |
my $lst = qr/(?:(lst|pyg)inline)(?!\*) $no_corchete /ix; |
| 977 |
my $mint = qr/(?: $mintline |SaveVerb) (?!\*) $no_corchete $no_llaves $llaves /ix; |
| 978 |
my $no_mint = qr/(?: $mintline) (?!\*) $no_corchete /ix; |
| 979 |
my $marca = qr/\\ (?:$verb | $lst |$scontents | $mint |$no_mint) (?:\s*)? (\S) .+? \g{-1} /sx; |
| 980 |
my $comentario = qr/^ \s* \%+ .+? $ /mx; |
| 981 |
my $definedel = qr/\\ (?: DefineShortVerb | lstMakeShortInline| MakeSpecialShortVerb ) [*]? $no_corchete $delimitador /ix; |
| 982 |
my $indefinedel = qr/\\ (?: (Undefine|Delete)ShortVerb | lstDeleteShortInline) $llaves /ix; |
| 983 |
|
| 984 |
Log('Making changes to inline/multiline verbatim macro before extraction'); |
| 985 |
|
| 986 |
### Changes in input file for verbatim inline/multiline |
| 987 |
while ($document =~ |
| 988 |
/ $marca |
| 989 |
| $comentario |
| 990 |
| $definedel |
| 991 |
| $indefinedel |
| 992 |
| $del .+? $del |
| 993 |
/pgmx) { |
| 994 |
my ($pos_inicial, $pos_final) = ($-[0], $+[0]); |
| 995 |
my $encontrado = ${^MATCH}; |
| 996 |
if ($encontrado =~ /$definedel/) { |
| 997 |
$del = $+{del}; |
| 998 |
$del = "\Q$+{del}" if substr($del,0,1) ne '\\'; |
| 999 |
} |
| 1000 |
elsif ($encontrado =~ /$indefinedel/) { |
| 1001 |
$del = $no_del; |
| 1002 |
} |
| 1003 |
else { |
| 1004 |
while (my($busco, $cambio) = each %cambios) { |
| 1005 |
$encontrado =~ s/\Q$busco\E/$cambio/g; |
| 1006 |
} |
| 1007 |
substr $document, $pos_inicial, $pos_final-$pos_inicial, $encontrado; |
| 1008 |
pos ($document) = $pos_inicial + length $encontrado; |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
### Change "escaped braces" to <LTXSB.> (this label is not the one in the document) |
| 1013 |
$document =~ s/\\[{]/<LTXSBO>/g; |
| 1014 |
$document =~ s/\\[}]/<LTXSBC>/g; |
| 1015 |
|
| 1016 |
### Regex for verbatim inline/multiline with braces {...} |
| 1017 |
my $nestedbr = qr / ( [{] (?: [^{}]++ | (?-1) )*+ [}] ) /x; |
| 1018 |
my $fvextra = qr /\\ (?: (Save|Esc)Verb [*]?) $no_corchete /x; |
| 1019 |
my $mintedbr = qr /\\ (?:$mintline|pygment) (?!\*) $no_corchete $no_llaves /x; |
| 1020 |
my $tcbxverb = qr /\\ (?: tcboxverb [*]?| Scontents [*]? |$myverb [*]?|lstinline) $no_corchete /x; |
| 1021 |
my $verb_brace = qr / (?:$tcbxverb|$mintedbr|$fvextra) (?:\s*)? $nestedbr /x; |
| 1022 |
|
| 1023 |
### Change \verb*{code} for verbatim inline/multiline |
| 1024 |
while ($document =~ /$verb_brace/pgmx) { |
| 1025 |
my ($pos_inicial, $pos_final) = ($-[0], $+[0]); |
| 1026 |
my $encontrado = ${^MATCH}; |
| 1027 |
while (my($busco, $cambio) = each %cambios) { |
| 1028 |
$encontrado =~ s/\Q$busco\E/$cambio/g; |
| 1029 |
} |
| 1030 |
substr $document, $pos_inicial, $pos_final-$pos_inicial, $encontrado; |
| 1031 |
pos ($document) = $pos_inicial + length $encontrado; |
| 1032 |
} |
| 1033 |
|
| 1034 |
### We recovered the escaped braces |
| 1035 |
$document =~ s/<LTXSBO>/\\{/g; |
| 1036 |
$document =~ s/<LTXSBC>/\\}/g; |
| 1037 |
|
| 1038 |
### We recovered CleanPST in all file, but only at begin of line |
| 1039 |
$document =~ s/^%PSTCLEAN/%CleanPST/gmsx; |
| 1040 |
|
| 1041 |
### First we do some security checks to ensure that they are verbatim and |
| 1042 |
### verbatim write environments are unique and disjointed |
| 1043 |
@verb_env_tmp = array_minus(@verb_env_tmp, @verw_env_tmp); #disjointed |
| 1044 |
my @verbatim = uniq(@verb_env_tmp); |
| 1045 |
|
| 1046 |
Log('The environments that are considered verbatim:'); |
| 1047 |
Logarray(\@verbatim); |
| 1048 |
|
| 1049 |
### Create a Regex for verbatim standart environment |
| 1050 |
my $verbatim = join q{|}, map { quotemeta } sort { length $a <=> length $b } @verbatim; |
| 1051 |
$verbatim = qr/$verbatim/x; |
| 1052 |
my $verb_std = qr { |
| 1053 |
( |
| 1054 |
(?: |
| 1055 |
\\begin\{$verbatim\*?\} |
| 1056 |
(?: |
| 1057 |
(?>[^\\]+)| |
| 1058 |
\\ |
| 1059 |
(?!begin\{$verbatim\*?\}) |
| 1060 |
(?!end\{$verbatim\*?\})| |
| 1061 |
(?-1) |
| 1062 |
)* |
| 1063 |
\\end\{$verbatim\*?\} |
| 1064 |
) |
| 1065 |
) |
| 1066 |
}x; |
| 1067 |
|
| 1068 |
### Verbatim write |
| 1069 |
@verw_env_tmp = array_minus(@verw_env_tmp, @verb_env_tmp); #disjointed |
| 1070 |
my @verbatim_w = uniq(@verw_env_tmp); |
| 1071 |
|
| 1072 |
Log('The environments that are considered verbatim write:'); |
| 1073 |
Logarray(\@verbatim_w); |
| 1074 |
|
| 1075 |
### Create a Regex for verbatim write environment |
| 1076 |
my $verbatim_w = join q{|}, map { quotemeta } sort { length $a <=> length $b } @verbatim_w; |
| 1077 |
$verbatim_w = qr/$verbatim_w/x; |
| 1078 |
my $verb_wrt = qr { |
| 1079 |
( |
| 1080 |
(?: |
| 1081 |
\\begin\{$verbatim_w\*?\} |
| 1082 |
(?: |
| 1083 |
(?>[^\\]+)| |
| 1084 |
\\ |
| 1085 |
(?!begin\{$verbatim_w\*?\}) |
| 1086 |
(?!end\{$verbatim_w\*?\})| |
| 1087 |
(?-1) |
| 1088 |
)* |
| 1089 |
\\end\{$verbatim_w\*?\} |
| 1090 |
) |
| 1091 |
) |
| 1092 |
}x; |
| 1093 |
|
| 1094 |
### The environments that will be searched for extraction |
| 1095 |
Log('The environments that will be searched for extraction:'); |
| 1096 |
Logarray(\@extractenv); |
| 1097 |
|
| 1098 |
### Create a regex to extract environments |
| 1099 |
my $environ = join q{|}, map { quotemeta } sort { length $a <=> length $b } @extractenv; |
| 1100 |
$environ = qr/$environ/x; |
| 1101 |
my $extr_tmp = qr { |
| 1102 |
( |
| 1103 |
(?: |
| 1104 |
\\begin\{$environ\*?\} |
| 1105 |
(?: |
| 1106 |
(?>[^\\]+)| |
| 1107 |
\\ |
| 1108 |
(?!begin\{$environ\*?\}) |
| 1109 |
(?!end\{$environ\*?\})| |
| 1110 |
(?-1) |
| 1111 |
)* |
| 1112 |
\\end\{$environ\*?\} |
| 1113 |
) |
| 1114 |
) |
| 1115 |
}x; |
| 1116 |
|
| 1117 |
### Hash for replace in verbatim begin -> Begin end -> END |
| 1118 |
my %extract_env = crearhash(@extractenv); |
| 1119 |
|
| 1120 |
### The preview and nopreview environments are "special", need replace |
| 1121 |
### in verbatim's environments begin -> Begin end -> END |
| 1122 |
my @preview_env = qw(preview nopreview); |
| 1123 |
my %preview_env = crearhash(@preview_env); |
| 1124 |
|
| 1125 |
Log('Making changes to verbatim/verbatim write environments before extraction'); |
| 1126 |
|
| 1127 |
### Hash and Regex for changes, this "regex" is re-used in ALL script |
| 1128 |
my %replace = (%extract_env, %preview_env, %changes_in, %document); |
| 1129 |
my $find = join q{|}, map { quotemeta } sort { length $a <=> length $b } keys %replace; |
| 1130 |
|
| 1131 |
### We go line by line and make the changes [need /p for ${^MATCH}] |
| 1132 |
while ($document =~ /$verb_wrt | $verb_std /pgmx) { |
| 1133 |
my ($pos_inicial, $pos_final) = ($-[0], $+[0]); |
| 1134 |
my $encontrado = ${^MATCH}; |
| 1135 |
$encontrado =~ s/($find)/$replace{$1}/g; |
| 1136 |
substr $document, $pos_inicial, $pos_final-$pos_inicial, $encontrado; |
| 1137 |
pos ($document) = $pos_inicial + length $encontrado; |
| 1138 |
} |
| 1139 |
|
| 1140 |
### Now match preview environment in <input file> |
| 1141 |
my @env_preview = $document =~ m/(\\begin\{preview\}.+?\\end\{preview\})/gmsx; |
| 1142 |
|
| 1143 |
### Convert preview to nopreview environment |
| 1144 |
if (@env_preview) { |
| 1145 |
my $preNo = scalar @env_preview; |
| 1146 |
Log("Found $preNo preview environments in $name$ext"); |
| 1147 |
Log("Pass all preview environments to \\begin{nopreview}\%TMP$tmp ... \\end{nopreview}\%TMP$tmp"); |
| 1148 |
$document =~ s/(?:(\\begin\{|\\end\{))(preview\})/$1no$2\%TMP$tmp/gmsx; |
| 1149 |
} |
| 1150 |
|
| 1151 |
### Pass verbatim write environments to dtxtag |
| 1152 |
Log("Pass verbatim write environments to %<*$dtxverb> ... %</$dtxverb>"); |
| 1153 |
$document =~ s/\\begin\{nopreview\}.+?\\end\{nopreview\}(*SKIP)(*F)| |
| 1154 |
($verb_wrt)/\%<\*$dtxverb>$1\%<\/$dtxverb>/gmsx; |
| 1155 |
$atbegindoc =~ s/($verb_wrt)/\%<\*$dtxverb>$1\%<\/$dtxverb>/gmsx; |
| 1156 |
|
| 1157 |
### Pass verbatim environments to dtxtag |
| 1158 |
Log("Pass verbatim environments to %<*$dtxverb> ... %</$dtxverb>"); |
| 1159 |
$document =~ s/\\begin\{nopreview\}.+?\\end\{nopreview\}(*SKIP)(*F)| |
| 1160 |
\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1161 |
($verb_std)/\%<\*$dtxverb>$1\%<\/$dtxverb>/gmsx; |
| 1162 |
|
| 1163 |
### Pass %CleanPST to dtxtag |
| 1164 |
Log("Pass %CleanPST ... %CleanPST to %<*remove$tmp> ... %</remove$tmp>"); |
| 1165 |
$document =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1166 |
^(?:%CleanPST) (.+?) (?:%CleanPST)/\%<\*remove$tmp>$1\%<\/remove$tmp>/gmsx; |
| 1167 |
|
| 1168 |
### Check plain TeX syntax for pspicture [skip PSTexample] |
| 1169 |
Log('Convert plain \pspicture to LaTeX syntax [skip in PSTexample]'); |
| 1170 |
$document =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1171 |
\\begin\{nopreview\}.+?\\end\{nopreview\}(*SKIP)(*F)| |
| 1172 |
\\begin\{PSTexample\}.+?\\end\{PSTexample\}(*SKIP)(*F)| |
| 1173 |
\\pspicture(\*)?(.+?)\\endpspicture/\\begin\{pspicture$1\}$2\\end\{pspicture$1\}/gmsx; |
| 1174 |
|
| 1175 |
### Check plain TeX syntax for psgraph [skip PSTexample] |
| 1176 |
Log('Convert plain \psgraph to LaTeX syntax [skip in PSTexample]'); |
| 1177 |
$document =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1178 |
\\begin\{nopreview\}.+?\\end\{nopreview\}(*SKIP)(*F)| |
| 1179 |
\\begin\{PSTexample\}.+?\\end\{PSTexample\}(*SKIP)(*F)| |
| 1180 |
\\psgraph(\*)?(.+?)\\endpsgraph/\\begin\{psgraph$1\}$2\\end\{psgraph$1\}/gmsx; |
| 1181 |
|
| 1182 |
### Pass all postscript environments [skip in PSTexample] |
| 1183 |
Log("Pass all postscript environments to \\begin{$wrapping} ... \\end{$wrapping}"); |
| 1184 |
$document =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1185 |
\\begin\{PSTexample\}.+?\\end\{PSTexample\}(*SKIP)(*F)| |
| 1186 |
\\begin\{nopreview\}.+?\\end\{nopreview\}(*SKIP)(*F)| |
| 1187 |
(?:\\begin\{postscript\})(?:\s*\[ [^]]*? \])? |
| 1188 |
(?<code>.+?) |
| 1189 |
(?:\\end\{postscript\}) |
| 1190 |
/\\begin\{$wrapping\}$+{code}\\end\{$wrapping\}/gmsx; |
| 1191 |
|
| 1192 |
### Pass all pstricks environments to \\begin{$wrapping} ... \\end{$wrapping}"); |
| 1193 |
if ($force) { |
| 1194 |
# Try to capture \psset{...} for pstricks and psgraph [force] |
| 1195 |
Log('Try to capture \psset{...} for pstricks environments [force mode]'); |
| 1196 |
Log("Pass all pstricks environments to \\begin{$wrapping} ... \\end{$wrapping} [force mode]"); |
| 1197 |
$document =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1198 |
\\begin\{nopreview\}.+?\\end\{nopreview\}(*SKIP)(*F)| |
| 1199 |
\\begin\{PSTexample\}.+?\\end\{PSTexample\}(*SKIP)(*F)| |
| 1200 |
\\begin\{$wrapping\}.+?\\end\{$wrapping\}(*SKIP)(*F)| |
| 1201 |
(?<code> |
| 1202 |
(?:\\psset\{(?:\{.*?\}|[^\{])*\}.+?)? # if exist ...save |
| 1203 |
\\begin\{(?<env> pspicture\*?| psgraph)\} .+? \\end\{\k<env>\} |
| 1204 |
) |
| 1205 |
/\\begin\{$wrapping\}$+{code}\\end\{$wrapping\}/gmsx; |
| 1206 |
} |
| 1207 |
else { |
| 1208 |
Log("Pass all pstricks environments to \\begin{$wrapping} ... \\end{$wrapping}"); |
| 1209 |
$document =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1210 |
\\begin\{nopreview\}.+?\\end\{nopreview\}(*SKIP)(*F)| |
| 1211 |
\\begin\{$wrapping\}.+?\\end\{$wrapping\}(*SKIP)(*F)| |
| 1212 |
($extr_tmp)/\\begin\{$wrapping\}$1\\end\{$wrapping\}/gmsx; |
| 1213 |
} |
| 1214 |
|
| 1215 |
### All environments are now classified: |
| 1216 |
### Extraction -> \begin{$wrapping} ... \end{$wrapping} |
| 1217 |
### Verbatim's -> %<\*$dtxverb> ... <\/$dtxverb> |
| 1218 |
|
| 1219 |
### Now split document |
| 1220 |
my ($preamble,$bodydoc,$enddoc) = $document =~ m/\A (.+?) (\\begin\{document\} .+?)(\\end\{document\}.*)\z/msx; |
| 1221 |
|
| 1222 |
### Hash for reverse changes for extract and <output file> |
| 1223 |
my %changes_out = ( |
| 1224 |
'\PSSET' => '\psset', |
| 1225 |
'\TIKZSET' => '\tikzset', |
| 1226 |
'\TRICKS' => '\pspicture', |
| 1227 |
'\ENDTRICKS' => '\endpspicture', |
| 1228 |
'\PSGRAPHTRICKS' => '\psgraph', |
| 1229 |
'\ENDPSGRAPHTRICKS' => '\endpsgraph', |
| 1230 |
'\USEPACKAGE' => '\usepackage', |
| 1231 |
'{GRAPHICX}' => '{graphicx}', |
| 1232 |
'\GRAPHICSPATH{' => '\graphicspath{', |
| 1233 |
'\BEGIN{' => '\begin{', |
| 1234 |
'\END{' => '\end{', |
| 1235 |
'\DOCUMENTCLASS' => '\documentclass', |
| 1236 |
'\PAGESTYLE{' => '\pagestyle{', |
| 1237 |
'\THISPAGESTYLE{' => '\thispagestyle{', |
| 1238 |
'%PSTCLEAN' => '%CleanPST', |
| 1239 |
); |
| 1240 |
|
| 1241 |
### We restore the changes in body of environments and dtxverb |
| 1242 |
%replace = (%changes_out); |
| 1243 |
$find = join q{|}, map { quotemeta } sort { length $a <=> length $b } keys %replace; |
| 1244 |
$bodydoc =~ s/($find)/$replace{$1}/g; |
| 1245 |
$preamble =~ s/($find)/$replace{$1}/g; |
| 1246 |
|
| 1247 |
### First search PSTexample environment for extract |
| 1248 |
my @exa_extract = $bodydoc =~ m/(?:\\begin\{$wrapping\})($BE.+?$EE)(?:\\end\{$wrapping\})/gmsx; |
| 1249 |
my $exaNo = scalar @exa_extract; |
| 1250 |
|
| 1251 |
### Set vars for log and print in terminal |
| 1252 |
my $envEXA = $exaNo > 1 ? 'PSTexample environments' : 'PSTexample environment'; |
| 1253 |
my $fileEXA = $exaNo > 1 ? 'files' : 'file'; |
| 1254 |
|
| 1255 |
### If PSTexample environment found |
| 1256 |
if ($exaNo!=0) { |
| 1257 |
$PSTexa = 1; |
| 1258 |
Log("Found $exaNo $envEXA in $name$ext"); |
| 1259 |
my $figNo = 1; |
| 1260 |
for my $item (@exa_extract) { |
| 1261 |
Logline("%##### PSTexample environment captured number $figNo ######%"); |
| 1262 |
Logline($item); |
| 1263 |
$figNo++; |
| 1264 |
} |
| 1265 |
# Add [graphic={[...]...}] to \begin{PSTexample}[...] |
| 1266 |
Log('Append [graphic={[...]...}] to \begin{PSTexample}[...]'); |
| 1267 |
$figNo = 1; |
| 1268 |
while ($bodydoc =~ /\\begin\{$wrapping\}(\s*)?\\begin\{PSTexample\}(\[.+?\])?/gsm) { |
| 1269 |
my $swpl_grap = "graphic=\{\[scale=1\]$imgdir/$name-fig-exa"; |
| 1270 |
my $corchetes = $1; |
| 1271 |
my ($pos_inicial, $pos_final) = ($-[1], $+[1]); |
| 1272 |
if (not $corchetes) { $pos_inicial = $pos_final = $+[0]; } |
| 1273 |
if (not $corchetes or $corchetes =~ /\[\s*\]/) { |
| 1274 |
$corchetes = "[$swpl_grap-$figNo}]"; |
| 1275 |
} |
| 1276 |
else { $corchetes =~ s/\]/,$swpl_grap-$figNo}]/; } |
| 1277 |
substr($bodydoc, $pos_inicial, $pos_final - $pos_inicial) = $corchetes; |
| 1278 |
pos($bodydoc) = $pos_inicial + length $corchetes; |
| 1279 |
} |
| 1280 |
continue { $figNo++; } |
| 1281 |
Log('Pass PSTexample environments to \begin{nopreview} ... \end{nopreview}'); |
| 1282 |
$bodydoc =~ s/\\begin\{$wrapping\} |
| 1283 |
(?<code>\\begin\{PSTexample\} .+? \\end\{PSTexample\}) |
| 1284 |
\\end\{$wrapping\} |
| 1285 |
/\\begin\{nopreview\}\%$tmp$+{code}\\end\{nopreview\}\%$tmp/gmsx; |
| 1286 |
} |
| 1287 |
|
| 1288 |
### Second search any pstricks environment for extract |
| 1289 |
my @env_extract = $bodydoc =~ m/(?:$BP)(.+?)(?:$EP)/gmsx; |
| 1290 |
my $envNo = scalar @env_extract; |
| 1291 |
|
| 1292 |
### Set vars for log and print in terminal |
| 1293 |
my $envSTD = $envNo > 1 ? 'pstricks environments' : 'pstricks environment'; |
| 1294 |
my $fileSTD = $envNo > 1 ? 'files' : 'file'; |
| 1295 |
|
| 1296 |
### If any pstricks environments found |
| 1297 |
if ($envNo!=0) { |
| 1298 |
$STDenv = 1; |
| 1299 |
Log("Found $envNo $envSTD in $name$ext"); |
| 1300 |
my $fig = 1; |
| 1301 |
for my $item (@env_extract) { |
| 1302 |
Logline("%##### Environment pstricks captured number $fig ######%"); |
| 1303 |
Logline($item); |
| 1304 |
$fig++; |
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
### Run script process only if any enviroment(s) found in <input file> |
| 1309 |
if ($envNo == 0 and $exaNo == 0) { |
| 1310 |
errorUsage("$scriptname can not find any environment to extract in $name$ext"); |
| 1311 |
} |
| 1312 |
|
| 1313 |
### Storing ALL current options of script process for .log file |
| 1314 |
if ($zip) { $opts_cmd{boolean}{zip} = 1; } |
| 1315 |
if ($tar) { $opts_cmd{boolean}{tar} = 1; } |
| 1316 |
if ($shell) { $opts_cmd{boolean}{shell} = 1; } |
| 1317 |
if ($nopdf) { $opts_cmd{boolean}{nopdf} = 1; } |
| 1318 |
if ($norun) { $opts_cmd{boolean}{norun} = 1; } |
| 1319 |
if ($nocrop) { $opts_cmd{boolean}{nocrop} = 1; } |
| 1320 |
if ($srcenv) { $opts_cmd{boolean}{srcenv} = 1; } |
| 1321 |
if ($gray) { $opts_cmd{boolean}{gray} = 1; } |
| 1322 |
if ($force) { $opts_cmd{boolean}{force} = 1; } |
| 1323 |
if ($noprew) { $opts_cmd{boolean}{noprew} = 1; } |
| 1324 |
if ($runbibtex) { $opts_cmd{boolean}{bibtex} = 1; } |
| 1325 |
if ($runbiber) { $opts_cmd{boolean}{biber} = 1; } |
| 1326 |
if ($clear) { $opts_cmd{boolean}{clear} = 1; } |
| 1327 |
if ($nosource) { $opts_cmd{boolean}{nosource} = 1; } |
| 1328 |
if ($arara) { $opts_cmd{compiler}{arara} = 1; } |
| 1329 |
if ($latexmk) { $opts_cmd{compiler}{latexmk} = 1; } |
| 1330 |
if ($luatex) { $opts_cmd{compiler}{luatex} = 1; } |
| 1331 |
if ($xetex) { $opts_cmd{compiler}{xetex} = 1; } |
| 1332 |
if ($tmpverbenv) { $opts_cmd{string}{ignore} = $tmpverbenv; } |
| 1333 |
$opts_cmd{string}{myverb} = $myverb; |
| 1334 |
$opts_cmd{string}{dpi} = $dpi; |
| 1335 |
$opts_cmd{string}{runs} = $runs; |
| 1336 |
$opts_cmd{string}{margins} = $margins; |
| 1337 |
$opts_cmd{string}{imgdir} = $imgdir; |
| 1338 |
|
| 1339 |
foreach my $key (keys %{$opts_cmd{boolean}}) { |
| 1340 |
if (defined $opts_cmd{boolean}{$key}) { push @currentopt, "--$key"; } |
| 1341 |
} |
| 1342 |
foreach my $key (keys %{$opts_cmd{compiler}}) { |
| 1343 |
if (defined $opts_cmd{compiler}{$key}) { push @currentopt, "--$key"; } |
| 1344 |
} |
| 1345 |
foreach my $key (keys %{$opts_cmd{image}}) { |
| 1346 |
if (defined $opts_cmd{image}{$key}) { push @currentopt, "--$key"; } |
| 1347 |
} |
| 1348 |
foreach my $key (keys %{$opts_cmd{string}}) { |
| 1349 |
if (defined $opts_cmd{string}{$key}) { push @currentopt, "--$key $opts_cmd{string}{$key}"; } |
| 1350 |
} |
| 1351 |
|
| 1352 |
@currentopt = grep !/--pdf/, @currentopt; |
| 1353 |
my @sorted_words = sort { length $a <=> length $b } @currentopt; |
| 1354 |
|
| 1355 |
Log('The script will execute the following options:'); |
| 1356 |
Logarray(\@sorted_words); |
| 1357 |
|
| 1358 |
### Set directory to save generated files, need full path for goog log :) |
| 1359 |
my $imgdirpath = File::Spec->rel2abs($imgdir); |
| 1360 |
|
| 1361 |
if (-e $imgdir) { |
| 1362 |
Infoline("The generated files will be saved in $imgdirpath"); |
| 1363 |
} |
| 1364 |
else { |
| 1365 |
Infoline("Creating the directory $imgdirpath to save the generated files"); |
| 1366 |
Infocolor('Running', "mkdir $imgdirpath"); |
| 1367 |
Logline("[perl] mkdir($imgdir,0744)"); |
| 1368 |
mkdir $imgdir,0744 or errorUsage("Can't create the directory $imgdir"); |
| 1369 |
} |
| 1370 |
|
| 1371 |
### Set compiler for process <input file> |
| 1372 |
my $compiler = $xetex ? 'xelatex' |
| 1373 |
: $luatex ? 'dvilualatex' |
| 1374 |
: 'latex' |
| 1375 |
; |
| 1376 |
|
| 1377 |
### Set options for pdfcrop |
| 1378 |
my $opt_crop = $xetex ? "--xetex --margins $margins" |
| 1379 |
: $luatex ? "--luatex --margins $margins" |
| 1380 |
: "--margins $margins" |
| 1381 |
; |
| 1382 |
|
| 1383 |
### Set options for preview package |
| 1384 |
my $opt_prew = $xetex ? 'xetex,' : q{}; |
| 1385 |
|
| 1386 |
### Set message in terminal |
| 1387 |
my $msg_compiler = $xetex ? 'xelatex' |
| 1388 |
: $luatex ? 'dvilualatex>dvips>ps2pdf' |
| 1389 |
: 'latex>dvips>ps2pdf' |
| 1390 |
; |
| 1391 |
|
| 1392 |
### Set write18 for compiler in TeXLive and MikTeX |
| 1393 |
if ($shell) { |
| 1394 |
$write18 = '-shell-escape'; |
| 1395 |
$write18 = '--enable-write18' if defined $ENV{'TEXSYSTEM'} and $ENV{'TEXSYSTEM'} =~ /miktex/i; |
| 1396 |
} |
| 1397 |
else { |
| 1398 |
$write18 = '-no-shell-escape'; |
| 1399 |
$write18 = '--disable-write18' if defined $ENV{'TEXSYSTEM'} and $ENV{'TEXSYSTEM'} =~ /miktex/i; |
| 1400 |
} |
| 1401 |
|
| 1402 |
### Set options for compiler |
| 1403 |
my $opt_compiler = "$write18 -interaction=nonstopmode -recorder"; |
| 1404 |
|
| 1405 |
if (!$norun) { |
| 1406 |
Log("The options '$opt_compiler' will be passed to $compiler"); |
| 1407 |
} |
| 1408 |
|
| 1409 |
### Set -q for system command line (gs, poppler-utils, dvips) |
| 1410 |
my $quiet = $verbose ? q{} : '-q'; |
| 1411 |
|
| 1412 |
### Set options for ghostscript |
| 1413 |
my %opt_gs_dev = ( |
| 1414 |
pdf => '-dNOSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite', |
| 1415 |
gray => '-dNOSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sColorConversionStrategy=Gray -sProcessColorModel=DeviceGray', |
| 1416 |
png => "-dNOSAFER -dBATCH -dNOPAUSE -sDEVICE=pngalpha -r$dpi", |
| 1417 |
bmp => "-dNOSAFER -dBATCH -dNOPAUSE -sDEVICE=bmp32b -r$dpi", |
| 1418 |
jpg => "-dNOSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -r$dpi -dJPEGQ=100 -dGraphicsAlphaBits=4 -dTextAlphaBits=4", |
| 1419 |
tif => "-dNOSAFER -dBATCH -dNOPAUSE -sDEVICE=tiff32nc -r$dpi", |
| 1420 |
); |
| 1421 |
|
| 1422 |
### Set poppler-utils executables |
| 1423 |
my %cmd_poppler = ( |
| 1424 |
eps => "pdftops", |
| 1425 |
ppm => "pdftoppm", |
| 1426 |
svg => "pdftocairo", |
| 1427 |
); |
| 1428 |
|
| 1429 |
### Set options for poppler-utils |
| 1430 |
my %opt_poppler = ( |
| 1431 |
eps => "$quiet -eps", |
| 1432 |
ppm => "$quiet -r $dpi", |
| 1433 |
svg => "$quiet -svg", |
| 1434 |
); |
| 1435 |
|
| 1436 |
### Copy preamble and body for temp file with all environments |
| 1437 |
my $atbeginout = $atbegindoc; |
| 1438 |
my $preamout = $preamble; |
| 1439 |
my $tmpbodydoc = $bodydoc; |
| 1440 |
|
| 1441 |
### Match \pagestyle and \thispagestyle in preamble |
| 1442 |
my $style_page = qr /(?:\\)(?:this)?(?:pagestyle\{) (.+?) (?:\})/x; |
| 1443 |
my @style_page = $preamout =~ m/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| $style_page/gmsx; |
| 1444 |
my %style_page = map { $_ => 1 } @style_page; # anon hash |
| 1445 |
|
| 1446 |
### Set \pagestyle{empty} for standalone files and process |
| 1447 |
if (@style_page) { |
| 1448 |
if (!exists $style_page{empty}) { |
| 1449 |
Log("Replacing page style for generated files"); |
| 1450 |
$preamout =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1451 |
(\\(this)?pagestyle)(?:\{.+?\})/$1\{empty\}/gmsx; |
| 1452 |
} |
| 1453 |
} |
| 1454 |
else { |
| 1455 |
Log('Add \pagestyle{empty} for generated files'); |
| 1456 |
$preamout = $preamout."\\pagestyle\{empty\}\n"; |
| 1457 |
} |
| 1458 |
|
| 1459 |
### We created a preamble for the individual files |
| 1460 |
my $sub_prea = "$atbeginout$preamout".'\begin{document}'; |
| 1461 |
|
| 1462 |
### Revert changes |
| 1463 |
$sub_prea =~ s/\%<\*$dtxverb>\s*(.+?)\s*\%<\/$dtxverb>/$1/gmsx; |
| 1464 |
%replace = (%changes_out); |
| 1465 |
$find = join q{|}, map { quotemeta } sort { length $a <=> length $b } keys %replace; |
| 1466 |
$sub_prea =~ s/($find)/$replace{$1}/g; |
| 1467 |
$sub_prea =~ s/^(?:\%<\*remove$tmp>)(.+?)(?:\%<\/remove$tmp>)/%CleanPST$1%CleanPST/gmsx; |
| 1468 |
|
| 1469 |
### Write standalone files for environments |
| 1470 |
if (!$nosource) { |
| 1471 |
my $src_name = "$name-fig-"; |
| 1472 |
my $srcNo = 1; |
| 1473 |
if ($srcenv) { |
| 1474 |
Log('Extract source code of all captured environments without preamble'); |
| 1475 |
if ($STDenv) { |
| 1476 |
if (-e "$imgdir/$name-fig-1$ext") { |
| 1477 |
Log("Recreating $envNo $fileSTD [$ext] with source code for $envSTD in $imgdirpath"); |
| 1478 |
print "Recreating $envNo $fileSTD ", color('magenta'), "[$ext]", |
| 1479 |
color('reset'), " with source code for $envSTD\r\n"; |
| 1480 |
} |
| 1481 |
else { |
| 1482 |
Log("Creating $envNo $fileSTD [$ext] with source code for $envSTD in $imgdirpath"); |
| 1483 |
print "Creating $envNo $fileSTD ", color('magenta'), "[$ext]", |
| 1484 |
color('reset'), " with source code for $envSTD\r\n"; |
| 1485 |
} |
| 1486 |
# Write files |
| 1487 |
while ($tmpbodydoc =~ m/$BP(?:\s*)?(?<env_src>.+?)(?:\s*)?$EP/gms) { |
| 1488 |
open my $outexasrc, '>', "$imgdir/$src_name$srcNo$ext"; |
| 1489 |
print {$outexasrc} $+{'env_src'}; |
| 1490 |
close $outexasrc; |
| 1491 |
} |
| 1492 |
continue { $srcNo++; } |
| 1493 |
} |
| 1494 |
if ($PSTexa) { |
| 1495 |
if (-e "$imgdir/$name-fig-exa-1$ext") { |
| 1496 |
Log("Recreating $exaNo $fileEXA [$ext] with source code for $envEXA in $imgdirpath"); |
| 1497 |
print "Recreating $exaNo $fileEXA ", color('magenta'), "[$ext]", |
| 1498 |
color('reset'), " with source code for $envEXA\r\n"; |
| 1499 |
} |
| 1500 |
else { |
| 1501 |
Log("Creating $exaNo $fileEXA [$ext] with source code for $envEXA in $imgdirpath"); |
| 1502 |
print "Creating $exaNo $fileEXA ", color('magenta'), "[$ext]", |
| 1503 |
color('reset'), " with source code for $envEXA\r\n"; |
| 1504 |
} |
| 1505 |
# Write files |
| 1506 |
while ($tmpbodydoc =~ m/$BE\[.+?(?<pst_exa_name>$imgdir\/.+?-\d+)\}\]\s*(?<exa_src>.+?)\s*$EE/gms) { |
| 1507 |
open my $outstdsrc, '>', "$+{'pst_exa_name'}$ext"; |
| 1508 |
print {$outstdsrc} $+{'exa_src'}; |
| 1509 |
close $outstdsrc; |
| 1510 |
} |
| 1511 |
} |
| 1512 |
} |
| 1513 |
else { |
| 1514 |
Log('Extract source code of all captured environments with preamble'); |
| 1515 |
if ($STDenv) { |
| 1516 |
if (-e "$imgdir/$name-fig-1$ext") { |
| 1517 |
Log("Recreating $envNo standalone $fileSTD [$ext] for $envSTD in $imgdirpath"); |
| 1518 |
print "Recreating $envNo standalone $fileSTD ", color('magenta'), "[$ext]", |
| 1519 |
color('reset'), " for $envSTD\r\n"; |
| 1520 |
} |
| 1521 |
else { |
| 1522 |
Log("Creating $envNo standalone $fileSTD [$ext] for $envSTD in $imgdirpath"); |
| 1523 |
print "Creating $envNo standalone $fileSTD ", color('magenta'), "[$ext]", |
| 1524 |
color('reset'), " for $envSTD\r\n"; |
| 1525 |
} |
| 1526 |
# Write files |
| 1527 |
while ($tmpbodydoc =~ m/$BP(?:\s*)?(?<env_src>.+?)(?:\s*)?$EP/gms) { |
| 1528 |
open my $outstdfile, '>', "$imgdir/$src_name$srcNo$ext"; |
| 1529 |
print {$outstdfile} "$sub_prea\n$+{'env_src'}\n\\end\{document\}"; |
| 1530 |
close $outstdfile; |
| 1531 |
} |
| 1532 |
continue { $srcNo++; } |
| 1533 |
} |
| 1534 |
if ($PSTexa) { |
| 1535 |
if (-e "$imgdir/$name-fig-exa-1$ext") { |
| 1536 |
Log("Recreating $exaNo standalone $fileEXA [$ext] for $envEXA in $imgdirpath"); |
| 1537 |
print "Recreating $exaNo standalone $fileEXA ", color('magenta'), "[$ext]", |
| 1538 |
color('reset'), " for $envEXA\r\n"; |
| 1539 |
} |
| 1540 |
else { |
| 1541 |
Log("Creating $exaNo standalone $fileEXA [$ext] for $envEXA in $imgdirpath"); |
| 1542 |
print "Creating $exaNo standalone $fileEXA ", color('magenta'), "[$ext]", |
| 1543 |
color('reset'), " for $envEXA\r\n"; |
| 1544 |
} |
| 1545 |
# Write files |
| 1546 |
while ($tmpbodydoc =~ m/$BE\[.+?(?<pst_exa_name>$imgdir\/.+?-\d+)\}\]\s*(?<exa_src>.+?)\s*$EE/gms) { |
| 1547 |
open my $outexafile, '>', "$+{'pst_exa_name'}$ext"; |
| 1548 |
print {$outexafile} "$sub_prea\n$+{'exa_src'}\n\\end\{document\}"; |
| 1549 |
close $outexafile; |
| 1550 |
} |
| 1551 |
} |
| 1552 |
} |
| 1553 |
} |
| 1554 |
|
| 1555 |
### Store options for preview and pst-pdf (add at begin document) |
| 1556 |
my $previewpkg = <<"EXTRA"; |
| 1557 |
\\PassOptionsToPackage\{inactive\}\{pst-pdf\}% |
| 1558 |
\\AtBeginDocument\{% |
| 1559 |
\\RequirePackage\[inactive\]\{pst-pdf\}% |
| 1560 |
\\RequirePackage\[${opt_prew}active,tightpage\]\{preview\}% |
| 1561 |
\\renewcommand\\PreviewBbAdjust\{-60pt -60pt 60pt 60pt\}\}% |
| 1562 |
EXTRA |
| 1563 |
|
| 1564 |
### Store options for pst-pdf (add at begin document) |
| 1565 |
my $pstpdfpkg = <<'EXTRA'; |
| 1566 |
\PassOptionsToPackage{inactive}{pst-pdf} |
| 1567 |
\AtBeginDocument{% |
| 1568 |
\RequirePackage[inactive]{pst-pdf}}% |
| 1569 |
EXTRA |
| 1570 |
|
| 1571 |
### First match preview package in preamble (prevent option clash) |
| 1572 |
my $REQPACK = quotemeta'\RequirePackage'; |
| 1573 |
my $USEPACK = quotemeta'\usepackage'; |
| 1574 |
my $CORCHETES = qr/\[ [^]]*? \]/x; |
| 1575 |
|
| 1576 |
my $PALABRAS = qr/\b (?: preview )/x; |
| 1577 |
my $FAMILIA = qr/\{ \s* $PALABRAS (?: \s* [,] \s* $PALABRAS )* \s* \}(\%*)?/x; |
| 1578 |
|
| 1579 |
Log('Remove preview package in preamble [memory]'); |
| 1580 |
$preamout =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1581 |
^ $USEPACK (?: $CORCHETES )? $FAMILIA \s*//msxg; |
| 1582 |
$preamout =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1583 |
(?: ^ $USEPACK \{ | \G) [^}]*? \K (,?) \s* $PALABRAS (\s*) (,?) /$1 and $3 ? ',' : $1 ? $2 : ''/gemsx; |
| 1584 |
$preamout =~ s/^\\usepackage\{\}(?:[\t ]*(?:\r?\n|\r))+/\n/gmsx; |
| 1585 |
|
| 1586 |
### Remove %<*$dtxverb> ... %</$dtxverb> in tmpbodydoc and preamout |
| 1587 |
$tmpbodydoc =~ s/\%<\*$dtxverb>(.+?)\%<\/$dtxverb>/$1/gmsx; |
| 1588 |
$preamout =~ s/\%<\*$dtxverb>(.+?)\%<\/$dtxverb>/$1/gmsx; |
| 1589 |
$atbeginout =~ s/\%<\*$dtxverb>(.+?)\%<\/$dtxverb>/$1/gmsx; |
| 1590 |
|
| 1591 |
### Adjust nopreview environments |
| 1592 |
$tmpbodydoc =~ s/\\begin\{nopreview\}\%$tmp |
| 1593 |
(?<code> .+?) |
| 1594 |
\\end\{nopreview\}\%$tmp |
| 1595 |
/\\begin\{nopreview\}\n$+{code}\n\\end\{nopreview\}/gmsx; |
| 1596 |
|
| 1597 |
### Adjust $wrapping environments (no need realy) |
| 1598 |
$tmpbodydoc =~ s/\\begin\{$wrapping\} |
| 1599 |
(?<code>.+?) |
| 1600 |
\\end\{$wrapping\} |
| 1601 |
/\\begin\{$wrapping\}\n$+{code}\n\\end\{$wrapping\}/gmsx; |
| 1602 |
|
| 1603 |
### Reverse changes for temporary file with all env (no in -exa file) |
| 1604 |
$tmpbodydoc =~ s/($find)/$replace{$1}/g; |
| 1605 |
$tmpbodydoc =~ s/(\%TMP$tmp)//g; |
| 1606 |
$preamout =~ s/($find)/$replace{$1}/g; |
| 1607 |
$preamout =~ s/^(?:\%<\*remove$tmp>)(.+?)(?:\%<\/remove$tmp>)/%CleanPST$1%CleanPST/gmsx; |
| 1608 |
$atbeginout =~ s/($find)/$replace{$1}/g; |
| 1609 |
|
| 1610 |
### We created a preamble for individual files with all environments |
| 1611 |
$sub_prea = $noprew ? "$atbeginout$pstpdfpkg$preamout".'\begin{document}' |
| 1612 |
: "$atbeginout$previewpkg$preamout" |
| 1613 |
; |
| 1614 |
|
| 1615 |
### Create a one file with "all" PSTexample environments extracted |
| 1616 |
if ($PSTexa) { |
| 1617 |
@exa_extract = undef; |
| 1618 |
Log("Adding packages to $name-fig-exa-$tmp$ext [memory]"); |
| 1619 |
Logline($pstpdfpkg); |
| 1620 |
Log('Convert plain Tex syntax for pspicture and psgraph to LaTeX syntax in PSTexample environments'); |
| 1621 |
while ($tmpbodydoc =~ m/$BE\[.+? $imgdir\/.+?-\d+\}\] .+?$EE/pgsmx ) { # search |
| 1622 |
my ($pos_inicial, $pos_final) = ($-[0], $+[0]); |
| 1623 |
my $encontrado = ${^MATCH}; |
| 1624 |
$encontrado =~ s/\\pspicture(\*)?(.+?)\\endpspicture/\\begin\{pspicture$1\}$2\\end\{pspicture$1\}/gmsx; |
| 1625 |
$encontrado =~ s/\\psgraph(\*)?(.+?)\\endpsgraph/\\begin\{psgraph$1\}$2\\end\{psgraph$1\}/gmsx; |
| 1626 |
substr $tmpbodydoc, $pos_inicial, $pos_final-$pos_inicial, $encontrado; |
| 1627 |
pos ($tmpbodydoc) = $pos_inicial + length $encontrado; |
| 1628 |
} |
| 1629 |
# Write file |
| 1630 |
Infoline("Creating $name-fig-exa-$tmp$ext with $exaNo $envEXA extracted"); |
| 1631 |
while ($tmpbodydoc =~ m/$BE\[.+? $imgdir\/.+?-\d+\}\](?<exa_src>.+?)$EE/gmsx ) { # search |
| 1632 |
push @exa_extract, $+{'exa_src'}."\\newpage\n"; |
| 1633 |
open my $allexaenv, '>', "$name-fig-exa-$tmp$ext"; |
| 1634 |
print {$allexaenv} "$atbeginout$pstpdfpkg$preamout".'\begin{document}'."@exa_extract"."\\end\{document\}"; |
| 1635 |
close $allexaenv; |
| 1636 |
} |
| 1637 |
# Remove [graphic={...}] in PSTexample example environments |
| 1638 |
$tmpbodydoc =~ s/($BE)(?:\[graphic=\{\[scale=1\]$imgdir\/.+?-\d+\}\])/$1/gmsx; |
| 1639 |
$tmpbodydoc =~ s/($BE\[.+?)(?:,graphic=\{\[scale=1\]$imgdir\/.+?-\d+\})(\])/$1$2/gmsx; |
| 1640 |
# Moving and renaming |
| 1641 |
if ($norun) { |
| 1642 |
Infoline("Moving and renaming $name-fig-exa-$tmp$ext to $name-fig-exa-all$ext"); |
| 1643 |
if (-e "$imgdir/$name-fig-exa-all$ext") { |
| 1644 |
Infocolor('Warning', "The file [$name-fig-exa-all$ext] already exists and will be rewritten"); |
| 1645 |
Log("Rewriting the file $name-fig-exa-all$ext in $imgdirpath"); |
| 1646 |
} |
| 1647 |
else { |
| 1648 |
Log("Writing the file $name-fig-exa-all$ext in $imgdirpath"); |
| 1649 |
} |
| 1650 |
if ($verbose) { |
| 1651 |
Infocolor('Running', "mv $workdir/$name-fig-exa-$tmp$ext $imgdirpath/$name-fig-exa-all$ext"); |
| 1652 |
} |
| 1653 |
else { |
| 1654 |
Infocolor('Running', "mv $name-fig-exa-$tmp$ext ./$imgdir/$name-fig-exa-all$ext"); |
| 1655 |
} |
| 1656 |
Logline("[perl] move($workdir/$name-fig-exa-$tmp$ext, $imgdirpath/$name-fig-exa-all$ext)"); |
| 1657 |
move("$workdir/$name-fig-exa-$tmp$ext", "$imgdir/$name-fig-exa-all$ext") |
| 1658 |
or die "* Error!!: Couldn't be renamed $name-fig-exa-$tmp$ext to ./$imgdir/$name-fig-exa-all$ext"; |
| 1659 |
} |
| 1660 |
} |
| 1661 |
|
| 1662 |
### Create a one file with "all" standard environments extracted |
| 1663 |
if ($STDenv) { |
| 1664 |
if ($noprew) { |
| 1665 |
Log("Creating $name-fig-$tmp$ext with $envNo $envSTD extracted [no preview]"); |
| 1666 |
print "Creating $name-fig-$tmp$ext with $envNo $envSTD extracted", |
| 1667 |
color('magenta'), " [no preview]\r\n",color('reset'); |
| 1668 |
} |
| 1669 |
else { |
| 1670 |
Log("Creating $name-fig-$tmp$ext with $envNo $envSTD extracted [preview]"); |
| 1671 |
print "Creating $name-fig-$tmp$ext with $envNo $envSTD extracted", |
| 1672 |
color('magenta'), " [preview]\r\n",color('reset'); |
| 1673 |
} |
| 1674 |
open my $allstdenv, '>', "$name-fig-$tmp$ext"; |
| 1675 |
if ($noprew) { |
| 1676 |
my @env_extract; |
| 1677 |
while ($tmpbodydoc =~ m/(?:$BP)(?<env_src>.+?)(?:$EP)/gms) { |
| 1678 |
push @env_extract,$+{'env_src'}."\\newpage\n"; |
| 1679 |
} |
| 1680 |
Log("Adding packages to $name-fig-$tmp$ext"); |
| 1681 |
Logline($pstpdfpkg); |
| 1682 |
print {$allstdenv} $sub_prea."@env_extract"."\\end{document}"; |
| 1683 |
} |
| 1684 |
else { |
| 1685 |
Log("Adding packages to $name-fig-$tmp$ext"); |
| 1686 |
Logline($previewpkg); |
| 1687 |
Log("Convert $wrapping to preview environments in $name-fig-$tmp$ext"); |
| 1688 |
# Convert $wrapping to preview environments |
| 1689 |
$tmpbodydoc =~ s/\\begin\{$wrapping\}(?<code>.+?)\\end\{$wrapping\} |
| 1690 |
/\\begin\{preview\}\n$+{code}\n\\end\{preview\}\n/gmsx; |
| 1691 |
print {$allstdenv} $sub_prea.$tmpbodydoc."\n\\end{document}"; |
| 1692 |
} |
| 1693 |
close $allstdenv; |
| 1694 |
# Moving and renaming |
| 1695 |
if ($norun) { |
| 1696 |
Infoline("Moving and renaming $name-fig-$tmp$ext to $name-fig-all$ext"); |
| 1697 |
if (-e "$imgdir/$name-fig-all$ext") { |
| 1698 |
Infocolor('Warning', "The file [$name-fig-all$ext] already exists and will be rewritten"); |
| 1699 |
Log("Rewriting the file $name-fig-all$ext in $imgdirpath"); |
| 1700 |
} |
| 1701 |
else { |
| 1702 |
Log("Writing the file $name-fig-all$ext in $imgdirpath"); |
| 1703 |
} |
| 1704 |
if ($verbose) { |
| 1705 |
Infocolor('Running', "mv $workdir/$name-fig-$tmp$ext $imgdirpath/$name-fig-all$ext"); |
| 1706 |
} |
| 1707 |
else { |
| 1708 |
Infocolor('Running', "mv $name-fig-$tmp$ext ./$imgdir/$name-fig-all$ext"); |
| 1709 |
} |
| 1710 |
Logline("[perl] move($workdir/$name-fig-$tmp$ext, $imgdirpath/$name-fig-all$ext)"); |
| 1711 |
move("$workdir/$name-fig-$tmp$ext", "$imgdir/$name-fig-all$ext") |
| 1712 |
or die "* Error!!: Couldn't be renamed $name-fig-$tmp$ext to ./$imgdir/$name-fig-all$ext"; |
| 1713 |
} |
| 1714 |
} |
| 1715 |
|
| 1716 |
### Compiler and generate PDF files |
| 1717 |
if (!$norun) { |
| 1718 |
Log('Generate a PDF file with all captured environments'); |
| 1719 |
my @compiler = (1..$runs); |
| 1720 |
opendir (my $DIR, $workdir); |
| 1721 |
while (readdir $DIR) { |
| 1722 |
if (/(?<name>$name-fig(-exa)?)(?<type>-$tmp$ext)/) { |
| 1723 |
Log("Compiling the file $+{name}$+{type} using [$msg_compiler]"); |
| 1724 |
print "Compiling the file $+{name}$+{type} using ", color('magenta'), "[$msg_compiler]\r\n",color('reset'); |
| 1725 |
# Compiling file |
| 1726 |
for (@compiler){ |
| 1727 |
RUNOSCMD("$compiler $opt_compiler","$+{name}$+{type}",'show'); |
| 1728 |
} |
| 1729 |
# Using dvips>ps2pdf |
| 1730 |
if ($compiler eq 'latex' or $compiler eq 'dvilualatex') { |
| 1731 |
RUNOSCMD("dvips $quiet -Ppdf", "-o $+{name}-$tmp.ps $+{name}-$tmp.dvi",'show'); |
| 1732 |
RUNOSCMD("ps2pdf -sPDFSETTINGS=prepress -sAutoRotatePages=None", "$+{name}-$tmp.ps $+{name}-$tmp.pdf",'show'); |
| 1733 |
} |
| 1734 |
# Moving and renaming temp files with source code |
| 1735 |
Infoline("Moving and renaming $+{name}$+{type} to $+{name}-all$ext"); |
| 1736 |
if (-e "$imgdir/$+{name}-all$ext") { |
| 1737 |
Infocolor('Warning', "The file [$+{name}-all$ext] already exists and will be rewritten"); |
| 1738 |
Log("Rewriting the file $+{name}-all$ext with all source for environments in $imgdirpath"); |
| 1739 |
} |
| 1740 |
else { |
| 1741 |
Log("Writing the file $+{name}-all$ext with all source for environments in $imgdirpath"); |
| 1742 |
} |
| 1743 |
if ($verbose){ |
| 1744 |
Infocolor('Running', "mv $workdir/$+{name}$+{type} $imgdirpath/$+{name}-all$ext"); |
| 1745 |
} |
| 1746 |
else { |
| 1747 |
Infocolor('Running', "mv $+{name}$+{type} ./$imgdir/$+{name}-all$ext"); |
| 1748 |
} |
| 1749 |
Logline("[perl] move($workdir/$+{name}$+{type}, $imgdirpath/$+{name}-all$ext)"); |
| 1750 |
move("$workdir/$+{name}$+{type}", "$imgdir/$+{name}-all$ext") |
| 1751 |
or die "* Error!!: Couldn't be renamed $+{name}$+{type} to $imgdir/$+{name}-all$ext"; |
| 1752 |
# pdfcrop |
| 1753 |
if (!$nocrop) { |
| 1754 |
Infoline("Cropping the file $+{name}-$tmp.pdf"); |
| 1755 |
RUNOSCMD("pdfcrop $opt_crop", "$+{name}-$tmp.pdf $+{name}-$tmp.pdf",'show'); |
| 1756 |
} |
| 1757 |
# gray |
| 1758 |
if ($gray) { |
| 1759 |
Infoline("Creating the file $+{name}-all.pdf [gray] in $tempDir"); |
| 1760 |
RUNOSCMD("$gscmd $quiet $opt_gs_dev{gray} ","-o $tempDir/$+{name}-all.pdf $workdir/$+{name}-$tmp.pdf",'show'); |
| 1761 |
} |
| 1762 |
else { |
| 1763 |
Infoline("Creating the file $+{name}-all.pdf in $tempDir"); |
| 1764 |
if ($verbose){ |
| 1765 |
Infocolor('Running', "mv $workdir/$+{name}-$tmp.pdf $tempDir/$+{name}-all.pdf"); |
| 1766 |
} |
| 1767 |
else { Infocolor('Running', "mv $+{name}-$tmp.pdf $tempDir/$+{name}-all.pdf"); } |
| 1768 |
# Renaming pdf file |
| 1769 |
Logline("[perl] move($workdir/$+{name}-$tmp.pdf, $tempDir/$+{name}-all.pdf)"); |
| 1770 |
move("$workdir/$+{name}-$tmp.pdf", "$tempDir/$+{name}-all.pdf") |
| 1771 |
or die "* Error!!: Couldn't be renamed $+{name}-$tmp.pdf to $tempDir/$+{name}-all.pdf"; |
| 1772 |
} |
| 1773 |
} |
| 1774 |
} |
| 1775 |
closedir $DIR; |
| 1776 |
} |
| 1777 |
|
| 1778 |
### Create image formats in separate files |
| 1779 |
if (!$norun) { |
| 1780 |
Log("Creating the image formats: $format, working on $tempDir"); |
| 1781 |
opendir(my $DIR, $tempDir); |
| 1782 |
while (readdir $DIR) { |
| 1783 |
# PDF/PNG/JPG/BMP/TIFF format suported by ghostscript |
| 1784 |
if (/(?<name>$name-fig(-exa)?)(?<type>-all\.pdf)/) { |
| 1785 |
for my $var (qw(pdf png jpg bmp tif)) { |
| 1786 |
if (defined $opts_cmd{image}{$var}) { |
| 1787 |
Log("Generating format [$var] from file $+{name}$+{type} in $imgdirpath using $gscmd"); |
| 1788 |
print 'Generating format', color('blue'), " [$var] ", color('reset'),"from file $+{name}$+{type}\r\n"; |
| 1789 |
RUNOSCMD("$gscmd $quiet $opt_gs_dev{$var} ", "-o $workdir/$imgdir/$+{name}-%1d.$var $tempDir/$+{name}$+{type}",'show'); |
| 1790 |
} |
| 1791 |
} |
| 1792 |
} |
| 1793 |
# EPS/PPM/SVG format suported by poppler-utils |
| 1794 |
if (/(?<name>$name-fig-exa)(?<type>-all\.pdf)/) { # pst-exa package |
| 1795 |
for my $var (qw(eps ppm svg)) { |
| 1796 |
if (defined $opts_cmd{image}{$var}) { |
| 1797 |
Log("Generating format [$var] from file $+{name}$+{type} in $imgdirpath using $cmd_poppler{$var}"); |
| 1798 |
print 'Generating format', color('blue'), " [$var] ", color('reset'),"from file $+{name}$+{type}\r\n"; |
| 1799 |
if (!$verbose){ |
| 1800 |
Infocolor('Running', "$cmd_poppler{$var} $opt_poppler{$var}"); |
| 1801 |
} |
| 1802 |
for (my $epsNo = 1; $epsNo <= $exaNo; $epsNo++) { |
| 1803 |
RUNOSCMD("$cmd_poppler{$var} $opt_poppler{$var}", "-f $epsNo -l $epsNo $tempDir/$+{name}$+{type} $workdir/$imgdir/$+{name}-$epsNo.$var",'only'); |
| 1804 |
} |
| 1805 |
} |
| 1806 |
} |
| 1807 |
} |
| 1808 |
if (/(?<name>$name-fig)(?<type>-all\.pdf)/) { |
| 1809 |
for my $var (qw(eps ppm svg)) { |
| 1810 |
if (defined $opts_cmd{image}{$var}) { |
| 1811 |
Log("Generating format [$var] from file $+{name}$+{type} in $imgdirpath using $cmd_poppler{$var}"); |
| 1812 |
print 'Generating format', color('blue'), " [$var] ", color('reset'),"from file $+{name}$+{type}\r\n"; |
| 1813 |
if (!$verbose){ |
| 1814 |
Infocolor('Running', "$cmd_poppler{$var} $opt_poppler{$var}"); |
| 1815 |
} |
| 1816 |
for (my $epsNo = 1; $epsNo <= $envNo; $epsNo++) { |
| 1817 |
RUNOSCMD("$cmd_poppler{$var} $opt_poppler{$var}", "-f $epsNo -l $epsNo $tempDir/$+{name}$+{type} $workdir/$imgdir/$+{name}-$epsNo.$var",'only'); |
| 1818 |
} |
| 1819 |
} |
| 1820 |
} |
| 1821 |
} |
| 1822 |
} # close while |
| 1823 |
closedir $DIR; |
| 1824 |
# Renaming PPM image files |
| 1825 |
if (defined $opts_cmd{image}{ppm}) { |
| 1826 |
Log("Renaming [ppm] images in $imgdirpath"); |
| 1827 |
if ($verbose){ |
| 1828 |
print 'Renaming', color('blue'), " [ppm] ", color('reset'),"images in $imgdirpath\r\n"; |
| 1829 |
} |
| 1830 |
opendir(my $DIR, $imgdir); |
| 1831 |
while (readdir $DIR) { |
| 1832 |
if (/(?<name>$name-fig(-exa)?-\d+\.ppm)(?<sep>-\d+)(?<ppm>\.ppm)/) { |
| 1833 |
if ($verbose){ |
| 1834 |
Infocolor('Running', "mv $+{name}$+{sep}$+{ppm} $+{name}"); |
| 1835 |
} |
| 1836 |
Logline("[perl] move($imgdirpath/$+{name}$+{sep}$+{ppm}, $imgdirpath/$+{name})"); |
| 1837 |
move("$imgdir/$+{name}$+{sep}$+{ppm}", "$imgdir/$+{name}") |
| 1838 |
or die "* Error!!: Couldn't be renamed $+{name}$+{sep}$+{ppm} to $+{name}"; |
| 1839 |
} |
| 1840 |
} |
| 1841 |
closedir $DIR; |
| 1842 |
} |
| 1843 |
} # close run |
| 1844 |
|
| 1845 |
### Constant |
| 1846 |
my $findgraphicx = 'true'; |
| 1847 |
|
| 1848 |
### Replacing the extracted environments with \includegraphics |
| 1849 |
Log("Convert pstricks extracted environments to \\includegraphics for $name-pdf$ext"); |
| 1850 |
my $grap = "\\includegraphics[scale=1]{$name-fig-"; |
| 1851 |
my $close = '}'; |
| 1852 |
my $imgNo = 1; |
| 1853 |
$bodydoc =~ s/$BP.+?$EP/$grap@{[$imgNo++]}$close/msg; |
| 1854 |
|
| 1855 |
### Add $atbegindoc to $preamble |
| 1856 |
$preamble = "$atbegindoc$preamble"; |
| 1857 |
|
| 1858 |
### Remove content in preamble |
| 1859 |
my @tag_remove_preamble = $preamble =~ m/(?:^\%<\*remove$tmp>.+?\%<\/remove$tmp>)/gmsx; |
| 1860 |
if (@tag_remove_preamble) { |
| 1861 |
Log("Removing the content between %CleanPST ... %CleanPST in preamble for $name-pdf$ext"); |
| 1862 |
$preamble =~ s/^\%<\*remove$tmp>\s*(.+?)\s*\%<\/remove$tmp>(?:[\t ]*(?:\r?\n|\r))?+//gmsx; |
| 1863 |
} |
| 1864 |
|
| 1865 |
### Suport for pst-exa package |
| 1866 |
my $pstexa = qr/(?:\\ usepackage) \[\s*(.+?)\s*\] (?:\{\s*(pst-exa)\s*\} ) /x; |
| 1867 |
my @pst_exa; |
| 1868 |
my %pst_exa; |
| 1869 |
|
| 1870 |
### Search pst-exa package |
| 1871 |
@pst_exa = $preamble =~ m/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)|$pstexa/gmsx; |
| 1872 |
%pst_exa = map { $_ => 1 } @pst_exa; |
| 1873 |
|
| 1874 |
### If found comment and adjust path for graphic files |
| 1875 |
if (@pst_exa) { |
| 1876 |
Log("Comment pst-exa package in preamble for $name-pdf$ext"); |
| 1877 |
$findgraphicx = 'false'; |
| 1878 |
$preamble =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1879 |
(\\usepackage\[)\s*(swpl|tcb)\s*(\]\{pst-exa\})/\%$1$2,pdf$3/msxg; |
| 1880 |
# Change for [tcb] option (path for graphic files) |
| 1881 |
if (exists $pst_exa{tcb}) { |
| 1882 |
Log("Suport for \\usepackage[tcb,pdf]\{pst-exa\} for $name-pdf$ext"); |
| 1883 |
$bodydoc =~ s/(graphic=\{)\[(scale=\d*)\]($imgdir\/$name-fig-exa-\d*)\}/$1$2\}\{$3\}/gsmx; |
| 1884 |
} |
| 1885 |
} |
| 1886 |
|
| 1887 |
### Regex for remove (pst-?) packages in preamble |
| 1888 |
$PALABRAS = qr/\b (?: pst-\w+ | pstricks (?: -add | -pdf )? | psfrag |psgo |vaucanson-g| auto-pst-pdf(?: -lua )? )/x; |
| 1889 |
$FAMILIA = qr/\{ \s* $PALABRAS (?: \s* [,] \s* $PALABRAS )* \s* \}(\%*)?/x; |
| 1890 |
|
| 1891 |
Log("Remove pstricks packages in preamble for $name-pdf$ext [no pst-exa]"); |
| 1892 |
$preamble =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1893 |
^ $USEPACK (?: $CORCHETES )? $FAMILIA \s*//msxg; |
| 1894 |
$preamble =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1895 |
(?: ^ $USEPACK \{ | \G) [^}]*? \K (,?) \s* $PALABRAS (\s*) (,?) /$1 and $3 ? ',' : $1 ? $2 : ''/gemsx; |
| 1896 |
|
| 1897 |
### Uncomment pst-exa package |
| 1898 |
if (@pst_exa) { |
| 1899 |
Log("Uncomment pst-exa package in preamble for $name-pdf$ext"); |
| 1900 |
$preamble =~ s/(?:\%)(\\usepackage\[\s*)(swpl|tcb)(,pdf\s*\]\{pst-exa\})/$1$2$3/msxg; |
| 1901 |
} |
| 1902 |
|
| 1903 |
Log("Remove \\psset\{...\} in preamble for $name-pdf$ext"); |
| 1904 |
$preamble =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1905 |
\\psset\{(?:\{.*?\}|[^\{])*\}(?:[\t ]*(?:\r?\n|\r))+//gmsx; |
| 1906 |
|
| 1907 |
Log("Remove \\SpecialCoor in preamble for $name-pdf$ext"); |
| 1908 |
$preamble =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1909 |
\\SpecialCoor(?:[\t ]*(?:\r?\n|\r))+//gmsx; |
| 1910 |
|
| 1911 |
Log("Remove empty lines in preamble for $name-pdf$ext"); |
| 1912 |
$preamble =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1913 |
^\\usepackage\{\}(?:[\t ]*(?:\r?\n|\r))+/\n/gmsx; |
| 1914 |
$preamble =~ s/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)| |
| 1915 |
^(?:[\t ]*(?:\r?\n|\r))?+//gmsx; |
| 1916 |
|
| 1917 |
### To be sure that the package graphicx and \graphicspath is in the main |
| 1918 |
### document and not in a verbatim write environment we make the changes |
| 1919 |
my %tmpreplace = ( |
| 1920 |
'graphicx' => 'TMPGRAPHICXTMP', |
| 1921 |
'graphicspath' => 'TMPGRAPHICSPATHTMP', |
| 1922 |
); |
| 1923 |
my $findtmp = join q{|}, map { quotemeta } sort { length $a <=> length $b } keys %tmpreplace; |
| 1924 |
|
| 1925 |
### We go line by line and make the changes [need /p for ${^MATCH}] |
| 1926 |
while ($preamble =~ /\%<\*$dtxverb>(.+?)\%<\/$dtxverb> | $tmpcomment /pgmsx) { |
| 1927 |
my ($pos_inicial, $pos_final) = ($-[0], $+[0]); |
| 1928 |
my $encontrado = ${^MATCH}; |
| 1929 |
$encontrado =~ s/($findtmp)/$tmpreplace{$1}/g; |
| 1930 |
substr $preamble, $pos_inicial, $pos_final-$pos_inicial, $encontrado; |
| 1931 |
pos ($preamble) = $pos_inicial + length $encontrado; |
| 1932 |
} |
| 1933 |
|
| 1934 |
### Now we're trying to capture \graphicspath{...} |
| 1935 |
my $graphicspath= qr/\\ graphicspath \{ ((?: $llaves )+) \}/ix; |
| 1936 |
my @graphicspath; |
| 1937 |
@graphicspath = $preamble =~ m/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)|($graphicspath)/gmsx; |
| 1938 |
|
| 1939 |
if (@graphicspath) { |
| 1940 |
Log("Found \\graphicspath in preamble for $name-pdf$ext"); |
| 1941 |
$findgraphicx = 'false'; |
| 1942 |
while ($preamble =~ /$graphicspath /pgmx) { |
| 1943 |
my ($pos_inicial, $pos_final) = ($-[0], $+[0]); |
| 1944 |
my $encontrado = ${^MATCH}; |
| 1945 |
if ($encontrado =~ /$graphicspath/) { |
| 1946 |
my $argumento = $1; |
| 1947 |
if ($argumento !~ /\{$imgdir\/\}/) { |
| 1948 |
$argumento .= "\{$imgdir/\}"; |
| 1949 |
my $cambio = "\\graphicspath{$argumento}"; |
| 1950 |
substr $preamble, $pos_inicial, $pos_final-$pos_inicial, $cambio; |
| 1951 |
pos($preamble) = $pos_inicial + length $cambio; |
| 1952 |
} |
| 1953 |
} |
| 1954 |
} |
| 1955 |
} |
| 1956 |
|
| 1957 |
### Possible packages that load graphicx |
| 1958 |
my @pkgcandidates = qw ( |
| 1959 |
rotating epsfig lyluatex xunicode parsa xepersian-hm gregoriotex teixmlslides |
| 1960 |
teixml fotex hvfloat pgfplots grfpaste gmbase hep-paper postage schulealt |
| 1961 |
schule utfsym cachepic abc doclicense rotating epsfig semtrans mgltex |
| 1962 |
graphviz revquantum mpostinl cmpj cmpj2 cmpj3 chemschemex register papercdcase |
| 1963 |
flipbook wallpaper asyprocess draftwatermark rutitlepage dccpaper-base |
| 1964 |
nbwp-manual mandi fmp toptesi isorot pinlabel cmll graphicx-psmin ptmxcomp |
| 1965 |
countriesofeurope iodhbwm-templates fgruler combinedgraphics pax pdfpagediff |
| 1966 |
psfragx epsdice perfectcut upmethodology-fmt ftc-notebook tabvar vtable |
| 1967 |
teubner pas-cv gcard table-fct pdfpages keyfloat pdfscreen showexpl simplecd |
| 1968 |
ifmslide grffile reflectgraphics markdown bclogo tikz-page pst-uml realboxes |
| 1969 |
musikui csbulobalka lwarp mathtools sympytex mpgraphics miniplot.sty:77 |
| 1970 |
dottex pdftricks2 feupphdteses tex4ebook axodraw2 hagenberg-thesis dlfltxb |
| 1971 |
hu-berlin-bundle draftfigure quicktype endofproofwd euflag othelloboard |
| 1972 |
pdftricks unswcover breqn pdfswitch latex-make figlatex repltext etsvthor |
| 1973 |
cyber xcookybooky xfrac mercatormap chs-physics-report tikzscale ditaa |
| 1974 |
pst-poker gmp CJKvert asypictureb hletter tikz-network powerdot-fuberlin |
| 1975 |
skeyval gnuplottex plantslabels fancytooltips ieeepes pst-vectorian |
| 1976 |
phfnote overpic xtuformat stubs graphbox ucs pdfwin metalogo mwe |
| 1977 |
inline-images asymptote UNAMThesis authorarchive amscdx adjustbox |
| 1978 |
trimclip fixmetodonotes incgraph scanpages alertmessage |
| 1979 |
svg quiz2socrative realhats autopdf egplot decorule figsize tikzexternal |
| 1980 |
pgfcore frontespizio textglos graphicx tikz tcolorbox |
| 1981 |
); |
| 1982 |
|
| 1983 |
my $pkgcandidates = join q{|}, map { quotemeta } sort { length $a <=> length $b } @pkgcandidates; |
| 1984 |
$pkgcandidates = qr/$pkgcandidates/x; |
| 1985 |
my @graphicxpkg; |
| 1986 |
|
| 1987 |
### Now we're trying to capture graphicx package |
| 1988 |
@graphicxpkg = $preamble =~ m/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)|($pkgcandidates)/gmsx; |
| 1989 |
|
| 1990 |
if (@graphicxpkg and $findgraphicx ne 'false') { |
| 1991 |
Log("Found graphicx package in preamble for $name-pdf$ext"); |
| 1992 |
$findgraphicx = 'false'; |
| 1993 |
} |
| 1994 |
|
| 1995 |
### Revert changes in preamble for temp <output file> |
| 1996 |
my %tmpoutreplace = ( |
| 1997 |
'TMPGRAPHICXTMP' => 'graphicx', |
| 1998 |
'TMPGRAPHICSPATHTMP' => 'graphicspath', |
| 1999 |
); |
| 2000 |
|
| 2001 |
%replace = (%changes_out,%tmpoutreplace); |
| 2002 |
$find = join q{|}, map {quotemeta} sort { length $a <=> length $b } keys %replace; |
| 2003 |
|
| 2004 |
### Try to capture arara:compiler in preamble of <output file> |
| 2005 |
my @arara_engines = qw (latex pdflatex lualatex xelatex luahbtex); |
| 2006 |
my $arara_engines = join q{|}, map { quotemeta} sort { length $a <=> length $b } @arara_engines; |
| 2007 |
$arara_engines = qr/\b(?:$arara_engines)/x; |
| 2008 |
my $arara_rule = qr /^(?:\%\s{1}arara[:]\s{1}) ($arara_engines) /msx; |
| 2009 |
|
| 2010 |
### Capture graphicx.sty in .log of LaTeX file |
| 2011 |
if ($findgraphicx ne 'false') { |
| 2012 |
Log("Couldn't capture the graphicx package for $name-pdf$ext in preamble"); |
| 2013 |
my $ltxlog; |
| 2014 |
my @graphicx; |
| 2015 |
my $null = devnull(); |
| 2016 |
# Copy preamble and revert changes |
| 2017 |
my $preambletmp = $preamble; |
| 2018 |
$preambletmp =~ s/($find)/$replace{$1}/g; |
| 2019 |
$preambletmp =~ s/\%<\*$dtxverb>(.+?)\%<\/$dtxverb>/$1/gmsx; |
| 2020 |
Log("Creating $name-fig-$tmp$ext [only preamble]"); |
| 2021 |
if ($verbose) { say "Creating [$name-fig-$tmp$ext] with only preamble"; } |
| 2022 |
open my $OUTfile, '>', "$name-fig-$tmp$ext"; |
| 2023 |
print {$OUTfile} "$preambletmp\n\\stop"; |
| 2024 |
close $OUTfile; |
| 2025 |
# Set compiler for arara |
| 2026 |
if ($opts_cmd{compiler}{arara}) { |
| 2027 |
my @engine = $atbegindoc =~ m/\%<\*$dtxverb> .+?\%<\/$dtxverb>(*SKIP)(*F)|$arara_rule/msx; |
| 2028 |
my %engine = map { $_ => 1 } @engine; |
| 2029 |
if (%engine) { |
| 2030 |
for my $var (@arara_engines) { |
| 2031 |
if (defined $engine{$var}) { |
| 2032 |
Log("Found engine [$var] for arara"); |
| 2033 |
$compiler = $var; |
| 2034 |
} |
| 2035 |
} |
| 2036 |
} |
| 2037 |
else { |
| 2038 |
Log("Not detected engine for arara, use default [pdflatex]"); |
| 2039 |
$compiler = 'pdflatex'; |
| 2040 |
} |
| 2041 |
} |
| 2042 |
if ($compiler eq 'latex') { $compiler = 'pdflatex'; } |
| 2043 |
if ($compiler eq 'dvilualatex') { $compiler = 'lualatex'; } |
| 2044 |
# Compiling file |
| 2045 |
RUNOSCMD("$compiler $write18 -interaction=batchmode", "$name-fig-$tmp$ext >$null", 'only'); |
| 2046 |
# Restore arara compiler |
| 2047 |
if ($arara) { $compiler = 'arara'; } |
| 2048 |
Log("Search graphicx package in $name-fig-$tmp.log"); |
| 2049 |
open my $LaTeXlog, '<', "$name-fig-$tmp.log"; |
| 2050 |
{ |
| 2051 |
local $/; |
| 2052 |
$ltxlog = <$LaTeXlog>; |
| 2053 |
} |
| 2054 |
close $LaTeXlog; |
| 2055 |
# Try to capture graphicx |
| 2056 |
@graphicx = $ltxlog =~ m/.+? (graphicx\.sty)/xg; |
| 2057 |
if (@graphicx) { |
| 2058 |
Log("Found graphicx package in $name-fig-$tmp.log"); |
| 2059 |
} |
| 2060 |
else { |
| 2061 |
Log("Not found graphicx package in $name-fig-$tmp.log"); |
| 2062 |
Log("Add \\usepackage\{graphicx\} to preamble of $name-pdf$ext"); |
| 2063 |
$preamble= "$preamble\\usepackage\{graphicx\}\n"; |
| 2064 |
} |
| 2065 |
} |
| 2066 |
|
| 2067 |
### Add last lines |
| 2068 |
if (!@graphicspath) { |
| 2069 |
Log("Not found \\graphicspath in preamble for $name-pdf$ext"); |
| 2070 |
Log("Add \\graphicspath\{\{$imgdir/\}\} to preamble for $name-pdf$ext"); |
| 2071 |
$preamble= "$preamble\\graphicspath\{\{$imgdir/\}\}\n"; |
| 2072 |
} |
| 2073 |
|
| 2074 |
Log("Add \\usepackage\{grfext\} to preamble for $name-pdf$ext"); |
| 2075 |
$preamble = "$preamble\\usepackage\{grfext\}\n"; |
| 2076 |
Log("Add \\PrependGraphicsExtensions\*\{\.pdf\} to preamble for $name-pdf$ext"); |
| 2077 |
$preamble = "$preamble\\PrependGraphicsExtensions\*\{\.pdf\}"; |
| 2078 |
$preamble =~ s/\%<\*$dtxverb>(.+?)\%<\/$dtxverb>/$1/gmsx; |
| 2079 |
|
| 2080 |
### Create a <output file> |
| 2081 |
my $out_file = "$preamble\n$bodydoc\n$enddoc"; |
| 2082 |
|
| 2083 |
### Clean \psset{...} content in <output file> |
| 2084 |
Log("Remove \\psset\{...\} in body for $name-pdf$ext"); |
| 2085 |
$out_file =~ s/\\begin\{nopreview\}\%$tmp.+?\\end\{nopreview\}\%$tmp(*SKIP)(*F)| |
| 2086 |
\%<\*$dtxverb> .+? \%<\/$dtxverb>(*SKIP)(*F)| |
| 2087 |
\\psset\{(?:\{.*?\}|[^\{])*\}(?:[\t ]*(?:\r?\n|\r))?+//gmsx; |
| 2088 |
|
| 2089 |
### Revert preview environments |
| 2090 |
if (@env_preview) { |
| 2091 |
Log("Revert \\begin{nopreview}\%TMP$tmp ... \\end{nopreview}\%TMP$tmp"); |
| 2092 |
$out_file =~ s/\\begin\{nopreview\}\%TMP$tmp |
| 2093 |
(?<code> .+?) |
| 2094 |
\\end\{nopreview\}\%TMP$tmp |
| 2095 |
/\\begin\{preview\}$+{code}\\end\{preview\}/gmsx; |
| 2096 |
} |
| 2097 |
|
| 2098 |
$out_file =~ s/\\begin\{nopreview\}%$tmp |
| 2099 |
(?<code>\\begin\{PSTexample\} .+? \\end\{PSTexample\}) |
| 2100 |
\\end\{nopreview\}%$tmp |
| 2101 |
/$+{code}/gmsx; |
| 2102 |
|
| 2103 |
### Remove internal mark for verbatim and verbatim write environments |
| 2104 |
$out_file =~ s/\%<\*$dtxverb>(.+?)\%<\/$dtxverb>/$1/gmsx; |
| 2105 |
%replace = (%changes_out,%tmpoutreplace); |
| 2106 |
$find = join q{|}, map {quotemeta} sort { length $a <=> length $b } keys %replace; |
| 2107 |
$out_file =~ s/($find)/$replace{$1}/g; |
| 2108 |
|
| 2109 |
### Write <output file> |
| 2110 |
if (-e "$name-pdf$ext") { |
| 2111 |
Log("Rewriting the file $name-pdf$ext in $workdir"); |
| 2112 |
Infocolor('Warning', "The file [$name-pdf$ext] already exists and will be rewritten"); |
| 2113 |
} |
| 2114 |
else{ |
| 2115 |
Infoline("Creating the file $name-pdf$ext"); |
| 2116 |
Log("Write the file $name-pdf$ext in $workdir"); |
| 2117 |
} |
| 2118 |
open my $OUTfile, '>', "$name-pdf$ext"; |
| 2119 |
print {$OUTfile} $out_file; |
| 2120 |
close $OUTfile; |
| 2121 |
|
| 2122 |
### Set compiler for process <output file> |
| 2123 |
$compiler = $xetex ? 'xelatex' |
| 2124 |
: $luatex ? 'lualatex' |
| 2125 |
: 'pdflatex' |
| 2126 |
; |
| 2127 |
|
| 2128 |
### Set options for latexmk |
| 2129 |
my $ltxmkopt = $xetex ? "-pdfxe -silent -xelatex=\"xelatex $write18 -recorder %O %S\"" |
| 2130 |
: $luatex ? "-pdflua -silent -lualatex=\"lualatex $write18 -recorder %O %S\"" |
| 2131 |
: "-pdf -silent -pdflatex=\"pdflatex $write18 -recorder %O %S\"" |
| 2132 |
; |
| 2133 |
|
| 2134 |
### Set options for compiler |
| 2135 |
$opt_compiler = $arara ? '--log' |
| 2136 |
: $latexmk ? "$ltxmkopt" |
| 2137 |
: "$write18 -interaction=nonstopmode -recorder" |
| 2138 |
; |
| 2139 |
|
| 2140 |
### Set message in terminal |
| 2141 |
$msg_compiler = $xetex ? 'xelatex' |
| 2142 |
: $luatex ? 'lualatex' |
| 2143 |
: 'pdflatex' |
| 2144 |
; |
| 2145 |
|
| 2146 |
### Set compiler message and compiler for arara and latexmk |
| 2147 |
if ($arara) { $compiler = $msg_compiler = 'arara'; } |
| 2148 |
if ($latexmk) { $compiler = $msg_compiler = 'latexmk'; } |
| 2149 |
|
| 2150 |
### Process <output file> |
| 2151 |
if (!$norun) { |
| 2152 |
Log("Compiling the file $name-pdf$ext using [$msg_compiler]"); |
| 2153 |
print "Compiling the file $name-pdf$ext using ", color('magenta'), "[$msg_compiler]\r\n",color('reset'); |
| 2154 |
RUNOSCMD("$compiler $opt_compiler", "$name-pdf$ext",'show'); |
| 2155 |
# biber |
| 2156 |
if ($runbiber && -e "$name-pdf.bcf" && !$arara && !$latexmk) { |
| 2157 |
RUNOSCMD("biber", "$name-pdf",'show'); |
| 2158 |
RUNOSCMD("$compiler $opt_compiler", "$name-pdf$ext",'show'); |
| 2159 |
} |
| 2160 |
# bibtex |
| 2161 |
if ($runbibtex && -e "$name-pdf.aux" && !$arara && !$latexmk) { |
| 2162 |
RUNOSCMD("bibtex", "$name-pdf",'show'); |
| 2163 |
RUNOSCMD("$compiler $opt_compiler", "$name-pdf$ext",'show'); |
| 2164 |
} |
| 2165 |
} |
| 2166 |
|
| 2167 |
### Remove temporary files |
| 2168 |
my @tmpfiles; |
| 2169 |
my @protected = qw(); |
| 2170 |
my $flsline = 'OUTPUT'; |
| 2171 |
my @flsfile; |
| 2172 |
|
| 2173 |
### Protect generated files |
| 2174 |
push @protected, "$name-pdf$ext", "$name-pdf.pdf"; |
| 2175 |
|
| 2176 |
### Find files |
| 2177 |
find(\&aux_files, $workdir); |
| 2178 |
sub aux_files{ |
| 2179 |
my $findtmpfiles = $_; |
| 2180 |
if (-f $findtmpfiles && $findtmpfiles =~ m/$name-fig(-exa)?-$tmp.+?$/) { # search |
| 2181 |
push @tmpfiles, $_; |
| 2182 |
} |
| 2183 |
return; |
| 2184 |
} |
| 2185 |
|
| 2186 |
### Add if exists |
| 2187 |
if (-e 'arara.log') { |
| 2188 |
push @flsfile, 'arara.log'; |
| 2189 |
} |
| 2190 |
if (-e "$name-fig-$tmp.fls") { |
| 2191 |
push @flsfile, "$name-fig-$tmp.fls"; |
| 2192 |
} |
| 2193 |
if (-e "$name-fig-exa-$tmp.fls") { |
| 2194 |
push @flsfile, "$name-fig-exa-$tmp.fls"; |
| 2195 |
} |
| 2196 |
if (-e "$name-pdf.fls") { |
| 2197 |
push @flsfile, "$name-pdf.fls"; |
| 2198 |
} |
| 2199 |
|
| 2200 |
### Read .fls file |
| 2201 |
for my $filename(@flsfile){ |
| 2202 |
open my $RECtmp, '<', $filename; |
| 2203 |
push @tmpfiles, grep /^$flsline/,<$RECtmp>; |
| 2204 |
close $RECtmp; |
| 2205 |
} |
| 2206 |
|
| 2207 |
foreach (@tmpfiles) { s/^$flsline\s+|\s+$//g; } |
| 2208 |
push @tmpfiles, @flsfile; |
| 2209 |
|
| 2210 |
@tmpfiles = uniq(@tmpfiles); |
| 2211 |
@tmpfiles = array_minus(@tmpfiles, @protected); |
| 2212 |
|
| 2213 |
Log('The files that will be deleted are:'); |
| 2214 |
Logarray(\@tmpfiles); |
| 2215 |
|
| 2216 |
### Remove only if exist |
| 2217 |
if (@tmpfiles) { |
| 2218 |
Infoline("Remove temporary files created in $workdir"); |
| 2219 |
foreach my $tmpfiles (@tmpfiles) { |
| 2220 |
move($tmpfiles, $tempDir); |
| 2221 |
} |
| 2222 |
} |
| 2223 |
|
| 2224 |
### Find dirs created by minted |
| 2225 |
my @deldirs; |
| 2226 |
my $mintdir = "\_minted\-$name-fig-$tmp"; |
| 2227 |
my $mintdirexa = "\_minted\-$name-fig-exa-$tmp"; |
| 2228 |
if (-e $mintdir) { push @deldirs, $mintdir; } |
| 2229 |
if (-e $mintdirexa) { push @deldirs, $mintdirexa; } |
| 2230 |
|
| 2231 |
Log('The directory that will be deleted are:'); |
| 2232 |
Logarray(\@deldirs); |
| 2233 |
|
| 2234 |
### Remove only if exist |
| 2235 |
if (@deldirs) { |
| 2236 |
Infoline("Remove temporary directories created by minted in $workdir"); |
| 2237 |
foreach my $deldirs (@deldirs) { |
| 2238 |
remove_tree($deldirs); |
| 2239 |
} |
| 2240 |
} |
| 2241 |
|
| 2242 |
### Compress "./images" with generated files |
| 2243 |
my $archivetar; |
| 2244 |
if ($zip or $tar) { |
| 2245 |
my $stamp = strftime("%Y-%m-%d", localtime); |
| 2246 |
$archivetar = "$imgdir-$stamp"; |
| 2247 |
|
| 2248 |
my @savetozt; |
| 2249 |
find(\&zip_tar, $imgdir); |
| 2250 |
sub zip_tar{ |
| 2251 |
my $filesto = $_; |
| 2252 |
if (-f $filesto && $filesto =~ m/$name-fig-.+?$/) { # search |
| 2253 |
push @savetozt, $File::Find::name; |
| 2254 |
} |
| 2255 |
return; |
| 2256 |
} |
| 2257 |
Log("The files are compress found in $imgdirpath are:"); |
| 2258 |
Logarray(\@savetozt); |
| 2259 |
if ($zip) { |
| 2260 |
if (-e "$archivetar.zip") { |
| 2261 |
Infocolor('Warning', "The file [$archivetar.zip] already exists and will be rewritten"); |
| 2262 |
Log("Rewriting the file $archivetar.zip in $workdir"); |
| 2263 |
} |
| 2264 |
else{ |
| 2265 |
print "Creating the file ", color('magenta'), "[$archivetar.zip]", |
| 2266 |
color('reset'), " with generate files in ./$imgdir\r\n"; |
| 2267 |
Log("Writen the file $archivetar.tar.gz in $workdir"); |
| 2268 |
} |
| 2269 |
zip \@savetozt => "$archivetar.zip"; |
| 2270 |
} |
| 2271 |
if ($tar) { |
| 2272 |
if (-e "$archivetar.tar.gz") { |
| 2273 |
Infocolor('Warning', "The file [$archivetar.tar.gz] already exists and will be rewritten"); |
| 2274 |
Log("Rewriting the file $archivetar.tar.gz in $workdir"); |
| 2275 |
} |
| 2276 |
else{ |
| 2277 |
print "Creating the file ", color('magenta'), "[$archivetar.tar.gz]", |
| 2278 |
color('reset'), " with generate files in ./$imgdir\r\n"; |
| 2279 |
Log("Writen the file $archivetar.tar.gz in $workdir"); |
| 2280 |
} |
| 2281 |
my $imgdirtar = Archive::Tar->new(); |
| 2282 |
$imgdirtar->add_files(@savetozt); |
| 2283 |
$imgdirtar->write( "$archivetar.tar.gz" , 9 ); |
| 2284 |
} |
| 2285 |
} |
| 2286 |
|
| 2287 |
### End of script process |
| 2288 |
if (!$norun && !$nosource) { |
| 2289 |
Log("The image files: $format and generated files are in $imgdirpath"); |
| 2290 |
} |
| 2291 |
if (!$norun && $nosource) { |
| 2292 |
Log("The image files: $format are in $imgdirpath"); |
| 2293 |
} |
| 2294 |
if ($norun && !$nosource) { |
| 2295 |
Log("The generated files are in $imgdirpath"); |
| 2296 |
} |
| 2297 |
Log("The output file $name-pdf$ext are in $workdir"); |
| 2298 |
|
| 2299 |
Infocolor('Finish', "The execution of $scriptname has been successfully completed"); |
| 2300 |
|
| 2301 |
Log("The execution of $scriptname has been successfully completed"); |
| 2302 |
|
| 2303 |
__END__ |