From: Fred Drake Date: Mon, 17 Jun 2002 15:01:05 +0000 (+0000) Subject: Ensure \verbatiminput always uses a unique filename for each input file in X-Git-Tag: v2.3c1~5291 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6fc22f6c3d566587247510d5b255ccbb564c73f4;p=python Ensure \verbatiminput always uses a unique filename for each input file in the "Download as text" link. Previously, it could map multiple source files to a single name since all files end up with the same extension. This closes SF bug #558279. --- diff --git a/Doc/perl/python.perl b/Doc/perl/python.perl index 38c174fc28..3d32923303 100644 --- a/Doc/perl/python.perl +++ b/Doc/perl/python.perl @@ -1977,6 +1977,59 @@ sub do_env_alltt{ $_; } +# List of all filenames produced ny do_cmd_verbatiminput() +%VerbatimFiles = (); +@VerbatimOutputs = (); + +sub get_verbatim_output_name($){ + my $file = @_[0]; + # + # Re-write the source filename to always use a .txt extension + # so that Web servers will present it as text/plain. This is + # needed since there is no other even moderately reliable way + # to get the right Content-Type header on text files for + # servers which we can't configure (like python.org mirrors). + # + if (defined $VerbatimFiles{$file}) { + # We've seen this one before; re-use the same output file. + return $VerbatimFiles{$file}; + } + use File::Basename; + my $srcname, $srcdir, $srcext; + ($srcname, $srcdir, $srcext) = fileparse($file, '\..*'); + $filename = "$srcname.txt"; + # + # We need to determine if our default filename is already + # being used, and find a new one it it is. If the name is in + # used, this algorithm will first attempt to include the + # source extension as part of the name, and if that is also in + # use (if the same file is included multiple times, or if + # another source file has that as the base name), a counter is + # used instead. + # + my $found = 1; + FIND: + while ($found) { + foreach $fn (@VerbatimOutputs) { + if ($fn eq $filename) { + if ($found == 1) { + $srcext =~ s/^[.]//; # Remove '.' from extension + $filename = "$srcname-$srcext.txt"; + } + else { + $filename = "$srcname-$found.txt"; + } + ++$found; + next FIND; + } + } + $found = 0; + } + push @VerbatimOutputs, $filename; + $VerbatimFiles{$file} = $filename; + return $filename; +} + sub do_cmd_verbatiminput{ local($_) = @_; my $fname = next_argument(); @@ -1988,16 +2041,15 @@ sub do_cmd_verbatiminput{ $file = "$texpath$dd$fname"; last if ($found = (-f $file)); } - my $srcname; + my $filename = ''; my $text; if ($found) { open(MYFILE, "<$file") || die "\n$!\n"; read(MYFILE, $text, 1024*1024); close(MYFILE); - use File::Basename; - my $srcdir, $srcext; - ($srcname, $srcdir, $srcext) = fileparse($file, '\..*'); - open(MYFILE, ">$srcname.txt"); + $filename = get_verbatim_output_name($file); + # Now that we have a filename, write it out. + open(MYFILE, ">$filename"); print MYFILE $text; close(MYFILE); # @@ -2023,13 +2075,19 @@ sub do_cmd_verbatiminput{ $text =~ s/\\/\&\#92;/g; } else { - $text = 'Could not locate requested file $fname!\n'; + return 'Could not locate requested file $fname!\n'; + } + my $note = 'Download as text.'; + if ($file ne $filename) { + $note = ('Download as text (original file name: ' + . $fname + . ').'); } return ("
\n
"
             . $text
             . "
\n
\n" - . "Download as text." + . "$note" . "\n
" . $_); }