texlive[66129] trunk: latexpand (24feb23)

commits+karl at tug.org commits+karl at tug.org
Fri Feb 24 22:33:16 CET 2023


Revision: 66129
          http://tug.org/svn/texlive?view=revision&revision=66129
Author:   karl
Date:     2023-02-24 22:33:16 +0100 (Fri, 24 Feb 2023)
Log Message:
-----------
latexpand (24feb23)

Modified Paths:
--------------
    trunk/Build/source/texk/texlive/linked_scripts/latexpand/latexpand
    trunk/Master/texmf-dist/doc/support/latexpand/LICENCE
    trunk/Master/texmf-dist/doc/support/latexpand/README
    trunk/Master/texmf-dist/doc/support/latexpand/version.txt
    trunk/Master/texmf-dist/scripts/latexpand/latexpand

Modified: trunk/Build/source/texk/texlive/linked_scripts/latexpand/latexpand
===================================================================
--- trunk/Build/source/texk/texlive/linked_scripts/latexpand/latexpand	2023-02-24 21:33:01 UTC (rev 66128)
+++ trunk/Build/source/texk/texlive/linked_scripts/latexpand/latexpand	2023-02-24 21:33:16 UTC (rev 66129)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 # Inspired by latexpand by D. Musliner, University of Michigan
-# 2012-2019: Matthieu Moy <git at matthieu-moy.fr>
+# 2012-2023: Matthieu Moy <git at matthieu-moy.fr>
 # BSD License
 
 use strict;
@@ -20,6 +20,7 @@
 my $empty_comments;
 my $help;
 my $long_help;
+my %defines = ();
 my $output;
 my $explain;
 my $show_graphics;
@@ -41,6 +42,7 @@
 	'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 @@
 {
 	# $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 @@
 	$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 @@
 	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 @@
 		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 @@
 
 			# 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 @@
 			$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 @@
 				$_ = "";
 			}
 		} 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 @@
                         }
                         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 @@
                         }
                         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 @@
 	                 (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 @@
 
 =head1 VERSION
 
-This is latexpand version v1.6.
+This is latexpand version v1.7.

Modified: trunk/Master/texmf-dist/doc/support/latexpand/LICENCE
===================================================================
--- trunk/Master/texmf-dist/doc/support/latexpand/LICENCE	2023-02-24 21:33:01 UTC (rev 66128)
+++ trunk/Master/texmf-dist/doc/support/latexpand/LICENCE	2023-02-24 21:33:16 UTC (rev 66129)
@@ -1,4 +1,4 @@
-Copyright (c) 2012-2019, Matthieu Moy <git at matthieu-moy.fr> and
+Copyright (c) 2012-2023, Matthieu Moy <git at matthieu-moy.fr> and
 contributors.
 
 All rights reserved.

Modified: trunk/Master/texmf-dist/doc/support/latexpand/README
===================================================================
--- trunk/Master/texmf-dist/doc/support/latexpand/README	2023-02-24 21:33:01 UTC (rev 66128)
+++ trunk/Master/texmf-dist/doc/support/latexpand/README	2023-02-24 21:33:16 UTC (rev 66129)
@@ -25,6 +25,10 @@
                              (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 @@
     https://lacl.fr/~caubert/notes/portabilite-du-tex.html#dependances
 
 VERSION
-    This is latexpand version v1.6.
+    This is latexpand version v1.7.
 

Modified: trunk/Master/texmf-dist/doc/support/latexpand/version.txt
===================================================================
--- trunk/Master/texmf-dist/doc/support/latexpand/version.txt	2023-02-24 21:33:01 UTC (rev 66128)
+++ trunk/Master/texmf-dist/doc/support/latexpand/version.txt	2023-02-24 21:33:16 UTC (rev 66129)
@@ -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.

Modified: trunk/Master/texmf-dist/scripts/latexpand/latexpand
===================================================================
--- trunk/Master/texmf-dist/scripts/latexpand/latexpand	2023-02-24 21:33:01 UTC (rev 66128)
+++ trunk/Master/texmf-dist/scripts/latexpand/latexpand	2023-02-24 21:33:16 UTC (rev 66129)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 # Inspired by latexpand by D. Musliner, University of Michigan
-# 2012-2019: Matthieu Moy <git at matthieu-moy.fr>
+# 2012-2023: Matthieu Moy <git at matthieu-moy.fr>
 # BSD License
 
 use strict;
@@ -20,6 +20,7 @@
 my $empty_comments;
 my $help;
 my $long_help;
+my %defines = ();
 my $output;
 my $explain;
 my $show_graphics;
@@ -41,6 +42,7 @@
 	'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 @@
 {
 	# $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 @@
 	$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 @@
 	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 @@
 		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 @@
 
 			# 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 @@
 			$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 @@
 				$_ = "";
 			}
 		} 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 @@
                         }
                         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 @@
                         }
                         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 @@
 	                 (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 @@
 
 =head1 VERSION
 
-This is latexpand version v1.6.
+This is latexpand version v1.7.



More information about the tex-live-commits mailing list.