]> git.sur5r.net Git - bacula/docs/blob - docs/manuals/de/old/concepts/translate_images.pl
Add more print info to send
[bacula/docs] / docs / manuals / de / old / concepts / translate_images.pl
1 #!/usr/bin/perl -w
2 #
3 use strict;
4
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 
9 #  bacula.perl.
10
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.
18 #
19 # Original creation: 3-27-05  by Karl Cunningham.
20 #   Modified 5-21-05 to go FROM and TO meaningful filenames.
21 #
22 my $TRANSFILE = "imagename_translations";
23 my $path;
24
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 
32 sub read_transfile {
33         my ($trans,$direction) = @_;
34
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";
38                 exit 0;
39         }
40
41         while (<IN>) {
42                 chomp;
43                 my ($new,$old) = split(/\001/);
44
45                 # Old filenames will usually have a leading ./ which we don't need.
46                 $old =~ s/^\.\///;
47
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;
54                 } else {
55                         $trans->{$old} = $new;
56                 }
57         }
58         close IN;
59 }
60         
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.
67 #
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).
70 sub translate_html {
71         my ($filename,$trans,$filelist) = @_;
72         my ($contents,$out,$this,$img,$dest);
73         my $cnt = 0;
74
75         # If the filename is an external link ignore it.  And drop any file:// from
76         #  the filename.
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";
83                 exit 0;
84         }
85
86         while (<IF>) {
87                 $contents .= $_;
88         }
89         close IF;
90
91         # Now do the translation...
92         #  First, search for an image filename.
93         while ($contents =~ /\<\s*IMG[^\>]*SRC=\"/si) {
94                 $contents = $';
95                 $out .= $` . $&;
96                 
97                 # The next thing is an image name.  Get it and translate it.
98                 $contents =~ /^(.*?)\"/s;
99                 $contents = $';
100                 $this = $&;
101                 $img = $1;
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}));
105                 $out .= $this;
106         }
107         $out .= $contents;
108
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";
111         print OF $out;
112         close OF;
113
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) {
116                 $out = $';
117                 $dest = $1;
118                 # Drop an # and anything after it.
119                 $dest =~ s/\#.*//;
120                 $filelist->{$dest} = '' if $dest;
121         }
122         return $cnt;
123 }
124         
125 # REnames the image files spefified in the %translate hash.
126 sub rename_images {
127         my $translate = shift;
128         my ($response);
129
130         foreach (keys(%$translate)) {
131                 if (! $translate->{$_}) {
132                         print "    WARNING: No destination Filename for $_\n";
133                 } else {
134                         $response = `mv -f $path$_ $path$translate->{$_} 2>&1`;
135                         $response and print "ERROR from system    $response\n";
136                 }
137         }
138 }
139
140 #################################################
141 ############# MAIN #############################
142 ################################################
143
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.
147
148 # The first argument must be either --to_meaningful_names or --from_meaningful_names
149
150 my (%translate,$search_regex,%filelist,%completed,$thisfile);
151 my ($cnt,$direction);
152
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";
156
157 $direction = ($arg0 eq '--to_meaningful_names') ? 0 : 1;
158
159 (@ARGV) or die "ERROR: Filename(s) to process must be given as arguments\n";
160
161 # Use the first argument to get the path to the file of translations.
162 my $tmp = $ARGV[0];
163 ($path) = $tmp =~ /(.*\/)/;
164 $path = '' unless $path;
165
166 read_transfile(\%translate,$direction);
167
168 foreach (@ARGV) {
169         # Strip the path from the filename, and use it later on.
170         if (s/(.*\/)//) {
171                 $path = $1;
172         } else {
173                 $path = '';
174         }
175         $filelist{$_} = '';
176
177         while ($thisfile = (keys(%filelist))[0]) {
178                 $cnt += translate_html($thisfile,\%translate,\%filelist) if (!exists($completed{$thisfile}));
179                 delete($filelist{$thisfile});
180                 $completed{$thisfile} = '';
181         }
182         print "translate_images.pl: $cnt image filenames translated ",($direction)?"from":"to"," meaningful names\n";
183 }
184
185 rename_images(\%translate);