source: trunk/gsdl/perllib/printusage.pm@ 4873

Last change on this file since 4873 was 4873, checked in by mdewsnip, 21 years ago

Further work on standardising option descriptions. Specifically, in preparation for translating the option descriptions into other languages, all the option description strings have been moved in a "resource bundle" file (modelled on a Java resource bundle). (This also has the advantage of reducing the number of duplicate descriptions). The option descriptions in the plugins, classifiers, mkcol.pl, import.pl and buildcol.pl have been replaced with keys into this resource bundle (perllib/strings.rb). When translating the strings in this file into a new language, the new resource bundle should be named strings_<language-code>.rb (where <language-code> is a combination of language and country, eg. 'fr_FR' for the version of French spoken in France).

To support these changes, the PrintUsage module (perllib/printusage.pm) has new code for reading resource bundles and displaying the correct strings. Also, pluginfo.pl, classinfo.pl, mkcol.pl, import.pl and buildcol.pl have a new option (-language) for specifying the language code to display option descriptions in.

If a resource bundle for the specified language code does not exist, a generic resource bundle is used (strings.rb). This currently contains the English text descriptions. However, for users who always use Greenstone in another language, it would be easier to rename the standard file to strings_en_US.rb and rename the resource bundle of their desired language to strings.rb. This would mean they would not have to constantly specify their language with the -language option, since the default resource bundle will suit them.

Currently, the encoding names (in encodings.pm) are not part of this scheme. These are displayed as part of BasPlug's input_encoding option. It is debatable whether these names would be worth translating into other languages.

Parse errors in plugins and classifiers currently cause them to display the usage information using the default resource bundle. It is likely that BasPlug will soon have an option added to specify the language for the usage information in this case. (Note that this does not include using pluginfo.pl or classinfo.pl to display usage information - these have a -language option).

  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1###########################################################################
2#
3# printusage.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26
27package PrintUsage;
28
29
30sub print_xml_header
31{
32 print STDERR "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
33 print STDERR "<!DOCTYPE Info [\n";
34 print STDERR " <!ELEMENT Info (Name, Desc, Arguments)>\n";
35 print STDERR " <!ELEMENT Arguments (Option*)>\n";
36 print STDERR " <!ELEMENT Option (Name, Desc, Type, Required, Default?, List?)>\n";
37 print STDERR " <!ELEMENT Name (#PCDATA)>\n";
38 print STDERR " <!ELEMENT Desc (#PCDATA)>\n";
39 print STDERR " <!ELEMENT Type (#PCDATA)>\n";
40 print STDERR " <!ELEMENT Required (#PCDATA)>\n";
41 print STDERR " <!ELEMENT Default (#PCDATA)>\n";
42 print STDERR " <!ELEMENT List (Value*)>\n";
43 print STDERR " <!ELEMENT Value (Name, Desc?)>\n";
44 print STDERR "]>\n\n";
45}
46
47
48sub print_options_xml
49{
50 local $language = shift(@_);
51 local $options = shift(@_);
52
53 foreach $option (@$options) {
54 local $optionname = $option->{'name'};
55 local $optiondesc = &lookup_string($language, $option->{'desc'});
56
57 # Escape '<' and '>' characters
58 $optiondesc =~ s/</&lt;/g;
59 $optiondesc =~ s/>/&gt;/g;
60
61 # Display option name, description and type
62 print STDERR " <Option>\n";
63 print STDERR " <Name>$optionname</Name>\n";
64 print STDERR " <Desc>$optiondesc</Desc>\n";
65 print STDERR " <Type>$option->{'type'}</Type>\n";
66
67 # If the option has a required field, display this
68 if (defined($option->{'reqd'})) {
69 print STDERR " <Required>$option->{'reqd'}</Required>\n";
70 }
71
72 # If the option has a list of possible values, display these
73 if (defined $option->{'list'}) {
74 print STDERR " <List>\n";
75 local $optionvalueslist = $option->{'list'};
76 foreach $optionvalue (@$optionvalueslist) {
77 print STDERR " <Value>\n";
78 print STDERR " <Name>$optionvalue->{'name'}</Name>\n";
79 if (defined $optionvalue->{'desc'}) {
80 local $optionvaluedesc = &lookup_string($language, $optionvalue->{'desc'});
81
82 # Escape '<' and '>' characters
83 $optionvaluedesc =~ s/</&lt;/g;
84 $optionvaluedesc =~ s/>/&gt;/g;
85
86 print STDERR " <Desc>$optionvaluedesc</Desc>\n";
87 }
88 print STDERR " </Value>\n";
89 }
90
91 # Special case for 'input_encoding'
92 if ($optionname =~ m/^input_encoding$/i) {
93 my $e = $encodings::encodings;
94 foreach $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e)) {
95 print STDERR " <Value>\n";
96 print STDERR " <Name>$enc</Name>\n";
97 print STDERR " <Desc>$e->{$enc}->{'name'}</Desc>\n";
98 print STDERR " </Value>\n";
99 }
100 }
101
102 print STDERR " </List>\n";
103 }
104
105 # Show the default value for the option, if there is one
106 if (defined $option->{'deft'}) {
107 print STDERR " <Default>$option->{'deft'}</Default>\n";
108 }
109
110 print STDERR " </Option>\n";
111 }
112}
113
114
115sub find_longest_option_string
116{
117 local $options = shift(@_);
118
119 local $maxlength = 0;
120 foreach $option (@$options) {
121 local $optionname = $option->{'name'};
122 local $optiontype = $option->{'type'};
123
124 local $optionlength = length(" -$optionname");
125 if ($optiontype ne "flag") {
126 $optionlength = $optionlength + length(" <$optiontype>");
127 }
128
129 # Remember the longest
130 if ($optionlength > $maxlength) {
131 $maxlength = $optionlength;
132 }
133 }
134 return $maxlength;
135}
136
137
138sub print_options_txt
139{
140 local $language = shift(@_);
141 local $options = shift(@_);
142 local $optiondescoffset = shift(@_);
143
144 foreach $option (@$options) {
145 # Display option name
146 local $optionname = $option->{'name'};
147 print STDERR " -$optionname";
148 local $optionstringlength = length(" -$optionname");
149
150 # Display option type, if the option is not a flag
151 local $optiontype = $option->{'type'};
152 if ($optiontype ne "flag") {
153 print STDERR " <$optiontype>";
154 $optionstringlength = $optionstringlength + length(" <$optiontype>");
155 }
156
157 # Display the option description
158 local $optiondesc = &lookup_string($language, $option->{'desc'});
159 local $optionreqd = $option->{'reqd'};
160 if (defined($optionreqd) && $optionreqd eq "yes") {
161 $optiondesc = "(REQUIRED) " . $optiondesc;
162 }
163 &display_text_in_column($optiondesc, $optiondescoffset, $optionstringlength, 80);
164
165 # Show the default value for the option, if there is one
166 local $optiondefault = $option->{'deft'};
167 if (defined($optiondefault)) {
168 print STDERR " " x $optiondescoffset;
169 print STDERR "Default: " . $optiondefault . "\n";
170 }
171
172 # If the option has a list of possible values, display these
173 local $optionvalueslist = $option->{'list'};
174 if (defined($optionvalueslist)) {
175 print STDERR "\n";
176 foreach $optionvalue (@$optionvalueslist) {
177 local $optionvaluename = $optionvalue->{'name'};
178 print STDERR " " x $optiondescoffset;
179 print STDERR "$optionvaluename:";
180
181 local $optionvaluedesc = &lookup_string($language, $optionvalue->{'desc'});
182 &display_text_in_column($optionvaluedesc, $optiondescoffset + 2,
183 $optiondescoffset + length($optionvaluename), 80);
184 }
185 }
186
187 # Special case for 'input_encoding'
188 if ($optionname =~ m/^input_encoding$/i) {
189 my $e = $encodings::encodings;
190 foreach $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e)) {
191 print STDERR " " x $optiondescoffset;
192 print STDERR "$enc:";
193
194 local $encodingdesc = $e->{$enc}->{'name'};
195 &display_text_in_column($encodingdesc, $optiondescoffset + 2,
196 $optiondescoffset + length("$enc:"), 80);
197 }
198 }
199
200 # Add a blank line to separate options
201 print STDERR "\n";
202 }
203}
204
205
206sub lookup_string
207{
208 local ($language, $stringkey) = @_;
209
210 # Load the appropriate resource bundle
211 local %resourcebundle = &load_resource_bundle($language);
212
213 # Return the text matching the key (or just the key, if no match was found)
214 return $resourcebundle{$stringkey} || $stringkey;
215}
216
217
218my $cachedlanguage = "<none>";
219my %cachedresourcebundle = ();
220
221sub load_resource_bundle
222{
223 local $language = shift(@_);
224
225 # If the desired resource bundle is the one cached, return it
226 if ($language eq $cachedlanguage) {
227 return %cachedresourcebundle;
228 }
229
230 # Open the appropriate resource bundle
231 local $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
232 local $resourcebundlename = "strings_" . $language . ".rb";
233 local $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
234
235 # If the specific resource bundle cannot be opened, use the generic (English) one
236 if (!open(RESOURCE_BUNDLE, "<$resourcebundlefile")) {
237 $resourcebundlename = "strings.rb";
238 $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
239 open(RESOURCE_BUNDLE, "<$resourcebundlefile")
240 or die "Error: Could not open generic resource bundle $resourcebundlefile.\n";
241 }
242
243 local @resourcebundlelines = <RESOURCE_BUNDLE>;
244 close(RESOURCE_BUNDLE);
245
246 # Load and cache this resource bundle
247 $cachedlanguage = $language;
248 %cachedresourcebundle = ();
249 foreach $line (@resourcebundlelines) {
250 # Remove any trailing whitespace
251 $line =~ s/(\s*)$//;
252
253 # Ignore comments and empty lines
254 if ($line !~ /^\#/ && $line ne "") {
255 # Parse key (everything up to the first colon)
256 $line =~ /^([^:]+):(.+)$/;
257 local $linekey = "{" . $1 . "}";
258 local $linetext = $2;
259
260 # Map key to text
261 $cachedresourcebundle{$linekey} = $linetext;
262 }
263 }
264
265 return %cachedresourcebundle;
266}
267
268
269sub display_text_in_column
270{
271 local ($text, $columnbeg, $firstlineoffset, $columnend) = @_;
272
273 # Spaces are put *before* words, so treat the column beginning as 1 smaller than it is
274 $columnbeg = $columnbeg - 1;
275
276 # Add some padding (if needed) for the first line
277 local $linelength = $columnbeg;
278 if ($firstlineoffset < $columnbeg) {
279 print STDERR " " x ($columnbeg - $firstlineoffset);
280 }
281 else {
282 $linelength = $firstlineoffset;
283 }
284
285 # Break the text into words, and display one at a time
286 local @words = split(/ /, $text);
287
288 foreach $word (@words) {
289 # If printing this word would exceed the column end, start a new line
290 if (($linelength + length($word)) >= $columnend) {
291 print STDERR "\n";
292 print STDERR " " x $columnbeg;
293 $linelength = $columnbeg;
294 }
295
296 # Write the word
297 print STDERR " $word";
298 $linelength = $linelength + length(" $word");
299 }
300
301 print STDERR "\n";
302}
303
304
3051;
Note: See TracBrowser for help on using the repository browser.