1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
|
#!/usr/bin/env perl
# /=====================================================================\ #
# | texscan | #
# | scan & analyze style, class file coverage | #
# |=====================================================================| #
# | support tools for LaTeXML: | #
# | Public domain software, produced as part of work done by the | #
# | United States Government & not subject to copyright in the US. | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov> #_# | #
# | http://dlmf.nist.gov/LaTeXML/ (o o) | #
# \=========================================================ooo==U==ooo=/ #
use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case);
use Pod::Usage;
use FindBin;
# Assume we're in the tools directory of a development version of latexml (next to lib, blib..)
use lib "$FindBin::RealBin/../blib/lib";
use LaTeXML::Util::Pathname;
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# texscan : TeX class/style file scanner for LaTeXML.
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
my $identity = 'texscan';
my ($help) = (0);
local $::VERBOSITY = 0;
local $::AMSTEX = 0;
my ($show, $internal, $cumulative, $basepkgs) = ('diff', undef, undef, undef);
my $do_all = 0;
my @searchpaths = ();
GetOptions("tex" => sub { $show = 'tex'; },
"latexml" => sub { $show = 'latexml'; },
"diff" => sub { $show = 'diff'; },
"signature" => sub { $show = 'signature'; },
"stub" => sub { $show = 'stub'; },
"internal!" => \$internal,
"cumulative!" => \$cumulative,
"amstex!" => \$::AMSTEX,
"help" => \$help,
"verbose" => sub { $::VERBOSITY++; },
"quiet" => sub { $::VERBOSITY--; },
"path=s" => \@searchpaths,
"base=s" => \$basepkgs,
"all" => \$do_all,
) or pod2usage(-message => $identity, -exitval => 1, -verbose => 0, -output => \*STDERR);
pod2usage(-message => $identity, -exitval => 1, -verbose => 2, -output => \*STDOUT) if $help;
pod2usage(-message => $identity, -exitval => 1, -verbose => 0, -output => \*STDOUT)
unless $do_all or @ARGV;
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Find available package/class implementations
# [Should get this from LaTeXML?]
our $LTXPACKAGEDIR = "$FindBin::RealBin/../blib/lib/LaTeXML/Package/";
my @files = ();
if ($do_all) {
foreach my $dir ($LTXPACKAGEDIR, @searchpaths) {
my $DIR_FH;
opendir($DIR_FH, $dir) or die "Couldn't read LaTeXML Package directory $dir: $!";
foreach my $file (sort readdir($DIR_FH)) {
push(@files, $1) if $file =~ /^(.*)\.ltxml$/; }
closedir($DIR_FH); } }
else {
@files = @ARGV; }
foreach my $file (@files) {
if ($show eq 'tex') {
print "" . ("=" x 50) . "\n";
print "Showing symbols in LaTeX package $file\n";
if (my $pkg = StylePackage->new($file, searchpaths => [@searchpaths])) {
$pkg->cleanup($cumulative, $internal);
$pkg->convertEnvironments;
$pkg->show; } }
elsif ($show eq 'latexml') {
print "" . ("=" x 50) . "\n";
print "Showing symbols in LaTeXML implementation $file\n";
if (my $pkg = ImplementationPackage->new($file, searchpaths => [@searchpaths])) {
$pkg->cleanup($cumulative, $internal);
$pkg->convertEnvironments;
$pkg->show; } }
elsif ($show eq 'diff') {
print "" . ("=" x 50) . "\n";
print "Showing unimplemented symbols for package $file\n";
if (my $pkg = StylePackage->new($file, searchpaths => [@searchpaths])) {
if (my $ipkg = ImplementationPackage->new($file, searchpaths => [@searchpaths])) {
# $pkg->subtract($ipkg);
# $pkg->cleanup($cumulative, $internal);
# $pkg->convertEnvironments;
# $pkg->show; } } }
print "Symbols missing from LaTeXML:\n";
my $sdiff = $pkg->clone->subtract($ipkg);
$sdiff->cleanup($cumulative, $internal);
$sdiff->convertEnvironments;
$sdiff->show;
print "Extra Symbols in LaTeXML:\n";
my $idiff = $ipkg->clone->subtract($pkg);
$idiff->cleanup($cumulative, $internal);
$idiff->convertEnvironments;
$idiff->show;
} } }
elsif ($show eq 'signature') {
if (my $pkg = StylePackage->new($file, searchpaths => [@searchpaths])) {
$cumulative = 1 unless defined $cumulative; # default IS cumulative
$pkg->cleanup($cumulative, $internal);
$pkg->convertEnvironments;
$pkg->signature; } }
elsif ($show eq 'stub') {
if (my $pkg = StylePackage->new($file, searchpaths => [@searchpaths])) {
$pkg->cleanup(1, $internal);
$pkg->generateStub($basepkgs); } }
}
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Abstract Package package.
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# To put things on common footing --- or at least as common as we can
# --- we initially load a package or class by "cumulative" & "internal".
# Additionally, we load latex.ltx, possibly article.cls...
# Think of it this way: The basic load/scan of a package contains all
# control sequences that would be accessible if the package were
# loaded "normally". Thus loading a class also has latex.ltx's symbols.
# Loading a style file also has some class, might as well be article, symbols.
# (this only affects *.cls and *.sty files)
#
# ->new gets a copy of the class, so you can use add, subtract and
# various remove methods (which are otherwise `destructive') to get
# the collection of symbols you want.
# newInternal loads as needed and caches the result, so that a
# large collection of packages can be processed in batch.
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
package Package;
use strict;
use LaTeXML::Util::Pathname;
our %CACHE;
# Get a copy of the Package for $path; all subpackages & internal symbols are included.
sub new {
my ($classorself, $path, %options) = @_;
my $package = $classorself->newInternal($path, searchpaths => $options{searchpaths})->clone;
foreach my $add ($package->getBase) {
if (my $pkg = $package->newInternal($add)) {
$package->add($pkg); } }
return $package; }
# Get the cached version of Package;
# you must use ->clone() if you're going to modify it.
sub newInternal {
my ($classorself, $path, %options) = @_;
my $class = ref $classorself || $classorself;
my ($dir, $name, $type) = pathname_split($path);
$name .= '.' . $type if $type;
$name = "latex.ltx" if $name eq "LaTeX.pool";
$name = "plain.tex" if $name eq "TeX.pool";
$dir = '.' unless $dir;
if (my $cached = $CACHE{$class}{$name}) {
return $cached; }
my @paths = ();
push(@paths, $dir);
push(@paths, @{ $options{searchpaths} }) if $options{searchpaths};
push(@paths, @{ $$classorself{searchpaths} }) if ref $classorself;
my $self = bless { name => $name, %options, symbols => {}, includes => {}, searchpaths => [@paths] }, $class;
$CACHE{$class}{$name} = $self;
$self->scan;
return $self; }
sub clone {
my ($self) = @_;
return bless {
name => $$self{name},
declared_type => $$self{declared_type},
declared_as => $$self{declared_as},
symbols => { map { ($_ => 1) } keys %{ $$self{symbols} } },
includes => { map { ($_ => 1) } keys %{ $$self{includes} } },
searchpaths => [@{ $$self{searchpaths} }] }, ref $self; }
sub show {
my ($self) = @_;
my @syms = sort keys %{ $$self{symbols} };
print " Macros : " . (@syms ? join(', ', @syms) : "None") . "\n";
return; }
# A variant of show that prints a compact signature (file:cs,...)
sub signature {
my ($self) = @_;
my @syms = keys %{ $$self{symbols} };
print $$self{name} . ":" . ($$self{declared_as} || '') . ":" . join(',', sort @syms) . "\n";
return; }
sub cleanup {
my ($self, $include_inclusions, $include_internal) = @_;
if ($include_inclusions) {
$self->removeBase; } # Remove at least Base
else {
$self->removeInclusions; } # if not base and all other included
$self->removeInternal unless $include_internal;
return $self; }
# Convert any \foo, \endfoo combinations into {foo} an (assumed) environment.
sub convertEnvironments {
my ($self) = @_;
foreach my $cs (keys %{ $$self{symbols} }) {
if ($cs =~ /^\\(.*)$/) {
my $env = $1;
my $endcs = "\\end$env";
if ($$self{symbols}{$endcs}) {
$$self{symbols}{"{$env}"} = 1;
delete $$self{symbols}{$cs};
delete $$self{symbols}{$endcs}; } }
}
return; }
# Add any symbols defined in $package into the $self. Modifies $self!
sub add {
my ($self, $package) = @_;
print "Adding $$package{name} symbols to $$self{name}\n" if $::VERBOSITY >= 1;
foreach my $cs (%{ $$package{symbols} }) {
$$self{symbols}{$cs} = 1; }
return $self; }
# Remove any symbols defined in $package from the $self. Modifies $self!
sub subtract {
my ($self, $package) = @_;
print "Removing $$package{name} symbols from $$self{name}\n" if $::VERBOSITY >= 1;
foreach my $cs (%{ $$package{symbols} }) {
delete $$self{symbols}{$cs}; }
return $self; }
# Remove the contributions of any included packages. Modifies $self!
sub removeInclusions {
my ($self) = @_;
foreach my $remove ($self->getBase, keys %{ $$self{includes} }) {
if (my $pkg = $self->newInternal($remove)) {
$self->subtract($pkg); } }
return; }
# Remove the contributions of any base packages. Modifies $self!
sub removeBase {
my ($self) = @_;
foreach my $remove ($self->getBase) {
if (my $pkg = $self->newInternal($remove)) {
$self->subtract($pkg); } }
return; }
# Remove any "internal" symbols (those containing '@')
sub removeInternal {
my ($self) = @_;
foreach my $cs (keys %{ $$self{symbols} }) {
delete $$self{symbols}{$cs} if $cs =~ /\@/; }
return; }
# Return a list of packages that should be implicitly included.
sub getBase {
my ($self) = @_;
my @base = ();
if ($::AMSTEX) {
push(@base, "amstex.tex") if $$self{name} =~ /\.sty$/;
}
else {
push(@base, "article.cls") if $$self{name} =~ /\.sty$/;
push(@base, "latex.ltx") if $$self{name} =~ /\.(sty|cls)$/;
}
return @base; }
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# StylePackage
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# A package scanned from a (La)TeX style/class file.
# The patterns may need more work;
# Some operations very sneakily end up defining symbols,
# which we'll miss if we don't work at it.
# We also scan any included files and include thier contributions.
# Of course, some definitions and inclusions are sufficiently
# indirect, via parameters or computed macros, that you would only
# know if you executed TeX.
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
package StylePackage;
use strict;
use LaTeXML::Util::Pathname;
use base qw(Package);
our $CSREGEXP;
our $ARGREGEXP;
our $BRACEREGEXP;
our $OPTREGEXP;
BEGIN {
$CSREGEXP = qr/(\\(?:[\w\@]+|.))/;
$ARGREGEXP = qr/(?:\{([^\}\#]+)\}|(\\(?:[\w\@]+|.)))/;
$BRACEREGEXP = qr/\{([^\}\#]+)\}/;
$OPTREGEXP = qr/(\[[^\]]*\])?/; }
sub scan {
my ($self) = @_;
my $name = $$self{name};
my $file = `kpsewhich $name`; chomp($file);
if (!$file) {
$file = pathname_find($name, paths => $$self{searchpaths}); }
if (!$file) {
print STDERR "WARN: No stylefile found for $name\n";
return $self; }
# Scan the stylefile for apparently defined symbols
$self->scan_internal($file);
# Many important inclusions are invoked with macros for arguments (eg art1\@ptsize.sty !)
# We'll make a grudging attempt to rescan for possible assignments to
# these macros (within the same stylefile!)
# see ->noteInclusion for how rescanfor got set up.
# NOTE Also, that some latex209 `classes' REQUIRE that certain options
# (which will load files) are used and that these classes are incomplete without them!!!
if ($$self{rescanfor}) {
$self->rescan_internal($file); }
# Remove trivial things that probably are just font declarations
foreach my $sym (keys %{ $$self{symbols} }) {
delete $$self{symbols}{$sym} if length($sym) == 1; }
# Need to explicitly add contributes from included packages.
foreach my $add (keys %{ $$self{includes} }) {
if (my $pkg = $self->newInternal($add)) {
$self->add($pkg);
if (($add =~ /\.cls$/) && ($name =~ /\.cls$/) && !$$self{declared_as}) {
$$self{declared_as} = $$pkg{declared_as};
$$self{declared_type} = $$pkg{declared_type}; } } }
if ($name =~ /\.cls$/) {
my $c = $$self{declared_as};
my $t = $$self{declared_type};
if (!$c) {
print STDERR "WARN: $name not declared with \\ProvidesClass\n"; }
elsif (("$c.cls" ne $name) || ($t ne 'Class')) {
print STDERR "NOTE: $name declared as $t $c\n"; } }
return $self; }
sub scan_internal {
my ($self, $file) = @_;
print "Scanning style of $$self{name} from $file\n" if $::VERBOSITY >= 1;
my $STYLE_FH;
open($STYLE_FH, '<', $file) or die "Couldn't open stylefile $file: $!";
while (<$STYLE_FH>) {
s/%.*$//;
# Note that more than 1 "defn" can appear per line! so no "elsif"
# Various standard definitions
if (/\\(?:define|def|xdef|gdef|edef)\s*$CSREGEXP/o) {
$$self{symbols}{$1} = 1; }
if (/\\new(?:count|dimen|skip|muskip|box|help|toks|read|write|fam|language|insert)\s*$CSREGEXP/o) {
$$self{symbols}{$1} = 1; }
if (/\\newif\s*\\if([\w\@]+)/) {
$$self{symbols}{"\\if$1"} = 1;
$$self{symbols}{"\\${1}true"} = 1;
$$self{symbols}{"\\${1}false"} = 1; }
if (/\\(?:renew|new|provide)command\s*\*?\s*$ARGREGEXP/o) {
$$self{symbols}{ $1 || $2 } = 1; }
if (/\\(?:Declare|Provide)(?:TextFont|Text|Robust)Command(?:Default)?\s*\*?\s*$ARGREGEXP/o) {
$$self{symbols}{ $1 || $2 } = 1; }
if (/\\let$CSREGEXP\s*=?\s*$CSREGEXP/o) {
$$self{symbols}{$1} = 1; }
# Defining math/text symbols
if (/\\DeclareMath(?:Symbol|Accent|Delimiter)\s*$ARGREGEXP/o) {
$$self{symbols}{ (defined $1 ? $1 : $2) } = 1; }
if (/\\re\@DeclareMath(?:Symbol|Accent|Delimiter)\s*$ARGREGEXP/o) { # txfonts
$$self{symbols}{ (defined $1 ? $1 : $2) } = 1; }
# Other packages used.
if (/\\(?:usepackage|RequirePackage)\s*$OPTREGEXP\s*$BRACEREGEXP/o) {
map { $self->noteInclusion($_, 'sty') } split(/,/, $2); }
if (/\\(?:LoadClassWithOptions|LoadClass)\s*$OPTREGEXP\s*$BRACEREGEXP/o) {
$self->noteInclusion($2, 'cls'); }
if (/\\(?:input|InputIfFileExists)\s*$BRACEREGEXP/o) {
$self->noteInclusion($1 || $2); }
elsif (/\\(?:input)\s+(.*?)(?:\s|\\relax|}|$)/o) {
$self->noteInclusion($1); }
# DLMF functions
if (/\\defSpecFun$ARGREGEXP/) {
$$self{symbols}{"\\$1"} = 1; }
# Environments
if (/\\(?:re)?newenvironment\s*\*?\s*$BRACEREGEXP/o) {
$$self{symbols}{ "\\" . $1 } = 1;
$$self{symbols}{ "\\end" . $1 } = 1; }
# Other signifcant declarations
if (/\\Provides(Class|Package)\s*$BRACEREGEXP/o) {
$$self{declared_type} = $1;
$$self{declared_as} = $2; }
if (/\\(?:include|exclude)comment$ARGREGEXP/) {
$$self{symbols}{ "\\" . $1 } = 1;
$$self{symbols}{ "\\end" . $1 } = 1; }
}
close($STYLE_FH);
return; }
sub noteInclusion {
my ($self, $file, $defaulttype) = @_;
$file =~ s/^\s+//; $file =~ s/\s+$//;
my ($dir, $name, $type) = pathname_split($file);
$type = $defaulttype unless $type;
$name .= '.' . $type if $type;
return if $name eq $$self{name};
$name =~ s/1?\\\@ptsize/10/;
if ($name =~ /^([^\\]*)\\([a-zA-Z\@]+)([^\\]*)$/) { # Ugh...
$$self{rescanfor}{$2} = [$1, $3]; }
elsif ($name =~ /(?:\\|#)/) { } # just skip it if too hard or unknowable
else {
$$self{includes}{$name} = 1; }
return; }
sub rescan_internal {
my ($self, $file) = @_;
my $rescan = '(' . join('|', map { $_ } keys %{ $$self{rescanfor} }) . ')';
my $STYLE_FH;
open($STYLE_FH, '<', $file) or die "Couldn't open stylefile $file: $!";
while (<$STYLE_FH>) {
s/%.*$//;
# Line defines one of the indirection macros, so reconstruct the likely file to be input.
if (/\\(?:def|xdef|gdef|edef|newcommand|renewcommand)\s*\\$rescan\s*\{(\w+)\}/) {
my ($pre, $post) = @{ $$self{rescanfor}{$1} };
my $inclusion = $pre . $2 . $post;
$self->noteInclusion($inclusion); } }
close($STYLE_FH);
return; }
# Generate a stub implementation for a given package.
our $STUB_HEADER;
# NOTE: Rearrange this so that the base packages are ALWAYS loaded (?)
# and inclusions only if they actually contribute something?
sub generateStub {
my ($self, $base) = @_;
my $dest = $$self{name} . ".ltxml";
# Copy any existing file to *.bak --- but ONLY if there isn't already a bak!
if ((-f $dest) && !(-f "$dest.bak")) {
rename $dest, "$dest.bak" or die "Couldn't rename $dest to $dest.bak: $!";
print "Saved previous $dest to $dest.bak\n"; }
my $STUB_FH;
open($STUB_FH, '>', $dest) or die "Couldn't open '$dest' for writing: $!";
my %inc = %{ $$self{includes} };
my @base = split(/,/, $base);
my %base = ($base ? map { ($_ => 1) } @base : ());
$self->removeBase;
$self->removeInternal unless $internal;
my $header = $STUB_HEADER;
my $headername = $$self{name} . (' ' x (68 - length($$self{name}))) . "| #";
$header =~ s/BINDINGNAME/$headername/;
print $STUB_FH $header;
# get unique classes, starting with those in $base
my @classes = ((grep { /\.cls$/ } keys %base), (grep { /\.cls$/ && !$base{$_} } keys %inc));
if (($$self{name} =~ /\.sty$/) && @classes) {
print STDERR "WARN: Ignoring class file used in style file $$self{name}: " . join(', ', @classes) . "\n";
@classes = (); }
elsif (@classes > 1) {
print STDERR "WARN: Ignoring excess classes for $$self{name}: " . join(', ', @classes[1 .. $#classes]) . "\n";
@classes = ($classes[0]); }
if (@classes) {
my $c = $classes[0]; $c =~ s/\.cls$//;
print $STUB_FH "LoadClass('$c',withoptions=>1);\n";
$self->subtract($self->new($classes[0])); }
# The process inclusions starting with $base, then the actual package's inclusions
# But only include the ones that have an "effect".
foreach my $pkg (@base, grep { /\.sty$/ } keys %inc) {
my $nbefore = scalar(keys %{ $$self{symbols} });
my $package = $self->new($pkg);
$package->removeBase; #?
$self->subtract($package);
my $p = $pkg; $p =~ s/\.sty$//;
my $nafter = scalar(keys %{ $$self{symbols} });
if ($nbefore > $nafter) {
if ($base{$pkg}) {
print $STUB_FH "RequirePackage('$p',withoptions=>1);\n"; }
else {
print $STUB_FH "RequirePackage('$p');\n"; } }
else {
print $STUB_FH "# No effect from $pkg\n"; } }
$self->convertEnvironments;
my @syms = sort keys %{ $$self{symbols} };
if (@syms) {
printStubComments($STUB_FH, "-",
"INCOMPLETE IMPLEMENTATION",
"remove this comment, when done.",
"Drafted by texscan --stub" . ($base ? " --base=$base" : '') . ' ' . $$self{name},
"-");
foreach my $sym (@syms) {
if ($sym =~ /^\{.*\}$/) {
print $STUB_FH "# DefEnvironment('$sym','#body');\n"; }
else {
print $STUB_FH "# DefMacro('$sym',Tokens());\n"; } } }
print $STUB_FH "#" . ("=" x 70) . "\n1;\n";
close($STUB_FH);
print "Wrote binding of $$self{name} to $dest\n";
print "NOTE: Please edit to complete " . scalar(@syms) . " definitions.\n" if @syms;
return; }
BEGIN {
$STUB_HEADER = <<'EOHeader';
# -*- mode: Perl -*-
# /=====================================================================\ #
# | BINDINGNAME
# | Implementation for LaTeXML | #
# |=====================================================================| #
# | Part of LaTeXML: | #
# | Public domain software, produced as part of work done by the | #
# | United States Government & not subject to copyright in the US. | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov> #_# | #
# | http://dlmf.nist.gov/LaTeXML/ (o o) | #
# \=========================================================ooo==U==ooo=/ #
package LaTeXML::Package::Pool;
use strict;
use warnings;
use LaTeXML::Package;
#======================================================================
EOHeader
}
sub printStubComments {
my ($out, @lines) = @_;
my $LENGTH = 70;
my $N = scalar(@lines);
for (my $i = 0 ; $i < $N ; $i++) {
my $line = $lines[$i];
if ($line =~ /^[-_=%\*#]$/) {
print $out "# "
. ($i == 0 ? " /" : ($i == $N - 1 ? " \\" : " >"))
. ($line x ($LENGTH - 2))
. ($i == 0 ? "\\" : ($i == $N - 1 ? "/" : "<")) . "\n"; }
else {
my $n = $LENGTH - length($line) - 1;
print $out "# | " . $line . (' ' x $n) . "|\n"; } }
return; }
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# ImplementationPackage
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# A package scanned from a LaTeXML implementation.
# This gets its list of symbols by loading the package into LaTeXML,
# and then extracting all defined symbols from the STATE table.
# In order to be able to load the package, TeX.pool and LaTeX.pool
# must be loadd, and by executing the laod, any included packages
# are automatically included (ie. it is automatically cumulative)
# To compute the non-cumulative list takes more work:
# We must find all included packages, load them, and subtract them.
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
package ImplementationPackage;
use strict;
use base qw(Package);
use LaTeXML::Core;
use LaTeXML::Global;
use LaTeXML::Util::Pathname;
sub scan {
my ($self) = @_;
my $name = $$self{name};
print "Loading $name into LaTeXML\n" if $::VERBOSITY >= 1;
my $latexml = LaTeXML::Core->new(searchpaths => ($$self{searchpaths} || []), verbosity => $::VERBOSITY - 1);
$latexml->withState(sub {
my $stomach = $STATE->getStomach; # The current Stomach;
my $gullet = $stomach->getGullet;
$stomach->initialize;
my $paths = $STATE->lookupValue('SEARCHPATHS');
my @pkgs = ("TeX.pool");
if ($::AMSTEX) {
push(@pkgs, "amstex.tex") if $$self{name} =~ /\.sty$/; }
elsif ($name eq 'plain.tex') { }
else {
push(@pkgs, "LaTeX.pool");
push(@pkgs, "article.cls") unless $name =~ /\.(cls|ltx)$/;
}
push(@pkgs, $name) unless $name eq 'latex.ltx';
## Since we're mucking with STATE, we'll reload, BUT we'll get redefinitions;
## We'd really rather not get all the warnings, though!!
#### These didn't work;
#### no warnings;
#### local $^W = 0;
# Try Deyan's heavier hammer!
foreach my $subname (keys %LaTeXML::Package::Pool::) {
undef $LaTeXML::Package::Pool::{$subname};
delete $LaTeXML::Package::Pool::{$subname}; }
foreach my $pkg (@pkgs) {
my $loadpath = pathname_find("$pkg.ltxml", paths => $paths, installation_subdir => 'Package');
if (!$loadpath) {
if ($pkg =~ /(.*)\.cls$/) {
print STDERR "WARN: Couldn't find LaTeXML implementation for $pkg; using article.cls\n";
LaTeXML::Package::LoadClass('article'); }
else {
print STDERR "WARN: Couldn't find LaTeXML implementation for $pkg\n";
return; } }
if ($pkg =~ /(.*)\.pool$/) { LaTeXML::Package::LoadPool($1); }
elsif ($pkg =~ /(.*)\.sty$/) { LaTeXML::Package::RequirePackage($1); }
elsif ($pkg =~ /(.*)\.cls$/) { LaTeXML::Package::LoadClass($1); }
else { LaTeXML::Package::InputDefinitions($loadpath); } }
print "Scanning defined symbols\n" if $::VERBOSITY >= 1;
my $table = $$STATE{meaning};
foreach my $entry (keys %{$table}) {
if ($entry =~ /^\\begin\{(.*?)\}$/) {
$$self{symbols}{ "\\" . $1 } = 1; }
elsif ($entry =~ /^\\end\{(.*?)\}$/) {
$$self{symbols}{ "\\end" . $1 } = 1; }
elsif ($entry =~ /^(\\.*)$/) {
$$self{symbols}{$1} = 1; }
elsif ($entry =~ /^(.*)\.ltxml_loaded$/) {
my $inc = $1;
$$self{includes}{$inc} = 1 unless ($inc eq $name) || $inc =~ /\\/; } } });
return $self; }
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
__END__
=head1 NAME
C<texscan> I<options> I<stylefile> ...
=head1 SYNOPSIS
texscan [options] styles
texscan scans (La)TeX class or style files or LaTeXML implementation files
for control sequences that are defined defined. The options
C<--tex>, C<--latexml>, C<--diff>, C<--signature>, C<--stub>
specify what to do with the symbol lists.
NOTE: You generally have to specify the style with explicit .sty
Options:
--tex Show the symbols defined in a (La)TeX package or class.
--latexml Show the symbols defined in a LaTeXML implementation.
--diff Show the symbols missing from a LaTeXML implementation.
--signature Generate a signature of a (La)TeX package. (see C<cluster>)
--stub Generate a stub implementation of a (La)TeX package.
--all Scan all implementation files.
--internal Include internal symbols (including '@'), default no.
--nointernal Don't include internal symbols
--cumulative Include the definitions from included files.
--nocumulative Don't include symbols defined in included files.
--amstex process in amstex mode (no LaTeX or article assumed)
--path=dir Add dir to the list of paths to search for files.
--base=names comma separated list of base packages for --stub
these are included in the code whether or not they
were loaded from the style file.
--help Shows this help message.
--quiet Runs more quietly
--verbose Runs more noisily
=head1 Scanning
Scanning of TeX files uses a variety of regular expressions to
recognize all control sequences that are (or might be) defined.
Any included files (\usepackage, \LoadClass, etc) are also scanned
and added.
Scanning of LaTeXML files is done by loading the the implementation
file into a C<LaTeXML> object and scanning for any symbols that
are bound in the C<State> object.
The base LaTeX file C<latex.ltx> (and its inclusions)
is considered to be implicitly included by all style (C<*.sty>)
and class files (C<*.cls>),
whereas C<article.cls> (and its inclusions) are considered to
be included by all style files (C<*.sty>).
However, these are removed before display, even when cumulative.
Inclusions are removed unless cumulative is requested.
The default is non-cumulative, except for C<--signature>,
which removes only the base inclusions (C<latex.ltx> and C<article.cls>).
Internal symbols (containing a C<@>) are removed unless
internal is requested. The default excludes internal symbols.
=head1 Stub implementations
texscan with option C<--tex> lists what control sequences are
defined in a given style file. With option C<--diff>, it lists
which control sequences are defined in the TeX style file, but
missing from the LaTeXML implementation. As a convenience,
using the option C<--stub> can be used to generate an initial
implementation for the package. In most cases, this will still
need to be editted to be complete and useful, however.
A command such as
texscan --stub --base=article.cls,graphicx.sty foo.sty bar.sty baz.sty
will create stub LaTeXML implementations for C<foo.sty.ltxml>,...,
consisting of commands to load the classes or styles in C<--base>.
Then commands are generated for any remaining inclusions
in the source file, provided they contribute anything
(after having removed the affect of previously loaded packages).
Finally, commented-out definitions are generated for any remaining
control sequences defined by in C<foo.sty>.
=head1 Bugs
The set of regular expressions used to scan TeX files is
undoubtedly incomplete. Since both definitions and inclusions
can also be effected at run time (eg. by passing or constructing
the symbol to define), such symbols are also missed.
The symbols scanned from a TeX file include any symbol that
may end up defined only within certain contexts, whereas
the LaTeXML scan includes only symbols that are defined globally.
For all these reasons, both lists and comparisons of defined symbols
can have differences that are not significant. The results
of texscan thus require supervision with at least minimal
understanding of the style files involved.
Additionally, in its current form, C<texscan> knows nothing
about the intended parameters for any control sequences.
=cut
|