summaryrefslogtreecommitdiffstats
path: root/lib/Locale/Po4a/Wml.pm
blob: 8c93d3bf915f2fafbad4e6491f9a617385c9748b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/perl -w

# Po4a::Wml.pm
#
# extract and translate translatable strings from a WML (web markup language) documents
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
########################################################################

=encoding UTF-8

=head1 NAME

Locale::Po4a::Wml - convert WML (web markup language) documents from/to PO files

=head1 DESCRIPTION

The po4a (PO for anything) project goal is to ease translations (and more
interestingly, the maintenance of translations) using gettext tools on
areas where they were not expected like documentation.

Locale::Po4a::Wml is a module to help the translation of WML documents into
other [human] languages. Do not mixup the WML we are speaking about here
(web markup language) and the WAP crap used on cell phones.

Please note that this module relies upon the Locale::Po4a::Xhtml
module, which also relies upon the Locale::Po4a::Xml module.  This
means that all tags for web page expressions are assumed to be written
in the XHTML syntax.

=head1 OPTIONS ACCEPTED BY THIS MODULE

NONE.

=head1 STATUS OF THIS MODULE

This module works for some simple documents, but is still young.
Currently, the biggest issue of the module is probably that it cannot
handle documents that contain non-XML inline tags such as <email
"foo@example.org">, which are often defined in the WML.  Improvements
will be added in the future releases.

=cut

package Locale::Po4a::Wml;

use 5.16.0;
use strict;
use warnings;

require Exporter;
use vars qw(@ISA @EXPORT);
@ISA    = qw(Locale::Po4a::Xhtml);
@EXPORT = qw();

use Locale::Po4a::Common;
use Locale::Po4a::Xhtml;
use File::Temp;

sub initialize {
    my $self    = shift;
    my %options = @_;

    $self->SUPER::initialize(%options);

    print "Call treat_options\n" if $self->{options}{'debug'};
    $self->treat_options;
}

sub read {
    my ( $self, $filename, $refname, $charset ) = @_;
    my $tmp_filename;
    ( undef, $tmp_filename ) = File::Temp::tempfile(
        "po4aXXXX",
        DIR    => File::Spec->tmpdir(),
        SUFFIX => ".xml",
        OPEN   => 0,
        UNLINK => 0
    ) or die wrap_msg( gettext("Cannot create a temporary XML file: %s"), $! );
    my $file;
    open FILEIN, "$filename" or die "Cannot read $filename: $!\n";
    {
        $/    = undef;
        $file = <FILEIN>;
    }
    $/ = "\n";

    # Mask perl cruft out of XML sight
    while (( $file =~ m|^(.*?)<perl>(.*?)</perl>(.*?)$|ms )
        or ( $file =~ m|^(.*?)<:(.*?):>(.*)$|ms ) )
    {
        my ( $pre, $in, $post ) = ( $1, $2, $3 );
        $in =~ s/</PO4ALT/g;
        $in =~ s/>/PO4AGT/g;
        $file = "${pre}<!--PO4ABEGINPERL${in}PO4AENDPERL-->$post";
    }

    # Mask mp4h cruft
    while ( $file =~ s|^#(.*)$|<!--PO4ASHARPBEGIN$1PO4ASHARPEND-->|m ) {
        my $line = $1;
        print STDERR "PROTECT HEADER: $line\n"
          if $self->{options}{'debug'};

        # If the wml tag has a title attribute, use a fake
        # <title> xml tag to enable the extraction
        # for translation in the xml parser.
        if ( $line =~ m/title="([^"]*)"/ ) {
            $file = "<title>$1</title>\n" . $file;
        }
    }

    # Validate define-tag tag's argument
    $file =~ s|(<define-tag\s+)([^\s>]+)|$1PO4ADUMMYATTR="$2"|g;

    # Flush the result to disk
    open OUTFILE, ">$tmp_filename";
    print OUTFILE $file;
    close INFILE;
    close OUTFILE or die "Cannot write $tmp_filename: $!\n";

    push @{ $self->{DOCXML}{infile} }, $tmp_filename;
    $self->{DOCWML}{$tmp_filename} = $filename;
    $self->Locale::Po4a::TransTractor::read( $tmp_filename, $refname, $charset );
    unlink "$tmp_filename";
}

sub parse {
    my $self = shift;

    foreach my $filename ( @{ $self->{DOCXML}{infile} } ) {
        $self->Locale::Po4a::Xml::parse_file($filename);
        my $org_filename = $self->{DOCWML}{$filename};

        # Fix the references
        foreach my $msgid ( keys %{ $self->{TT}{po_out}{po} } ) {
            $self->{TT}{po_out}{po}{$msgid}{'reference'} =~ s|$filename(:\d+)|$org_filename$1|o;
        }

        # Get the document back (undoing our WML masking)
        # FIXME: need to join the file first, and then split?
        my @doc_out;
        my $cnt = 0;
        my $title_node;
        my $title;

        foreach my $line ( @{ $self->{TT}{doc_out} } ) {
            if ( !$cnt ) {
                if ( !$title_node && $line =~ m/<title>/ ) {
                    $title_node = $line;
                } elsif ($title_node) {
                    $title_node .= $line;
                    if ( $title_node =~ m/<title>(.*?)<\/title>/ ) {
                        $title = $1;
                        $cnt   = 1;
                    }
                } else {
                    $cnt = 1;
                }
            } else {
                if ( $line =~ s/^<!--PO4ASHARPBEGIN(.*?)PO4ASHARPEND-->/#$1/mg && $title ) {
                    $line =~ s/title="[^"]*"$/title="$title"/mg;
                }
                $line =~ s/<!--PO4ABEGINPERL(.*?)PO4AENDPERL-->/<:$1:>/sg;
                $line =~ s/(<define-tag\s+)PO4ADUMMYATTR="([^"]*)"/$1$2/g;
                $line =~ s/PO4ALT/</sg;
                $line =~ s/PO4AGT/>/sg;
                push @doc_out, $line;
            }
        }

        # Do a simple left trim
        foreach my $line (@doc_out) {
            if ( $line =~ m/\s+/ ) {
                shift @doc_out;
            } else {
                last;
            }
        }

        $self->{TT}{doc_out} = \@doc_out;
    }
}

1;

=head1 AUTHORS

 Martin Quinson (mquinson#debian.org)
 Noriada Kobayashi <nori1@dolphin.c.u-tokyo.ac.jp>

=head1 COPYRIGHT AND LICENSE

 Copyright © 2005 SPI, Inc.

This program is free software; you may redistribute it and/or modify it
under the terms of GPL v2.0 or later (see the COPYING file).