File: templateparser.pl

package info (click to toggle)
jwchat 1.0%2Bdfsg-1.3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,944 kB
  • ctags: 372
  • sloc: perl: 479; xml: 462; java: 116; sh: 108; makefile: 54
file content (230 lines) | stat: -rwxr-xr-x 6,053 bytes parent folder | download | duplicates (5)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/perl -w
# BEGIN LICENSE BLOCK
###############################################################################
# Copyright (C) 2001-2003 Florian Bischof <flo@fxb.de>
#
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
###############################################################################
# END LICENSE BLOCK
#
# parses template files
#
###############################################################################

use strict;
use utf8;

my $DEBUG = 0;
my $TEMPDIR = "htdocs";
my $TEMPLATEDIR = "src";
my $DEFAULTLANG = "en";

require "scripts/JWCI18N.pm";
use File::Find;
use File::Path;
use File::Basename;
#use Encode::compat; # a no-op for Perl v5.7.1+
use Encode 'encode_utf8';
use Regexp::Common;

JWCI18N::Init();

# Tell me more about warnings
use Carp ();
$SIG{__WARN__} = \&Carp::cluck;


binmode(STDERR, ":utf8");

print "Parsing Templates...\n";

# get list with all languages
my @languages;
foreach my $lang (<po/*.po>) {
	$lang =~ s|.*/||;
	$lang =~ s|\.po$||;
	push(@languages, $lang);
}

my %templates;
my %isa_scan = ();

# get template-list
find(
     sub {
       return unless /\.(html|js|css|php|inc)$/;
			 return if /^\.#/;
			 $templates{substr($File::Find::name,length($TEMPLATEDIR))} = $File::Find::name;
		 },
     $TEMPLATEDIR
    );


foreach my $filename (keys %templates) {
	my $file = $templates{$filename};

	print STDERR "$file ... \n" if ($DEBUG);

	# update all language dictionaries
	foreach my $lang (@languages) {
		my $lh = JWCI18N->get_handle($lang) || die "What language?";

    unless (open _, $file) {
        print STDERR "Cannot open $file for reading ($!), skipping.\n" if ($DEBUG);
        next;
    }

		my $langfilename = "$filename.$lang";
		my $langfile = "$TEMPDIR/$langfilename";
		$langfile =~ /(.*)(\/|\\)[^\/\\]+$/;
		mkpath($1);

    unless (open LANGFILE, ">$langfile") {
        print STDERR "Cannot open $langfile for writing ($!), skipping.\n" if ($DEBUG);
        next;
    }
		binmode(LANGFILE, ":utf8");

    $_ = join('',<_>);
		my $lastpos = 0;
		my $translation = '';


    # pxx.to filter: <l>...</l>
		while (m!\G.*?(?=<l(\s.*?|)>(.*?)</l>)!sg) {
			my ( $vars, $str ) = ( $1, $2 );
			$str =~ s/\\'/\'/g;   # '
      my $pos = pos();

      $translation .= substr($_, $lastpos, $pos-$lastpos);
      $translation .= '<&|/l&>'.$lh->maketext($str);

      $lastpos = $pos + length($str.$vars)+7;
      pos() = $lastpos;

    }
    $translation .= substr($_, $lastpos);


    # Localization function: loc(...)
    $_ = $translation;
    $translation = '';
    $lastpos = 0;
		pos() = 0;
   # while (m/\G.*?\bloc$RE{balanced}{-parens=>'()'}{-keep}/sg) {
    while (m/\G.*?loc$RE{balanced}{-parens=>'()'}{-keep}/sg) {
        my $match = $1;
	my $pos = pos() - length($match) - 3;

        #$line += ( () = ( $& =~ /\n/g ) );    # cryptocontext!

        my ( $vars, $str );
        if ( $match =~
                /\(\s*($RE{delimited}{-delim=>q{'"}}{-keep})(.*?)\s*\)$/ ) {
	  #print STDERR $match;
            $str = substr( $1, 1, -1 );       # $str comes before $vars now
            $vars = $9;
        }
        else {
            next;
        }

        $vars =~ s/[\n\r]//g;
        $str  =~ s/\\'/\'/g;


        $vars =~ s/^,//;
	my @vars = split(/,/, $vars);
	for (my $i = 0; $i <= $#vars; $i++) {
	  $vars[$i] =~ s/^\s+//;
	  $vars[$i] = '"+'.$vars[$i].'+"';
	}
	#print STDERR "vars: *".join("*", @vars)."...";
	my $trans = '"'.$lh->maketext($str, @vars).'"';
	$trans =~ s/\+\"\"$//;

	$translation .= substr($_, $lastpos, $pos-$lastpos);
	$translation .= $trans;
	
	$lastpos = $pos + length($match)+3;

        #print STDERR "$filename: GOT $str ($vars) -> $trans\n";

	pos() = $lastpos; 
      }


		
    # sort <&|/sort&> regions
    while ($translation =~ m/<&\|\/sort&>(.+)<\/sort&>/sg) {
			my $region = join("\n", sort { 
				$a =~ /<&\|\/l&>(.*)/; my $aa = $1;
				$b =~ /<&\|\/l&>(.*)/; my $bb = $1;
				$aa cmp $bb;
			} split(/\n/,$1));

			#print "\nGOT THIS".('x'x30).'\n';
			$translation =~ s/<&\|\/sort&>(.+)<\/sort&>/$region/s;
		}

    $translation .= substr($_, $lastpos);

    # remove temporary language-marks
    $translation =~ s/<&\|\/l&>//g;

    # And now: Crunch it...
    # disabled by now ... should be comand line option
    if (0) {
      $translation =~ s/^\s+//mg;           # remove whitespaces
      $translation =~ s/^\/\/[^\-].*$//mg;       # remove // comments
      $translation =~ s/([{}\);])[ \t]*\/\/.*$/$1/mg;   # remove ; // comments
      $translation =~ s/^\/\*.*\*\/$//mg;       # remove /* comments */
    }

    print LANGFILE $translation;
    close(LANGFILE);

    # [zeank] 2005-09-01: create extra sets of each language
    my $dir = './htdocs.'.$lang;
    mkdir ($dir) or die "Can't create $dir: $!" unless (-d $dir);
    $langfile = "$dir/$filename";
    $langfile =~ /(.*)(\/|\\)[^\/\\]+$/;
    mkpath($1);
    open(LANGFILE,">$langfile") or die "Can't open $dir/$langfile: $!";
    binmode(LANGFILE, ":utf8");
    print LANGFILE $translation;
    close(LANGFILE);

    close(_);

	if($lang eq $DEFAULTLANG) { # create default files
		my ($name,$path,$suffix) = fileparse($filename,".html",".js");

		unless (open LANGFILE, ">$TEMPDIR/$filename$suffix") {
			print STDERR "can't open $TEMPDIR/$filename.$suffix for writing ... skipping\n";
			next;
		}
		print LANGFILE $translation;
		close LANGFILE;
	}

  }
}

print STDERR "done.\n";


1;