source: trunk/gsdl/bin/script/translate.pl@ 1062

Last change on this file since 1062 was 1062, checked in by sjboddie, 24 years ago

got translate.pl to handle umlauts

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# translate.pl
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28# translate.pl takes a translated macro file (filename passed in on command
29# line) and generates any images required by it. Check out english.dm for
30# an example of the format translate.pl expects
31
32# translate.pl uses gimp to generate images so needs gimp installed and set
33# up for scripting with perl
34
35BEGIN {
36 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
38}
39
40use Gimp;
41use parsargv;
42use util;
43
44# these html entities will be translated correctly when occurring in
45# images. Any entities not in this list will not.
46my %rmap = ('auml' => chr (228),
47 'euml' => chr (235),
48 'iuml' => chr (239),
49 'ouml' => chr (246),
50 'uuml' => chr (252),
51 'Auml' => chr (196),
52 'Euml' => chr (203),
53 'Iuml' => chr (207),
54 'Ouml' => chr (214),
55 'Uuml' => chr (220),
56 'szlig' => chr (223));
57
58my $hand_made = 0;
59
60sub print_usage {
61 print STDERR "\n usage: $0 [options] macrofile\n\n";
62 print STDERR " options:\n";
63 print STDERR " -save_orig_file edited macrofile will be written to\n";
64 print STDERR " macrofile.new leaving macrofile unchanged\n";
65 print STDERR " -language_symbol ISO abbreviation of language (e.g. German=de,\n";
66 print STDERR " French=fr, Maori=mi)\n";
67 print STDERR " -image_dir directory full path to directory in which to create images\n";
68 print STDERR " (defaults to `pwd`/images)\n\n";
69}
70
71sub gsdl_translate {
72
73 if (!parsargv::parse(\@ARGV,
74 'save_orig_file', \$save_orig_file,
75 'language_symbol/[A-Za-z]{2}', \$language_symbol,
76 'image_dir/.*/images', \$image_dir)) {
77 &print_usage();
78 die "\n";
79 }
80
81 if ($image_dir eq "images") {
82 $image_dir = `pwd`;
83 chomp $image_dir;
84 $image_dir = &util::filename_cat ($image_dir, "images");
85 }
86
87 if (!defined $ARGV[0]) {
88 print STDERR "no macro file supplied\n\n";
89 &print_usage();
90 die "\n";
91 }
92 my $macrofile = $ARGV[0];
93 die "\nmacrofile $macrofile does not exist\n\n" unless -e $macrofile;
94
95 if (!-e $image_dir) {
96 mkdir ($image_dir, 511) || die "\ncouldn't create image_dir $image_dir\n\n";
97 }
98
99 open (INPUT, $macrofile) || die "\ncouldn't open $macrofile for reading\n\n";
100 open (OUTPUT, ">$macrofile.new") || die "\ncouldn't open temporary file $macrofile.new for writing\n\n";
101
102 &parse_file (INPUT, OUTPUT);
103
104 close OUTPUT;
105 close INPUT;
106
107 if (!$save_orig_file) {
108 `mv $macrofile.new $macrofile`;
109 }
110
111 print STDERR "\n\n";
112 print STDERR "translation of macro file $macrofile completed\n";
113 print STDERR "the translated macro file is $macrofile.new\n" if $save_orig_file;
114 print STDERR "\n";
115 if ($hand_made) {
116 print STDERR "$hand_made hand made images were found within $macrofile,\n";
117 print STDERR "these will need to be made by hand (grep $macrofile for 'hand_made'\n\n";
118 }
119 print STDERR "To add your new interface translation to Greenstone you'll need to:\n";
120 print STDERR " 1. Copy your new macro file to your GSDLHOME/macros directory\n";
121 print STDERR " 2. Add your new macro file to the macrofiles list in your\n";
122 print STDERR " GSDLHOME/etc/main.cfg configuration file\n";
123 print STDERR " 3. Copy your newly created images from $image_dir to \n";
124 print STDERR " GSDLHOME/images/$language_symbol/\n\n";
125 print STDERR "Access your new interface language by setting the language (l) cgi\n";
126 print STDERR "argument to '$language_symbol'\n\n";
127
128}
129
130sub parse_file {
131 my ($input, $output) = @_;
132
133 undef $/;
134 my $dmfile = <$input>;
135 $/ = "\n";
136
137 # process all the images
138
139 $dmfile =~ s/\n\#\#\s*\"([^\"]*)\"\s*\#\#\s*([^\s\#]*)\s*\#\#\s*([^\s\#]*)\s*\#\#(.*?)(?=(\n\#|\s*\Z))/
140 &process_image ($1, $2, $3, $4)/esg;
141
142 # add language parameter to each macro
143 $dmfile =~ s/(\n\s*)(_[^_]*_)\s*(\[[^\]]*\])?\s*\{/$1 . &add_language_param ($2, $3)/esg;
144
145 print $output $dmfile;
146}
147
148sub process_image {
149 my ($text, $image_type, $image_name, $image_macros) = @_;
150
151 my $origtext = $text;
152 $text =~ s/&([^;]*);/$rmap{$1}/g;
153
154 # edit image macros
155 $image_macros =~ s/(_httpimg_\/)(?:[^\/\}]*\/)?([^\}]*\.(?:gif|jpe?g|png))/$1$language_symbol\/$2/gs;
156
157 if ($image_type eq "top_nav_button") {
158
159 # generate images
160 my $options = "-text \"$text\" -filenamestem $image_name";
161 $options .= " -fontsize 12 -height 20 -whitespace -image_dir $image_dir";
162 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button.pl $options`;
163
164 # get width of new images and edit width macro
165 my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
166 &process_width_macro ($fullfilename, $image_name, \$image_macros);
167
168 } elsif ($image_type eq "nav_bar_button") {
169
170 # generate on and off images
171 my $options = "-text \"$text\" -filenamestem $image_name";
172 $options .= " -fontsize 17 -height 17 -fixed_width -width 87";
173 $options .= " -image_dir $image_dir";
174 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button.pl $options`;
175
176 # generate green image
177 $options = "-text \"$text\" -filename ${image_name}gr.gif";
178 $options .= " -image_dir $image_dir";
179 `$ENV{'GSDLHOME'}/bin/script/gimp/green_bar.pl $options`;
180
181 # get width of new images and edit width macro
182 my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
183 &process_width_macro ($fullfilename, $image_name, \$image_macros);
184
185 } elsif ($image_type eq "document_button") {
186
187 # generate on and off images
188 my $options = "-text \"$text\" -filenamestem $image_name";
189 $options .= " -fixed_width -whitespace -image_dir $image_dir";
190 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button.pl $options`;
191
192 # get width of new images and edit width macro
193 my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
194 &process_width_macro ($fullfilename, $image_name, \$image_macros);
195
196 } elsif ($image_type eq "green_bar_left_aligned") {
197
198 # generate green bar image (we're assuming these bars are always 537
199 # pixels and are never stretched by excess text
200 my $options = "-text \"$text\" -filename ${image_name}.gif -dont_center";
201 $options .= " -width 537 -width_space 15 -image_dir $image_dir";
202 `$ENV{'GSDLHOME'}/bin/script/gimp/green_bar.pl $options`;
203
204 } elsif ($image_type eq "green_title") {
205
206 # generate green title image
207 my $options = "-text \"$text\" -filename ${image_name}.gif -image_dir $image_dir";
208 $options .= " -width 200 -height 57 -stripe_alignment right -text_alignment right";
209 $options .= " -fontsize 26 -fontweight bold";
210 `$ENV{'GSDLHOME'}/bin/script/gimp/title_icon.pl $options`;
211
212 # get width of new images and edit width macro
213 # we'll do this even though title_icon.pl will always create images of the
214 # width specified (200)
215 my $fullfilename = &util::filename_cat ($image_dir, "${image_name}.gif");
216 &process_width_macro ($fullfilename, $image_name, \$image_macros);
217
218 } elsif ($image_type eq "hand_made") {
219
220 $hand_made ++;
221
222 } else {
223
224 print STDERR "WARNING (translate.pl): unknown image type found ($image_type)\n";
225
226 }
227
228 return "\n\#\# \"$text\" \#\# $image_type \#\# $image_name \#\#$image_macros";
229}
230
231sub process_width_macro {
232 my ($filename, $image_name, $image_macros) = @_;
233
234 my $img_info = &get_img_info ($filename);
235 $$image_macros =~ s/(_width${image_name}x_\s*(?:\[[^\]]*\])?\s*\{)(\d+)(\})/$1$img_info->{'width'}$3/s;
236}
237
238sub add_language_param {
239 my ($macroname, $paramlist) = @_;
240
241 my $first = 1;
242 if (defined $paramlist) {
243 $paramlist =~ s/^\[//;
244 $paramlist =~ s/\]$//;
245 my @params = split /\,/, $paramlist;
246 $paramlist = "";
247 foreach $param (@params) {
248 # remove any existing language parameter
249 if ($param !~ /^l=/) {
250 $paramlist .= "," unless $first;
251 $paramlist .= $param;
252 $first = 0;
253 }
254 }
255 }
256 $paramlist .= "," unless $first;
257 $paramlist .= "l=" . $language_symbol;
258 return "$macroname [$paramlist] {";
259}
260
261sub get_img_info {
262 my ($imagefile) = @_;
263 my %info = ();
264
265 if (!-r $imagefile) {
266 print STDERR "ERROR (translate.pl): couldn't open $imagefile to get dimensions\n";
267 $info{'width'} = 0;
268 $info{'height'} = 0;
269 } else {
270 my $image = gimp_file_load (RUN_NONINTERACTIVE, $imagefile, $imagefile);
271 $info{'width'} = gimp_image_width ($image);
272 $info{'height'} = gimp_image_height ($image);
273 }
274
275 return \%info;
276}
277
278sub query {
279
280 gimp_install_procedure("gsdl_translate", "translate macro files and create images",
281 "", "Stefan Boddie", "Stefan Boddie", "2000-03-14",
282 "<Toolbox>/Xtns/gsdl_translate", "*", &PROC_EXTENSION,
283 [[PARAM_INT32, "run_mode", "Interactive, [non-interactive]"]], []);
284}
285
286sub net {
287 gsdl_translate;
288}
289
290exit main;
Note: See TracBrowser for help on using the repository browser.