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.
16 use vars qw( $input );
17 use vars qw( $debug_dump_only_p );
19 ###############################################################################
22 use vars qw( $re_directive );
24 # $re_directive must not do any backreferences
25 $re_directive = q{<(?:(?i)(?:!--\s*)?\/?TMPL_(?:VAR|LOOP|INCLUDE|IF|ELSE|UNLESS)\b(?:\s+(?:[a-zA-Z][-a-zA-Z0-9]*=)?(?:'[^']*'|"[^"]*"|[^\s<>]+))\s*(?:--)?)>};
28 # Hideous stuff from subst.pl, slightly modified to use the above hideous stuff
29 # Note: The $re_tag's set $1 (<tag), $2 (>), and $3 (rest of string)
30 use vars qw( $re_comment $re_entity_name $re_end_entity $re_etag );
31 use vars qw( $re_tag_strict $re_tag_compat @re_tag );
34 my $etag = $compat? '>': '<>\/';
35 # See the file "subst.pl.test1" for how the following mess is derived
36 # Unfortunately, inserting $re_directive's has made this even messier
37 q{(<\/?(?:|(?:"(?:} . $re_directive . q{|[^"])*"|'(?:} . $re_directive . q{|[^'])*'|--(?:[^-]|-[^-])*--|(?:} . $re_directive . q{|[^-"'} . $etag . q{]|-[^-]))+))([} . $etag . q{])(.*)};
40 $re_comment = '(?:--(?:[^-]|-[^-])*--)';
41 $re_entity_name = '(?:[^&%#;<>\s]+)'; # NOTE: not really correct SGML
42 $re_end_entity = '(?:;|$|(?=\s))'; # semicolon or before-whitespace
43 $re_etag = q{(?:<\/?(?:"[^"]*"|'[^']*'|[^"'>\/])*[>\/])}; # end-tag
44 @re_tag = ($re_tag_strict, $re_tag_compat) = (re_tag(0), re_tag(1));
47 # End of the hideous stuff
49 sub KIND_TEXT () { 'TEXT' }
50 sub KIND_CDATA () { 'CDATA' }
51 sub KIND_TAG () { 'TAG' }
52 sub KIND_DECL () { 'DECL' }
53 sub KIND_PI () { 'PI' }
54 sub KIND_DIRECTIVE () { 'HTML::Template' }
55 sub KIND_COMMENT () { 'COMMENT' } # empty DECL with exactly one SGML comment
56 sub KIND_UNKNOWN () { 'ERROR' }
58 use vars qw( $readahead $lc_0 $lc $syntaxerror_p );
59 use vars qw( $cdata_mode_p $cdata_close );
61 sub extract_attributes ($;$) {
64 $s = $1 if $s =~ /^<\S+(.*)\S$/s; # should be always true
65 for (my $i = 0; $s =~ /^\s+(?:([a-zA-Z][-a-zA-Z0-9]*)=)?('((?:$re_directive|[^'])*)'|"((?:$re_directive|[^"])*)"|(($re_directive|[^\s<>])+))/os;) {
66 my($key, $val, $val_orig, $rest)
67 = ($1, (defined $3? $3: defined $4? $4: $5), $2, $');
69 $attr{+lc($key)} = [$key, $val, $val_orig, $i];
72 if ($s =~ /\S/s) { # should never happen
73 warn "Warning: Strange attribute syntax"
74 . (defined $lc? " in line $lc": '') . ": $s\n";
80 sub next_token_internal (*) {
84 if (!defined $readahead || !length $readahead) {
85 my $next = scalar <$h>;
86 $eof_p = !defined $next;
92 $lc_0 = $lc; # remember line number of first line
93 if ($eof_p && !length $readahead) { # nothing left to do
95 } elsif ($readahead =~ /^\s+/s) { # whitespace
96 ($kind, $it, $readahead) = (KIND_TEXT, $&, $');
97 # FIXME the following (the [<\s] part) is an unreliable HACK :-(
98 } elsif ($readahead =~ /^(?:[^<]|<[<\s])+/s) { # non-space normal text
99 ($kind, $it, $readahead) = (KIND_TEXT, $&, $');
100 } else { # tag/declaration/processing instruction
104 if ($readahead =~ /^$cdata_close/) {
105 ($kind, $it, $readahead) = (KIND_TAG, $&, $');
108 ($kind, $it, $readahead) = (KIND_TEXT, $readahead, undef);
111 } elsif ($readahead =~ /^$re_tag_compat/os) {
112 ($kind, $it, $readahead) = (KIND_TAG, "$1$2", $3);
114 } elsif ($readahead =~ /^<!--(?:(?!-->).)*-->/s) {
115 ($kind, $it, $readahead) = (KIND_COMMENT, $&, $');
117 warn "Warning: Syntax error in comment at line $lc_0: $&\n";
121 my $next = scalar <$h>;
122 $eof_p = !defined $next;
127 if ($kind ne KIND_TAG) {
129 } elsif ($it =~ /^<!/) {
131 $kind = KIND_COMMENT if $it =~ /^<!--(?:(?!-->).)*-->/;
132 } elsif ($it =~ /^<\?/) {
135 if ($it =~ /^$re_directive/ios && !$cdata_mode_p) {
136 $kind = KIND_DIRECTIVE;
138 ($kind, $it) = (KIND_UNKNOWN, $readahead)
139 if !$ok_p && $eof_p && !length $readahead;
141 return defined $it? (wantarray? ($kind, $it):
142 [$kind, $it]): undef;
148 if (!$cdata_mode_p) {
149 $it = next_token_internal($h);
150 if (defined $it && $it->[0] eq KIND_TAG) { # FIXME
151 ($cdata_mode_p, $cdata_close) = (1, "</$1\\s*>")
152 if $it->[1] =~ /^<(script|style|textarea)\b/i; #FIXME
153 push @$it, extract_attributes($it->[1], $lc); #FIXME
158 my $next = next_token_internal($h);
159 last if !defined $next;
160 if (defined $next && $next->[1] =~ /$cdata_close/i) { #FIXME
161 ($lc, $readahead) = ($lc_prev, $next->[1] . $readahead); #FIXME
164 last unless $cdata_mode_p;
165 $it .= $next->[1]; #FIXME
167 $it = [KIND_CDATA, $it] if defined $it; #FIXME
168 $cdata_close = undef;
170 return defined $it? (wantarray? @$it: $it): undef;
173 ###############################################################################
175 sub debug_dump (*) { # for testing only
177 print "re_tag_compat is /$re_tag_compat/\n";
179 my $s = next_token $h;
180 last unless defined $s;
181 printf "%s\n", ('-' x 79);
182 my($kind, $t, $attr) = @$s; # FIXME
183 printf "%s:\n", $kind;
184 printf "%4dH%s\n", length($t),
185 join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $t));
186 if ($kind eq KIND_TAG && %$attr) {
187 printf "Attributes:\n";
188 for my $a (keys %$attr) {
189 my($key, $val, $val_orig, $order) = @{$attr->{$a}};
190 printf "%s = %dH%s -- %s\n", $a, length $val,
191 join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $val)),
198 ###############################################################################
200 sub text_extract (*) {
204 my $s = next_token $h;
205 last unless defined $s;
206 my($kind, $t, $attr) = @$s; # FIXME
207 if ($kind eq KIND_TEXT) {
209 $text{$t} = 1 if $t =~ /\S/s;
210 } elsif ($kind eq KIND_TAG && %$attr) {
211 # value [tag=input], meta
212 my $tag = lc($1) if $t =~ /^<(\S+)/s;
213 for my $a ('alt', 'content', 'title', 'value') {
215 next if $a eq 'content' && $tag ne 'meta';
216 next if $a eq 'value' && ($tag ne 'input'
217 || (ref $attr->{'type'} && $attr->{'type'}->[1] eq 'hidden')); # FIXME
218 my($key, $val, $val_orig, $order) = @{$attr->{$a}}; #FIXME
220 $text{$val} = 1 if $val =~ /\S/s;
225 for my $t (keys %text) {
226 printf "%s\n", $t unless $t =~ /^(?:\s|\ )*$/s;
230 ###############################################################################
233 'f|file=s' => \$input,
234 'debug-dump-only-p' => \$debug_dump_only_p,
237 open(INPUT, "<$input") || die "$0: $input: $!\n";
238 if ($debug_dump_only_p) {
241 text_extract(*INPUT);
244 warn "Warning: This input will not work with Mozilla standards-compliant mode\n"