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