[Python-checkins] python/dist/src/Doc/perl python.perl,1.125,1.126

fdrake@users.sourceforge.net fdrake@users.sourceforge.net
Mon, 17 Jun 2002 08:01:12 -0700


Update of /cvsroot/python/python/dist/src/Doc/perl
In directory usw-pr-cvs1:/tmp/cvs-serv17548/perl

Modified Files:
	python.perl 
Log Message:
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.


Index: python.perl
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/perl/python.perl,v
retrieving revision 1.125
retrieving revision 1.126
diff -C2 -d -r1.125 -r1.126
*** python.perl	23 May 2002 17:59:16 -0000	1.125
--- python.perl	17 Jun 2002 15:01:05 -0000	1.126
***************
*** 1978,1981 ****
--- 1978,2034 ----
  }
  
+ # 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($_) = @_;
***************
*** 1989,1993 ****
          last if ($found = (-f $file));
      }
!     my $srcname;
      my $text;
      if ($found) {
--- 2042,2046 ----
          last if ($found = (-f $file));
      }
!     my $filename = '';
      my $text;
      if ($found) {
***************
*** 1995,2002 ****
          read(MYFILE, $text, 1024*1024);
          close(MYFILE);
!         use File::Basename;
!         my $srcdir, $srcext;
!         ($srcname, $srcdir, $srcext) = fileparse($file, '\..*');
!         open(MYFILE, ">$srcname.txt");
          print MYFILE $text;
          close(MYFILE);
--- 2048,2054 ----
          read(MYFILE, $text, 1024*1024);
          close(MYFILE);
!         $filename = get_verbatim_output_name($file);
!         # Now that we have a filename, write it out.
!         open(MYFILE, ">$filename");
          print MYFILE $text;
          close(MYFILE);
***************
*** 2024,2034 ****
      }
      else {
!         $text = '<b>Could not locate requested file <i>$fname</i>!</b>\n';
      }
      return ("<div class=\"verbatim\">\n<pre>"
              . $text
              . "</pre>\n<div class=\"footer\">\n"
!             . "<a href=\"$srcname.txt\" type=\"text/plain\""
!             . ">Download as text.</a>"
              . "\n</div></div>"
              . $_);
--- 2076,2092 ----
      }
      else {
!         return '<b>Could not locate requested file <i>$fname</i>!</b>\n';
!     }
!     my $note = 'Download as text.';
!     if ($file ne $filename) {
!         $note = ('Download as text (original file name: <span class="file">'
!                  . $fname
!                  . '</span>).');
      }
      return ("<div class=\"verbatim\">\n<pre>"
              . $text
              . "</pre>\n<div class=\"footer\">\n"
!             . "<a href=\"$filename\" type=\"text/plain\""
!             . ">$note</a>"
              . "\n</div></div>"
              . $_);