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 # FIXME: Strings like "<< Prev" or "Next >>" may confuse *this* filter
11 # TODO: Need to detect unclosed tags, empty tags, and other such stuff.
12 # (Why? Because Mozilla apparently knows what SGML unclosed tags are :-/ )
14 # A grander plan: Code could be written to detect template variables and
15 # construct gettext-c-format-string-like meta-strings (e.g., "Results %s
16 # through %s of %s records" that will be more likely to be translatable
17 # to languages where word order is very unlike English word order.
18 # --> This will be relatively major rework, and requires corresponding
19 # rework in tmpl_process.pl
24 use vars qw( $input );
25 use vars qw( $debug_dump_only_p );
26 use vars qw( $pedantic_p );
27 use vars qw( $fatal_p );
29 ###############################################################################
32 use vars qw( $re_directive );
34 # $re_directive must not do any backreferences
35 $re_directive = q{<(?:(?i)(?:!--\s*)?\/?TMPL_(?:VAR|LOOP|INCLUDE|IF|ELSE|UNLESS)(?:\s+(?:[a-zA-Z][-a-zA-Z0-9]*=)?(?:'[^']*'|"[^"]*"|[^\s<>]+))*\s*(?:--)?)>};
38 # Hideous stuff from subst.pl, slightly modified to use the above hideous stuff
39 # Note: The $re_tag's set $1 (<tag), $2 (>), and $3 (rest of string)
40 use vars qw( $re_comment $re_entity_name $re_end_entity $re_etag );
41 use vars qw( $re_tag_strict $re_tag_compat @re_tag );
44 my $etag = $compat? '>': '<>\/';
45 # See the file "subst.pl.test1" for how the following mess is derived
46 # Unfortunately, inserting $re_directive's has made this even messier
47 q{(<\/?(?:|(?:"(?:} . $re_directive . q{|[^"])*"|'(?:} . $re_directive . q{|[^'])*'|--(?:[^-]|-[^-])*--|(?:} . $re_directive . q{|[^-"'} . $etag . q{]|-[^-]))+))([} . $etag . q{])(.*)};
50 $re_comment = '(?:--(?:[^-]|-[^-])*--)';
51 $re_entity_name = '(?:[^&%#;<>\s]+)'; # NOTE: not really correct SGML
52 $re_end_entity = '(?:;|$|(?=\s))'; # semicolon or before-whitespace
53 $re_etag = q{(?:<\/?(?:"[^"]*"|'[^']*'|[^"'>\/])*[>\/])}; # end-tag
54 @re_tag = ($re_tag_strict, $re_tag_compat) = (re_tag(0), re_tag(1));
57 # End of the hideous stuff
59 sub KIND_TEXT () { 'TEXT' }
60 sub KIND_CDATA () { 'CDATA' }
61 sub KIND_TAG () { 'TAG' }
62 sub KIND_DECL () { 'DECL' }
63 sub KIND_PI () { 'PI' }
64 sub KIND_DIRECTIVE () { 'HTML::Template' }
65 sub KIND_COMMENT () { 'COMMENT' } # empty DECL with exactly one SGML comment
66 sub KIND_UNKNOWN () { 'ERROR' }
68 use vars qw( $readahead $lc_0 $lc $syntaxerror_p );
69 use vars qw( $cdata_mode_p $cdata_close );
71 sub extract_attributes ($;$) {
74 $s = $1 if $s =~ /^<\S+(.*)\/\S$/s # XML-style self-closing tags
75 || $s =~ /^<\S+(.*)\S$/s; # SGML-style tags
77 for (my $i = 0; $s =~ /^\s+(?:([a-zA-Z][-a-zA-Z0-9]*)=)?('((?:$re_directive|[^'])*)'|"((?:$re_directive|[^"])*)"|(($re_directive|[^\s<>])+))/os;) {
78 my($key, $val, $val_orig, $rest)
79 = ($1, (defined $3? $3: defined $4? $4: $5), $2, $');
81 $attr{+lc($key)} = [$key, $val, $val_orig, $i];
83 warn "Warning: Attribute should be quoted"
84 . (defined $lc? " near line $lc": '') . ": $val_orig\n"
86 && $val =~ /[^-\.A-Za-z0-9]/s && $val_orig !~ /^['"]/;
88 if ($s =~ /\S/s) { # should never happen
89 if ($s =~ /^([^\n]*)\n/s) { # this is even worse
90 warn "Error: Completely confused while extracting attributes"
91 . (defined $lc? " near line $lc": '') . ": $1\n";
92 warn "Error: " . (scalar split(/\n/, $s) - 1) . " more line(s) not shown.\n";
95 warn "Warning: Strange attribute syntax"
96 . (defined $lc? " near line $lc": '') . ": $s\n";
102 sub next_token_internal (*) {
106 if (!defined $readahead || !length $readahead) {
107 my $next = scalar <$h>;
108 $eof_p = !defined $next;
114 $lc_0 = $lc; # remember line number of first line
115 if ($eof_p && !length $readahead) { # nothing left to do
117 } elsif ($readahead =~ /^\s+/s) { # whitespace
118 ($kind, $it, $readahead) = (KIND_TEXT, $&, $');
119 # FIXME the following (the [<\s] part) is an unreliable HACK :-(
120 } elsif ($readahead =~ /^(?:[^<]|<[<\s])+/s) { # non-space normal text
121 ($kind, $it, $readahead) = (KIND_TEXT, $&, $');
122 warn "Warning: Unescaped < near line $lc_0: $it\n" if $it =~ /</s;
123 } else { # tag/declaration/processing instruction
127 if ($readahead =~ /^$cdata_close/) {
128 ($kind, $it, $readahead) = (KIND_TAG, $&, $');
131 ($kind, $it, $readahead) = (KIND_TEXT, $readahead, undef);
134 } elsif ($readahead =~ /^$re_tag_compat/os) {
135 ($kind, $it, $readahead) = (KIND_TAG, "$1$2", $3);
137 } elsif ($readahead =~ /^<!--(?:(?!-->).)*-->/s) {
138 ($kind, $it, $readahead) = (KIND_COMMENT, $&, $');
140 warn "Warning: Syntax error in comment at line $lc_0: $&\n";
144 my $next = scalar <$h>;
145 $eof_p = !defined $next;
150 if ($kind ne KIND_TAG) {
152 } elsif ($it =~ /^<!/) {
154 $kind = KIND_COMMENT if $it =~ /^<!--(?:(?!-->).)*-->/;
155 } elsif ($it =~ /^<\?/) {
158 if ($it =~ /^$re_directive/ios && !$cdata_mode_p) {
159 $kind = KIND_DIRECTIVE;
161 if (!$ok_p && $eof_p) {
162 ($kind, $it, $readahead) = (KIND_UNKNOWN, $readahead, undef);
166 warn "Warning: Unrecognizable token found near line $lc_0: $it\n"
167 if $kind eq KIND_UNKNOWN;
168 return defined $it? (wantarray? ($kind, $it):
169 [$kind, $it]): undef;
175 if (!$cdata_mode_p) {
176 $it = next_token_internal($h);
177 if (defined $it && $it->[0] eq KIND_TAG) { # FIXME
178 ($cdata_mode_p, $cdata_close) = (1, "</$1\\s*>")
179 if $it->[1] =~ /^<(script|style|textarea)\b/i; #FIXME
180 push @$it, extract_attributes($it->[1], $lc_0); #FIXME
185 my $next = next_token_internal($h);
186 last if !defined $next;
187 if (defined $next && $next->[1] =~ /$cdata_close/i) { #FIXME
188 ($lc, $readahead) = ($lc_prev, $next->[1] . $readahead); #FIXME
191 last unless $cdata_mode_p;
192 $it .= $next->[1]; #FIXME
194 $it = [KIND_CDATA, $it]; #FIXME
195 $cdata_close = undef;
197 return defined $it? (wantarray? @$it: $it): undef;
200 ###############################################################################
202 sub debug_dump (*) { # for testing only
204 print "re_tag_compat is /$re_tag_compat/\n";
206 my $s = next_token $h;
207 last unless defined $s;
208 printf "%s\n", ('-' x 79);
209 my($kind, $t, $attr) = @$s; # FIXME
210 printf "%s:\n", $kind;
211 printf "%4dH%s\n", length($t),
212 join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $t));
213 if ($kind eq KIND_TAG && %$attr) {
214 printf "Attributes:\n";
215 for my $a (keys %$attr) {
216 my($key, $val, $val_orig, $order) = @{$attr->{$a}};
217 printf "%s = %dH%s -- %s\n", $a, length $val,
218 join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $val)),
225 ###############################################################################
229 $s =~ s/^(?:\s|\ $re_end_entity)+//os;
230 $s =~ s/(?:\s|\ $re_end_entity)+$//os;
234 ###############################################################################
236 sub text_extract (*) {
240 my $s = next_token $h;
241 last unless defined $s;
242 my($kind, $t, $attr) = @$s; # FIXME
243 if ($kind eq KIND_TEXT) {
245 $text{$t} = 1 if $t =~ /\S/s;
246 } elsif ($kind eq KIND_TAG && %$attr) {
247 # value [tag=input], meta
248 my $tag = lc($1) if $t =~ /^<(\S+)/s;
249 for my $a ('alt', 'content', 'title', 'value') {
251 next if $a eq 'content' && $tag ne 'meta';
252 next if $a eq 'value' && ($tag ne 'input'
253 || (ref $attr->{'type'} && $attr->{'type'}->[1] eq 'hidden')); # FIXME
254 my($key, $val, $val_orig, $order) = @{$attr->{$a}}; #FIXME
256 $text{$val} = 1 if $val =~ /\S/s;
261 # Emit all extracted strings. Don't emit pure whitespace or pure numbers.
262 for my $t (keys %text) {
264 unless $t =~ /^(?:\s|\ $re_end_entity)*$/os || $t =~ /^\d+$/;
268 ###############################################################################
272 my $h = $exitcode? *STDERR: *STDOUT;
275 Extract strings from HTML file.
277 --debug-dump-only Do not extract strings; but display scanned tokens
278 -f, --file=FILE Extract from the specified FILE
279 --pedantic-warnings Issue warnings even for detected problems which
280 are likely to be harmless
281 --help Display this help and exit
286 ###############################################################################
288 sub usage_error (;$) {
289 print STDERR "$_[0]\n" if @_;
290 print STDERR "Try `$0 --help' for more information.\n";
294 ###############################################################################
297 'f|file=s' => \$input,
298 'debug-dump-only' => \$debug_dump_only_p,
299 'pedantic-warnings' => sub { $pedantic_p = 1 },
300 'help' => sub { usage(0) },
302 usage_error('Missing mandatory option -f') unless defined $input;
304 open(INPUT, "<$input") || die "$0: $input: $!\n";
305 if ($debug_dump_only_p) {
308 text_extract(*INPUT);
311 warn "Warning: This input will not work with Mozilla standards-compliant mode\n"
316 exit(-1) if $fatal_p;