Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/docs/gcompris2spip.pl
blob: 114a5f510ba6dd82106b42238ab3febd3a08e595 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/perl
use strict;
use Data::Dumper;

#
#  The translation status from .po files
#
#  Copyright (C) 2004-2005 Bruno Coudoin.
#
#  gcompris2spip is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License 
#  version 2 published by the Free Software Foundation.
#
#  Intltool 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#  As a special exception to the GNU General Public License, if you
#  distribute this file as part of a program that contains a
#  configuration script generated by Autoconf, you may include it under
#  the same distribution terms that you use for the rest of that program.
#
#  Authors: Bruno Coudoin <bruno.coudoin@free.fr>
#

# -----------------------------------------------------------------------------------------
# USAGE:
# This tool requires no parameters. It must be run withing the gcompris/docs directory
# It will extract xml menus from gcompris/boards and create a spip XML version for them
#------------------------------------------------------------------------------------------


#-------------------------------------------------------------------------------
# Define some constants related to spip rubrique and section organisation
# We do not detect which locale sections are. They are hardcoded here.
my %sections = (
		"am", 0,
		"ar", 0,
		"az", 0,
		"bg", 0,
		"ca", 0,
		"cs", 0,
		"da", 37,
		"de", 54,
		"el", 0,
		"en", 2,
		"en_CA", 0,
		"en_GB", 0,
		"es", 40,
		"et", 0,
		"fi", 36,
		"fr", 1,
		"ga", 0,
		"gu", 0,
		"he", 0,
		"hi", 0,
		"hr", 0,
		"hu", 0,
		"it", 60,
		"lt", 0,
		"mk", 0,
		"ml", 0,
		"ms", 0,
		"nl", 92,
		"nb", 107,
		"nn", 38,
		"ne", 0,
		"pa", 0,
		"pl", 0,
		"pt", 28,
		"pt_BR", 0,
		"ro", 0,
		"ru", 0,
		"rw", 0,
		"sk", 0,
		"sl", 0,
		"sq", 0,
		"sr", 0,
		'sr@Latn', 0,
		"sv", 0,
		"tr", 0,
		"wa", 0,
		"zh_CN", 0,
	       );

my %rubriques = (
		 "am", 0,
		 "ar", 0,
		 "az", 0,
		 "bg", 0,
		 "ca", 0,
		 "cs", 0,
		 "da", 61,
		 "de", 64,
		 "el", 0,
		 "en", 12,
		 "en_CA", 0,
		 "en_GB", 0,
		 "es", 47,
		 "et", 0,
		 "fi", 73,
		 "fr", 6,
		 "ga", 0,
		 "gu", 0,
		 "he", 0,
		 "hi", 0,
		 "hr", 0,
		 "hu", 0,
		 "it", 66,
		 "lt", 0,
		 "mk", 0,
		 "ml", 0,
		 "ms", 0,
		 "nl", 93,
		 "nb", 108,
		 "nn", 69,
		 "ne", 0,
		 "pa", 0,
		 "pl", 0,
		 "pt", 56,
		 "pt_BR", 0,
		 "ro", 0,
		 "ru", 0,
		 "rw", 0,
		 "sk", 0,
		 "sl", 0,
		 "sq", 0,
		 "sr", 0,
		 'sr@Latn', 0,
		 "sv", 0,
		 "tr", 0,
		 "wa", 0,
		 "zh_CN", 0,
		);


my %rubriques_all = (
		 "am", 0,
		 "ar", 0,
		 "az", 0,
		 "bg", 0,
		 "ca", 0,
		 "cs", 0,
		 "da", 62,
		 "de", 65,
		 "el", 0,
		 "en", 68,
		 "en_CA", 0,
		 "en_GB", 0,
		 "es", 71,
		 "et", 0,
		 "fi", 74,
		 "fr", 63,
		 "ga", 0,
		 "gu", 0,
		 "he", 0,
		 "hi", 0,
		 "hr", 0,
		 "hu", 0,
		 "it", 67,
		 "lt", 0,
		 "mk", 0,
		 "ml", 0,
		 "ms", 0,
		 "nl", 94,
		 "nb", 109,
		 "nn", 70,
		 "ne", 0,
		 "pa", 0,
		 "pl", 0,
		 "pt", 72,
		 "pt_BR", 0,
		 "ro", 0,
		 "ru", 0,
		 "rw", 0,
		 "sk", 0,
		 "sl", 0,
		 "sq", 0,
		 "sr", 0,
		 'sr@Latn', 0,
		 "sv", 0,
		 "tr", 0,
		 "wa", 0,
		 "zh_CN", 0,
		);
#-------------------------------------------------------------------------------
sub spip_cleanup {
  my $output = shift;

  # Spip doesn't like shortucted tags like <ps/>. But xsltproc does this by default
  # and I dod not find a way to avoid that.
  # I put __REMOVEME__ in empty tags and I remove it there
  $output =~ s/__REMOVEME__//g;
  $output =~ s/__NBSP__/&nbsp;/g;

  # We need to html backquote html tags for spip
  # I put in uppercase all html tags
  my @html_tag = qw/HTML IMG A BR DIV P/;
  foreach my $tag (@html_tag) {
    $output =~ s/<$tag/&lt;$tag/g;
    $output =~ s/<\/$tag/&lt;\/$tag/g;
  }

  # Fix the tag <lien:auteur> because wa cannot use it in xslt name space is not defined
  $output =~ s/lien_auteur/lien:auteur/g;

  # Remove eMail adresses
  $output =~ s/[a-zA-Z0-9\_\-\.\(]+@[a-zA-Z0-9\_\-\.\)]+//g;

  return $output;
}


#-------------------------------------------------------------------------------
# Initialisation

my ($sec, $min, $hours, $day, $month, $year) = (localtime)[0,1,2,3,4,5];
my $date = "".($year+1900)."-".($month+1)."-"."$day $hours:$min:$sec";

my $gcompris_root_dir = "..";
my $boards_dir        = "$gcompris_root_dir/boards";
my $ALL_LINGUAS_STR   = `grep "ALL_LINGUAS=" $gcompris_root_dir/configure.in | cut -d= -f2`;
$ALL_LINGUAS_STR      =~ s/\"//g;
my @ALL_LINGUAS       = split(' ', $ALL_LINGUAS_STR);
push @ALL_LINGUAS, "en";	# Add english, it's not in the po list
# Debug
#@ALL_LINGUAS = qw/fr en/;

# Check we have all we need for each defined lang
my $error = 0;
foreach my $lang (@ALL_LINGUAS) {
  if(! defined $sections{$lang} ||
     ! defined $rubriques{$lang} ||
     ! defined $rubriques_all{$lang} ) {
    print "ERROR: Edit this script and add a lang definition for lang '$lang' in \%sections \%rubriques \%rubriques_all\n";
    $error = 1;
  }
}

exit 1 if $error;

my $tmp_file    = "temp_file.spip";
my $output_file = "all_article.spip";

# Erase previous output
unlink $tmp_file;
unlink $output_file;

my $first_article = 999;
my $article_id    = $first_article;



# First, Get all the boards description files
opendir DIR, $boards_dir or die "cannot open dir $boards_dir: $!";
my @files = grep { $_ =~ /\.xml$/} readdir DIR;
closedir DIR;

#-------------------------------------------------------------------------------
# Menu creation

my @sections;

my $all_boards_file = "all_boards.xml";
# Erase previous output
unlink $all_boards_file;

open(OUTPUT, ">>$all_boards_file");

print OUTPUT "<GComprisBoards>\n";

foreach my $board (@files) {

  print "Processing $board\n";
  my $file = "$boards_dir/$board";
  open(BOARD, "<$file");

  my $board_content;
  my $board_section;
  my $board_name;
  my $board_type;
  read(BOARD, $board_content, 65535);

  $board_section = ($board_content =~ /section=\"([a-zA-Z\/\.]+)\"/)[0];
  $board_name    = ($board_content =~ /name=\"([a-zA-Z\/\.\:]+)\"/)[0];
  $board_type    = ($board_content =~ /type=\"([a-zA-Z\/\.\:]+)\"/)[0];

  if($board_type eq "menu" and $board_name ne "root") {
    print "   This is a menu\n";
    my @section;
    if( $board_section ne "/") {
      push(@section, "$board_section/$board_name");
    } else {
      push(@section, "/$board_name");
    }
    push(@section, $board_name);

    print "   Section=$board_section\n";
    print "      Type=$board_type\n";
    print "      Name=$board_name\n";

    push(@sections, \@section);
  }

  if($board_content =~ /difficulty=\"0\"/) {
    goto done;
  }
  # Some filtering
  $board_content =~ s/<\?xml version="1.0" encoding="UTF-8"\?>//g;

  print OUTPUT $board_content;

 done:
  close(BOARD);
}
print OUTPUT "\n</GComprisBoards>\n";

close (OUTPUT);

# Loop over each menu entry
foreach my $onesection (@sections) {
  my @deref = @{$onesection};
  my $section = $deref[0];
  my $name    = $deref[1];
  print "\nProcessing section=$section name=$name\nLang:";

  # The first article is the reference article
  my $traduction_id = $article_id;

  # 2nd loop over each language
  foreach my $lang (@ALL_LINGUAS) {

    my $xslfile = "spip_menuboard.xsl";

    $article_id++;

    print "$lang ";

    my $output = `xsltproc --stringparam language $lang --stringparam date "${date}" --stringparam article_id ${article_id} --stringparam rubrique_id $rubriques{$lang} --stringparam section $section --stringparam name $name --stringparam section_id $sections{$lang} --stringparam traduction_id ${traduction_id} $xslfile $all_boards_file`;

    #print "xsltproc --stringparam language $lang --stringparam date \"${date}\" --stringparam article_id ${article_id} --stringparam rubrique_id $rubriques{$lang} --stringparam section $section --stringparam name $name --stringparam section_id $sections{$lang} --stringparam traduction_id ${traduction_id} $xslfile $all_boards_file\n";

    if ($?>>8) {
      print "#\n";
      print "#\n";
      print "#\n";
      print "An error as been encountered in xslt processing: processing is left uncomplete\n";
      print "ERROR on section $section lang $lang#\n";
      print "#\n";
      print "#\n";
      exit 1;

    } else {

      # Make some cleanup where needed
      # ------------------------------

      $output = spip_cleanup($output);

      open(OUTPUT, ">>$tmp_file")
	or die "Can't open: $!";

      print OUTPUT $output . "\n";

      close (OUTPUT);
    }
  }
}

#-------------------------------------------------------------------------------
# Article creation

# We record in a hash the article number for each lang/board name
my %articles;

foreach my $board (@files) {

  print "\nProcessing $board\nLang:";

  # Skip some boards
  if($board eq "administration.xml") {
    print " (administration is skipped)";
    next;
  }

  # The first article is the reference article
  my $traduction_id = $article_id;

  # 2nd loop over each language
  foreach my $lang (@ALL_LINGUAS) {

    my $file = "$boards_dir/$board";
    my $xslfile = "spip_oneboard.xsl";

    $article_id++;

    print "$lang ";

    # Hide the article
#    $rubriques{$lang} = 0;

    my $output = `xsltproc --stringparam language $lang --stringparam date "${date}" --stringparam article_id ${article_id} --stringparam rubrique_id $rubriques_all{$lang} --stringparam section_id $sections{$lang} --stringparam traduction_id ${traduction_id} $xslfile $file`;

    if ($?>>8) {
      print "#\n";
      print "#\n";
      print "#\n";
      print "An error as been encountered in xslt processing: processing is left uncomplete\n";
      print "ERROR on file $file lang $lang#\n";
      print "#\n";
      print "#\n";
      exit 1;

    } else {

      # Make some cleanup where needed
      # ------------------------------

      $output = spip_cleanup($output);

      open(OUTPUT, ">>$tmp_file")
	or die "Can't open: $!";

      print OUTPUT $output . "\n";

      close (OUTPUT);

      # Record the article
      $articles{"$lang:$board"} = $article_id;
    }
  }
}

#-------------------------------------------------------------------------------
# Replace article link as created in the menu by the real article number
# as we created it.
open(FILEREAD, "< $tmp_file");
open(FILEWRITE, "> $output_file");

my $line = 0;
while (<FILEREAD>){
  my $line = $_;
  m/href=\"(\w+:\w+\.xml)/g;
  if(defined($articles{"$1"})) {
    print "Found article " . $articles{"$1"} . "\n";
    my $article = "article.php3?id_article=$articles{$1}";
    my $r = s/(^.*)(href=\")(\w+:\w+\.xml)(.*)/$1$2$article$4/g;
  }
  print FILEWRITE;
}
close FILEWRITE;
close FILEREAD;

#-------------------------------------------------------------------------------
# Summary
print "\n\nCreated " . ($article_id - $first_article) . " Articles in $output_file\n";
print "Insert the content of this file in the SPIP dump.xml file.\n";
print "If screenshots were already in, remove them first\n";

# Cleanup
unlink $tmp_file;

exit 0;