Continuing on my tests mission
[koha.git] / misc / translator / text-extract2.pl
1 #!/usr/bin/perl
2
3 # Test filter partially based on Ambrose's hideous subst.pl code
4 # The idea is that the .tmpl files are not valid HTML, and as a result
5 # HTML::Parse would be completely confused by these templates.
6 # This is just a simple scanner (not a parser) & should give better results.
7
8 # This script is meant to be a drop-in replacement of text-extract.pl
9
10 # A grander plan: Code could be written to detect template variables and
11 # construct gettext-c-format-string-like meta-strings (e.g., "Results %s
12 # through %s of %s records" that will be more likely to be translatable
13 # to languages where word order is very unlike English word order.
14 # --> This will be relatively major rework, and requires corresponding
15 # rework in tmpl_process.pl
16
17 use Getopt::Long;
18 use TmplTokenizer;
19 use VerboseWarnings;
20 use strict;
21
22 use vars qw( $input );
23 use vars qw( $debug_dump_only_p );
24 use vars qw( $pedantic_p );
25 use vars qw( $allow_cformat_p ); # FOR TESTING PURPOSES ONLY!!
26
27 ###############################################################################
28
29 sub underline ($) { # for testing only
30     my($s) = @_;
31     join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $s));
32 }
33
34 sub debug_dump ($) { # for testing only
35     my($h) = @_;
36     print "re_tag_compat is /", TmplTokenizer::re_tag(1), "/\n";
37     for (;;) {
38         my $s = TmplTokenizer::next_token $h;
39     last unless defined $s;
40         printf "%s\n", ('-' x 79);
41         my($kind, $t, $attr) = ($s->type, $s->string, $s->attributes);
42         printf "%s [line %d]:\n", $kind->to_string, $s->line_number;
43         printf "%4dH%s\n", length($t), underline($t);
44         if ($kind == TmplTokenType::TAG && %$attr) {
45             printf "Attributes:\n";
46             for my $a (keys %$attr) {
47                 my($key, $val, $val_orig, $order) = @{$attr->{$a}};
48                 printf "%s = %dH%s -- %s\n", $a, length $val, underline $val,
49                 $val_orig;
50             }
51         }
52         if ($kind == TmplTokenType::TEXT_PARAMETRIZED) {
53             printf "Form (c-format string):\n";
54             printf "%dH%s\n", length $s->form, underline $s->form;
55             printf "Parameters:\n";
56             my $i = 1;
57             for my $a ($s->parameters) {
58                 my $t = $a->string;
59                 printf "%%%d\$s = %dH%s\n", $i, length $t, underline $t;
60                 $i += 1;
61             }
62         }
63         if ($s->has_js_data) {
64             printf "JavaScript translatable strings:\n";
65             for my $t (@{$s->js_data}) {
66                 printf "%dH%s\n", length $t->[3], underline $t->[3] if $t->[0]; # FIXME
67             }
68         }
69     }
70 }
71
72 ###############################################################################
73
74 sub text_extract ($) {
75     my($h) = @_;
76     my %text = ();
77     for (;;) {
78         my $s = TmplTokenizer::next_token $h;
79     last unless defined $s;
80         my($kind, $t, $attr) = ($s->type, $s->string, $s->attributes);
81         if ($kind == TmplTokenType::TEXT) {
82             $t = TmplTokenizer::trim $t;
83             $text{$t} = 1 if $t =~ /\S/s;
84         } elsif ($kind == TmplTokenType::TAG && %$attr) {
85             # value [tag=input], meta
86             my $tag = lc($1) if $t =~ /^<(\S+)/s;
87             for my $a ('alt', 'content', 'title', 'value') {
88                 if ($attr->{$a}) {
89                     next if $a eq 'content' && $tag ne 'meta';
90                     next if $a eq 'value' && ($tag ne 'input'
91                         || (ref $attr->{'type'} && $attr->{'type'}->[1] =~ /^(?:hidden|radio)$/)); # FIXME
92                     my($key, $val, $val_orig, $order) = @{$attr->{$a}}; #FIXME
93                     $val = TmplTokenizer::trim $val;
94                     $text{$val} = 1 if $val =~ /\S/s;
95                 }
96             }
97         } elsif ($s->has_js_data) {
98             for my $t (@{$s->js_data}) {
99                 remember( $s, $t->[3] ) if $t->[0]; # FIXME
100             }
101         }
102     }
103     # Emit all extracted strings.
104     # Don't emit pure whitespace, pure numbers, or TMPL_VAR's.
105     for my $t (keys %text) {
106         printf "%s\n", $t
107             unless TmplTokenizer::blank_p($t) || $t =~ /^\d+$/;
108     }
109 }
110
111 ###############################################################################
112
113 sub usage ($) {
114     my($exitcode) = @_;
115     my $h = $exitcode? *STDERR: *STDOUT;
116     print $h <<EOF;
117 Usage: $0 [OPTIONS]
118 Extract strings from HTML file.
119
120       --debug-dump-only     Do not extract strings; but display scanned tokens
121   -f, --file=FILE           Extract from the specified FILE
122       --pedantic-warnings   Issue warnings even for detected problems which
123                             are likely to be harmless
124       --help                Display this help and exit
125 EOF
126     exit($exitcode);
127 }
128
129 ###############################################################################
130
131 sub usage_error (;$) {
132     print STDERR "$_[0]\n" if @_;
133     print STDERR "Try `$0 --help' for more information.\n";
134     exit(-1);
135 }
136
137 ###############################################################################
138
139 GetOptions(
140     'enable-cformat'    => \$allow_cformat_p,
141     'f|file=s'          => \$input,
142     'debug-dump-only'   => \$debug_dump_only_p,
143     'pedantic-warnings' => sub { $pedantic_p = 1 },
144     'help'              => sub { usage(0) },
145 ) || usage_error;
146
147 VerboseWarnings::set_application_name $0;
148 VerboseWarnings::set_input_file_name $input;
149 VerboseWarnings::set_pedantic_mode $pedantic_p;
150
151 usage_error('Missing mandatory option -f') unless defined $input;
152
153 my $h = TmplTokenizer->new( $input );
154 $h->set_allow_cformat( 1 ) if $allow_cformat_p;
155 if ($debug_dump_only_p) {
156     debug_dump( $h );
157 } else {
158     text_extract( $h );
159 }
160
161 warn "This input will not work with Mozilla standards-compliant mode\n", undef
162         if TmplTokenizer::syntaxerror_p;
163
164 close INPUT;
165
166 exit(-1) if TmplTokenizer::fatal_p;