Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / solenv / bin / modules / pre2par / work.pm
blobb05940c441b23d0d4501d7eeee9297ba80c7051c
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 package pre2par::work;
22 use pre2par::exiter;
23 use pre2par::remover;
24 use pre2par::pathanalyzer;
26 ############################################
27 # pre2par working module
28 ############################################
30 ############################################
31 # procedure to split a line, that contains
32 # more than one par file lines
33 ############################################
35 sub split_line
37 my ($line, $parfile) = @_;
39 while ( $line =~ /^((?:[^"]|\"(?:[^"\\]|\\.)*\")*?\;\s+)\s*(.*)$/ )
41 my $oneline = $1;
42 $line = $2;
43 pre2par::remover::remove_leading_and_ending_whitespaces(\$oneline);
44 $oneline = $oneline . "\n";
45 push(@{$parfile}, $oneline);
47 if ( $line =~ /^\s*End\s+(\w+.*$)/i )
49 $line = $1;
50 push(@{$parfile}, "End\n\n");
54 # the last line
56 pre2par::remover::remove_leading_and_ending_whitespaces(\$line);
57 $line = $line . "\n";
58 push(@{$parfile}, $line);
60 if ( $line =~ /^\s*End\s*$/i ) { push(@{$parfile}, "\n"); }
63 ###################################################################
64 # Preprocessing the pre file to split all lines with semicolon
65 ###################################################################
67 sub preprocess_macros
69 my ($prefile) = @_;
71 my @newprefile = ();
73 for ( my $i = 0; $i <= $#{$prefile}; $i++ )
75 my $oneline = ${$prefile}[$i];
76 if ( $oneline =~ /\;\s*\w+/ )
78 split_line($oneline, \@newprefile);
80 else
82 push(@newprefile, $oneline);
86 return \@newprefile;
89 ############################################
90 # main working procedure
91 ############################################
93 sub convert
95 my ($prefile) = @_;
97 my @parfile = ();
99 my $iscodesection = 0;
100 my $ismultiliner = 0;
101 my $globalline = "";
103 # Preprocessing the pre file to split all lines with semicolon
104 $prefile = preprocess_macros($prefile);
106 for ( my $i = 0; $i <= $#{$prefile}; $i++ )
108 my $oneline = ${$prefile}[$i];
110 if ($iscodesection)
112 if ( $oneline =~ /^\s*\}\;\s*$/ )
114 $iscodesection = 0;
116 else # nothing to do for code inside a code section
118 push(@parfile, $oneline);
119 next;
123 if ( $oneline =~ /^\s*$/ ) { next; }
125 if ( $oneline =~ /^\s*Code\s+\=\s+\{/ )
127 $iscodesection = 1;
130 pre2par::remover::remove_leading_and_ending_whitespaces(\$oneline);
132 my $insertemptyline = 0;
134 if ( $oneline =~ /^\s*End\s*$/i ) { $insertemptyline = 1; }
136 # Sometimes the complete file is in one line, then the gid line has to be separated
138 if ( $oneline =~ /^\s*(\w+\s+\w+)\s+(\w+\s+\=.*$)/ ) # three words before the equal sign
140 my $gidline = $1;
141 $oneline = $2;
142 $gidline = $gidline . "\n";
144 push(@parfile, $gidline);
147 if ( $oneline =~ /\;\s*\w+/ )
149 split_line($oneline, \@parfile);
150 next;
153 # searching for lines with brackets, like Customs = { ..., which can be parted above several lines
155 if ( $oneline =~ /^\s*\w+\s+\=\s*\(.*\)\s*\;\s*$/ ) # only one line
157 if (( ! ( $oneline =~ /^\s*Assignment\d+\s*\=/ )) && ( ! ( $oneline =~ /^\s*PatchAssignment\d+\s*\=/ )))
159 $oneline =~ s/\s//g; # removing whitespaces in lists
160 $oneline =~ s/\=/\ \=\ /; # adding whitespace around equals sign
164 if ( $oneline =~ /^\s*\w+\s+\=\s*$/ )
166 $oneline =~ s/\s*$//;
167 pre2par::exiter::exit_program("Error: Illegal syntax, no line break after eqals sign allowed. Line: \"$oneline\"", "convert");
170 if (( $oneline =~ /^\s*\w+\s+\=\s*\(/ ) && (!( $oneline =~ /\)\s*\;\s*$/ ))) # several lines
172 $ismultiliner = 1;
173 $oneline =~ s/\s//g;
174 $globalline .= $oneline;
175 next; # not including yet
178 if ( $ismultiliner )
180 $oneline =~ s/\s//g;
181 $globalline .= $oneline;
183 if ( $oneline =~ /\)\s*\;\s*$/ ) { $ismultiliner = 0; }
185 if (! ( $ismultiliner ))
187 $globalline =~ s/\=/\ \=\ /; # adding whitespace around equals sign
188 $globalline .= "\n";
189 push(@parfile, $globalline);
190 $globalline = "";
193 next;
196 $oneline = $oneline . "\n";
198 $oneline =~ s/\s*\=\s*/ \= /; # nice, to have only one whitespace around equal signs
200 # Concatenate adjacent string literals:
201 while ($oneline =~
202 s/^((?:[^"]*
203 \"(?:[^\\"]|\\.)*\"
204 (?:[^"]*[^[:blank:]"][^"]*\"(?:[^\\"]|\\.)*\")*)*
205 [^"]*
206 \"(?:[^\\"]|\\.)*)
207 \"[[:blank:]]*\"
208 ((?:[^\\"]|\\.)*\")
209 /\1\2/x)
212 push(@parfile, $oneline);
214 if ($insertemptyline) { push(@parfile, "\n"); }
218 return \@parfile;
221 ############################################
222 # formatting the par file
223 ############################################
225 sub formatter
227 my ($parfile) = @_;
229 my $iscodesection = 0;
231 my $tabcounter = 0;
232 my $isinsideitem = 0;
233 my $currentitem;
235 for ( my $i = 0; $i <= $#{$parfile}; $i++ )
237 my $oneline = ${$parfile}[$i];
238 my $isitemline = 0;
240 if (! $isinsideitem )
242 for ( my $j = 0; $j <= $#pre2par::globals::allitems; $j++ )
244 if ( $oneline =~ /^\s*$pre2par::globals::allitems[$j]\s+\w+\s*$/ )
246 $currentitem = $pre2par::globals::allitems[$j];
247 $isitemline = 1;
248 $isinsideitem = 1;
249 $tabcounter = 0;
250 last;
255 if ( $isitemline )
257 next; # nothing to do
260 if ( $oneline =~ /^\s*end\s*$/i )
262 $isinsideitem = 0;
263 $tabcounter--;
266 if ( $isinsideitem )
268 $oneline = "\t" . $oneline;
269 ${$parfile}[$i] = $oneline;
274 ###################################################
275 # Returning the language file name
276 ###################################################
278 sub getlangfilename
280 return $pre2par::globals::langfilename;
283 ############################################
284 # Checking if a file exists
285 ############################################
287 sub fileexists
289 my ($langfilename) = @_;
291 my $fileexists = 0;
293 if( -f $langfilename ) { $fileexists = 1; }
295 return $fileexists;
298 ############################################
299 # Checking the existence of ulf file
300 ############################################
302 sub check_existence_of_langfiles
304 my ($langfilename) = @_;
306 my $do_localize = 0;
308 if ( fileexists($langfilename) ) { $do_localize = 1; }
310 return $do_localize;
313 ############################################
314 # Checking that the pre file has content
315 ############################################
317 sub check_content
319 my ($filecontent, $filename) = @_;
321 if ( $#{$filecontent} < 0 ) { pre2par::exiter::exit_program("Error: $filename has no content!", "check_content"); }
324 ############################################
325 # Checking content of par files.
326 # Currently only size.
327 ############################################
329 sub diff_content
331 my ($content1, $content2, $filename) = @_;
333 if ( $#{$content1} != $#{$content2} ) { pre2par::exiter::exit_program("Error: $filename was not saved correctly!", "diff_content"); }