]> git.sur5r.net Git - bacula/docs/blob - docs/manual-fr/translate_images.pl
Initial revision
[bacula/docs] / docs / manual-fr / translate_images.pl
1 #!/usr/bin/perl -w
2 #
3 use strict;
4
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.
9 #
10 # Original creation: 3-27-05  by Karl Cunningham.
11 #
12 my $TRANSFILE = "imagename_translations";
13 my $path;
14
15 # Loads the contents of $TRANSFILE file into the hash referenced in the first 
16 #  argument.
17 sub read_transfile {
18         my $trans = shift;
19
20         open IN,"<$path$TRANSFILE" or die "Cannot open image translation file $path$TRANSFILE for reading\n";
21         while (<IN>) {
22                 chomp;
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;
28         }
29         close IN;
30 }
31         
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.
38 sub translate_html {
39         my ($filename,$trans,$filelist) = @_;
40         my ($contents,$out,$this,$img,$dest);
41         my $cnt = 0;
42
43         # If the filename is an external link ignore it.  And drop any file:// from
44         #  the filename.
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";
49         while (<IF>) {
50                 $contents .= $_;
51         }
52         close IF;
53
54         # Now do the translation...
55         #  First, search for an image filename.
56         while ($contents =~ /\<\s*IMG[^\>]*SRC=\"/si) {
57                 $contents = $';
58                 $out .= $` . $&;
59                 
60                 # The next thing is an image name.  Get it and translate it.
61                 $contents =~ /^(.*?)\"/s;
62                 $contents = $';
63                 $this = $&;
64                 $img = $1;
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}));
68                 $out .= $this;
69         }
70         $out .= $contents;
71
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";
74         print OF $out;
75         close OF;
76
77         # Now look for any links to other files.
78         while ($out =~ /\<\s*A[^\>]*HREF=\"(.*?)\"/si) {
79                 $out = $';
80                 $dest = $1;
81                 # Drop an # and anything after it.
82                 $dest =~ s/\#.*//;
83                 $filelist->{$dest} = '' if $dest;
84         }
85         return $cnt;
86 }
87         
88 # REnames the image files spefified in the %translate hash.
89 sub rename_images {
90         my $translate = shift;
91         my ($response);
92
93         foreach (keys(%$translate)) {
94                 $response = `mv -f $path$_ $path$translate->{$_} 2>&1`;
95                 $response and die $response;
96         }
97 }
98
99 #################################################
100 ############# MAIN #############################
101 ################################################
102
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.
106
107 my (%translate,$search_regex,%filelist,%completed,$thisfile);
108 my $cnt;
109
110 (@ARGV) or die "ERROR: Filename(s) to process must be given as arguments\n";
111
112 # Use the first argument to get the path to the file of translations.
113 my $tmp = $ARGV[0];
114 ($path) = $tmp =~ /(.*\/)/;
115 $path = '' unless $path;
116
117 read_transfile(\%translate);
118
119 foreach (@ARGV) {
120         # Strip the path from the filename, and use it later on.
121         s/(.*\/)//;
122         $path = $1;
123         $path = '' unless $path;
124         $filelist{$_} = '';
125
126         while ($thisfile = (keys(%filelist))[0]) {
127                 $cnt += translate_html($thisfile,\%translate,\%filelist) if (!exists($completed{$thisfile}));
128                 delete($filelist{$thisfile});
129                 $completed{$thisfile} = '';
130         }
131         print "translate_images.pl: $cnt images translated to meaningful names\n";
132 }
133
134 rename_images(\%translate);