Warn about unquoted attribute values containing [^-\.a-zA-Z0-9]
[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 # FIXME: Strings like "<< Prev" or "Next >>" may confuse *this* filter
11 # TODO: Need to detect unclosed tags, empty tags, and other such stuff.
12
13 use Getopt::Long;
14 use strict;
15
16 use vars qw( $input );
17 use vars qw( $debug_dump_only_p );
18
19 ###############################################################################
20
21 # Hideous stuff
22 use vars qw( $re_directive );
23 BEGIN {
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*(?:--)?)>};
26 }
27
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 );
32 sub re_tag ($) {
33    my($compat) = @_;
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    # FIXME: The following is somehow wrong. Paul's 1st report shouldn't happen.
38    q{(<\/?(?:|(?:"(?:} . $re_directive . q{|[^"])*"|'(?:} . $re_directive . q{|[^'])*'|--(?:[^-]|-[^-])*--|(?:} . $re_directive . q{|[^-"'} . $etag . q{]|-[^-]))+))([} . $etag . q{])(.*)};
39 }
40 BEGIN {
41     $re_comment = '(?:--(?:[^-]|-[^-])*--)';
42     $re_entity_name = '(?:[^&%#;<>\s]+)'; # NOTE: not really correct SGML
43     $re_end_entity = '(?:;|$|(?=\s))'; # semicolon or before-whitespace
44     $re_etag = q{(?:<\/?(?:"[^"]*"|'[^']*'|[^"'>\/])*[>\/])}; # end-tag
45     @re_tag = ($re_tag_strict, $re_tag_compat) = (re_tag(0), re_tag(1));
46 }
47
48 # End of the hideous stuff
49
50 sub KIND_TEXT      () { 'TEXT' }
51 sub KIND_CDATA     () { 'CDATA' }
52 sub KIND_TAG       () { 'TAG' }
53 sub KIND_DECL      () { 'DECL' }
54 sub KIND_PI        () { 'PI' }
55 sub KIND_DIRECTIVE () { 'HTML::Template' }
56 sub KIND_COMMENT   () { 'COMMENT' }   # empty DECL with exactly one SGML comment
57 sub KIND_UNKNOWN   () { 'ERROR' }
58
59 use vars qw( $readahead $lc_0 $lc $syntaxerror_p );
60 use vars qw( $cdata_mode_p $cdata_close );
61
62 sub extract_attributes ($;$) {
63     my($s, $lc) = @_;
64     my %attr;
65     $s = $1 if $s =~ /^<\S+(.*)\/\S$/s  # XML-style self-closing tags
66             || $s =~ /^<\S+(.*)\S$/s;   # SGML-style tags
67
68     for (my $i = 0; $s =~ /^\s+(?:([a-zA-Z][-a-zA-Z0-9]*)=)?('((?:$re_directive|[^'])*)'|"((?:$re_directive|[^"])*)"|(($re_directive|[^\s<>])+))/os;) {
69         my($key, $val, $val_orig, $rest)
70                 = ($1, (defined $3? $3: defined $4? $4: $5), $2, $');
71         $i += 1;
72         $attr{+lc($key)} = [$key, $val, $val_orig, $i];
73         $s = $rest;
74         warn "Warning: Attribute unquoted but needs quoting"
75                 . (defined $lc? " in line $lc": '') . ": $val_orig\n"
76                 if $val =~ /[^-\.A-Za-z0-9]/s && $val_orig !~ /^['"]/;
77     }
78     if ($s =~ /\S/s) { # should never happen
79         warn "Warning: Strange attribute syntax"
80                 . (defined $lc? " in line $lc": '') . ": $s\n";
81     } else {
82     }
83     return \%attr;
84 }
85
86 sub next_token_internal (*) {
87     my($h) = @_;
88     my($it, $kind);
89     my $eof_p = 0;
90     if (!defined $readahead || !length $readahead) {
91         my $next = scalar <$h>;
92         $eof_p = !defined $next;
93         if (!$eof_p) {
94             $lc += 1;
95             $readahead .= $next;
96         }
97     }
98     $lc_0 = $lc;                        # remember line number of first line
99     if ($eof_p && !length $readahead) { # nothing left to do
100         ;
101     } elsif ($readahead =~ /^\s+/s) {   # whitespace
102         ($kind, $it, $readahead) = (KIND_TEXT, $&, $');
103     # FIXME the following (the [<\s] part) is an unreliable HACK :-(
104     } elsif ($readahead =~ /^(?:[^<]|<[<\s])+/s) {      # non-space normal text
105         ($kind, $it, $readahead) = (KIND_TEXT, $&, $');
106         warn "Warning: Unescaped < in line $lc: $it\n" if $it =~ /</s;
107     } else {                            # tag/declaration/processing instruction
108         my $ok_p = 0;
109         for (;;) {
110             if ($cdata_mode_p) {
111                 if ($readahead =~ /^$cdata_close/) {
112                     ($kind, $it, $readahead) = (KIND_TAG, $&, $');
113                     $ok_p = 1;
114                 } else {
115                     ($kind, $it, $readahead) = (KIND_TEXT, $readahead, undef);
116                     $ok_p = 1;
117                 }
118             } elsif ($readahead =~ /^$re_tag_compat/os) {
119                 ($kind, $it, $readahead) = (KIND_TAG, "$1$2", $3);
120                 $ok_p = 1;
121             } elsif ($readahead =~ /^<!--(?:(?!-->).)*-->/s) {
122                 ($kind, $it, $readahead) = (KIND_COMMENT, $&, $');
123                 $ok_p = 1;
124                 warn "Warning: Syntax error in comment at line $lc_0: $&\n";
125                 $syntaxerror_p = 1;
126             }
127         last if $ok_p;
128             my $next = scalar <$h>;
129             $eof_p = !defined $next;
130         last if $eof_p;
131             $lc += 1;
132             $readahead .= $next;
133         }
134         if ($kind ne KIND_TAG) {
135             ;
136         } elsif ($it =~ /^<!/) {
137             $kind = KIND_DECL;
138             $kind = KIND_COMMENT if $it =~ /^<!--(?:(?!-->).)*-->/;
139         } elsif ($it =~ /^<\?/) {
140             $kind = KIND_PI;
141         }
142         if ($it =~ /^$re_directive/ios && !$cdata_mode_p) {
143             $kind = KIND_DIRECTIVE;
144         }
145         ($kind, $it) = (KIND_UNKNOWN, $readahead)
146                 if !$ok_p && $eof_p && !length $readahead;
147     }
148     return defined $it? (wantarray? ($kind, $it):
149                                     [$kind, $it]): undef;
150 }
151
152 sub next_token (*) {
153     my($h) = @_;
154     my $it;
155     if (!$cdata_mode_p) {
156         $it = next_token_internal($h);
157         if (defined $it && $it->[0] eq KIND_TAG) { # FIXME
158             ($cdata_mode_p, $cdata_close) = (1, "</$1\\s*>")
159                     if $it->[1] =~ /^<(script|style|textarea)\b/i; #FIXME
160             push @$it, extract_attributes($it->[1], $lc); #FIXME
161         }
162     } else {
163         for (;;) {
164             my $lc_prev = $lc;
165             my $next = next_token_internal($h);
166         last if !defined $next;
167             if (defined $next && $next->[1] =~ /$cdata_close/i) { #FIXME
168                 ($lc, $readahead) = ($lc_prev, $next->[1] . $readahead); #FIXME
169                 $cdata_mode_p = 0;
170             }
171         last unless $cdata_mode_p;
172             $it .= $next->[1]; #FIXME
173         }
174         $it = [KIND_CDATA, $it] if defined $it; #FIXME
175         $cdata_close = undef;
176     }
177     return defined $it? (wantarray? @$it: $it): undef;
178 }
179
180 ###############################################################################
181
182 sub debug_dump (*) { # for testing only
183     my($h) = @_;
184     print "re_tag_compat is /$re_tag_compat/\n";
185     for (;;) {
186         my $s = next_token $h;
187     last unless defined $s;
188         printf "%s\n", ('-' x 79);
189         my($kind, $t, $attr) = @$s; # FIXME
190         printf "%s:\n", $kind;
191         printf "%4dH%s\n", length($t),
192                 join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $t));
193         if ($kind eq KIND_TAG && %$attr) {
194             printf "Attributes:\n";
195             for my $a (keys %$attr) {
196                 my($key, $val, $val_orig, $order) = @{$attr->{$a}};
197                 printf "%s = %dH%s -- %s\n", $a, length $val,
198                 join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $val)),
199                 $val_orig;
200             }
201         }
202     }
203 }
204
205 ###############################################################################
206
207 sub text_extract (*) {
208     my($h) = @_;
209     my %text = ();
210     for (;;) {
211         my $s = next_token $h;
212     last unless defined $s;
213         my($kind, $t, $attr) = @$s; # FIXME
214         if ($kind eq KIND_TEXT) {
215             $t =~ s/\s+$//s;
216             $text{$t} = 1 if $t =~ /\S/s;
217         } elsif ($kind eq KIND_TAG && %$attr) {
218             # value [tag=input], meta
219             my $tag = lc($1) if $t =~ /^<(\S+)/s;
220             for my $a ('alt', 'content', 'title', 'value') {
221                 if ($attr->{$a}) {
222                     next if $a eq 'content' && $tag ne 'meta';
223                     next if $a eq 'value' && ($tag ne 'input'
224                         || (ref $attr->{'type'} && $attr->{'type'}->[1] eq 'hidden')); # FIXME
225                     my($key, $val, $val_orig, $order) = @{$attr->{$a}}; #FIXME
226                     $val =~ s/\s+$//s;
227                     $text{$val} = 1 if $val =~ /\S/s;
228                 }
229             }
230         }
231     }
232     for my $t (keys %text) {
233         printf "%s\n", $t unless $t =~ /^(?:\s|\&nbsp;)*$/s;
234     }
235 }
236
237 ###############################################################################
238
239 GetOptions(
240     'f|file=s' => \$input,
241     'debug-dump-only-p' => \$debug_dump_only_p,
242 ) || exit(-1);
243
244 open(INPUT, "<$input") || die "$0: $input: $!\n";
245 if ($debug_dump_only_p) {
246     debug_dump(*INPUT);
247 } else {
248     text_extract(*INPUT);
249 }
250
251 warn "Warning: This input will not work with Mozilla standards-compliant mode\n"
252         if $syntaxerror_p;
253
254 close INPUT;
255