4fbcbb278fc61f57be383760c0f444c52986d0ef
[koha_gimpoz] / 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    q{(<\/?(?:|(?:"(?:} . $re_directive . q{|[^"])*"|'(?:} . $re_directive . q{|[^'])*'|--(?:[^-]|-[^-])*--|(?:} . $re_directive . q{|[^-"'} . $etag . q{]|-[^-]))+))([} . $etag . q{])(.*)};
38 }
39 BEGIN {
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));
45 }
46
47 # End of the hideous stuff
48
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' }
57
58 use vars qw( $readahead $lc_0 $lc $syntaxerror_p );
59 use vars qw( $cdata_mode_p $cdata_close );
60
61 sub extract_attributes ($;$) {
62     my($s, $lc) = @_;
63     my %attr;
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, $');
68         $i += 1;
69         $attr{+lc($key)} = [$key, $val, $val_orig, $i];
70         $s = $rest;
71     }
72     if ($s =~ /\S/s) { # should never happen
73         warn "Warning: Strange attribute syntax"
74                 . (defined $lc? " in line $lc": '') . ": $s\n";
75     } else {
76     }
77     return \%attr;
78 }
79
80 sub next_token_internal (*) {
81     my($h) = @_;
82     my($it, $kind);
83     my $eof_p = 0;
84     if (!defined $readahead || !length $readahead) {
85         my $next = scalar <$h>;
86         $eof_p = !defined $next;
87         if (!$eof_p) {
88             $lc += 1;
89             $readahead .= $next;
90         }
91     }
92     $lc_0 = $lc;                        # remember line number of first line
93     if ($eof_p && !length $readahead) { # nothing left to do
94         ;
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         warn "Warning: Unescaped < in line $lc: $it\n" if $it =~ /</s;
101     } else {                            # tag/declaration/processing instruction
102         my $ok_p = 0;
103         for (;;) {
104             if ($cdata_mode_p) {
105                 if ($readahead =~ /^$cdata_close/) {
106                     ($kind, $it, $readahead) = (KIND_TAG, $&, $');
107                     $ok_p = 1;
108                 } else {
109                     ($kind, $it, $readahead) = (KIND_TEXT, $readahead, undef);
110                     $ok_p = 1;
111                 }
112             } elsif ($readahead =~ /^$re_tag_compat/os) {
113                 ($kind, $it, $readahead) = (KIND_TAG, "$1$2", $3);
114                 $ok_p = 1;
115             } elsif ($readahead =~ /^<!--(?:(?!-->).)*-->/s) {
116                 ($kind, $it, $readahead) = (KIND_COMMENT, $&, $');
117                 $ok_p = 1;
118                 warn "Warning: Syntax error in comment at line $lc_0: $&\n";
119                 $syntaxerror_p = 1;
120             }
121         last if $ok_p;
122             my $next = scalar <$h>;
123             $eof_p = !defined $next;
124         last if $eof_p;
125             $lc += 1;
126             $readahead .= $next;
127         }
128         if ($kind ne KIND_TAG) {
129             ;
130         } elsif ($it =~ /^<!/) {
131             $kind = KIND_DECL;
132             $kind = KIND_COMMENT if $it =~ /^<!--(?:(?!-->).)*-->/;
133         } elsif ($it =~ /^<\?/) {
134             $kind = KIND_PI;
135         }
136         if ($it =~ /^$re_directive/ios && !$cdata_mode_p) {
137             $kind = KIND_DIRECTIVE;
138         }
139         ($kind, $it) = (KIND_UNKNOWN, $readahead)
140                 if !$ok_p && $eof_p && !length $readahead;
141     }
142     return defined $it? (wantarray? ($kind, $it):
143                                     [$kind, $it]): undef;
144 }
145
146 sub next_token (*) {
147     my($h) = @_;
148     my $it;
149     if (!$cdata_mode_p) {
150         $it = next_token_internal($h);
151         if (defined $it && $it->[0] eq KIND_TAG) { # FIXME
152             ($cdata_mode_p, $cdata_close) = (1, "</$1\\s*>")
153                     if $it->[1] =~ /^<(script|style|textarea)\b/i; #FIXME
154             push @$it, extract_attributes($it->[1], $lc); #FIXME
155         }
156     } else {
157         for (;;) {
158             my $lc_prev = $lc;
159             my $next = next_token_internal($h);
160         last if !defined $next;
161             if (defined $next && $next->[1] =~ /$cdata_close/i) { #FIXME
162                 ($lc, $readahead) = ($lc_prev, $next->[1] . $readahead); #FIXME
163                 $cdata_mode_p = 0;
164             }
165         last unless $cdata_mode_p;
166             $it .= $next->[1]; #FIXME
167         }
168         $it = [KIND_CDATA, $it] if defined $it; #FIXME
169         $cdata_close = undef;
170     }
171     return defined $it? (wantarray? @$it: $it): undef;
172 }
173
174 ###############################################################################
175
176 sub debug_dump (*) { # for testing only
177     my($h) = @_;
178     print "re_tag_compat is /$re_tag_compat/\n";
179     for (;;) {
180         my $s = next_token $h;
181     last unless defined $s;
182         printf "%s\n", ('-' x 79);
183         my($kind, $t, $attr) = @$s; # FIXME
184         printf "%s:\n", $kind;
185         printf "%4dH%s\n", length($t),
186                 join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $t));
187         if ($kind eq KIND_TAG && %$attr) {
188             printf "Attributes:\n";
189             for my $a (keys %$attr) {
190                 my($key, $val, $val_orig, $order) = @{$attr->{$a}};
191                 printf "%s = %dH%s -- %s\n", $a, length $val,
192                 join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $val)),
193                 $val_orig;
194             }
195         }
196     }
197 }
198
199 ###############################################################################
200
201 sub text_extract (*) {
202     my($h) = @_;
203     my %text = ();
204     for (;;) {
205         my $s = next_token $h;
206     last unless defined $s;
207         my($kind, $t, $attr) = @$s; # FIXME
208         if ($kind eq KIND_TEXT) {
209             $t =~ s/\s+$//s;
210             $text{$t} = 1 if $t =~ /\S/s;
211         } elsif ($kind eq KIND_TAG && %$attr) {
212             # value [tag=input], meta
213             my $tag = lc($1) if $t =~ /^<(\S+)/s;
214             for my $a ('alt', 'content', 'title', 'value') {
215                 if ($attr->{$a}) {
216                     next if $a eq 'content' && $tag ne 'meta';
217                     next if $a eq 'value' && ($tag ne 'input'
218                         || (ref $attr->{'type'} && $attr->{'type'}->[1] eq 'hidden')); # FIXME
219                     my($key, $val, $val_orig, $order) = @{$attr->{$a}}; #FIXME
220                     $val =~ s/\s+$//s;
221                     $text{$val} = 1 if $val =~ /\S/s;
222                 }
223             }
224         }
225     }
226     for my $t (keys %text) {
227         printf "%s\n", $t unless $t =~ /^(?:\s|\&nbsp;)*$/s;
228     }
229 }
230
231 ###############################################################################
232
233 GetOptions(
234     'f|file=s' => \$input,
235     'debug-dump-only-p' => \$debug_dump_only_p,
236 ) || exit(-1);
237
238 open(INPUT, "<$input") || die "$0: $input: $!\n";
239 if ($debug_dump_only_p) {
240     debug_dump(*INPUT);
241 } else {
242     text_extract(*INPUT);
243 }
244
245 warn "Warning: This input will not work with Mozilla standards-compliant mode\n"
246         if $syntaxerror_p;
247
248 close INPUT;
249