5 # Opens the file imagename_translations and reads the contents into a hash.
6 # Then opens the html file(s) indicated in the command-line arguments and
7 # changes all image references according to the translations described in the
8 # above file. Finally, it renames the image files.
10 # Original creation: 3-27-05 by Karl Cunningham.
12 my $TRANSFILE = "imagename_translations";
15 # Loads the contents of $TRANSFILE file into the hash referenced in the first
20 open IN,"<$path$TRANSFILE" or die "Cannot open image translation file $path$TRANSFILE for reading\n";
23 my ($new,$old) = split(/\001/);
24 # The filename extension of the new one must be made to be the old one.
25 my ($ext) = $new =~ /.*(\..*)$/;
26 $old =~ s/(.*)\..*$/$1$ext/;
27 $trans->{$new} = $old;
32 # Translates the image names in the file given as the first argument, according to
33 # the translations in the hash that is given as the second argument.
34 # The file contents are read in entirely into a string, the string is processed, and
35 # the file contents are then written. No particular care is taken to ensure that the
36 # file is not lost if a system failure occurs at an inopportune time. It is assumed
37 # that the html files being processed here can be recreated on demand.
39 my ($filename,$trans,$filelist) = @_;
40 my ($contents,$out,$this,$img,$dest);
43 # If the filename is an external link ignore it. And drop any file:// from
45 $filename =~ /^(http|ftp|mailto)\:/ and return 0;
46 $filename =~ s/^file\:\/\///;
47 # Load the contents of the html file.
48 open IF,"<$path$filename" or die "Cannot open $path$filename for reading\n";
54 # Now do the translation...
55 # First, search for an image filename.
56 while ($contents =~ /\<\s*IMG[^\>]*SRC=\"/si) {
60 # The next thing is an image name. Get it and translate it.
61 $contents =~ /^(.*?)\"/s;
65 # If the image is in our list of ones to be translated, do it
66 # and feed the result to the output.
67 $cnt += $this =~ s/$img/$trans->{$img}/ if (defined($trans->{$img}));
72 # Now send the translated text to the html file, overwriting what's there.
73 open OF,">$path$filename" or die "Cannot open $path$filename for writing\n";
77 # Now look for any links to other files.
78 while ($out =~ /\<\s*A[^\>]*HREF=\"(.*?)\"/si) {
81 # Drop an # and anything after it.
83 $filelist->{$dest} = '' if $dest;
88 # REnames the image files spefified in the %translate hash.
90 my $translate = shift;
93 foreach (keys(%$translate)) {
94 $response = `mv -f $path$_ $path$translate->{$_} 2>&1`;
95 $response and die $response;
99 #################################################
100 ############# MAIN #############################
101 ################################################
103 # %filelist starts out with keys from the @ARGV list. As files are processed,
104 # any links to other files are added to the %filelist. A hash of processed
105 # files is kept so we don't do any twice.
107 my (%translate,$search_regex,%filelist,%completed,$thisfile);
110 (@ARGV) or die "ERROR: Filename(s) to process must be given as arguments\n";
112 # Use the first argument to get the path to the file of translations.
114 ($path) = $tmp =~ /(.*\/)/;
115 $path = '' unless $path;
117 read_transfile(\%translate);
120 # Strip the path from the filename, and use it later on.
123 $path = '' unless $path;
126 while ($thisfile = (keys(%filelist))[0]) {
127 $cnt += translate_html($thisfile,\%translate,\%filelist) if (!exists($completed{$thisfile}));
128 delete($filelist{$thisfile});
129 $completed{$thisfile} = '';
131 print "translate_images.pl: $cnt images translated to meaningful names\n";
134 rename_images(\%translate);