source: branches/New_Config_Format-branch/gsdl/bin/script/translate.pl@ 1279

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

merged changes to trunk into New_Config_Format branch

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 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 'aacute' => chr (225),
58 'eacute' => chr (233),
59 'iacute' => chr (237),
60 'oacute' => chr (243),
61 'uacute' => chr (250),
62 'Aacute' => chr (193),
63 'Eacute' => chr (201),
64 'Iacute' => chr (205),
65 'Oacute' => chr (211),
66 'Uacute' => chr (218),
67 'ntilde' => chr (241),
68 'Ntilde' => chr (209));
69
70my $hand_made = 0;
71
72sub print_usage {
73 print STDERR "\n usage: $0 [options] macrofile\n\n";
74 print STDERR " options:\n";
75 print STDERR " -save_orig_file edited macrofile will be written to\n";
76 print STDERR " macrofile.new leaving macrofile unchanged\n";
77 print STDERR " -language_symbol ISO abbreviation of language (e.g. German=de,\n";
78 print STDERR " French=fr, Maori=mi)\n";
79 print STDERR " -image_dir directory full path to directory in which to create images\n";
80 print STDERR " (defaults to `pwd`/images)\n\n";
81}
82
83sub gsdl_translate {
84
85 if (!parsargv::parse(\@ARGV,
86 'save_orig_file', \$save_orig_file,
87 'language_symbol/[A-Za-z]{2}', \$language_symbol,
88 'image_dir/.*/images', \$image_dir)) {
89 &print_usage();
90 die "\n";
91 }
92
93 if ($image_dir eq "images") {
94 $image_dir = `pwd`;
95 chomp $image_dir;
96 $image_dir = &util::filename_cat ($image_dir, "images");
97 }
98
99 if (!defined $ARGV[0]) {
100 print STDERR "no macro file supplied\n\n";
101 &print_usage();
102 die "\n";
103 }
104 my $macrofile = $ARGV[0];
105 die "\nmacrofile $macrofile does not exist\n\n" unless -e $macrofile;
106
107 if (!-e $image_dir) {
108 mkdir ($image_dir, 511) || die "\ncouldn't create image_dir $image_dir\n\n";
109 }
110
111 open (INPUT, $macrofile) || die "\ncouldn't open $macrofile for reading\n\n";
112 open (OUTPUT, ">$macrofile.new") || die "\ncouldn't open temporary file $macrofile.new for writing\n\n";
113
114 &parse_file (INPUT, OUTPUT);
115
116 close OUTPUT;
117 close INPUT;
118
119 if (!$save_orig_file) {
120 `mv $macrofile.new $macrofile`;
121 }
122
123 print STDERR "\n\n";
124 print STDERR "translation of macro file $macrofile completed\n";
125 print STDERR "the translated macro file is $macrofile.new\n" if $save_orig_file;
126 print STDERR "\n";
127 if ($hand_made) {
128 print STDERR "$hand_made hand made images were found within $macrofile,\n";
129 print STDERR "these will need to be made by hand (grep $macrofile for 'hand_made'\n\n";
130 }
131 print STDERR "To add your new interface translation to Greenstone you'll need to:\n";
132 print STDERR " 1. Copy your new macro file to your GSDLHOME/macros directory\n";
133 print STDERR " 2. Add your new macro file to the macrofiles list in your\n";
134 print STDERR " GSDLHOME/etc/main.cfg configuration file\n";
135 print STDERR " 3. Copy your newly created images from $image_dir to \n";
136 print STDERR " GSDLHOME/images/$language_symbol/\n\n";
137 print STDERR "Access your new interface language by setting the language (l) cgi\n";
138 print STDERR "argument to '$language_symbol'\n\n";
139
140}
141
142sub parse_file {
143 my ($input, $output) = @_;
144
145 undef $/;
146 my $dmfile = <$input>;
147 $/ = "\n";
148
149 # process all the images
150
151 $dmfile =~ s/\n\#\#\s*\"([^\"]*)\"\s*\#\#\s*([^\s\#]*)\s*\#\#\s*([^\s\#]*)\s*\#\#(.*?)(?=(\n\#|\s*\Z))/&process_image ($1, $2, $3, $4)/esg;
152
153 # add language parameter to each macro
154 $dmfile =~ s/(\n\s*)(_[^_]*_)\s*(\[[^\]]*\])?\s*\{/$1 . &add_language_param ($2, $3)/esg;
155
156 print $output $dmfile;
157}
158
159sub process_image {
160 my ($text, $image_type, $image_name, $image_macros) = @_;
161
162 my $origtext = $text;
163 $text =~ s/&(\d{3,4});/chr($1)/ge;
164 $text =~ s/&([^;]*);/$rmap{$1}/g;
165
166 # edit image macros
167 $image_macros =~ s/(_httpimg_\/)(?:[^\/\}]*\/)?([^\}]*\.(?:gif|jpe?g|png))/$1$language_symbol\/$2/gs;
168
169 if ($image_type eq "top_nav_button") {
170
171 # generate images
172 my $options = "-text \"$text\" -filenamestem $image_name";
173 $options .= " -fontsize 12 -height 20 -whitespace -image_dir $image_dir";
174 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button.pl $options`;
175
176 # get width of new images and edit width macro
177 my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
178 &process_width_macro ($fullfilename, $image_name, \$image_macros);
179
180 } elsif ($image_type eq "nav_bar_button") {
181
182 # generate on and off images
183 my $options = "-text \"$text\" -filenamestem $image_name";
184 $options .= " -fontsize 17 -height 17 -fixed_width -width 87";
185 $options .= " -image_dir $image_dir";
186 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button.pl $options`;
187
188 # generate green image
189 $options = "-text \"$text\" -filename ${image_name}gr.gif";
190 $options .= " -image_dir $image_dir";
191 `$ENV{'GSDLHOME'}/bin/script/gimp/green_bar.pl $options`;
192
193 # get width of new images and edit width macro
194 my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
195 &process_width_macro ($fullfilename, $image_name, \$image_macros);
196
197 } elsif ($image_type eq "document_button") {
198
199 # generate on and off images
200 my $options = "-text \"$text\" -filenamestem $image_name";
201 $options .= " -fixed_width -whitespace -image_dir $image_dir";
202 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button.pl $options`;
203
204 # get width of new images and edit width macro
205 my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
206 &process_width_macro ($fullfilename, $image_name, \$image_macros);
207
208 } elsif ($image_type eq "green_bar_left_aligned") {
209
210 # generate green bar image (we're assuming these bars are always 537
211 # pixels and are never stretched by excess text
212 my $options = "-text \"$text\" -filename ${image_name}.gif -dont_center";
213 $options .= " -width 537 -width_space 15 -image_dir $image_dir";
214 `$ENV{'GSDLHOME'}/bin/script/gimp/green_bar.pl $options`;
215
216 } elsif ($image_type eq "green_title") {
217
218 # read the width if it is specified in $image_macros
219 my ($width) = $image_macros =~ /_width${image_name}x?_\s*[^\{]*\{(\d+)\}/;
220 $width = 200 unless ($width);
221
222 # generate green title image
223 my $options = "-text \"$text\" -filename ${image_name}.gif -image_dir $image_dir";
224 $options .= " -width $width -height 57 -stripe_alignment right -text_alignment right";
225 $options .= " -fontsize 26 -fontweight bold";
226 `$ENV{'GSDLHOME'}/bin/script/gimp/title_icon.pl $options`;
227
228 # get width of resulting image and edit _width..._ macro in $image_macros
229 # (no longer needed since we always resize to the width read from $image_macros.)
230 # my $fullfilename = &util::filename_cat ($image_dir, "${image_name}.gif");
231 # &process_width_macro ($fullfilename, $image_name, \$image_macros);
232
233 } elsif ($image_type eq "hand_made") {
234
235 $hand_made ++;
236
237 } else {
238
239 print STDERR "WARNING (translate.pl): unknown image type found ($image_type)\n";
240
241 }
242
243 return "\n\#\# \"$origtext\" \#\# $image_type \#\# $image_name \#\#$image_macros";
244}
245
246sub process_width_macro {
247 my ($filename, $image_name, $image_macros) = @_;
248
249 my $img_info = &get_img_info ($filename);
250 $$image_macros =~ s/(_width${image_name}x?_\s*(?:\[[^\]]*\])?\s*\{)(\d+)(\})/$1$img_info->{'width'}$3/s;
251}
252
253sub add_language_param {
254 my ($macroname, $paramlist) = @_;
255
256 my $first = 1;
257 if (defined $paramlist) {
258 $paramlist =~ s/^\[//;
259 $paramlist =~ s/\]$//;
260 my @params = split /\,/, $paramlist;
261 $paramlist = "";
262 foreach $param (@params) {
263 # remove any existing language parameter
264 if ($param !~ /^l=/) {
265 $paramlist .= "," unless $first;
266 $paramlist .= $param;
267 $first = 0;
268 }
269 }
270 }
271 $paramlist .= "," unless $first;
272 $paramlist .= "l=" . $language_symbol;
273 return "$macroname [$paramlist] {";
274}
275
276sub get_img_info {
277 my ($imagefile) = @_;
278 my %info = ();
279
280 if (!-r $imagefile) {
281 print STDERR "ERROR (translate.pl): couldn't open $imagefile to get dimensions\n";
282 $info{'width'} = 0;
283 $info{'height'} = 0;
284 } else {
285 my $image = gimp_file_load (RUN_NONINTERACTIVE, $imagefile, $imagefile);
286 $info{'width'} = gimp_image_width ($image);
287 $info{'height'} = gimp_image_height ($image);
288 }
289
290 return \%info;
291}
292
293sub query {
294
295 gimp_install_procedure("gsdl_translate", "translate macro files and create images",
296 "", "Stefan Boddie", "Stefan Boddie", "2000-03-14",
297 "<Toolbox>/Xtns/gsdl_translate", "*", &PROC_EXTENSION,
298 [[PARAM_INT32, "run_mode", "Interactive, [non-interactive]"]], []);
299}
300
301sub net {
302 gsdl_translate;
303}
304
305exit main;
Note: See TracBrowser for help on using the repository browser.