Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fc22f6

Browse files
committed
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.
1 parent 9f6e104 commit 6fc22f6

1 file changed

Lines changed: 66 additions & 8 deletions

File tree

Doc/perl/python.perl

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,59 @@ sub do_env_alltt{
19771977
$_;
19781978
}
19791979

1980+
# List of all filenames produced ny do_cmd_verbatiminput()
1981+
%VerbatimFiles = ();
1982+
@VerbatimOutputs = ();
1983+
1984+
sub get_verbatim_output_name($){
1985+
my $file = @_[0];
1986+
#
1987+
# Re-write the source filename to always use a .txt extension
1988+
# so that Web servers will present it as text/plain. This is
1989+
# needed since there is no other even moderately reliable way
1990+
# to get the right Content-Type header on text files for
1991+
# servers which we can't configure (like python.org mirrors).
1992+
#
1993+
if (defined $VerbatimFiles{$file}) {
1994+
# We've seen this one before; re-use the same output file.
1995+
return $VerbatimFiles{$file};
1996+
}
1997+
use File::Basename;
1998+
my $srcname, $srcdir, $srcext;
1999+
($srcname, $srcdir, $srcext) = fileparse($file, '\..*');
2000+
$filename = "$srcname.txt";
2001+
#
2002+
# We need to determine if our default filename is already
2003+
# being used, and find a new one it it is. If the name is in
2004+
# used, this algorithm will first attempt to include the
2005+
# source extension as part of the name, and if that is also in
2006+
# use (if the same file is included multiple times, or if
2007+
# another source file has that as the base name), a counter is
2008+
# used instead.
2009+
#
2010+
my $found = 1;
2011+
FIND:
2012+
while ($found) {
2013+
foreach $fn (@VerbatimOutputs) {
2014+
if ($fn eq $filename) {
2015+
if ($found == 1) {
2016+
$srcext =~ s/^[.]//; # Remove '.' from extension
2017+
$filename = "$srcname-$srcext.txt";
2018+
}
2019+
else {
2020+
$filename = "$srcname-$found.txt";
2021+
}
2022+
++$found;
2023+
next FIND;
2024+
}
2025+
}
2026+
$found = 0;
2027+
}
2028+
push @VerbatimOutputs, $filename;
2029+
$VerbatimFiles{$file} = $filename;
2030+
return $filename;
2031+
}
2032+
19802033
sub do_cmd_verbatiminput{
19812034
local($_) = @_;
19822035
my $fname = next_argument();
@@ -1988,16 +2041,15 @@ sub do_cmd_verbatiminput{
19882041
$file = "$texpath$dd$fname";
19892042
last if ($found = (-f $file));
19902043
}
1991-
my $srcname;
2044+
my $filename = '';
19922045
my $text;
19932046
if ($found) {
19942047
open(MYFILE, "<$file") || die "\n$!\n";
19952048
read(MYFILE, $text, 1024*1024);
19962049
close(MYFILE);
1997-
use File::Basename;
1998-
my $srcdir, $srcext;
1999-
($srcname, $srcdir, $srcext) = fileparse($file, '\..*');
2000-
open(MYFILE, ">$srcname.txt");
2050+
$filename = get_verbatim_output_name($file);
2051+
# Now that we have a filename, write it out.
2052+
open(MYFILE, ">$filename");
20012053
print MYFILE $text;
20022054
close(MYFILE);
20032055
#
@@ -2023,13 +2075,19 @@ sub do_cmd_verbatiminput{
20232075
$text =~ s/\\/\&\#92;/g;
20242076
}
20252077
else {
2026-
$text = '<b>Could not locate requested file <i>$fname</i>!</b>\n';
2078+
return '<b>Could not locate requested file <i>$fname</i>!</b>\n';
2079+
}
2080+
my $note = 'Download as text.';
2081+
if ($file ne $filename) {
2082+
$note = ('Download as text (original file name: <span class="file">'
2083+
. $fname
2084+
. '</span>).');
20272085
}
20282086
return ("<div class=\"verbatim\">\n<pre>"
20292087
. $text
20302088
. "</pre>\n<div class=\"footer\">\n"
2031-
. "<a href=\"$srcname.txt\" type=\"text/plain\""
2032-
. ">Download as text.</a>"
2089+
. "<a href=\"$filename\" type=\"text/plain\""
2090+
. ">$note</a>"
20332091
. "\n</div></div>"
20342092
. $_);
20352093
}

0 commit comments

Comments
 (0)