-
Notifications
You must be signed in to change notification settings - Fork 38
/
Extract.pm
174 lines (133 loc) · 4.68 KB
/
Extract.pm
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
# See bottom of file for license and copyright information
=begin TML
---+ package Foswiki::I18N::Extract
Support translatable strings extraction from Foswiki topics and templates.
Depends on Locale::Maketext::Extract (part of CPAN::Locale::Maketext::Lexicon).
=cut
package Foswiki::I18N::Extract;
use v5.14;
use Assert;
use Try::Tiny;
use Foswiki::Class qw(app);
extends qw(Foswiki::Object);
our $initError;
BEGIN {
if ( $Foswiki::cfg{UseLocale} ) {
require locale;
import locale();
}
require Locale::Maketext::Extract;
if ($@) {
$initError = $@;
}
else {
@Foswiki::I18N::Extract::ISA = ('Locale::Maketext::Extract');
}
}
has extractor => (
is => 'rw',
lazy => 1,
default => sub {
return new Locale::Maketext::Extract;
},
handles => [qw(extract_file compile write_po)],
);
##########################################################
=begin TML
---++ ClassMethod new ( app => $app ) -> $extract
Constructor. Creates a fresh new Extract object. A $app object, instance of
the Foswiki class, is optional: if it's available, it'll be used for printing
warnings.
=cut
around BUILDARGS => sub {
my $orig = shift;
try {
Foswiki::load_package('Locale::Maketext::Extract');
}
catch {
$initError = ref($_) ? $_->stringify : $_;
};
return $orig->(@_);
};
sub BUILD {
my $this = shift;
if ( defined $initError ) {
$this->app->logger->log( 'warning', $initError );
}
}
=begin TML
---++ ObjectMethod extract ( $file , $text )
Extract the strings from =$text=,m using =$file= as the name of the current
file being read (for comments in PO file, for example). Overrides the base
class method but calls it so the base behavior is preserved.
As in base class, extracted strings are just stored in the =$this='s internal
table for further use (e.g. creating/updating a PO file). Nothing is returned.
=cut
sub extract {
my $this = shift;
my $file = shift;
local $_ = shift;
# do existing extraction
$this->extractor->extract( $file, $_ ) unless ( $file =~ m/\.(txt|tmpl)$/ );
my $line;
my $doublequoted = '"(\\\"|[^"])*"';
# Foswiki's %MAKETEXT{...}% into topics and templates :
$line = 1;
pos($_) = 0;
my @_lines = split( /\n/, $_ );
foreach (@_lines) {
while (m/%MAKETEXT\{\s*(string=)?($doublequoted)/gm) {
my $str = substr( $2, 1, -1 );
$str =~ s/\\"/"/g;
$this->add_entry( $str, [ $file, $line, '' ] );
}
$line++;
}
# Foswiki's %MAKETEXT{...}% inside a search format would look like this:
# %SEARCH{... format=" ... $percntMAKETEXT{\"...\" args=\"\"}$percent ..." ...}%
#
# XXX: the regex down there matches a sequence formed be an escaped double
# quote (\"), followed by characters that are not doublequotes OR
# double-escaped doublequotes (\\\"), and terminated with another escaped
# double-quote.
#
# SMELL: although here we can extract properly the string, %SEARCH{...}%
# won't convert (\\\") inside format into (") as we do here. So it's best
# to avoid trying to put doublequotes inside a MAKETEXT that is inside
# a %SEARCH{...}% format.
$line = 1;
pos($_) = 0;
@_lines = split( /\n/, $_ );
foreach (@_lines) {
while (m/\$perce?ntMAKETEXT\{\s*(string=)?(\\"(\\\\\\"|[^"])*\\")/gm) {
# remove the enclosing [\"]'s:
my $str = substr( $2, 2, -2 );
# remove escaped stuff:
$str =~ s/\\\\"/"/g;
$str =~ s/\\"/"/g;
# collect the string:
$this->add_entry( $str, [ $file, $line, '' ] );
}
$line++;
}
}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
Additional copyrights apply to some or all of the code in this
file as follows:
Copyright (C) 2005-2007 TWiki Contributors. All Rights Reserved.
TWiki Contributors are listed in the AUTHORS file in the root
of this distribution. NOTE: Please extend that file, not this notice.
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. For
more details read LICENSE in the root of this distribution.
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.
As per the GPL, removal of this notice is prohibited.