diff options
author | Karl Berry <karl@freefriends.org> | 2018-05-01 17:35:37 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2018-05-01 17:35:37 +0000 |
commit | 0654b90bb77ae003657942d523046abf5bac1426 (patch) | |
tree | 419e99812e1c4e2a9aebd08f12f8bd5bb6570224 /Build | |
parent | 71ec9693848d6ff56ab7f9b5a153df122ee87172 (diff) |
latexpand (18apr18)
git-svn-id: svn://tug.org/texlive/trunk@47535 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build')
-rwxr-xr-x | Build/source/texk/texlive/linked_scripts/latexpand/latexpand | 165 |
1 files changed, 147 insertions, 18 deletions
diff --git a/Build/source/texk/texlive/linked_scripts/latexpand/latexpand b/Build/source/texk/texlive/linked_scripts/latexpand/latexpand index f98c0ee9598..ea18e58cd42 100755 --- a/Build/source/texk/texlive/linked_scripts/latexpand/latexpand +++ b/Build/source/texk/texlive/linked_scripts/latexpand/latexpand @@ -1,13 +1,13 @@ #!/usr/bin/perl # Inspired by latexpand by D. Musliner, University of Michigan -# 2012, 2013, 2014, 2015, 2016: Matthieu Moy <Matthieu.Moy@imag.fr> +# 2012, 2013, 2014, 2015, 2016, 2017: Matthieu Moy <git@matthieu-moy.fr> # BSD License use strict; use Cwd; use Getopt::Long; -use Pod::Usage; use IO::Handle; +use File::Spec; my $TEXINPUTS = $ENV{'TEXINPUTS'}; if (!$TEXINPUTS) { $TEXINPUTS = getcwd(); } @@ -27,6 +27,7 @@ my $expand_bbl; my $fatal; my $version; my $makeatletter; +my $inside_import; GetOptions ( 'h' => \$help, @@ -44,16 +45,38 @@ GetOptions ( 'fatal' => \$fatal, 'version' => \$version, 'makeatletter' => \$makeatletter, -) or pod2usage(2); +) or pod2usage_wrapper(2); version() if $version; -pod2usage(1) if $help; -pod2usage(-exitstatus => 0, -verbose => 2) if $long_help; +pod2usage_wrapper(0) if $help; +pod2usage_wrapper(-exitstatus => 0, -output => \*STDOUT, -verbose => 2) if $long_help; + +sub pod2usage_wrapper +{ + # Like pod2usage, but fall back to a simpler implem in case + # pod2usage can't be found. + if (eval {require Pod::Usage;1;} ne 1) { + print "Please install perldoc and Pod::Usage to get proper help.\n"; + my $started = 0; + open (my $in, '<', "$0") or die $!; + while (<$in>) { + if ($started) { + print; + } + if (/^__END__$/) { + $started = 1; + } + } + } else { + Pod::Usage->import(); + pod2usage(@_); + } +} sub get_version { # $VERSION's value will be substituted by 'make dist', but the # next line won't (the string has to be broken to avoid it). - my $VERSION = 'v1.3'; + my $VERSION = 'v1.4-1-gb32a776'; if ($VERSION eq '@LATEXPAND' . '_VERSION@') { my($vol,$dir,$file) = File::Spec->splitpath($0); chdir($dir); @@ -97,6 +120,7 @@ foreach my $file (@ARGV) say "processing $file\n"; $makeatletter_found = 0; $in_preamble = 1; + $inside_import = ""; process_file($file, " "); } @@ -106,14 +130,15 @@ sub process_file my $prefix = (shift || ""); my $in_comment = 0; open(my $FILE, "<", $file) or die "could not open input file '$file'\n"; + my $commented_newline = 0; while (my $line = <$FILE>) { if ($line =~ /^[ \t]*\\endinput/) { $line =~ s/(\\endinput.*)\n/% $1/; $in_comment = 1; - process_line($line, $prefix); + process_line($line, $prefix, \$commented_newline); last; } - process_line($line, $prefix, $file); + process_line($line, $prefix, \$commented_newline, $file); if ($line =~ /^%.*[^\n]\z/ || $line =~ /[^\\]%.*[^\n]\z/) { # file ends with a comment not ending with a newline print "\n"; @@ -132,8 +157,22 @@ sub process_file sub process_line { - my ($line, $prefix, $file) = @_; + my ($line, $prefix, $commented_newline, $file) = @_; $_ = $line; + if ($$commented_newline && /^\s*$/) { + # Deal with: + # + # Line 1 % comment + # + # Line 2 + # + # The newline after Line 1 is commented, but we still + # want a new paragraph. We strip the comment together + # with its newline, but re-add a newline to chnge + # paragraph here if needed: + print "\n"; + } + $$commented_newline = 0; # Consider \makeatletter only in preamble, because we do want # to warn on \someCommand{\makeatletter\command@with@arobase}. if ($in_preamble && /^[^%]*\\makeatletter/) { @@ -154,10 +193,14 @@ sub process_line # comments at end of files # remove comments + whitespace-only lines completely - s/^\s*%.*\n//; + if (s/^\s*%.*\n//) { + $$commented_newline = 1; + } # remove only the comment if the line has actual content - s/([^\\])%.*\n/$1\n/; + if (s/([^\\])%.*\n/$1/) { + $$commented_newline = 1; + } } # Apply the "empty comments" treatment unconditionally # for end-of-files comments not matched above (it @@ -189,6 +232,9 @@ sub process_line } } elsif (my ($before, $ignored, $full_filename, $after) = /^(([^%]|[^\\]%)*)\\input[{\s]+(.*?)[\s}](.*)$/) { + if ($inside_import) { + $full_filename = $inside_import . $full_filename; + } $full_filename = find_tex_file($full_filename, ":.tex"); if ($full_filename) { say $prefix . "Found input for file: $full_filename\n"; @@ -208,6 +254,71 @@ sub process_line } $_ = ""; } + } elsif (my ($before, $ignored, $dir, $full_filename, $after) + = /^(([^%]|[^\\]%)*)\\(?:sub)?import[{\s]+(.*?)[\s}][{\s]+(.*?)[\s}](.*)$/) { + if ($explain) { + print "% dir " . $dir ."\n"; + print "% full_filename " . $full_filename ."\n"; + print "% after " . $after ."\n"; + print "% inside_import $inside_import\n"; + } + $full_filename = $dir . $full_filename; + if ($inside_import) { + $full_filename = $inside_import . $full_filename; + } + print "% cat(inside_import,dir,full_filename) " . $full_filename ."\n" if ($explain); + $full_filename = find_tex_file($full_filename, ":.tex"); + if ($full_filename) { + say $prefix . "Found input for file: $full_filename\n"; + print $before . $nl; + print "% start input $full_filename\n" if ($explain); + my $previous_import_dir = $inside_import; + $inside_import = $inside_import . $dir; + my $in_comment = process_file($full_filename, $prefix . " "); + $inside_import = $previous_import_dir; + if ($explain) { + print " % end input $full_filename\n"; + } elsif ($in_comment) { + print "\n"; + } + if ($after =~ /[^\s]/) { + # LaTeX produces this space, so let's do it also + print " " . $nl . $after . "\n"; + } else { + print " "; + } + $_ = ""; + } + } elsif (my ($before, $ignored, $args, $full_filename, $after) + = /^(([^%]|[^\\]%)*)\\includegraphics[\[\s]+(.*?)[\s\]][{\s]+(.*?)[\s}](.*)$/) { + if ($explain) { + print "% inside_import " . $inside_import ."\n"; + print "% before " . $before ."\n"; + print "% ignored " . $ignored ."\n"; + print "% args " . $args ."\n"; + print "% full_filename " . $full_filename ."\n"; + print "% after " . $after ."\n"; + } + if ($inside_import) { + $full_filename = $inside_import . $full_filename; + print "$before\\includegraphics[$args]{$full_filename}$after\n"; + $_ = ""; + } + } elsif (my ($before, $ignored, $args, $full_filename, $after) + = /^(([^%]|[^\\]%)*)\\lstinputlisting[\[\s]+(.*?)[\s\]][{\s]+(.*?)[\s}](.*)$/) { + if ($explain) { + print "% inside_import " . $inside_import ."\n"; + print "% before " . $before ."\n"; + print "% ignored " . $ignored ."\n"; + print "% args " . $args ."\n"; + print "% full_filename " . $full_filename ."\n"; + print "% after " . $after ."\n"; + } + if ($inside_import) { + $full_filename = $inside_import . $full_filename; + print "$before\\lstinputlisting[$args]{$full_filename}$after\n"; + $_ = ""; + } } } if ($expand_usepackage) { @@ -219,6 +330,9 @@ sub process_line say $prefix . "Found package file: $full\n"; process_file($full, $prefix . " "); $_ = ""; + # Forget about any commented newline + # before the \usepackage: + $$commented_newline = 0; } else { say $prefix . "Not including external package $package_name\n"; } @@ -226,7 +340,7 @@ sub process_line } if ($expand_bbl) { if (my ($before, $bib_name, $after) - = /^(.*)\\bibliography\{([^\}]*)\}(.*)$/) { + = /^(.*)\\(?:bibliography|bibselect)\{([^\}]*)\}(.*)$/) { # The BBL file is not necessarily $bib_name. # Take it from the command-line. print $before . $nl; @@ -238,7 +352,11 @@ sub process_line } if ($show_graphics) { if (/\\includegraphics(\[[^\]]*\])?{([^}]*)}/) { - my $full = find_tex_file($2, $graphics_extensions); + my $full_filename = $2; + if ($inside_import) { + $full_filename = $inside_import . $full_filename; + } + my $full = find_tex_file($full_filename, $graphics_extensions); say $prefix . "needs graphics file: "; print STDERR "$full\n"; } @@ -258,7 +376,7 @@ sub find_tex_file my $file = shift; my $extensions = (shift || ":"); foreach my $ext (split(':', $extensions, -1)) { - my $full = find_file($file . $ext, $TEXINPUTS); + my $full = find_file_global($file . $ext); if ($full) { return $full; } @@ -271,6 +389,20 @@ sub find_tex_file } } +sub find_file_global +{ + my $file = shift; + if (open(my $fh, "-|", "kpsewhich", $file)) { + my $full = <$fh>; + chomp($full); + close($fh); + if ($full) { + return $full; + } + } + return find_file($file, $TEXINPUTS); +} + sub find_file { my ($file, $path) = @_; @@ -356,9 +488,6 @@ Please, report bugs on the issue tracker on the project site: https://gitlab.com/latexpand/latexpand/issues -Alternatively, you may email directly the author: Matthieu Moy -<Matthieu.Moy@imag.fr>. - =head2 Known bugs =head3 Verbatim @@ -419,4 +548,4 @@ https://lacl.fr/~caubert/notes/portabilite-du-tex.html#dependances =head1 VERSION -This is latexpand version v1.3. +This is latexpand version v1.4-1-gb32a776. |