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.
8 # This script is meant to be a drop-in replacement of text-extract.pl
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
20 use vars qw( $input );
21 use vars qw( $debug_dump_only_p );
22 use vars qw( $pedantic_p $pedantic_tag );
23 use vars qw( $pedantic_attribute_error_in_nonpedantic_mode_p );
24 use vars qw( $pedantic_tmpl_var_use_in_nonpedantic_mode_p );
25 use vars qw( $fatal_p );
27 ###############################################################################
30 use vars qw( $re_directive $re_tmpl_var $re_tmpl_var_escaped $re_tmpl_include );
31 use vars qw( $re_directive_control $re_tmpl_endif_endloop );
33 # $re_directive must not do any backreferences
34 $re_directive = q{<(?:(?i)(?:!--\s*)?\/?TMPL_(?:VAR|LOOP|INCLUDE|IF|ELSE|UNLESS)(?:\s+(?:[a-zA-Z][-a-zA-Z0-9]*=)?(?:'[^']*'|"[^"]*"|[^\s<>]+))*\s*(?:--)?)>};
35 # TMPL_VAR or TMPL_INCLUDE
36 $re_tmpl_var = q{<(?:(?i)(?:!--\s*)?TMPL_(?:VAR)(?:\s+(?:[a-zA-Z][-a-zA-Z0-9]*=)?(?:'[^']*'|"[^"]*"|[^\s<>]+))*\s*(?:--)?)>};
37 $re_tmpl_include = q{<(?:(?i)(?:!--\s*)?TMPL_(?:INCLUDE)(?:\s+(?:[a-zA-Z][-a-zA-Z0-9]*=)?(?:'[^']*'|"[^"]*"|[^\s<>]+))*\s*(?:--)?)>};
38 # TMPL_VAR ESCAPE=1/HTML/URL
39 $re_tmpl_var_escaped = q{<(?:(?i)(?:!--\s*)?TMPL_(?:VAR|INCLUDE)(?:\s+(?:[a-zA-Z][-a-zA-Z0-9]*=)?(?:'[^']*'|"[^"]*"|[^\s<>]+))\s+ESCAPE=(?:1|HTML|URL)(?:\s+(?:[a-zA-Z][-a-zA-Z0-9]*=)?(?:'[^']*'|"[^"]*"|[^\s<>]+))*\s*(?:--)?)>};
40 # Any control flow directive
41 $re_directive_control = q{<(?:(?i)(?:!--\s*)?\/?TMPL_(?:LOOP|IF|ELSE|UNLESS)(?:\s+(?:[a-zA-Z][-a-zA-Z0-9]*=)?(?:'[^']*'|"[^"]*"|[^\s<>]+))*\s*(?:--)?)>};
42 # /LOOP or /IF or /UNLESS
43 $re_tmpl_endif_endloop = q{<(?:(?i)(?:!--\s*)?\/TMPL_(?:LOOP|IF|UNLESS)(?:\s+(?:[a-zA-Z][-a-zA-Z0-9]*=)?(?:'[^']*'|"[^"]*"|[^\s<>]+))*\s*(?:--)?)>};
46 # Hideous stuff from subst.pl, slightly modified to use the above hideous stuff
47 # Note: The $re_tag's set $1 (<tag), $2 (>), and $3 (rest of string)
48 use vars qw( $re_comment $re_entity_name $re_end_entity $re_etag );
49 use vars qw( $re_tag_strict $re_tag_compat @re_tag );
52 my $etag = $compat? '>': '<>\/';
53 # This is no longer similar to the original regexp in subst.pl :-(
54 # Note that we don't want <> in compat mode; Mozilla knows about <
55 q{(<\/?(?:|(?:"(?:} . $re_directive . q{|[^"])*"|'(?:} . $re_directive . q{|[^'])*'|--(?:[^-]|-[^-])*--|(?:}
57 . q{|(?!--)[^"'<>} . $etag . q{]))+))([} . $etag . q{]|(?=<))(.*)};
60 $re_comment = '(?:--(?:[^-]|-[^-])*--)';
61 $re_entity_name = '(?:[^&%#;<>\s]+)'; # NOTE: not really correct SGML
62 $re_end_entity = '(?:;|$|(?=\s))'; # semicolon or before-whitespace
63 $re_etag = q{(?:<\/?(?:"[^"]*"|'[^']*'|[^"'>\/])*[>\/])}; # end-tag
64 @re_tag = ($re_tag_strict, $re_tag_compat) = (re_tag(0), re_tag(1));
67 # End of the hideous stuff
69 sub KIND_TEXT () { 'TEXT' }
70 sub KIND_CDATA () { 'CDATA' }
71 sub KIND_TAG () { 'TAG' }
72 sub KIND_DECL () { 'DECL' }
73 sub KIND_PI () { 'PI' }
74 sub KIND_DIRECTIVE () { 'HTML::Template' }
75 sub KIND_COMMENT () { 'COMMENT' } # empty DECL with exactly one SGML comment
76 sub KIND_UNKNOWN () { 'ERROR' }
78 use vars qw( $readahead $lc_0 $lc $syntaxerror_p );
79 use vars qw( $cdata_mode_p $cdata_close );
81 ###############################################################################
83 sub warn_pedantic ($$) {
85 warn "Warning$pedantic_tag: $msg\n" if $pedantic_p || !$$flag;
87 warn "Warning$pedantic_tag: Further similar negligible warnings will not be reported, use --pedantic for details\n" unless $$flag;
92 ###############################################################################
94 sub extract_attributes ($;$) {
97 $s = $1 if $s =~ /^<\S+(.*)\/\S$/s # XML-style self-closing tags
98 || $s =~ /^<\S+(.*)\S$/s; # SGML-style tags
100 for (my $i = 0; $s =~ /^(?:$re_directive_control)?\s+(?:$re_directive_control)?(?:([a-zA-Z][-a-zA-Z0-9]*)\s*=\s*)?('((?:$re_directive|[^'])*)'|"((?:$re_directive|[^"])*)"|((?:$re_directive|[^\s<>])+))/os;) {
101 my($key, $val, $val_orig, $rest)
102 = ($1, (defined $3? $3: defined $4? $4: $5), $2, $');
104 $attr{+lc($key)} = [$key, $val, $val_orig, $i];
106 if ($val =~ /$re_tmpl_include/os) {
107 warn "Warning: TMPL_INCLUDE in attribute"
108 . (defined $lc? " near line $lc": '') . ": $val_orig\n";
109 } elsif ($val =~ /$re_tmpl_var/os && $val !~ /$re_tmpl_var_escaped/os) {
110 warn_pedantic \$pedantic_tmpl_var_use_in_nonpedantic_mode_p,
111 "Unescaped TMPL_VAR in attribute"
112 . (defined $lc? " near line $lc": '') . ": $val_orig"
113 if $pedantic_p || !$pedantic_tmpl_var_use_in_nonpedantic_mode_p;
114 } elsif ($val_orig !~ /^['"]/) {
115 warn_pedantic \$pedantic_attribute_error_in_nonpedantic_mode_p,
116 "Unquoted attribute contains character(s) that should be quoted"
117 . (defined $lc? " near line $lc": '') . ": $val_orig"
118 if $val =~ /[^-\.A-Za-z0-9]/s;
121 my $s2 = $s; $s2 =~ s/$re_tmpl_endif_endloop//g; # for the next check
122 if ($s2 =~ /\S/s) { # should never happen
123 if ($s =~ /^([^\n]*)\n/s) { # this is even worse
124 warn "Error: Completely confused while extracting attributes"
125 . (defined $lc? " near line $lc": '') . ": $1\n";
126 warn "Error: " . (scalar split(/\n/, $s) - 1) . " more line(s) not shown.\n";
129 warn "Warning: Strange attribute syntax"
130 . (defined $lc? " near line $lc": '') . ": $s\n";
136 sub next_token_internal (*) {
140 if (!defined $readahead || !length $readahead) {
141 my $next = scalar <$h>;
142 $eof_p = !defined $next;
148 $lc_0 = $lc; # remember line number of first line
149 if ($eof_p && !length $readahead) { # nothing left to do
151 } elsif ($readahead =~ /^\s+/s) { # whitespace
152 ($kind, $it, $readahead) = (KIND_TEXT, $&, $');
153 # FIXME the following (the [<\s] part) is an unreliable HACK :-(
154 } elsif ($readahead =~ /^(?:[^<]|<[<\s])+/s) { # non-space normal text
155 ($kind, $it, $readahead) = (KIND_TEXT, $&, $');
156 warn "Warning: Unescaped < near line $lc_0: $it\n" if $it =~ /</s;
157 } else { # tag/declaration/processing instruction
161 if ($readahead =~ /^$cdata_close/) {
162 ($kind, $it, $readahead) = (KIND_TAG, $&, $');
165 ($kind, $it, $readahead) = (KIND_TEXT, $readahead, undef);
168 } elsif ($readahead =~ /^$re_tag_compat/os) {
169 ($kind, $it, $readahead) = (KIND_TAG, "$1>", $3);
171 warn "Warning: SGML \"closed start tag\" notation near line $lc_0: $1$2\n" if $2 eq '';
172 } elsif ($readahead =~ /^<!--(?:(?!-->).)*-->/s) {
173 ($kind, $it, $readahead) = (KIND_COMMENT, $&, $');
175 warn "Warning: Syntax error in comment at line $lc_0: $&\n";
179 my $next = scalar <$h>;
180 $eof_p = !defined $next;
185 if ($kind ne KIND_TAG) {
187 } elsif ($it =~ /^<!/) {
189 $kind = KIND_COMMENT if $it =~ /^<!--(?:(?!-->).)*-->/;
190 } elsif ($it =~ /^<\?/) {
193 if ($it =~ /^$re_directive/ios && !$cdata_mode_p) {
194 $kind = KIND_DIRECTIVE;
196 if (!$ok_p && $eof_p) {
197 ($kind, $it, $readahead) = (KIND_UNKNOWN, $readahead, undef);
201 warn "Warning: Unrecognizable token found near line $lc_0: $it\n"
202 if $kind eq KIND_UNKNOWN;
203 return defined $it? (wantarray? ($kind, $it):
204 [$kind, $it]): undef;
210 if (!$cdata_mode_p) {
211 $it = next_token_internal($h);
212 if (defined $it && $it->[0] eq KIND_TAG) { # FIXME
213 ($cdata_mode_p, $cdata_close) = (1, "</$1\\s*>")
214 if $it->[1] =~ /^<(script|style|textarea)\b/i; #FIXME
215 push @$it, extract_attributes($it->[1], $lc_0); #FIXME
220 my $next = next_token_internal($h);
221 last if !defined $next;
222 if (defined $next && $next->[1] =~ /$cdata_close/i) { #FIXME
223 ($lc, $readahead) = ($lc_prev, $next->[1] . $readahead); #FIXME
226 last unless $cdata_mode_p;
227 $it .= $next->[1]; #FIXME
229 $it = [KIND_CDATA, $it]; #FIXME
230 $cdata_close = undef;
232 return defined $it? (wantarray? @$it: $it): undef;
235 ###############################################################################
237 sub debug_dump (*) { # for testing only
239 print "re_tag_compat is /$re_tag_compat/\n";
241 my $s = next_token $h;
242 last unless defined $s;
243 printf "%s\n", ('-' x 79);
244 my($kind, $t, $attr) = @$s; # FIXME
245 printf "%s:\n", $kind;
246 printf "%4dH%s\n", length($t),
247 join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $t));
248 if ($kind eq KIND_TAG && %$attr) {
249 printf "Attributes:\n";
250 for my $a (keys %$attr) {
251 my($key, $val, $val_orig, $order) = @{$attr->{$a}};
252 printf "%s = %dH%s -- %s\n", $a, length $val,
253 join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $val)),
260 ###############################################################################
264 $s =~ s/^(?:\s|\ $re_end_entity)+//os;
265 $s =~ s/(?:\s|\ $re_end_entity)+$//os;
269 ###############################################################################
271 sub text_extract (*) {
275 my $s = next_token $h;
276 last unless defined $s;
277 my($kind, $t, $attr) = @$s; # FIXME
278 if ($kind eq KIND_TEXT) {
280 $text{$t} = 1 if $t =~ /\S/s;
281 } elsif ($kind eq KIND_TAG && %$attr) {
282 # value [tag=input], meta
283 my $tag = lc($1) if $t =~ /^<(\S+)/s;
284 for my $a ('alt', 'content', 'title', 'value') {
286 next if $a eq 'content' && $tag ne 'meta';
287 next if $a eq 'value' && ($tag ne 'input'
288 || (ref $attr->{'type'} && $attr->{'type'}->[1] eq 'hidden')); # FIXME
289 my($key, $val, $val_orig, $order) = @{$attr->{$a}}; #FIXME
291 $text{$val} = 1 if $val =~ /\S/s;
296 # Emit all extracted strings.
297 # Don't emit pure whitespace, pure numbers, or TMPL_VAR's.
298 for my $t (keys %text) {
300 unless $t =~ /^(?:\s|\ $re_end_entity|$re_tmpl_var)*$/os || $t =~ /^\d+$/;
304 ###############################################################################
308 my $h = $exitcode? *STDERR: *STDOUT;
311 Extract strings from HTML file.
313 --debug-dump-only Do not extract strings; but display scanned tokens
314 -f, --file=FILE Extract from the specified FILE
315 --pedantic-warnings Issue warnings even for detected problems which
316 are likely to be harmless
317 --help Display this help and exit
322 ###############################################################################
324 sub usage_error (;$) {
325 print STDERR "$_[0]\n" if @_;
326 print STDERR "Try `$0 --help' for more information.\n";
330 ###############################################################################
333 'f|file=s' => \$input,
334 'debug-dump-only' => \$debug_dump_only_p,
335 'pedantic-warnings' => sub { $pedantic_p = 1 },
336 'help' => sub { usage(0) },
338 $pedantic_tag = $pedantic_p? '': ' (negligible)';
339 usage_error('Missing mandatory option -f') unless defined $input;
341 open(INPUT, "<$input") || die "$0: $input: $!\n";
342 if ($debug_dump_only_p) {
345 text_extract(*INPUT);
348 warn "Warning: This input will not work with Mozilla standards-compliant mode\n"
353 exit(-1) if $fatal_p;