Some functions should not be in the module; these are now removed.
[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 # 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
16
17 use Getopt::Long;
18 use TmplTokenizer;
19 use VerboseWarnings;
20 use strict;
21
22 use vars qw( $input );
23 use vars qw( $debug_dump_only_p );
24 use vars qw( $pedantic_p );
25
26 ###############################################################################
27
28 sub debug_dump (*) { # for testing only
29     my($h) = @_;
30     print "re_tag_compat is /$TmplTokenizer::re_tag_compat/\n";
31     for (;;) {
32         my $s = TmplTokenizer::next_token $h;
33     last unless defined $s;
34         printf "%s\n", ('-' x 79);
35         my($kind, $t, $attr) = @$s; # FIXME
36         printf "%s:\n", $kind;
37         printf "%4dH%s\n", length($t),
38                 join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $t));
39         if ($kind eq TmplTokenizer::KIND_TAG && %$attr) {
40             printf "Attributes:\n";
41             for my $a (keys %$attr) {
42                 my($key, $val, $val_orig, $order) = @{$attr->{$a}};
43                 printf "%s = %dH%s -- %s\n", $a, length $val,
44                 join('', map {/[\0-\37]/? $_: "$_\b$_"} split(//, $val)),
45                 $val_orig;
46             }
47         }
48     }
49 }
50
51 ###############################################################################
52
53 sub text_extract (*) {
54     my($h) = @_;
55     my %text = ();
56     for (;;) {
57         my $s = TmplTokenizer::next_token $h;
58     last unless defined $s;
59         my($kind, $t, $attr) = @$s; # FIXME
60         if ($kind eq TmplTokenizer::KIND_TEXT) {
61             $t = TmplTokenizer::trim $t;
62             $text{$t} = 1 if $t =~ /\S/s;
63         } elsif ($kind eq TmplTokenizer::KIND_TAG && %$attr) {
64             # value [tag=input], meta
65             my $tag = lc($1) if $t =~ /^<(\S+)/s;
66             for my $a ('alt', 'content', 'title', 'value') {
67                 if ($attr->{$a}) {
68                     next if $a eq 'content' && $tag ne 'meta';
69                     next if $a eq 'value' && ($tag ne 'input'
70                         || (ref $attr->{'type'} && $attr->{'type'}->[1] eq 'hidden')); # FIXME
71                     my($key, $val, $val_orig, $order) = @{$attr->{$a}}; #FIXME
72                     $val = TmplTokenizer::trim $val;
73                     $text{$val} = 1 if $val =~ /\S/s;
74                 }
75             }
76         }
77     }
78     # Emit all extracted strings.
79     # Don't emit pure whitespace, pure numbers, or TMPL_VAR's.
80     for my $t (keys %text) {
81         printf "%s\n", $t
82             unless $t =~ /^(?:\s|\&nbsp$TmplTokenizer::re_end_entity|$TmplTokenizer::re_tmpl_var)*$/os || $t =~ /^\d+$/;
83     }
84 }
85
86 ###############################################################################
87
88 sub usage ($) {
89     my($exitcode) = @_;
90     my $h = $exitcode? *STDERR: *STDOUT;
91     print $h <<EOF;
92 Usage: $0 [OPTIONS]
93 Extract strings from HTML file.
94
95       --debug-dump-only     Do not extract strings; but display scanned tokens
96   -f, --file=FILE           Extract from the specified FILE
97       --pedantic-warnings   Issue warnings even for detected problems which
98                             are likely to be harmless
99       --help                Display this help and exit
100 EOF
101     exit($exitcode);
102 }
103
104 ###############################################################################
105
106 sub usage_error (;$) {
107     print STDERR "$_[0]\n" if @_;
108     print STDERR "Try `$0 --help' for more information.\n";
109     exit(-1);
110 }
111
112 ###############################################################################
113
114 GetOptions(
115     'f|file=s'          => \$input,
116     'debug-dump-only'   => \$debug_dump_only_p,
117     'pedantic-warnings' => sub { $pedantic_p = 1 },
118     'help'              => sub { usage(0) },
119 ) || usage_error;
120
121 VerboseWarnings::set_application_name $0;
122 VerboseWarnings::set_input_file_name $input;
123 VerboseWarnings::set_pedantic_mode $pedantic_p;
124
125 usage_error('Missing mandatory option -f') unless defined $input;
126
127 open(INPUT, "<$input") || die "$0: $input: $!\n";
128 if ($debug_dump_only_p) {
129     debug_dump(*INPUT);
130 } else {
131     text_extract(*INPUT);
132 }
133
134 warn "This input will not work with Mozilla standards-compliant mode\n", undef
135         if TmplTokenizer::syntaxerror_p;
136
137 close INPUT;
138
139 exit(-1) if TmplTokenizer::fatal_p;