5 # Used to change the names of the image files generated by latex2html from imgxx.png
6 # to meaningful names. Provision is made to go either from or to the meaningful names.
7 # The meaningful names are obtained from a file called imagename_translations, which
8 # is generated by extensions to latex2html in the make_image_file subroutine in
11 # Opens the file imagename_translations and reads the contents into a hash.
12 # The hash is creaed with the imgxx.png files as the key if processing TO
13 # meaningful filenames, and with the meaningful filenames as the key if
14 # processing FROM meaningful filenames.
15 # Then opens the html file(s) indicated in the command-line arguments and
16 # changes all image references according to the translations described in the
17 # above file. Finally, it renames the image files.
19 # Original creation: 3-27-05 by Karl Cunningham.
20 # Modified 5-21-05 to go FROM and TO meaningful filenames.
22 my $TRANSFILE = "imagename_translations";
25 # Loads the contents of $TRANSFILE file into the hash referenced in the first
26 # argument. The hash is loaded to translate old to new if $direction is 0,
27 # otherwise it is loaded to translate new to old. In this context, the
28 # 'old' filename is the meaningful name, and the 'new' filename is the
29 # imgxx.png filename. It is assumed that the old image is the one that
30 # latex2html has used as the source to create the imgxx.png filename.
31 # The filename extension is taken from the file
33 my ($trans,$direction) = @_;
35 if (!open IN,"<$path$TRANSFILE") {
36 print "WARNING: Cannot open image translation file $path$TRANSFILE for reading\n";
37 print " Image filename translation aborted\n\n";
43 my ($new,$old) = split(/\001/);
45 # Old filenames will usually have a leading ./ which we don't need.
48 # The filename extension of the old filename must be made to match
49 # the new filename because it indicates the encoding format of the image.
50 my ($ext) = $new =~ /(\.[^\.]*)$/;
51 $old =~ s/\.[^\.]*$/$ext/;
52 if ($direction == 0) {
53 $trans->{$new} = $old;
55 $trans->{$old} = $new;
61 # Translates the image names in the file given as the first argument, according to
62 # the translations in the hash that is given as the second argument.
63 # The file contents are read in entirely into a string, the string is processed, and
64 # the file contents are then written. No particular care is taken to ensure that the
65 # file is not lost if a system failure occurs at an inopportune time. It is assumed
66 # that the html files being processed here can be recreated on demand.
68 # Links to other files are added to the %filelist for processing. That way,
69 # all linked files will be processed (assuming they are local).
71 my ($filename,$trans,$filelist) = @_;
72 my ($contents,$out,$this,$img,$dest);
75 # If the filename is an external link ignore it. And drop any file:// from
77 $filename =~ /^(http|ftp|mailto)\:/ and return 0;
78 $filename =~ s/^file\:\/\///;
79 # Load the contents of the html file.
80 if (!open IF,"<$path$filename") {
81 print "WARNING: Cannot open $path$filename for reading\n";
82 print " Image Filename Translation aborted\n\n";
91 # Now do the translation...
92 # First, search for an image filename.
93 while ($contents =~ /\<\s*IMG[^\>]*SRC=\"/si) {
97 # The next thing is an image name. Get it and translate it.
98 $contents =~ /^(.*?)\"/s;
102 # If the image is in our list of ones to be translated, do it
103 # and feed the result to the output.
104 $cnt += $this =~ s/$img/$trans->{$img}/ if (defined($trans->{$img}));
109 # Now send the translated text to the html file, overwriting what's there.
110 open OF,">$path$filename" or die "Cannot open $path$filename for writing\n";
114 # Now look for any links to other files and add them to the list of files to do.
115 while ($out =~ /\<\s*A[^\>]*HREF=\"(.*?)\"/si) {
118 # Drop an # and anything after it.
120 $filelist->{$dest} = '' if $dest;
125 # REnames the image files spefified in the %translate hash.
127 my $translate = shift;
130 foreach (keys(%$translate)) {
131 if (! $translate->{$_}) {
132 print " WARNING: No destination Filename for $_\n";
134 $response = `mv -f $path$_ $path$translate->{$_} 2>&1`;
135 $response and print "ERROR from system $response\n";
140 #################################################
141 ############# MAIN #############################
142 ################################################
144 # %filelist starts out with keys from the @ARGV list. As files are processed,
145 # any links to other files are added to the %filelist. A hash of processed
146 # files is kept so we don't do any twice.
148 # The first argument must be either --to_meaningful_names or --from_meaningful_names
150 my (%translate,$search_regex,%filelist,%completed,$thisfile);
151 my ($cnt,$direction);
153 my $arg0 = shift(@ARGV);
154 $arg0 =~ /^(--to_meaningful_names|--from_meaningful_names)$/ or
155 die "ERROR: First argument must be either \'--to_meaningful_names\' or \'--from_meaningful_names\'\n";
157 $direction = ($arg0 eq '--to_meaningful_names') ? 0 : 1;
159 (@ARGV) or die "ERROR: Filename(s) to process must be given as arguments\n";
161 # Use the first argument to get the path to the file of translations.
163 ($path) = $tmp =~ /(.*\/)/;
164 $path = '' unless $path;
166 read_transfile(\%translate,$direction);
169 # Strip the path from the filename, and use it later on.
177 while ($thisfile = (keys(%filelist))[0]) {
178 $cnt += translate_html($thisfile,\%translate,\%filelist) if (!exists($completed{$thisfile}));
179 delete($filelist{$thisfile});
180 $completed{$thisfile} = '';
182 print "translate_images.pl: $cnt image filenames translated ",($direction)?"from":"to"," meaningful names\n";
185 rename_images(\%translate);