diff options
author | Karl Berry <karl@freefriends.org> | 2023-02-24 21:34:32 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2023-02-24 21:34:32 +0000 |
commit | e651638b840553dada4878b1454f8de3431b1f82 (patch) | |
tree | ac53e148df3073fe5dc7e1742b659a91d0e4c24c /Master/texmf-dist | |
parent | 570371d7cddc35c7021dd18c617dbb8ce0229ae1 (diff) |
latexpand (24feb23) (branch)
git-svn-id: svn://tug.org/texlive/branches/branch2022.final@66132 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist')
-rw-r--r-- | Master/texmf-dist/doc/support/latexpand/LICENCE | 2 | ||||
-rw-r--r-- | Master/texmf-dist/doc/support/latexpand/README | 6 | ||||
-rw-r--r-- | Master/texmf-dist/doc/support/latexpand/version.txt | 4 | ||||
-rwxr-xr-x | Master/texmf-dist/scripts/latexpand/latexpand | 77 |
4 files changed, 66 insertions, 23 deletions
diff --git a/Master/texmf-dist/doc/support/latexpand/LICENCE b/Master/texmf-dist/doc/support/latexpand/LICENCE index 206b195a7bd..3a03024cb7e 100644 --- a/Master/texmf-dist/doc/support/latexpand/LICENCE +++ b/Master/texmf-dist/doc/support/latexpand/LICENCE @@ -1,4 +1,4 @@ -Copyright (c) 2012-2019, Matthieu Moy <git@matthieu-moy.fr> and +Copyright (c) 2012-2023, Matthieu Moy <git@matthieu-moy.fr> and contributors. All rights reserved. diff --git a/Master/texmf-dist/doc/support/latexpand/README b/Master/texmf-dist/doc/support/latexpand/README index 6f32dfd4e64..8933efb0745 100644 --- a/Master/texmf-dist/doc/support/latexpand/README +++ b/Master/texmf-dist/doc/support/latexpand/README @@ -25,6 +25,10 @@ SYNOPSIS (similar to --expand-bbl FILE, but for biber+biblatex). --help this help message + --define <key>=<val>, -d <key>=<val> + defines a macro key to be replaced by value, e.g., + when called with -d foo=bar would replace all occurences + of \foo in the code with bar. Can be supplied multiple times. --output <file>, -o <file> generate output in <file> --explain generate explanatory comments in output @@ -121,5 +125,5 @@ SEE ALSO https://lacl.fr/~caubert/notes/portabilite-du-tex.html#dependances VERSION - This is latexpand version v1.6. + This is latexpand version v1.7. diff --git a/Master/texmf-dist/doc/support/latexpand/version.txt b/Master/texmf-dist/doc/support/latexpand/version.txt index ae263a2919f..42952ccf837 100644 --- a/Master/texmf-dist/doc/support/latexpand/version.txt +++ b/Master/texmf-dist/doc/support/latexpand/version.txt @@ -1,2 +1,2 @@ -latexpand version v1.6 (ce086093a2413c99af11cc08aceab8e5483d65ff). -Committed on Thu Dec 12 09:37:27 2019 +0000. +latexpand version v1.7 (6c1d8d9e3b331247ab09bc533a4360ea37deead5). +Committed on Fri Feb 24 10:40:17 2023 +0100. diff --git a/Master/texmf-dist/scripts/latexpand/latexpand b/Master/texmf-dist/scripts/latexpand/latexpand index e7de1965f1c..ee97a46a090 100755 --- a/Master/texmf-dist/scripts/latexpand/latexpand +++ b/Master/texmf-dist/scripts/latexpand/latexpand @@ -1,6 +1,6 @@ #!/usr/bin/perl # Inspired by latexpand by D. Musliner, University of Michigan -# 2012-2019: Matthieu Moy <git@matthieu-moy.fr> +# 2012-2023: Matthieu Moy <git@matthieu-moy.fr> # BSD License use strict; @@ -20,6 +20,7 @@ my $keep_includes; my $empty_comments; my $help; my $long_help; +my %defines = (); my $output; my $explain; my $show_graphics; @@ -41,6 +42,7 @@ GetOptions ( 'keep-comments' => \$keep_comments, 'keep-includes' => \$keep_includes, 'empty-comments' => \$empty_comments, + 'define|d=s%' => \%defines, 'output|o=s' => \$output, 'explain' => \$explain, 'show-graphics' => \$show_graphics, @@ -84,7 +86,7 @@ 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.6'; + my $VERSION = 'v1.7'; if ($VERSION eq '@LATEXPAND' . '_VERSION@') { my($vol,$dir,$file) = File::Spec->splitpath($0); chdir($dir); @@ -131,6 +133,13 @@ foreach my $file (@ARGV) $makeatletter_found = 0; $in_preamble = 1; $inside_import = ""; + if ($file =~ /\.bib$/) { + warn "WARNING: latexpand is not meant to be used on BibTeX files like '$file'.\n" . + " Run latexpand on your main .tex file, using '--expand-bbl FILE'\n" . + " or '--biber FILE' if needed to inline the generated bbl file.\n"; + } elsif (not $file =~ /\.tex$/) { + warn "WARNING: latexpand is meant to be used on .tex files, which $file isn't.\n"; + } process_file($file, " "); } @@ -153,11 +162,19 @@ sub process_file my $commented_newline = 0; while (my $line = <$FILE>) { if ($line =~ /^[ \t]*\\endinput/) { - $line =~ s/(\\endinput.*)\n/% $1/; + # Surprisingly, text after \endinput on the + # same line is kept in output. Also, add a + # space (before %), automatically inserted by + # TeX at the end of file. + $line =~ s/\\endinput(.*)\n?/$1 % /; $in_comment = 1; process_line($line, $prefix, \$commented_newline); last; } + while (my ($k, $v) = each (%defines)) + { + $line=~s!\\$k!$v!g; + } process_line($line, $prefix, \$commented_newline, $file); if ($line =~ /^%.*[^\n]\z/ || $line =~ /[^\\]%.*[^\n]\z/) { # file ends with a comment not ending with a newline @@ -218,12 +235,26 @@ sub process_line print STDERR "Warning: consider using --makeatletter if the result is not compilable.\n"; } - # non-comment is a sequence of: - # - escaped character (\\.), including \% and \\ - # - neither '%' nor '\'. - my $NON_COMMENT = '([^\\\\%]|\\\\.)*'; + # non-comment is a sequence of: + # - escaped character (\\.), including \% and \\ + # - neither '%' nor '\'. + my $NON_COMMENT = '([^\\\\%]|\\\\.)*'; unless ($keep_comments) { + # Special-case for \url{} commands, which may contain '%' + # characters. It's hard to catch them in $NON_COMMENT since we'd + # need a regexp so that "\url{foo" can't match as non-comment in + # the line \url{foo%bar}, but "\url{foo%bar}" would match. + # Escaping these '%' is not mandatory, but allowed, hence we can + # pre-process the line by escaping them, and let latexpand work + # as normal afterwards. + # While there are \url{URL} with unescaped % in URL ... + while (/^(.*\\url\{)(([^\\]%|[^}%])*)(\}.*)$/) { + my ($before, $url, $after) = ($1, $2, $4); + # escape unescaped % in URL + $url =~ s/([^\\])%/$1\\%/g; + $_ = $before . $url . $after ."\n"; + } if (!$empty_comments) { # Include \n in pattern to avoid matching # comments at end of files @@ -235,8 +266,9 @@ sub process_line # Special-case commands at end of line. We # don't want "\\foo%\nbar" to become - # "\\foobar" - if (s/^($NON_COMMENT\\[[:alpha:]@]+)%.*\n/$1 /) { + # "\\foobar" (but we still want \@% to result + # in no space!) + if (s/^($NON_COMMENT\\([[:alpha:]]|[[:alpha:]@]{2,}))%.*\n/$1 /) { $$commented_newline = 1; } elsif (s/^($NON_COMMENT)%.*\n/$1/) { # remove only the comment if the line has actual content @@ -285,11 +317,14 @@ sub process_line $full_filename = find_tex_file($full_filename, ":.tex"); if ($full_filename) { say $prefix . "Found input for file: $full_filename\n"; - # Surprisingly, space after filename - # in \input{foo.tex } is inserted - # _before_ the inclusion. Apply this - # rule in latexpand. - print $before . $trailing . $nl; + # Apparently, in some versions of LaTeX, a space + # after filename in \input{foo.tex } is inserted + # _before_ the inclusion. That was the case for + # me when 31fa806 (deal with space after + # filename in \input and \include, 2019-12-11) + # was written, but is not anymore, hence we just + # throw $trailing away. + print $before . $nl; print "% start input $full_filename\n" if ($explain); my $in_comment = process_file($full_filename, $prefix . " "); if ($explain) { @@ -341,7 +376,7 @@ sub process_line $_ = ""; } } elsif (my ($before, $ignored, $args, $full_filename, $ignored, $after) - = /^($NON_COMMENT)\\includegraphics[\[\s]+(.*?)[\s\]]$ARGUMENT(.*)$/) { + = /^($NON_COMMENT)\\includegraphics(\[[^\]]*?\]|)$ARGUMENT(.*)$/) { if ($explain) { print "% inside_import " . $inside_import ."\n"; print "% before " . $before ."\n"; @@ -352,11 +387,11 @@ sub process_line } if ($inside_import) { $full_filename = $inside_import . $full_filename; - print "$before\\includegraphics[$args]{$full_filename}$after\n"; + print "$before\\includegraphics" . "$args" . "{$full_filename}$after\n"; $_ = ""; } } elsif (my ($before, $ignored, $args, $full_filename, $ignored, $after) - = /^($NON_COMMENT)\\lstinputlisting[\[\s]+(.*?)[\s\]]$ARGUMENT(.*)$/) { + = /^($NON_COMMENT)\\lstinputlisting(\[[^\]]*?\]|)$ARGUMENT(.*)$/) { if ($explain) { print "% inside_import " . $inside_import ."\n"; print "% before " . $before ."\n"; @@ -367,7 +402,7 @@ sub process_line } if ($inside_import) { $full_filename = $inside_import . $full_filename; - print "$before\\lstinputlisting[$args]{$full_filename}$after\n"; + print "$before\\lstinputlisting" . "$args" . "{$full_filename}$after\n"; $_ = ""; } } @@ -557,6 +592,10 @@ latexpand [options] FILE... (similar to --expand-bbl FILE, but for biber+biblatex). --help this help message + --define <key>=<val>, -d <key>=<val> + defines a macro key to be replaced by value, e.g., + when called with -d foo=bar would replace all occurences + of \foo in the code with bar. Can be supplied multiple times. --output <file>, -o <file> generate output in <file> --explain generate explanatory comments in output @@ -662,4 +701,4 @@ https://lacl.fr/~caubert/notes/portabilite-du-tex.html#dependances =head1 VERSION -This is latexpand version v1.6. +This is latexpand version v1.7. |