Subversion Repositories wimsdev

Rev

Rev 7228 | Rev 7387 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
23 reyssat 1
#!/usr/bin/perl
2
 
3
use strict "vars";
4
use strict "subs";
5
use locale;
6
use warnings;
7
use Text::Balanced qw (extract_bracketed extract_tagged);
8
 
9
$/ = undef; # slurp
10
#
11
###############################################################################
12
#
13
#    This file is part of latex2wims
14
#
15
#    latex2wims is free software; you can redistribute it and/or modify
16
#    it under the terms of the GNU General Public License as published by
17
#    the Free Software Foundation; either version 2 of the License, or
18
#    (at your option) any later version.
19
#
20
#    latex2wims is distributed in the hope that it will be useful,
21
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
22
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
#    GNU General Public License for more details.
24
#
25
#    You should have received a copy of the GNU General Public License
26
#    along with latex2wims; if not, write to the Free Software
27
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
#
29
###############################################################################
30
#
7232 bpr 31
my ($FILE, $MACRO, $EMBED, $NUMERO, $doc_DIR, $DIR, $author, $email, $worksheet, $SHEET,$ABOUT, $SUBDIR) ;
23 reyssat 32
my $verbose = 0;
33
my $depth = 2 ;
34
my $INDEX = 0 ;
734 bpr 35
my $TOOLTIP = 0 ;
23 reyssat 36
my $STYLE = '' ;
37
my $OPTION = '' ;
5696 bpr 38
my $tooltip_prompt = '<img src="gifs/picto.gif" alt="picto" />' ;
6614 bpr 39
$tooltip_prompt='';
743 bpr 40
my $linkout='' ;
23 reyssat 41
$worksheet= '';
42
$SHEET = '' ;
43
$DIR = '';
44
$doc_DIR = '';
7232 bpr 45
$SUBDIR='1';
4693 bpr 46
my @SECTIONS = qw(document part chapter section subsection subsubsection);
4850 bpr 47
#my @SECTIONS = ( document part entete frame subsection subsubsection );
23 reyssat 48
#TODO biblio dans un fichier séparé si on a rencontré \begin{thebibliography} Non,
49
# on n'a qu'a mettre cet environnement de type link
50
#$doc_DIR=$ENV{'w_docdir'}; 
51
#$DIR=$ENV{'w_dir'};
52
#$DIR =~ s/ +//; 
53
push (@ARGV,split(' ', $ENV{'wims_exec_parm'})) if ($ENV{'wims_exec_parm'}) ;
54
 
55
while ($_ = shift (@ARGV))
56
{
57
  last if (!/^--/);
58
     if (/^--style=(.*)$/) { $STYLE   = $1; }  
59
  elsif (/^--macro=(.*)$/) { $MACRO   = $1; }
60
  elsif (/^--docdir=(.*)$/){ $doc_DIR = $1; }
61
  elsif (/^--dir=(.*)$/)   { $DIR     = $1; }
7232 bpr 62
  elsif (/^--subdir=(.*)$/){ $SUBDIR  = $1; }
23 reyssat 63
  elsif (/^--embed=(.*)$/) { $EMBED   = $1; }
64
  elsif (/^--verbose$/)    { $verbose = 1; }
65
  elsif (/^--author=(.*)$/){ $author  = $1; }
66
  elsif (/^--email=(.*)$/) { $email   = $1; }
67
  elsif (/^--worksheet=(.*)$/) { $worksheet   = $1; }
743 bpr 68
  elsif (/^--tooltip_prompt=(.*)$/) { $tooltip_prompt = $1; }
4627 bpr 69
  elsif (/^--linkout=(.*)$/) { $linkout = $1; }
70
  elsif (/^--cut=(.*)$/) { @SECTIONS = split(',',$1) ;}
23 reyssat 71
  elsif (/^--help$/) {
72
   usage(); # includes --help !
73
    exit 1;
74
  }
75
} ;
76
 
77
$FILE = $_;
78
 
79
$DIR = $DIR . '/' if ($DIR) ;
6858 bpr 80
$doc_DIR = $doc_DIR . '/' if ($doc_DIR) ;
81
 
7232 bpr 82
my $BASE = $doc_DIR . "doc/$SUBDIR/src";
83
my $BASE0= $doc_DIR . "doc/$SUBDIR";
84
 
6156 bpr 85
my $LOAD = '\reload{<img src="gifs/doc/etoile.gif" alt="rechargez" style="width:20px;height:20px;" />}';
6612 bpr 86
my $FLECHE = '&#8594;';
743 bpr 87
$linkout = "\\doc{module=$linkout}" . $FLECHE if ($linkout) ;
88
 
23 reyssat 89
##################################
90
##Initialisation
91
#si je rajoute les listes : type=fold : signifierait que les item sont en fold [demande
92
#d'avoir des titres, ca serait du type description en latex
93
#pas de titre ? deb fin <ul ... >  </ul>
94
#type = 
95
#hash contenant les caractéristiques des environnements latex, voir \environmentwims
96
my %hash_environ = (
97
  titre  => {},
98
  style  => {},
99
  type   => {},
100
  deb    => {},
101
  fin    => {},
102
  parm   => {},
103
  origin => {},
734 bpr 104
  list   => {},
105
  tabular => {},
23 reyssat 106
);
107
 
108
 
109
#hash contenant les commandes venant de \defwims, \def & co ou par defaut
110
my %hash_command = (
111
  cnt_arg    => {},
112
  definition => {},
113
  origin     => {},
114
);
115
my @liste_env_list = ('itemize', 'description', 'enumerate', 'trivlist') ;
116
  $hash_environ{type}{'description_item'} = 'fold' ;
117
  $hash_environ{titre}{'description_item'} = ' ' ;
118
 
734 bpr 119
my @liste_env_tabular = ('tabular') ;
120
 
121
my @liste_env_spec = ('equation', 'multline', 'latexonly',
23 reyssat 122
  'pmatrix','smallmatrix', 'eqnarray', 'array', 'algorithmic', 'algorithm', 'align',
123
  'thebibliography', 'pspicture', 'picture', 'cases', 'gather',
6003 bpr 124
  'displaymath', 'math', 'center', 'minipage', 'lstlisting', 'columns', 'column');
23 reyssat 125
 
126
my @liste_com_spec = ('paragraph', 'href', 'url', 'exercise', 'doc') ; #je ne m'en sers pas encore 
127
 
128
#commandes par défaut : sont écrasés par un \def ou un \defwims 
129
 
130
#$hash_command{cnt_arg}{text} = 1 ;
131
#$hash_command{definition}{text} = '\) #1 \(' ;
132
#$hash_command{origin}{text} = 'defaut' ;
133
 
134
$hash_command{cnt_arg}{paragraph} = 1 ;
135
$hash_command{definition}{paragraph} = '<p class="paragraph"> #1 </p>' ;
136
$hash_command{origin}{paragraph} = 'defaut' ;
137
 
138
$hash_command{cnt_arg}{href} = 2 ;
5663 bpr 139
$hash_command{definition}{href} = '<a href="http:#1" target="wims_external">#2</a>';
23 reyssat 140
$hash_command{origin}{href} = 'defaut' ;
141
 
142
$hash_command{cnt_arg}{url} = 1 ;
5663 bpr 143
$hash_command{definition}{url} = '<a href="http:#1" target="wims_external">#1</a>';
23 reyssat 144
$hash_command{origin}{url} = 'defaut' ;
145
 
146
$hash_command{cnt_arg}{exercise} = 2 ;
147
$hash_command{definition}{exercise} = "\\exercise\{\#1\}\{\#2\}";
148
$hash_command{origin}{exercise} = 'defwims' ;
149
 
150
$hash_command{cnt_arg}{doc} = 2 ;
151
$hash_command{definition}{doc} = '\doc{#1&cmd=new}{#2}';
152
$hash_command{origin}{doc} = 'defwims' ;
153
 
154
#a un label est associé son bloc [nom de fichier]
155
my %hash_bloc = (
156
  fichier  => {},
157
  titre    => {},
158
);
159
 
160
my %hash_index = (
161
  page    => {},
162
);
163
 
164
#type sert à repérer les fichiers embed ou fold. 
165
my %hash = (
166
  text   => {},
167
  prev   => {},
168
  next   => {},
169
  upbl   => {},
170
  titb   => {},
171
  tittoc => {},
172
  keyw   => {},
173
  datm   => {},
174
  label  => {},
175
  toc    => {},
176
  chemin => {},
177
  niveau => {},
178
  type   => {},
179
  author => {},
180
  title => {},
181
  email => {},
734 bpr 182
  toctip => {},
23 reyssat 183
);
184
 
185
my %hash_toc = ();
186
 
187
my %prefixe = ( fold => 'F_' , link => 'L_' );
4627 bpr 188
 
23 reyssat 189
my %hash_secinv;
190
for (my $i = 0; $i <= $#SECTIONS; $i++) { $hash_secinv{$SECTIONS[$i]} = $i; }
191
 
192
my (%errmsg); # empèche le ré-affichage d'un même warning.
193
$SIG{__WARN__} = sub { my ($x) = @_;
194
  return if $errmsg{$x};
195
  $errmsg{$x} = 1;
196
  print STDERR "### $x";
197
};
198
 
199
system("mkdir -p $BASE0") if (!$ENV{'wims_exec_parm'});
200
system("mkdir -p $BASE") if (!$ENV{'wims_exec_parm'});
201
 
202
$hash{niveau}{'main'} = 0;
203
#pour algorithmic
204
#TODO récupérer les informations dans le fichier de l'utilisateur
205
 
206
my %hash_algo = (
207
  titre => {},
208
  apres => {},
209
  avant => {},
210
  suite => {}
211
) ;
212
my $algo_noend = 0 ;
213
my $voca = %{$hash_algo{titre}} ;
214
my @liste_voca = ('FOR', 'IF','WHILE','REPEAT','ELSE','ELSIF','ENDIF','DO',
215
 'ENDWHILE', 'REQUIRE','ENSURE','ENDFOR','STATE','UNTIL','THEN', 'RETURN') ;
216
 
217
$hash_command{definition}{algorithmicrequire}='<b>Require</b>';
218
$hash_command{origin}{algorithmicrequire}='defaut';
219
$hash_command{cnt_arg}{algorithmicrequire}= 0 ;
220
$hash_command{definition}{algorithmicensure}='<b>Ensure</b>';
221
$hash_command{definition}{algorithmicend}='<b>end</b>';
222
$hash_command{definition}{algorithmicif}='<b>if</b>';
223
$hash_command{definition}{algorithmicthen}='<b>then</b>';
224
$hash_command{definition}{algorithmicelse}='<b>else</b>';
225
$hash_command{definition}{algorithmicelsif}="<b>$hash_command{definition}{algorithmicelse} $hash_command{definition}{algorithmicif}</b>";
226
$hash_command{definition}{algorithmicendif}="<b>$hash_command{definition}{algorithmicend} $hash_command{definition}{algorithmicif}</b>";
227
$hash_command{definition}{algorithmicfor}='<b>for</b>';
228
$hash_command{definition}{algorithmicforall}='<b>for all</b>';
229
$hash_command{definition}{algorithmicdo}='<b>do</b>';
230
$hash_command{definition}{algorithmicendfor}="<b>$hash_command{definition}{algorithmicend} $hash_command{definition}{algorithmicfor}</b>";
231
$hash_command{definition}{algorithmicwhile}='<b>while</b>';
232
$hash_command{definition}{algorithmicendwhile}="<b>$hash_command{definition}{algorithmicend} $hash_command{definition}{algorithmicwhile}</b>";
233
$hash_command{definition}{algorithmicloop}='<b>loop</b>';
234
$hash_command{definition}{algorithmicendloop}="<b>$hash_command{definition}{algorithmicend} $hash_command{definition}{algorithmicloop}</b";
235
$hash_command{definition}{algorithmicrepeat}='<b>repeat</b>';
236
$hash_command{definition}{algorithmicuntil}='<b>until</b>';
237
$hash_command{definition}{algorithmicprint}='<b>print</b>';
238
$hash_command{definition}{algorithmicreturn}='<b>return</b>';
239
 
240
 
241
for my $cmd (@liste_voca) { $hash_command{origin}{"algorithmic\L$cmd\E"} = 'defaut' }
242
 
243
for my $cmd ('FOR','IF','WHILE','REPEAT','ELSE','ELSIF') {
244
          $hash_algo{apres}{$cmd} = 1 ;
245
} ;
246
for my $cmd ('IF') { $hash_algo{suite}{$cmd} = 'THEN' ;} ;
247
for my $cmd ('FOR','WHILE') { $hash_algo{suite}{$cmd} = 'DO' ;} ;
248
for my $cmd ('ENDFOR','ENDIF', 'ENDWHILE','UNTIL','ELSE','ELSIF') {
249
          $hash_algo{avant}{$cmd} = -1 ;
250
} ;
251
for my $cmd ('FOR','IF','WHILE','REQUIRE','ENSURE', 'STATE','UNTIL') {
252
          $hash_algo{avant}{$cmd} = 0 ;
253
} ;
254
for my $cmd ('ENDFOR','ENDIF','ENDWHILE','REQUIRE','ENSURE', 'STATE','UNTIL') {
255
          $hash_algo{apres}{$cmd} = 0 ;
256
}
257
 
258
 
259
# lit les fichiers wims.sty  puis les fichers .tex. pour éviter : un seul fichier tex, le premier ? 
260
 
261
my $TEXT = Init($FILE, \%hash_environ, \%hash_command, \%hash, \%hash_algo);
262
out1 ('sheet', $SHEET) ;
263
$ABOUT = $hash{about}{main} ;
264
out1 ('about.phtml', (($ABOUT)? $ABOUT :'') . '
265
<p>
6112 bpr 266
Ce document a été créé par Latex2wims.</p>
23 reyssat 267
 
1692 bpr 268
!changeto docu/about.phtml') ;
23 reyssat 269
if (!($hash{author}{main})) {
270
  warn " ATTENTION : Vous devez mettre un nom d'auteur \\author{xxx}" ;
271
  $hash{author}{main}=$author } ;
272
if (!($hash{title}{main})) {
273
  warn " ATTENTION : Vous devez mettre un titre : \\title{xx} " ;
274
  $hash{title}{main} = '??' } ;
275
if (!($hash{email}{main})) {
276
  warn " ATTENTION : Vous devez mettre un email \\email{xxx}" ;
277
  $hash{email}{main}=$email } ;
278
 
279
 
280
# PASSE 2: ferme les \section & co
281
my $SEC_MIN_GLOBAL = 10; # = \infty
282
#$SEC_MIN_GLOBAL = 3 ; 
283
my @cnt = (0) x ($#SECTIONS + 1);
284
my ($secpattern) = join('|', @SECTIONS);
285
$TEXT =~ s/\\begin\s*{($secpattern)\s*}/cnt_section($1,\@cnt)/eg;
4627 bpr 286
$TEXT =~ s/\[fragile\]//g;
23 reyssat 287
$TEXT =~ s/\\end\s*{\s*($secpattern)\s*}/<\/$1>/g;
3572 bpr 288
$TEXT =~ s/\\wimsentre{($secpattern)}/\\wimsentre$1/g;
23 reyssat 289
$TEXT =~ s/\\(wimsentre)?($secpattern)\b\*?/open_close($2,\@cnt,$1)/eg;
290
$TEXT =~ s|</document>.*||s;
291
$TEXT =~ s|.*<document>||s;
292
if($SEC_MIN_GLOBAL == 10) {$SEC_MIN_GLOBAL = 0} ;
293
my ($NIVEAU, $NIVEAU_max) = ($SEC_MIN_GLOBAL, $SEC_MIN_GLOBAL + $depth - 1);
294
 
295
 
296
 
297
# PASSE 3: crée les blocs venant des sections et co
298
#  et renvoie une partie de la table des matières
299
my $toc = analyse_texte ($TEXT, \%hash, 'main', $NIVEAU, $NIVEAU_max, '');
300
# PASSE 4: création de tous les blocs ( environnements de type fold ou link)
301
{
302
  my ($ref, $ref_env)  = (\%hash, \%hash_environ);
303
  while (my ($Id, $TEXT) = each (%{$hash{text}})) {
304
    $TEXT = TraiteText ($TEXT, $ref, $ref_env, $Id);
305
#TODO non testé l'utilisation de prev etc
306
    $ref->{text}{$Id} = $TEXT;
307
  }
308
}
309
#récupération de tous les labels et rajout du titre en haut de la table dans la toc du tag
310
#On les traite pour que le label soit associé au nom du fichier créé automatiquement.
311
#$hash{toc}{$tag} contient les fichiers des sections en dessous de $tag
312
 
313
# PASSE 5
314
for my $tag (keys %{$hash{text}}) {
315
  my $T = $hash{text}{$tag};
316
  $T =~ s/\\label\s*\{([^}]+)\}/store_label($1, $tag, \%hash_bloc)/eg;
317
  $T =~ s/\\index\s*\{([^}]+)\}/store_index($1, $tag, \%hash_index)/eg;
318
  $hash{text}{$tag} = $T;
319
  my $tagupbl = $hash{upbl}{$tag};
320
  #plus utilisé, mais j'hésite !
321
#  $hash_toc{$tag} = "\\link{$tagupbl}\n\n" . $hash{toc}{$tagupbl};
322
}
323
 
324
 
325
# PASSE 6: sort l'index mis à jour des ref à l'aide des labels créés auparavant  et rajoute 
326
#TODO rajouter eqref mais ca dépend vraiment de la phrase !
327
for my $tag (keys %{$hash{text}}) {
328
  my $macro = '\\\\ref|\\\\cite|\\\\eqref';
329
  my $T = $hash{text}{$tag};
330
  my $cle = 'prev|next|upbl|titb|keyw|datm';
331
  $T =~ s/($macro)\{([^}]+)\}\{([^}]+)\}/store_ref($2, $3, $2, \%hash_bloc)/eg;
332
# repere toto~\cite{}
333
  $T =~ s/([^\s]+)\~($macro)\s*([.*])?\s*\{([^}]+)\}/store_ref($4, $1 .
334
    ($3||''), $4, \%hash_bloc)/eg;
335
  $T =~ s/($macro)\s*([.*])?\{([^}]+)\}/store_ref($3, ($2 ? "$3: $2" : $3), $3, \%hash_bloc)/eg;  
336
  $T =~ s/\\($cle)\s*\{(\w*)\}/store_tag($1, $2, $tag, \%hash, \%hash_bloc)/eg;
337
  $hash{text}{$tag} = $T;
338
}
5664 bpr 339
my @ListIndex = sort {$a cmp $b} (keys %{$hash_index{page}}) ;                  
340
out('index', selection('<div class="index">' . makeindex (\%hash_index, 0, @ListIndex) . '</div>'
341
                         ,'left-selection','index')) if ((@ListIndex) && $INDEX == 1 && makeindex (\%hash_index));
23 reyssat 342
 
343
#crée les blocs [entourés de la table des matières]
344
 
345
#crée les deux sortes de fichiers demandés par wims .def (fichier de définition général) +
346
# nom.hd  qui gère les règles de navigation  pour chaque bloc
347
#attention : dans le cas où il y a un \\embed{toto}, il faut créer le fichier toto.hd ...
348
#TODO : \embed{toto} : je suppose ici que le contenu de toto est du wims à ne pas 
349
#interpréter
350
#pour l'instant je m'en suis servi pour stocker des programmes qui interviennent plusieurs fois. 
351
 
352
#Crée le vrai fichier qui est mis dans le dossier src
353
#On ne met pas de table de matières si le bloc est de type fold
354
#TODO option = chemin tout seul, toc à gauche + chemin, toc à gauche et à droite + chemin,  
355
#style selection droite, selection gauche
356
 
357
for my $tag (keys %{$hash{text}}) {
358
  complete ($tag, \%hash);
359
  out ("$tag.hd", hd($tag,\%hash));
360
  my $txt = traitement_final($hash{text}{$tag});
361
  my $tagupbl = $hash{upbl}{$tag};
362
  my $type = $hash{type}{$tag} ;
6210 bpr 363
  my $style = $hash{style}{$tag};
23 reyssat 364
 #si type est non vide il est égal à embed ou fold
365
  my $dotoc_left = ($OPTION =~ /toc_left/ && !$type);
366
  my $dotoc_right = ($OPTION =~ /toc_right/ && !$type);
367
  my $dotoc_up = ($OPTION =~ /toc_up/ && !$type);
743 bpr 368
  my $dotoc_down = ($OPTION =~ /toc_down/ && !$type);
23 reyssat 369
  my $CHEMIN = chemin($tag, \%hash);
370
  #J'ai enlevé $LOAD
743 bpr 371
  $CHEMIN = ($dotoc_up || $dotoc_down)  && ($CHEMIN =~ $FLECHE) ? $CHEMIN : '';
3334 bpr 372
  my $CHEMIN_up=($dotoc_up) ? "<div id=\"up_toc\">$CHEMIN</div>": '' ;
373
  my $CHEMIN_down=($dotoc_down) ? "<div id=\"down_toc\">$CHEMIN</div>" : '' ;
23 reyssat 374
  my @Chemin = split(',', $hash{chemin}{$tag});
375
  my $TOCg = $dotoc_left ? selection($hash{toc}{main}, 'left_selection', @Chemin) : '';
6641 bpr 376
  my $TOCd = ($dotoc_right && $tag ne 'main' && (!($dotoc_left) || $tagupbl ne 'main' )) ? selection($hash{toc}{$tagupbl}, 'right_selection', @Chemin) : '';
6614 bpr 377
 
23 reyssat 378
  my $tit_index = ($hash{titb}{index})? $hash{titb}{index} : 'Index' ;
6614 bpr 379
  my $index = ($INDEX == 1 && (@ListIndex)) ? "<li>\\link{index}{$tit_index}</li>" : '';
6619 bpr 380
  my $tooltip = "";
6693 bpr 381
  ##$txt="<div class=\"fold\"> ".$txt ."<\/div>" if ($type=~/fold/) ;
382
  my $pat= '<br\s+class="spacer"\s*/>';
383
  $txt = $tooltip . toc_HTML ($txt, clean($TOCg,\%hash), clean($TOCd,\%hash), $CHEMIN_up, $CHEMIN_down, $index);
384
  $txt =~ s/$pat\s*$pat/<br class="spacer" \>/g;
385
  out ($tag, $txt );
23 reyssat 386
}
387
if ($INDEX == 1) { out ('index.hd', hd('index',\%hash) )};
388
my @style = sortuniq(split(',',$STYLE)) if ($STYLE) ;
389
out_def ('.def', def (\%hash, @style ));
390
 
391
##############
392
 
393
sub analyse_texte { my ($TEXT, $ref, $Id, $niveau, $niveau_max, $Toc) = @_;
394
  my $link = ($niveau <= $niveau_max);
395
  return $Toc if $niveau > $#SECTIONS ;
396
  my $section = $SECTIONS[$niveau];
397
  my $sectiontag = "<$section>";
398
  my @decoup = split (/$sectiontag/, $TEXT);
399
  my $text = $decoup[0];
400
  my $toc_titre ;
401
  my ($cnt, $id) = (1, "");
402
  #On parcourt un texte $Id : $text  et on enlève tous les niveaux inférieurs
403
 # (boucle while)
404
  while ( $decoup[$cnt]) {
405
  #tient compte des titres courts pour la toc
406
    my @u = extract_tagged ($decoup[$cnt],'\[','\]');
407
    $toc_titre = ($u[4]) ? $u[4] : '' ;
408
    @u = extract_tagged ($u[1],'{','}');
409
    if (!$u[4]) { $NUMERO = 1 ; warn " ATTENTION : section dans $Id sans titre ; l'option numerotation a été rajoutée"};
410
    my @extract = extract_tagged ($sectiontag . $u[1], $sectiontag);
411
 
412
    my $idold =  $id;
413
    $id = $Id . ($link? 'S': "$prefixe{fold}S") . $cnt;
414
    $cnt++;
415
    my $titre = $u[4];
416
    $toc_titre = $titre if (!$toc_titre) ;
417
    $titre =~ s/\n/ /g;
418
    $titre = Numero($id) . "  $titre" if ($NUMERO);
419
    $toc_titre = Numero($id) . "  $toc_titre" if ($NUMERO);
6003 bpr 420
    $text .=  $link ?"<p>\\link{$id}</p>"
421
                    :"<p>\\fold{$id}{<span class=\"$section\">$titre</span>}</p>";
23 reyssat 422
    if ($link) {
6003 bpr 423
      $Toc .=  "<p><XXXX=\"$id\">\\link{$id}{$toc_titre}<YYYY=\"$id\"></p>";
23 reyssat 424
    }
425
    $text .= $extract[1];
426
    $ref->{titb}{$id} = $titre;
427
    $ref->{tittoc}{$id} = $toc_titre;
428
    $ref->{text}{$id} = $extract[4];
734 bpr 429
    $ref->{type}{$id} = 'fold' if !$link ;
23 reyssat 430
    $Toc = analyse_texte ($ref->{text}{$id}, $ref, $id, $niveau + 1, $niveau_max, $Toc) ;
431
    $ref->{upbl}{$id} = $Id;
432
    $ref->{prev}{$id} = $idold;
433
    $ref->{next}{$idold} = $id;
434
   #modifier avec selection
6641 bpr 435
    my $tp = '' ;
734 bpr 436
    if ($TOOLTIP==1) {
6614 bpr 437
    if (!$ref->{toctip}{$Id}) {$ref->{toctip}{$Id}=($ref->{tittoc}{$id}) ?  
438
      "<li>\\link{$id}{$ref->{tittoc}{$id}}</li>": '' ; }
439
    else {
440
      $ref->{toctip}{$Id} .= ($ref->{tittoc}{$id}) ?  "<li>\\link{$id}{$ref->{tittoc}{$id}}</li>": '' ;
441
    }
442
    #$ref->{toctip}{$Id} .= $ref->{tittoc}{$id} ;
734 bpr 443
    $tp = "ZZZZZ$id" ; }
6003 bpr 444
    $ref->{toc}{$Id} .= "\n<XXXX=\"$id\">\\link{$id}{$ref->{tittoc}{$id}
6112 bpr 445
    }$tp <YYYY=\"$id\">";
23 reyssat 446
  }
447
 #maintenant, ce qui reste dans $text est exactement ce qu'on doit mettre dans le hash->{text}{$Id}
448
  $ref->{text}{$Id} = $text;
449
  $ref->{titb}{main} = $ref->{title}{main};
450
  $ref->{tittoc}{main} = $ref->{title}{main} if (!$ref->{tittoc}{main});
451
  $Toc;
452
}
453
 
454
sub store_tag { my ($cle, $label, $tag, $ref, $ref_bloc) = @_;
455
  $ref->{$cle}{$tag} = $ref_bloc->{fichier}{$label} ;
456
  '';
457
}
458
 
459
sub store_label { my ($label, $Id, $ref) = @_;
460
   $ref->{fichier}{$label} = $Id;
5894 bpr 461
   "<a id=\"$label\"></a>";
23 reyssat 462
}
463
 
464
sub dbg { print STDERR "$_[0]\n" if ($verbose); }
465
 
466
sub store_index { my ($label, $Id, $ref_index) = @_;
467
   dbg("... index: \"$label\"");
468
   my $L = $ref_index->{page}{$label};
469
   $ref_index->{page}{$label} =  !$L ? $Id : "$L,$Id";
5894 bpr 470
   "<a id=\"$label\"></a>";
23 reyssat 471
}
472
 
473
sub class_index { my ($index,$level) = @_ ;
474
     my @a = split('!', $index) ;
475
     ($a[$level])? $a[$level] : $index ;
476
 }
477
 #!(keys %{$ref_index->{page}}
478
 #sort keys %{$ref_index->{page}} ; 
479
 
5664 bpr 480
sub makeindex { my ($ref_index, $level, @L ) = @_;
23 reyssat 481
   return '' if (!$#L) ;
482
   my $dejavu = '' ;
483
   my $TEXT = "\n<ul class=\"index\">";
484
   for my $index (@L) {
485
      next if ($dejavu =~ /\b$index\b/) ;
486
      my @list = sort {$a cmp $b} grep {class_index($_, $level) eq class_index($index, $level)} @L ;
487
      my @l = split('!', $index) ;
488
      if ($l[$level]) {
489
          $TEXT .= "<li> " ;  
490
          for my $ind (split(',',$ref_index->{page}{$index})) {
491
             if ($ind && !($dejavu =~ /\b$ind\b/)) {
492
                 $TEXT .= "\\link{$ind}{".  $l[$level] . " }{$ind}" ;
493
             }
494
          }
495
          $dejavu .= ' ' . join (' , ', @list) ;
496
          $TEXT .= makeindex ($ref_index, $level + 1, @list) . "</li>" ;
497
      };
498
   }
499
   "$TEXT </ul>";
500
}
501
 
502
sub TraiteText {my ($TEXT, $ref, $ref_env, $Id) = @_;
503
  $TEXT =~ s/\s*$//; # strip trailing whitespace
504
 #0 ul et li sans rien
505
 #1 avec style
6104 bpr 506
 $TEXT =~ s/\\(begin|end)\s*{wimsonly}//g;
23 reyssat 507
 for my $rubrique (keys %{$ref_env->{list}}) {
508
     $TEXT = traite_list ($TEXT, $ref, $ref_env, $Id, $rubrique,1);
509
 }
510
 
511
 for my $rubrique (@liste_env_list) {
512
     $TEXT = traite_list ($TEXT, $ref, $ref_env, $Id, $rubrique,0);
513
 }
514
 
734 bpr 515
for my $rubrique (keys %{$ref_env->{tabular}}) {
516
     if ($TEXT =~ /\\begin{$rubrique}/) {
517
       $TEXT = traite_environ ($TEXT, $ref, $ref_env, $Id, $rubrique,0);
518
     }
519
 }
520
 
521
for my $rubrique (@liste_env_tabular) {
522
   if ($TEXT =~ /\\begin{$rubrique}/) {
523
     $TEXT = traite_environ ($TEXT, $ref, $ref_env, $Id, $rubrique,0);
524
   }
525
}
526
 
178 bpr 527
 for my $rubrique (@liste_env_spec) {
528
    if ($TEXT =~ /\\begin{$rubrique(\*)?}/) {
529
      $TEXT = traite_environ ($TEXT, $ref, $ref_env, $Id, $rubrique, 1);
530
    }
531
  }
23 reyssat 532
#le 1 et 0 servent à initialiser le compteur dans le cas ou on doit créer de nouveaux blocs dans la même page
178 bpr 533
 
23 reyssat 534
  for my  $rubrique (keys %{$ref_env->{titre}}) {
535
    if ($TEXT =~ /\\begin{$rubrique}/) {
536
      $TEXT = traite_environ ($TEXT, $ref, $ref_env, $Id, $rubrique,1);
537
    }
538
  }
539
  for my $rubrique (keys %{$ref_env->{list}}) {
540
     $TEXT = traite_environ ($TEXT, $ref, $ref_env, $Id, $rubrique,0);
541
     $TEXT = traite_environ ($TEXT, $ref, $ref_env, $Id, $rubrique . '_item',0);
542
  }
178 bpr 543
 
23 reyssat 544
  if ($TEXT =~ /\\begin\{\s*(\w*)\s*\}/g) {
6855 bpr 545
     warn " ATTENTION : environnement non répertorié : $1" if $1 ne 'matrix' && $1 ne 'split';
23 reyssat 546
  }
3549 bpr 547
  $TEXT =~ s,<li>\n+</li>,<li></li>,g;
6104 bpr 548
  $TEXT =~ s,</div>\s+</div>,</div></div>,g;
549
  $TEXT =~ s,</div>\s+<,</div>\n<,g;
6360 bpr 550
  $TEXT =~ s,<div ([^>]+)>\s+<,<div $1><,g;
6693 bpr 551
  $TEXT =~ s/\n{2,5}/<br class=\"spacer\" \/>/g;
23 reyssat 552
  $TEXT;
553
}
554
 
555
#on pourrait faire une boucle  while ; on pourrait avoir deux fois le meme environnement imbrique ?
556
#begin{proof} \begin{proof} \end{proof}\end{proof} Je crois que c'est pour cela que je fais ce truc
557
#tordu. En fait  split me sert uniquement à trouver le premier <$environ>
558
#$cnt sert à numéroter semi-globalement (création de blocs correspondant à un même environnement dans une meme page
559
#exemple mainS4S3F_proof1,mainS4S3F_proof2,mainS4S3F_proof3,mainS4S3F_proof4,mainS4S3F_proof5
734 bpr 560
 
23 reyssat 561
sub traite_list {my ($TEXT, $ref, $ref_env, $Id, $environ, $option) = @_;
734 bpr 562
 my ($e_item, $b_item , $b_class, $e_class) = (' ',' ', ' ', ' ');
23 reyssat 563
  if ( $option == 1 ) {
564
         $e_item = "\\end{$environ\_item}" ;
565
         $b_item = "\\begin{$environ\_item}" ;
566
      my $style = $ref_env->{style}{$environ} ;
567
      $style = ($style) ? $style : $environ;
568
      $b_class= "ul class=\"$style\"" ;
569
      $e_class= "\/ul" ;
570
      }
571
      {
743 bpr 572
        if    ($environ eq 'enumerate'){ $b_class = "ol class=\"enumerate\"" ; $e_class= "\/ol" ; }
573
        elsif ($environ eq 'itemize'){ $b_class = "ul class=\"itemize\"" ;  $e_class= "\/ul" ;}
23 reyssat 574
        elsif ($environ eq 'description'){
575
              $b_class = "ul style=\"list-style:none;\"" ;  
576
              $e_class= "\/ul" ;
577
              $e_item = "\\end{$environ\_item}" ;
578
              $b_item = "\\begin{$environ\_item}"
579
              }
580
        elsif ($environ eq 'trivlist') { $b_class = "ul style=\"list-style:none;\"" ;  
581
               $e_class= "\/ul" ;
582
        }
583
      };
584
  $TEXT =~ s/\\begin{$environ(\*)?}/<$environ>/g;
585
  $TEXT =~ s|\\end{$environ(\*)?}|<\/$environ>|g;
586
 
587
  my @decoup = split ("<$environ>", $TEXT);
588
 
589
  my $a = join ("<$environ>", @decoup[1..$#decoup]);
743 bpr 590
  return $TEXT if (!$a) ;
23 reyssat 591
  my @u = extract_tagged ("<$environ>$a", "<$environ>");
592
  my $milieu = "<$environ>" . $u[4] . "<\/$environ>"   ;
593
#FIXME pas de listes emboitées de type différent !
743 bpr 594
  $milieu =~ s|<$environ>\s*\\item|<$environ><li>$b_item|g ;
595
  $milieu =~ s|</$environ>|</li><$e_class>|g;
596
  $milieu =~ s|\\item|$e_item</li><li>$b_item|g;
597
  $milieu =~ s|</li><$e_class>|$e_item</li><$e_class>|g;
598
  $milieu =~ s|<$environ>|<$b_class>|g;
23 reyssat 599
  $decoup[0] . $milieu . traite_list ($u[1], $ref, $ref_env, $Id, $environ,$option);
600
}
601
 
602
sub traite_environ {my ($TEXT, $ref, $ref_env, $Id, $environ, $cnt) = @_;
603
  $TEXT =~ s/\\begin{$environ\*?}/<$environ>/g;
604
  $TEXT =~ s|\\end{$environ\*?}|</$environ>|g;
605
 
606
  my @decoup = split ("<$environ>", $TEXT);
607
 
608
  my $a = join ("<$environ>", @decoup[1..$#decoup]);
609
  return $TEXT if (!$a);
610
 
611
  my @u = extract_tagged ("<$environ>$a", "<$environ>");
612
  my $milieu = $u[4];
613
  return $TEXT if (!$milieu);
614
 
615
  my $pat_env = join('|', @liste_env_spec);
734 bpr 616
  my $patt_env = join('|', @liste_env_tabular);
23 reyssat 617
  if ($environ =~ /\b($pat_env)\b/) { $milieu = $1->($milieu) ; }
734 bpr 618
  elsif ($environ =~ /\b($patt_env)\b/) { $milieu = tabular->($milieu,$environ) ; }
619
  else { my @milieu1 = extract_bracketed ($milieu, '{}');
23 reyssat 620
    if ($milieu1[0]) { $milieu = $milieu1[4] ; };
621
    my $type = $ref_env->{type}{$environ};
622
 
623
    if ($type && ($type eq 'fold' || $type eq 'link')) {
624
      my $titre = $ref_env->{titre}{$environ};
625
      my $newtag = $Id . $prefixe{$type} . $environ . $cnt;
626
      $ref->{type}{$newtag} = 'fold' if $type eq 'fold' ;
627
      $cnt++;
628
      # LaTeX interdit des [ ] imbriqués.
629
      if ($milieu =~ s/^\s*\[([^\]]+)\]//) {
743 bpr 630
        $titre =  ($titre) ? "$titre [ $1 ]" :  $1 ;
23 reyssat 631
      }
632
      $ref->{titb}{$newtag} = $titre;
633
      $ref->{text}{$newtag} = encadrement("<$environ>$milieu<\/$environ>", $environ, $ref_env);
634
      $ref->{upbl}{$newtag} = $Id;
635
      $milieu = "\n\\$type\{$newtag\}\{"
636
        . encadr_defaut("<$environ>$titre<\/$environ>", $environ, $ref_env,'titre')
6167 bpr 637
        . "\}\n" ;
23 reyssat 638
    } else {  my $milieu1 = $milieu ;
734 bpr 639
      $milieu = encadrement("<$environ>$milieu<\/$environ>", $environ, $ref_env, 'full');
23 reyssat 640
    }
641
  }
642
  $decoup[0] . $milieu . traite_environ ($u[1], $ref, $ref_env, $Id, $environ, $cnt);
643
}
644
 
645
sub hd {my ($tag, $ref) = @_;
646
  my $txt = '';
647
  for  my $cle ('prev','next','upbl','titb','keyw','datm') {
648
    my $KEY = $ref->{$cle}{$tag};
649
    $txt .= "!set $cle=$KEY\n" if ($KEY);
650
  }
651
  $txt;
652
}
653
#rajoute un next aux section/subsection/ si cela n'existe pas [dernier] dernier sur index si il y a
654
#rajoute main pour ceux qui n'ont pas de parents. 
655
sub complete {my ($tag, $ref) = @_;
656
  $ref->{datm}{$tag} = isotime() if  !($ref->{datm}{$tag}) ;
657
  $ref->{upbl}{$tag} = 'main' if  !($ref->{upbl}{$tag}) ;
658
  my $upbl = $ref->{upbl}{$tag};
659
  if (!$ref->{next}{$tag}) {
660
    my $a = $ref->{next}{$upbl};
661
    $ref->{next}{$tag} = $a || 'main';
662
  };
663
  if (!$ref->{prev}{$tag}) {
664
    my $b = $ref->{prev}{$upbl};
665
    $ref->{prev}{$tag} = $b || 'main';
666
  };
667
  if ($INDEX == 1 && !($ref->{titb}{index})) { $ref->{titb}{index} = 'Index' ;}
668
}
669
 
6472 bpr 670
#option full  <h2 class=" l2w_content defn">Définition [titre perso]</h2> <div class= " l2w_content definition">  </div> si cela existe
23 reyssat 671
#option bloc  <div class= "definition"> </div>  si cela existe (intérieur d'un fold ou d'un link)
6472 bpr 672
#option titre  <h2 class="l2w_content defn">Définition </h2>  si cela existe (titre d'un fold)
23 reyssat 673
sub encadr_defaut { my ($TEXT, $rubrique, $ref_env, $option) = @_;
674
  my $a = $ref_env->{titre}{$rubrique};
675
  my $b = $ref_env->{style}{$rubrique};
676
  if (!$b) {
677
    $b = $rubrique;
678
    $ref_env->{style}{$rubrique} = $b;
679
    dbg("... environnement $rubrique sans style css, par defaut $rubrique")
680
  };
6472 bpr 681
  my $div_d = "<div class=\"l2w_content $b\">";
23 reyssat 682
  my $div_f = '</div>';
683
  if ( $option eq 'titre') {
6472 bpr 684
    $TEXT =~ s/<$rubrique>/<span class=\"l2w_content $b\">/g;
6104 bpr 685
    $TEXT =~ s/<\/$rubrique>/<\/span>\n/g;
23 reyssat 686
  } elsif (!$a || $option eq 'bloc') {
687
    $TEXT =~ s/<$rubrique>\s*(\[[^\]]+\])?/$div_d/g;
6167 bpr 688
    $TEXT =~ s/<\/$rubrique>/$div_f/g;
23 reyssat 689
  } elsif ($option eq 'full') {
6472 bpr 690
    $TEXT =~ s/<$rubrique>\s*(\[[^\]]+\])/<h2 class=\"l2w_content $b\">$a $1<\/h2>$div_d/g;
691
    $TEXT =~ s/<$rubrique>/<h2 class=\"l2w_content $b\">$a<\/h2>$div_d/g;
6167 bpr 692
    $TEXT =~ s/<\/$rubrique>/$div_f/g;
23 reyssat 693
  } else  {
694
    $TEXT =~ s/<$rubrique>/<span class=\"$b\">/g;
6104 bpr 695
    $TEXT =~ s/<\/$rubrique>/<\/span>\n/g;
23 reyssat 696
  }
697
  $TEXT;
698
}
699
sub encadrement {  my ($TEXT, $rubrique, $ref_env) = @_;
700
  my $debut = $ref_env->{deb}{$rubrique};
701
  my $fin   = $ref_env->{fin}{$rubrique};
743 bpr 702
  my $opt= ($ref_env->{type}{$rubrique} && $ref_env->{type}{$rubrique}=~ /fold/) ? 'bloc' : 'full' ;
703
  return encadr_defaut ($TEXT, $rubrique, $ref_env, $opt) if (!$debut && !$fin);
23 reyssat 704
 
705
  $TEXT =~ s/<$rubrique>//;
706
  my $cnt_arg = $ref_env->{cnt_arg}{$rubrique};
707
  my $d = join('   ', subst($TEXT, $cnt_arg, $debut, $rubrique, $ref_env));
708
  my ($com,$txt) = subst($TEXT, $cnt_arg, $fin);
709
  $d =~ s/<\/$rubrique>/$com/;
710
  $d;
711
}
712
 
734 bpr 713
 
714
sub tabular { my ( $b, $style ) = @_;
715
  my @v = extract_bracketed ($b, '{}') ;
716
  my $stylerow = $style . "_row";
717
  my $stylecell = $style . "_cell";
3334 bpr 718
  $b =  "<table class=\"$style\"><tr class=\"$stylerow\"><td class=\"$stylecell\">" . $v[1] . '</table>';
719
  $b =~ s|\&|&nbsp;</td><td class=\"$stylecell\">&nbsp;|g;
23 reyssat 720
  $b =~ s/\\hline//g;
721
  $b =~ s|\\\\\s*</table>|</td></tr></table>|g;
743 bpr 722
  my $par="\\\\\\(|\\\\\\)" ;
723
  my @dectab = split(/$par/, $b) ;
724
  $b = $dectab[0] ;
3334 bpr 725
  $b =~ s|\\\\|</td></tr><tr class=\"$stylerow\"><td class=\"$stylecell\">|g;
743 bpr 726
  my $cnt = 0; $b = '' ;
727
  while ($cnt <= $#dectab/2) {
728
     my $c = $dectab[2*$cnt] ;
3334 bpr 729
     $c =~ s|\\\\|</td></tr><tr class=\"stylerow\"><td class=\"$stylecell\">|g;
743 bpr 730
     $b .=  $c . (($dectab[2*$cnt+1]) ? "\\(" . $dectab[2*$cnt+1] .  "\\)" : '' )  ;
731
     $cnt ++ ;
732
 };
733
 $b ;
23 reyssat 734
}
735
 
4852 bpr 736
###demande de convertir d'abord de manière indépendante les pdf tiff eps svg en un format d'image png
737
sub includegraphics{ my ( $b, $opt ) = @_;
738
   $b=~ s/.(pdf|tiff|eps|svg)/.png/ ;
4858 bpr 739
   $opt =~ s/.*(width|height)\s*=\s*([0-9]*\.?[0-9]*\s*)(\\(line|text)width)/linewidth("$2$3",$1)/eg;
5663 bpr 740
   $opt =~ s/.*(width|height)\s*=\s*([0-9]*\.?[0-9]*\s*)(cm|px)/ style=\"$1:$2$3;\"/;
6358 bpr 741
  "<img src=\"\\filedir\/$b\" $opt alt=\"\" />";
4852 bpr 742
}
743
 
4850 bpr 744
sub minipage { my ( $b ) = @_;
4865 bpr 745
 my @v = extract_bracketed ($b, '[]') ;
746
 my $option = $v[0] ;  
6858 bpr 747
 if($option) {
4865 bpr 748
 $option=~ s/\[\s*b\s*\]/bottom/ ;  
4866 bpr 749
 $option=~ s/\[\s*t\s*\]/top/ ;
750
 $option=~ s/\[\s*c\s*\]/middle/ ;
4865 bpr 751
 $option=~s/\[\s*\]/middle/;
6858 bpr 752
 } else {
753
 $option='middle'} ;
4865 bpr 754
 @v = extract_bracketed ($v[1], '{}') ;
4850 bpr 755
 my $width = $v[0] ;
756
 $width =~ s/\{(.*)\}/$1/;
6858 bpr 757
 $width = linewidth($width);
4865 bpr 758
  "<div style=\"width:$width; display:inline-block;vertical-align:$option;\" class=\"minipage\">
4850 bpr 759
   $v[1]
760
   </div>";
761
}
5286 bpr 762
sub columns { my ( $b ) = @_;
5289 bpr 763
  "<div class=\"columns\">$b</div>"
5286 bpr 764
}
765
 
766
sub column { my ( $b ) = @_;
767
 my @v = extract_bracketed ($b, '[]') ;
6104 bpr 768
 my $option = '';
769
 $option= $v[0] ;
770
 if($option) {
771
  $option=~ s/\[\s*b\s*\]/bottom/ ;  
772
  $option=~ s/\[\s*t\s*\]/top/ ;
773
  $option=~ s/\[\s*c\s*\]/middle/ ;
774
  $option=~s/\[\s*\]/middle/;
775
  }
776
 else{ $option='top'} ;
5286 bpr 777
 @v = extract_bracketed ($v[1], '{}') ;
778
 my $width = $v[0] ;
779
 $width =~ s/\{(.*)\}/$1/;
780
 $width = linewidth($width) ;
781
  "<div style=\"width:$width; display:inline-block;vertical-align:$option;\" class=\"column\">
782
   $v[1]
783
   </div>";
784
}
785
 
786
 
4851 bpr 787
sub lstlisting { my ($b,$id ) = @_ ;
788
  $b =~ s ,\\,\\\\,g ;
789
  "<pre class=\"lstlisting\" id=\"lstlisting$id\">$b</pre>";
790
}
743 bpr 791
 
4858 bpr 792
sub verbatim { my ($b,$id ) = @_ ;
793
  $b =~ s ,\\,\\\\,g ;
794
  "<pre class=\"verbatim\" id=\"verbatim$id\">$b</pre>";
795
}
796
 
23 reyssat 797
sub multline { my ( $b) = @_;
5762 bpr 798
  $b =~ s/\\\\\s*=/\\)<br\/>\\(== /g;
799
  $b =~ s|\\\\|\\)<br\/>\\(|g;
23 reyssat 800
  "<div class=\"math\">\\(" . $b . "\\)</div>\n";
801
}
802
 
803
sub equation { my ( $b) = @_;
804
  $b = "\\(  $b \\)";
805
  if ($b =~ s/\\label{([^\}]+)}//) { $b = "\\label{$1}" . $b };
806
  '<div class="math">' . $b . '</div>' ;
807
}
808
 
809
sub align1 { my ( $b) = @_;
810
  $b = "\\(\\begin{matrix}  $b  \\end{matrix} \\)";
811
  if ($b =~ s/\\label{([^\}])}//) { $b = "\\label{$1}" . $b };
812
  '<div class="math">' . $b . '</div>' ;
813
}
814
sub align { my ( $b) = @_;
6169 bpr 815
  $b =  '<table class="wimscenter wimsnoborder tableau" style="width:100%"><tr><td>\\(' . $b . '\\\\</table>';
23 reyssat 816
  if ($b =~ s/\\label{([^\}])}//) { $b = "\\label{$1}" . $b };
817
  $b =~ s|\&|\\)&nbsp;</td><td>&nbsp;\\(|g;
818
  $b =~ s|\\\\\s*</table>|\\)</td></tr></table>|g;
819
  $b =~ s|\\\\|\\)</td></tr><tr><td>\\(|g;
5291 bpr 820
  $b =~ s|\\\(\s+\\\)||g;
821
  $b =~ s|\n\\\)|\\\)|g;
822
  $b =~ s|\\\(\n|\\\(|g;
23 reyssat 823
  '<div class="math">' . $b . '</div>' ;
824
}
825
 
826
sub pmatrix {"\\left ( \\begin{matrix} " . $_[0] . "\\end{matrix} \\right )" ;}
827
sub smallmatrix {"\\left ( \\begin{matrix} " . $_[0] . "\\end{matrix} \\right )" ;}
828
sub eqnarray {" <div class=\"math\">\\(\\begin{matrix} " . $_[0] . "\\end{matrix})</div> " ;}
829
sub center {" <div class=\"center\">" . $_[0] . "</div>"}
830
 
831
sub array {my ( $b ) = @_ ;
832
  my @v = extract_bracketed ($b, '{}');
833
  "\\begin{matrix} " . $v[1] . "\\end{matrix} ";
834
}
835
#TODO pour l'instant
5292 bpr 836
sub cases {"\\left \\lbrace\\begin{matrix} " . $_[0] . "\\end{matrix} \\right ." ; }
23 reyssat 837
 
838
sub gather { my ($b) = @_;
839
  my @decoup = split ('\\\\intertext', $b);
840
  my $cnt = 1;
841
  $b = equation($decoup[0]);
842
  while ($cnt <= $#decoup) {
843
    my @a = extract_bracketed ( $decoup[$cnt], '{}' );
844
    my $c = $a[0];
845
    $c =~ s/\{(.*)\}/$1/;
846
    $b .= $c . equation($a[1]);
847
    $cnt ++;
848
  }
849
  $b;
850
}
851
 
6169 bpr 852
sub displaymath {"<div class=\"math\">\\(\\displaystyle{ " . $_[0]. "}\\)</div>"; }
23 reyssat 853
sub math {" \\( " . $_[0]. "\\) "; }
854
 
855
 
856
sub thebibliography { my ( $b ) = @_;
857
  $b =~ s/\\bibitem{([^}]+)}/<\/li>\n<li>\[$1\]\\label{$1} /g;
858
  $b =~ s/\{\d+\}\s*<\/li>//;
859
  '<h2 class="thebibliography">' . $hash{titb}{ref}
860
  . "</h2>\n<ul class=\"thebibliography\">$b </li></ul>\n";
861
}
862
sub pspicture { '<p>dessin à faire dans wims</p>' ; }
863
sub picture { '<p>dessin à faire dans wims</p>' ; }
864
 
865
#decoupe ce qui se trouve à l'intérieur de \begin{wims} \end{wims} pour ne pas y toucher.
4851 bpr 866
# même pour verbatim, lstlisting
23 reyssat 867
sub traite_special { my ( $TEXT, $ref_spec, $ref, $environ ) = @_;
868
  $TEXT = recup_embed($TEXT, $ref) ;
869
  $TEXT =~ s/\\begin{$environ}/<$environ>/g;
870
  $TEXT =~ s/\\end{$environ}/<\/$environ>/g;
871
  $TEXT =~ s/\r\n/\n/gs ;
872
  my @decoup = split ("<$environ>", $TEXT);
873
  my $cnt = 1;
874
  if ($#decoup) {
875
    $TEXT = $decoup[0];
876
    while ($cnt <= $#decoup) {
877
      my @a = extract_tagged("<$environ>" . $decoup[$cnt], "<$environ>");
878
      die "Bug dans $environ insertion: $decoup[$cnt]" if (!defined($a[4]));
879
      $TEXT .= $environ . 'insertion' . $cnt . $a[1];
880
      $ref_spec->{$cnt} = $a[4];
881
      $cnt++;
882
    }
883
  }
884
  $TEXT;
885
}
886
 
887
sub latexonly { '' }
888
 
889
sub store_cmd { my ($def, $id, $narg, $val, $ref_command) = @_;
890
  if ($hash_secinv{$id}) {
891
    warn " ATTENTION : commande perso \"$id: argument invalide\n";
892
    return '';
893
  }
894
  my $origin = $ref_command->{origin}{$id};
895
  return '' if ($origin && $origin eq 'defwims' && $def ne 'defwims');
896
  $ref_command->{origin}{$id} = $def;
897
  $ref_command->{cnt_arg}{$id} = $narg;
898
  $ref_command->{definition}{$id} = $val;
899
  dbg("... commande perso \"$id\": argument: \"$narg\" definition: \"$val\"");
900
 '' ;
901
}
902
 
903
sub store_environ { my ($def, $cmd, $narg, $titre, $deb, $fin, $ref_env) = @_;
904
  my $origin = $ref_env->{origin}{$cmd};
905
  return '' if ($origin && $origin eq 'environmentwims' && $def ne 'environmentwims');
906
  $ref_env->{deb}{$cmd} = $deb;
907
  $ref_env->{fin}{$cmd} = $fin;
908
  $ref_env->{cnt_arg}{$cmd} = $narg;
7228 bpr 909
## one can have macro in definition of an environment - not general but useful for language
910
  if ($titre=~/\\(\w+)/) {
911
    $titre=$hash_command{definition}{$1} if $hash_command{definition}{$1}
912
  }
23 reyssat 913
  $ref_env->{titre}{$cmd} = $titre;
914
  $ref_env->{origin}{$cmd} = $def;
915
  my $style = $ref_env->{style}{$cmd} ;
916
  $style = ($style)? $style : $cmd ;
734 bpr 917
  #if (!$ref_env->{deb}{$cmd}) { $ref_env->{deb}{$cmd} = "<div class=\"$style\"> "; }
918
  #if (!$ref_env->{fin}{$cmd}) { $ref_env->{fin}{$cmd} = "</div> "; }
23 reyssat 919
  dbg("... environnement perso \"$cmd\" argument: \"$narg\" titre: \"$titre\" style: \"$style\" debut: \"$deb\"  fin: \"$fin\"");
920
  '';
921
}
922
 
923
 
924
sub recup_command {my ($TEXT, $ref_command) = @_;
925
  my $DEF = '(defwims|def|newcommand|renewcommand)';
926
  #FIXME ? mauvais pour def \def\toto#1#2 ... demander de le refaire avec defwims s'il y a plus de 3 arguments ? 
927
  $TEXT =~ s/\\$DEF\s*\\(\w*)#(\d)\{(.*)\}/store_cmd($1,$2,$3,$4,$ref_command)/eg;
928
  #2 arguments
929
  $TEXT =~ s/\\$DEF\s*\\(\w*)#(\d)#(\d)\{(.*)\}/store_cmd($1,$2,$4,$5,$ref_command)/eg;
930
  $TEXT =~ s/\\$DEF\s*\\(\w*)\s*\{(.*)\}/store_cmd($1,$2,0,$3,$ref_command)/eg;
931
  #3 arguments
932
  $TEXT =~ s/\\$DEF\s*\\(\w*)#(\d)#(\d)#(\d)\{(.*)\}/store_cmd($1,$2,$5,$6,$ref_command)/eg;
933
  # newcommand avec paramètres
934
  $TEXT =~ s/\\$DEF\s*\{\\(\w*)\}\s*\[(\d)\]\s*\{(.*)\}/store_cmd($1,$2,$3,$4,$ref_command)/eg;
935
  # newcommand sans paramètres
936
  $TEXT =~ s/\\$DEF\s*\{\\(\w*)\}\s*\{(.*)\}/store_cmd($1,$2,0,$3,$ref_command)/eg;
937
  $TEXT;
938
}
939
 
940
sub recup_config { my ($cmd, $arg, $ref_env) = @_;
941
  my @L = (split (',', $arg));
942
  my $style = $L[0];
943
  my $type = 'style';
944
  if ($cmd eq 'typefold') { $style = 'fold';  $type = 'type' };
945
  if ($cmd eq 'typelink') { $style = 'link' ; $type = 'type' };
734 bpr 946
  if ($cmd eq 'tablewims') { $type = 'tabular' ; };
23 reyssat 947
  if ($cmd eq 'listwims') {                   $type = 'list' };
948
  for my $rubrique (@L) {
949
    $ref_env->{$type}{$rubrique} = $style;
950
    dbg("... commande $rubrique de $type $style");
951
    if ($cmd eq 'listwims') { $ref_env->{'style'}{$rubrique . '_item'} = $style . '_item' ;
952
                              $ref_env->{'titre'}{$rubrique . '_item'} = '' }
953
  }
734 bpr 954
  push @liste_env_tabular, (keys %{$ref_env->{tabular}}) ;
23 reyssat 955
 '';
956
}
957
sub recup_environ {my ($TEXT, $ref_env) = @_;
958
  my $pat = '\s*\{(.*)\}';
734 bpr 959
  $TEXT =~ s/\\(typefold|typelink|samestyle|listwims|tablewims)\{([^\}]+)\}/recup_config($1,$2, $ref_env)/eg;
23 reyssat 960
  # {nom}[#param]{titre}{debut}{fin}
961
  $TEXT =~ s/\\(environmentwims|[re]?newenvironment)\s*\{(\w*)\}\[(\d)\]$pat$pat$pat/store_environ($1,$2,$3,$4,$5,$6,$ref_env)/eg;
962
  # {nom}{titre}{debut}{fin}
963
  $TEXT =~ s/\\(environmentwims|[re]?newenvironment)\s*\{(\w*)\}$pat$pat/store_environ($1,$2,0,'', $3,$4,$ref_env)/eg;
964
 #\newtheorem{nom}[]?{titre}[]?
965
  $TEXT =~ s /(\\newtheorem\*?)\s*\{(\w*)\}\s*(\[[^\]]*\])?\s*\{([^\}]*)\}\s*(\[[^\]]*\])?\s*/store_environ($1,$2,0,$4,'','',$ref_env)/ge;
966
  $TEXT;
967
}
968
 
969
#on suppose que le texte commence par \command{}{}{}{} avec le bon nombre d'arguments
970
 
971
sub traite_command {my ($TEXT, $command, $ref_command) = @_;
972
  my $cnt_arg = $ref_command->{cnt_arg}{$command};
973
  my $com = $ref_command->{definition}{$command};
974
  if ($cnt_arg) {
975
    $TEXT =~ s/\\$command\s*\{/\{/;
976
    $TEXT = join ( "  ", subst($TEXT, $cnt_arg, $com));
977
  } else {
978
    $TEXT =~ s/\\$command\_/$com . '_'/ge;
979
    $TEXT =~ s/\\$command\b/$com/ge;
980
  }
981
  $TEXT;
982
}
983
#Texte = {}{}{} ou []{}{} qui sont les arguments
984
sub subst { my ($TEXT, $cnt_arg, $com, $environ, $ref_env ) = @_;
985
  my (@a,$u,$v);
986
  my $cnt = 0;
987
  while ($cnt < $cnt_arg) {
988
    @a = extract_tagged($TEXT, '{','}');
989
    ($u,$v) = ($a[1],$a[4]);
990
    if (!$a[0]) {
734 bpr 991
      @a = extract_tagged($TEXT, '\[','\]');
992
      ($u,$v) = ($a[1],$a[4]);
23 reyssat 993
    }
994
    $TEXT = $u;
995
    $cnt ++;
920 bpr 996
     my $sub="" ;
997
    if ($v) {
998
       $sub = $environ && $ref_env->{titre}{$environ} ? join (' ' , ( $ref_env->{titre}{$environ}, $v)) : $v;}
999
    else {$sub=''} ;
766 bpr 1000
     if (($com) && ("#$cnt")) { $com =~ s/#$cnt/$sub/ge ;  }  ;
23 reyssat 1001
  }
1002
  ($com, $TEXT);
1003
}
1004
 
1005
sub Traite_command { my ($TEXT, $command, $ref_command) = @_;
1006
  my $cnt_arg = $ref_command->{cnt_arg}{$command};
1007
  if ($cnt_arg) {
1008
    my @decoup = split ("\\\\$command\{", $TEXT );
1009
    my $cnt = 1;
1010
    $TEXT = $decoup[0];
1011
    while ($cnt <= $#decoup) {
1012
      $TEXT .= traite_command ('{' . $decoup[$cnt], $command, $ref_command);
1013
      $cnt++;
1014
    }
1015
  } else {
1016
    while( $TEXT =~ /\\$command\b/) {
1017
      $TEXT = traite_command ($TEXT, $command, $ref_command);
1018
    }
1019
  }
1020
  $TEXT;
1021
}
1022
 
1023
my %outagain;
1024
sub out { my ($bloc, $text) = @_;
1025
  warn "Écrase $bloc" if ($outagain{$bloc});
1026
  $outagain{$bloc} = 1;
1027
  open  (OUT, ">$BASE/$bloc") || die "ne peut pas créer $BASE/$bloc";
1028
  print OUT $text ; close OUT;
1029
}
1030
 
1031
sub out1 { my ($bloc, $text) = @_;
1032
  warn "Écrase $bloc" if ($outagain{$bloc});
178 bpr 1033
  $outagain{$bloc} = 1;
23 reyssat 1034
  open  (OUT, ">$doc_DIR$bloc") || die "ne peut pas créer $doc_DIR/$bloc";
1035
  print OUT $text ; close OUT;
1036
}
1037
 
1038
sub out_def { my ($bloc, $text) = @_;
1039
  open  (OUT, ">$BASE0/$bloc") || die "peut pas créer $BASE0/$bloc";
1040
  print OUT $text ; close OUT;
1041
}
1042
 
1043
# PASSE 1: développe 'input/include'
1044
sub find_expand { my ($file) = @_;
1045
  if (!open(IN, $DIR . $file)) { warn "$DIR$file n'existe pas"; return; }
1046
  dbg("... lecture de $file");
1047
  my $text = <IN>; close(IN);
743 bpr 1048
  $text =~ s/([^%]\s*\\end{document})[[:print:][:space:]]+/$1/;
1049
  $text =~ s/([^%])\s*\\endinput[[:print:][:space:]]+/$1/;
1050
  $text =~ s/\%\\(input|include|wimsinclude)([^\n]+)?//g;
1051
  $text =~ s/\\(input|include|wimsinclude)\s*{?([a-zA-Z0-9\-_\/]+)\.(sty|tex)\s*}?/find_expand("$2.$3")/eg;
4851 bpr 1052
  $text =~ s/\\lstinputlisting\s*\{([a-zA-Z0-9\-_\/\.]+)\s*\}/"\\begin\{lstlisting\}\n" . find_expand($1) . "\n\\end\{lstlisting\}"/eg;
1053
 
23 reyssat 1054
  $text;
1055
}
1056
 
1057
sub open_close { my ($sec, $cnt, $entre) = @_;
1058
  my ($txt) = '';
1059
  my ($ind) = $hash_secinv{$sec};
1060
  #ferme
1061
  for (my $i = $#SECTIONS; $i >= $ind; $i--)
1062
  {
1063
    if ($cnt->[$i]) { $txt .= "</$SECTIONS[$i]>\n"; $cnt->[$i] = 0; }
1064
  }
1065
  #ouvre
1066
  if (!$entre) {
1067
    $txt .= "<$sec>"; $cnt->[$ind] = 1;
1068
    $SEC_MIN_GLOBAL = $ind if ($ind < $SEC_MIN_GLOBAL && $ind);
1069
  }
1070
  $txt;
1071
}
1072
 
1073
sub cnt_section { my ($sec, $cnt) = @_ ;
1074
  my ($txt) = '';
1075
  my ($ind) = $hash_secinv{$sec};
1076
  $SEC_MIN_GLOBAL = $ind if ($ind < $SEC_MIN_GLOBAL && $ind);
1077
  "<$sec>"
1078
 }
1079
 
1080
 
1081
sub store { my ($ref, $cle, $id, $text, $court) = @_ ;
1082
   $ref->{$cle}{$id}=$text ;
1083
   if (defined($court)) { $court =~ s/\[|\]//g ; $ref->{tittoc}{$id} = $court ; } ;
1084
   '';
1085
 }
1086
sub store_option { my ($A) = @_ ; $A = join(' ' , split(',', $A)) ;
734 bpr 1087
    if ($A =~ s/numero//)  { $NUMERO = 1 ;} ;
23 reyssat 1088
    if ($A =~ s/index//)   { $INDEX = 1 ;}
734 bpr 1089
    if ($A =~ s/tooltip//) { $TOOLTIP = 1 ;}
23 reyssat 1090
    if ($A =~ s/depth\s*=\s*([0-8])//) { $depth = $1 ; }
1091
    $OPTION .= $A ;
1092
   '' ;
1093
 }
1094
 
1095
sub store_include { my ($A) = @_ ; $A = join(' ' , split(',', $A)) ;
1096
    if ($A =~ s/(\w*)\.(tex|sty)//) {
1097
         if ($MACRO) {$MACRO .= ",$1.$2" } else {$MACRO = "$1.$2" }
1098
    };
1099
    if ($A =~ s/(\w*\.css)\b//) { if ($STYLE) {$STYLE .= ",$1" } else {$STYLE = $1 } };
1100
    if ($A =~ s/embed\s*=\s*([^}]+)//) { $EMBED = $1 ; }
1101
   '' ;
1102
 }
1103
 
1104
sub add { my ($a,$b)=@_ ;
1105
    if ($a) {$a .= ",$b" } else {$a = $b }
1106
 }
1107
 
1108
sub Init { my ($file, $ref_env, $ref_command, $ref, $ref_algo) = @_;
1109
  my ($total, $TEXT) = (0, find_expand($file));
4851 bpr 1110
  my %hash_spec = (wims =>{}, verbatim =>{}, lstlisting => {});
23 reyssat 1111
  my $ref_spec = \%hash_spec;
1112
  $TEXT = traite_special ($TEXT, $ref_spec->{wims}, $ref,'wims');
1113
  $TEXT = traite_special ($TEXT, $ref_spec->{verbatim}, $ref,'verbatim');
4851 bpr 1114
  $TEXT = traite_special ($TEXT, $ref_spec->{lstlisting}, $ref,'lstlisting');
23 reyssat 1115
  $TEXT = traitement_initial ($TEXT);
1116
  $TEXT =~ s/\\wimsoption\s*\{([^\}]+)\}/store_option($1)/eg ;
1117
  $TEXT =~ s/\\makeindex/store_option('index')/eg ;
1118
  $TEXT =~ s/\\wimsinclude\s*\{([^\}]+)\}/store_include($1)/eg ;
1119
  $TEXT =~ s/\\(title|email|author|about)\s*(\[[^\]]+\])?\s*\{([^\}]+)\}/store($ref,$1,'main',$3,$2)/eg ;
1120
 
1121
  $TEXT = traite_preambule ($TEXT, $ref_env, $ref_command, $ref);
1122
  for my $command (keys %{$ref_command->{definition}}) {
1123
    $TEXT = Traite_command ($TEXT, $command, $ref_command);
1124
  }
1125
  for my $A (@liste_voca) {
1126
   $ref_algo->{titre}{$A} =  $ref_command->{definition}{"algorithmic\L$A\E"}  if ($ref_command->{definition}{"algorithmic\L$A\E"}) ;
1127
} ;
4851 bpr 1128
  $TEXT =~ s/wimsinsertion(\d*)/$ref_spec->{'wims'}{$1}/g;
4858 bpr 1129
  $TEXT =~ s/verbatiminsertion(\d*)/verbatim($ref_spec->{'verbatim'}{$1},$1)/eg;
4851 bpr 1130
  $TEXT =~ s/lstlistinginsertion(\d*)/lstlisting($ref_spec->{'lstlisting'}{$1},$1)/eg;
23 reyssat 1131
  $TEXT;
1132
}
1133
 
1134
#sub store_makeindex { my ($txt) = @_ ;
1135
#    $txt =~ s/\\printindex/\link{index}{Index}/ ;
1136
#   }
1137
sub traitement_final { my ($TEXT) = @_;
1138
 #FIXME : je ne peux pas faire ca à cause des exercices de développement dont l'adresse
1139
 #contienne un ~. De toute facon
1140
 #ca ne devrait pas exister, mais quand même. ou les wims only
1141
#   $TEXT =~ s/~/&nbsp;/g;
6858 bpr 1142
   $TEXT =~ s:(<br class="spacer" />\s*<br class="spacer" />):<br class="spacer" />:g;
1143
   $TEXT =~ s:<br class="spacer" />\s*(</?div):$1:g;
1144
   $TEXT =~ s:(class="minipage">)\s*<br class="spacer" />:$1:g;
23 reyssat 1145
   $TEXT;
1146
}
1147
 
1148
sub traitemath {my ($txt) = @_;
1149
  my $test = 0;
1150
  while ($txt =~ /\$\$/) {
6169 bpr 1151
    $txt = $` . ($test == 0 ? '<div class="math">\\(\\displaystyle{' : '}\\)</div>') . $';
23 reyssat 1152
    $test = 1-$test;
1153
  }
1154
  $txt;
1155
}
1156
 
1157
sub traitement_initial { my ($TEXT) = @_;
1158
  $TEXT =~ s/\s*$//; # strip trailing whitespace
1159
  $TEXT =~ s/\r\n/\n/gs ;
1160
  $TEXT = traitemath($TEXT);
1161
  $TEXT =~ s/{\s*\\(bf|tt|it)\b/\\text$1\{/g;
1162
  $TEXT =~ s/\\begin\b\s*/\\begin/g;
1163
  $TEXT =~ s/\\end\b\s*/\\end/g;
1164
  #$TEXT =~ s/\\text\b/\\hbox/g;
1165
  $TEXT =~ s,\\\[,<p class="math">\\(,g;
1166
  $TEXT =~ s,\\\],\\)</p>,g;
1167
  $TEXT =~ s/\$([^\$]+)\$/\\( $1 \\)/g;
1168
 
1169
  $TEXT =~ s/\\`\s*{a}/à/g;
1170
  $TEXT =~ s/\\\^\s*{a}/â/g;
1171
  $TEXT =~ s/\\'\s*{a}/á/g;
1172
  $TEXT =~ s/\\'\s*{e}/é/g;
1173
  $TEXT =~ s/\\`\s*{e}/è/g;
1174
  $TEXT =~ s/\\\^\s*{e}/ê/g;
1175
  $TEXT =~ s/\\\^\s*{i}/î/g;
1176
  $TEXT =~ s/\\\`\s*{i}/ì/g;
1177
  $TEXT =~ s/\\\"\s*{i}/ï/g;
178 bpr 1178
  $TEXT =~ s/\\\"\s*{\\i}/ï/g;
23 reyssat 1179
  $TEXT =~ s/\\\^\s*{o}/ô/g;
1180
  $TEXT =~ s/\\\"\s*{o}/ö/g;
1181
  $TEXT =~ s/\\\`\s*{o}/ò/g;
1182
  $TEXT =~ s/\\\^\s*{u}/û/g;
1183
  $TEXT =~ s/\\`\s*{u}/ù/g;
1184
  $TEXT =~ s/\\c\s*\{c\}/ç/g;
1185
 
1186
  $TEXT =~ s/\\`\s*a/à/g;
1187
  $TEXT =~ s/\\\^\s*a/â/g;
1188
  $TEXT =~ s/\\'\s*a/á/g;
1189
  $TEXT =~ s/\\'\s*e/é/g;
1190
  $TEXT =~ s/\\`\s*e/è/g;
1191
  $TEXT =~ s/\\\^\s*e/ê/g;
3846 bpr 1192
  $TEXT =~ s/\\`\s*E/È/g;
1193
  $TEXT =~ s/\\\'\s*E/É/g;
1194
  $TEXT =~ s/\\\^\s*E/Ê/g;
23 reyssat 1195
  $TEXT =~ s/\\\^\s*i/î/g;
1196
  $TEXT =~ s/\\\`\s*i/ì/g;
1197
  $TEXT =~ s/\\\"\s*i/ï/g;
1198
  $TEXT =~ s/\\\^\s*o/ô/g;
1199
  $TEXT =~ s/\\\"\s*o/ö/g;
1200
  $TEXT =~ s/\\\`\s*o/ò/g;
1201
  $TEXT =~ s/\\\^\s*u/û/g;
1202
  $TEXT =~ s/\\`\s*u/ù/g;
1203
  $TEXT =~ s/\\c \s*c/ç/g;
1204
 
1205
  $TEXT =~ s/{\s*\\`\s*a\s*}/à/g;
1206
  $TEXT =~ s/{\s*\\\^\s*a\s*}/â/g;
1207
  $TEXT =~ s/{\s*\\'\s*a\s*}/á/g;
1208
  $TEXT =~ s/{\s*\\'\s*e\s*}/é/g;
1209
  $TEXT =~ s/{\s*\\`\s*e\s*}/è/g;
1210
  $TEXT =~ s/{\s*\\\^\s*e\s*}/ê/g;
1211
  $TEXT =~ s/{\s*\\\^\s*i\s*}/î/g;
1212
  $TEXT =~ s/{\s*\\\`\s*i\s*}/ì/g;
1213
  $TEXT =~ s/{\s*\\\"\s*i\s*}/ï/g;
1214
  $TEXT =~ s/{\s*\\\^\s*o\s*}/ô/g;
1215
  $TEXT =~ s/{\s*\\\"\s*o\s*}/ö/g;
1216
  $TEXT =~ s/{\s*\\\`\s*o\s*}/ò/g;
1217
  $TEXT =~ s/{\s*\\\^\s*u\s*}/û/g;
1218
  $TEXT =~ s/{\s*\\`\s*u\s*}/ù/g;
1219
  $TEXT =~ s/{\s*\\c \s*c\s*}/ç/g;
6693 bpr 1220
  $TEXT =~ s/\\maketitle//g;
3846 bpr 1221
  $TEXT =~ s/{\\textquotesingle}/'/g;
1222
  $TEXT =~ s/\\textquotesingle/'/g;
1223
  $TEXT =~ s/\\guillemotleft/<</g;
1224
  $TEXT =~ s/\\guillemotright/>>/g;
4627 bpr 1225
# MODIF YVES NOEL 19/09/2011 (debut)
1226
  $TEXT =~ s/>>/&gt;&gt;/g;
1227
  $TEXT =~ s/~~/&nbsp;&nbsp;/g;
1228
  $TEXT =~ s/\\onslide\+<[0-9]*>//g;
1229
  $TEXT =~ s/\\onslide<[0-9]*->//g;
1230
  $TEXT =~ s/\\onslide<[0-9]*>//g;
1231
  $TEXT =~ s/\\onslide\+<[0-9]*\-[0-9]*>//g;
1232
  $TEXT =~ s/\\onslide<[0-9]*\-[0-9]*->//g;
1233
  $TEXT =~ s/\\onslide<[0-9]*\-[0-9]*>//g;
6003 bpr 1234
  $TEXT =~ s/\[<\+->\]//g;
1235
 
4627 bpr 1236
# MODIF YVES NOEL 19/09/2011 (fin)
3846 bpr 1237
  $TEXT =~ s/{}//g;
1238
  $TEXT =~ s/\\selectlanguage{french}\\sffamily//g;
23 reyssat 1239
 
1240
 #$TEXT =~ s/([^\\])\%+/$1/g;
1241
  $TEXT =~ s/([^\\])\%.*/$1/g;
1242
  $TEXT =~ s/\n\%.*/\n/g;
1243
  $TEXT =~ s/\\\\\[\S*\]/\n\n/g;
1244
  $TEXT =~ s/\\(vspace|hspace|vskip|hskip)\**{\S*}//g;
1245
  $TEXT =~ s/\\parskip=*[a-z0-9 \.]+//g;
1246
  $TEXT =~ s/\\(vskip|hskip)\s*[a-z0-9 \.]+//g;
1247
  $TEXT =~ s/\\smallskip/\n/g;
1248
  $TEXT =~ s/\\(med|big)skip/\n\n/g;
1249
#  $TEXT =~ s/\\makebox\[(\w)cm\]{ }/\&nbsp\&nbsp\&nbsp/g;
1250
  $TEXT =~ s/~(:|;|\?|\!)/&nbsp;$1/g;
1251
 #utiliser verb uniquement dans le cas d'un mot
1252
#FIXME:  $TEXT =~ s/\verb"([^"]+)"/<tt class=verb>$1<\/tt>/g;
4852 bpr 1253
  $TEXT =~ s/\\includegraphics\s*\[(.*)\]\s*{(.*)}/includegraphics($2,$1)/eg;
1254
  $TEXT =~ s/\\includegraphics\s*{([^}]+)}/includegraphics($1)/eg;
23 reyssat 1255
  $TEXT =~ s/\\(begin|end){document}/\\document /g;
1256
  $TEXT =~ s/\\exercise{module=([^\&]+)\&([^}]+)}{([^}]+)}/store_sheet($1,$2,$3,$worksheet)/eg ;
734 bpr 1257
  $TEXT =~ s/\\xspace//g;
5362 bpr 1258
  $TEXT = traite_beamer($TEXT) ;
23 reyssat 1259
  $TEXT;
1260
}
1261
 
5362 bpr 1262
sub traite_beamer {  my ($TEXT) = @_;
1263
   $TEXT =~ s/\\uncover\s*(<([^>]+)>)?\s*{(.*)}/\\fold{.}{-->}{$3}/g ;
1264
   $TEXT =~ s/\s*\\frametitle{([^}]+)}/store_frametitle($1)/ge;
6104 bpr 1265
   $TEXT =~ s/\\pause//g;
5362 bpr 1266
   $TEXT ;
1267
}
1268
 
1269
sub store_frametitle{ my ($TEXT)= @_ ;
1270
  "<div class=\"frametitle\">$TEXT</div>"
1271
}
4852 bpr 1272
sub linewidth { my ($line,$w)= @_ ;
4858 bpr 1273
  $line =~ s/1\.[0]\s*\\(line|text)width/100\%/g;
1274
  $line =~ s/0?\.([0-9])\s*\\(line|text)width/$1 0\%/g;
1275
  $line =~ s/0?\.([0-9]{2})[0-9]?\s*\\(line|text)width/$1\%/g;
4850 bpr 1276
  $line =~ s/ //g;
4852 bpr 1277
  $line = "$w=\"$line\"" if ($w) ;
1278
  $line ;
4850 bpr 1279
}
178 bpr 1280
sub store_sheet { my ($ad1,$ad2,$titre,$worksheet) = @_ ;
1281
   $ad2 =~ s/worksheet=(\d)+//g ;
1282
   $SHEET .= ":$ad1\n$ad2\n$titre\n\n" ;
23 reyssat 1283
   "\\exercise\{module=$ad1\&$ad2\&worksheet=$worksheet\}\{$titre\}" ;
1284
 }
1285
 
1286
sub traite_preambule { my ($TEXT, $ref_env, $ref_command, $ref) = @_;
1287
  if ($TEXT=~ s/\\usepackage\[([^]]+)\]\{algorithmic\}//) {
1288
       $algo_noend = 1  if ($1 =~ /noend/);
7228 bpr 1289
  };
1290
  $TEXT = recup_command($TEXT, $ref_command);
1291
  $TEXT = traite_command($TEXT, $ref_command);
23 reyssat 1292
  $TEXT = recup_environ($TEXT, $ref_env);
7228 bpr 1293
 
1294
 
23 reyssat 1295
  $TEXT = recup_embed($TEXT, $ref) ;
1296
  for my $cmd ('ref','index') {
1297
     $ref->{titb}{$cmd} = $ref_command->{definition}{$cmd . "name"}
1298
  }
1299
  $TEXT;
1300
}
1301
 
1302
sub store_embed { my ($id, $titre, $ref) = @_ ;
1303
     $ref->{titb}{$id} = $titre ; $ref->{text}{$id} = `cat $EMBED/$id` ;
1304
     $ref->{upbl}{$id}='main'; $ref->{type}{$id}='embed';
1305
     "\\embed{$id}{$titre}" ;
1306
}
1307
 
1308
sub recup_embed { my ($TEXT, $ref) = @_ ;
1309
     $TEXT =~ s /\\embed\s*{([^}]+)}\s*{(.*)}/store_embed ($1, $2, $ref)/eg ;
1310
     $TEXT ;
1311
 }
1312
 
1313
#FIXME on ne peut prendre qu'un seul fichier de style
1314
#crée le fichier 1/.def
1315
sub def { my ($ref, @style) = @_;
1316
  my $tit = $ref->{title}{main};
1317
  my $aut = $ref->{author}{main};
1318
  my $mail= $ref->{email}{main};
1319
  my $datm= $ref->{datm}{main};
6644 bpr 1320
  my $header = '<link rel="stylesheet" href="html/themes/_css/l2w.css" type="text/css" />'  ;
23 reyssat 1321
  my $header_tmp ;
1322
  for my $file (@style){
1323
    if (!open(IN, $DIR . $file)) { die "le fichier $DIR$file n'existe pas";}
1324
    open (IN, $DIR . $file);
1325
    ($header_tmp = <IN>) =~ s/\n/\t/g;
1326
    $header .= "\t $header_tmp" ;
1327
  }
1328
  close IN;
6644 bpr 1329
  $header .= "\t<script type=\"text/javascript\">"
1330
   . "jQuery(function() {jQuery( \"#left_toc\" ).menu();});"
1331
   . "jQuery(function() {jQuery( \"#right_toc\" ).menu();});"
6619 bpr 1332
   . "</script>" if ($TOOLTIP==1);
1333
 
23 reyssat 1334
"copyright=gnu
1335
docopen=yes
1336
dlang=fr
1337
tit=$tit
1338
author=$aut
1339
email=$mail
1340
header=$header
1341
datm=$datm";
1342
}
1343
#TODO en fait il faudrait renvoyer dans le cas ou le fichier est de type fold à la page en dessus
1344
# dépliée. Je ne suis pas sure de savoir faire ! sinon, on perd la table des matières.
1345
 
1346
sub store_ref { my ($link, $titre, $anchor, $ref_bloc) = @_;
1347
  my $txt = '' ;
1348
  my @list = (split(',', $link)) ;
1349
  for my $l (@list) {
1350
  dbg("... référence fichier: \"$l\" titre \"$l\"");
1351
  my $page = $ref_bloc->{fichier}{$l} ;
1352
  warn  "label $link n'existe pas" if !($page) ;
1353
  $txt .= ($#list) ? " \\link{$page}{$l}{$anchor}":
1354
             " \\link{$page}{$titre}{$anchor}";
1355
 }
1356
 $txt ;
1357
};
1358
 
1359
 
1360
#crée la page
1361
 
743 bpr 1362
sub toc_HTML {my ($text, $toc_g, $toc_d, $CHEMIN_up, $CHEMIN_down, $index) = @_ ;
5362 bpr 1363
   my $s='' ;
1364
   $s= "l"  if($toc_g) ; $s .= "r" if($toc_d) ;
23 reyssat 1365
  if (($toc_g) || ($toc_d)) {
5362 bpr 1366
    $CHEMIN_up . '<div class="doc_latex2wims' . $s . '">'
6638 bpr 1367
   . (($toc_g) ? '<ul id="left_toc" class="left_toc">'. $toc_g
6614 bpr 1368
   . $index . '</ul>' : '')
5362 bpr 1369
   . '<div class="wimsdoc">'
23 reyssat 1370
   . $text
5362 bpr 1371
   . '</div>'
6614 bpr 1372
   . (($toc_d) ? '<ul id="right_toc" class="right_toc">'
23 reyssat 1373
   . $toc_d
5874 bpr 1374
   . '<div class="wimscenter">'
23 reyssat 1375
   . $LOAD
5874 bpr 1376
   . '</div></div>' : '')
5664 bpr 1377
   . $CHEMIN_down .
1378
   '</div>';
1379
 
5362 bpr 1380
   }
1381
 
743 bpr 1382
   else {$CHEMIN_up . $text . $CHEMIN_down };
23 reyssat 1383
 }
1384
 
1385
 #################################
1386
 
1387
sub store_algo { my ($txt, $acc, $cmd, $comment, $indent) = @_ ;
1388
  $txt .= " " . $acc ;
1389
  $txt .= $hash_algo{titre}{$hash_algo{suite}{$cmd}} if  ($hash_algo{suite}{$cmd}) ;
1390
  $indent = $indent + $hash_algo{avant}{$cmd} ;
6472 bpr 1391
  if($hash_algo{titre}{$cmd}) {
1392
   $txt = "\n" . indent($indent) .  $hash_algo{titre}{$cmd} . $txt  if ( !($cmd =~ /END/) || $algo_noend == 0);
1393
  }
1394
  else
1395
  {
1396
   $txt = "\n" . indent($indent) . $txt  if ( !($cmd =~ /END/) || $algo_noend == 0);
1397
  }
1398
  $txt .=  indent('3') . "{<i>$comment</i>}" if ($comment) ;
23 reyssat 1399
  $txt .= "\n" ;
1400
  $txt =~ s/\n+/\n/ ;
1401
  $indent += $hash_algo{apres}{$cmd} ;
1402
  ($txt, $indent) ;
1403
}
1404
 
1405
sub algorithm { '<div class="algorithm">' . algorithmic (@_) . '</div>' } ;
1406
 
1407
sub algorithmic { my ($Text) = @_;
1408
  my $text ='' ; my $indent = 0 ;
1409
  my @T = split('\n', $Text);
1410
  for my $i (1..$#T) {
1411
    my $ligne = $T[$i];
1412
 #   $ligne =~ s/\$([^\$]+)\$/$1/g;
1413
  #  $ligne =~ s/\\\(//g;
1414
 #   $ligne =~ s/\\\)//g;
1415
    $ligne =~ s/\$([^\$]+)\$/\\( $1 \\)/g;
1416
    $ligne =~ s/^\[H\]//;
1417
    next if ($ligne =~ /\\begin\{algorithmic\}\[1\]/);
1418
    $ligne = "\n\n" if ($ligne =~ /\\end\{algorithmic\}/);
1419
    $ligne =~ s/\\caption\s*\{\s*([^\}]*)\}/<h4 class=\"algo_titre\"> $1 <\/h4>/;
1420
    $ligne =~ s/\\cdots/.../g;
1421
    $ligne =~ s/\\geq/>=/g;
1422
    $ligne =~ s/\\leq/<=/g;
1423
    $ligne =~ s/\\bmod/mod/g;
1424
#    $ligne =~ s/\[/<i>/g;
1425
#    $ligne =~ s/\]/<\/i>/g;
1426
    my $cle = 'FOR|WHILE|IF|UNTIL|ELSE|ELSIF|REQUIRE|ENSURE|STATE|ENDFOR|ENDWHILE|ENDIF' ;
1427
    if ($ligne =~ /\s*\\($cle)\s*(\[[^\]]+\]*)?\{([^\n]+)\}\s*([^\n]*)/) {
1428
         ($ligne,$indent) = store_algo($3, $4, $1, $2, $indent) ;
1429
    }    
1430
    if ($ligne =~ /\s*\\($cle)\s*(\[[^\]]+\]*)?\s*([^\n]*)/) {($ligne,$indent) = store_algo('',$3, $1, $2, $indent);}
1431
  #TODO accepter des commentaires de plusieurs lignes ; présentation
1432
    #des commentaires ?
1433
    if ($ligne =~ /^\s*\\COMMENT/) { $ligne = "<i>$ligne</i>\n" };
1434
    $text .= "\n" . $ligne;
6472 bpr 1435
    $text =~ s/\n{2,}/\n/g;
1436
    $text =~ s/(<br\s*\/>)+/<br \/>/g;
1437
    $text =~ s/(<br>)+/<br \/>/g;
23 reyssat 1438
  }
6472 bpr 1439
  $text =~ s/\n/<br \/>/g;
23 reyssat 1440
  $text ;
1441
}
1442
 
1443
sub indent { my $espace = "&nbsp;" x  6 ; $espace x $_[0]; }
1444
 
1445
sub Numero { my ($id) = @_;
1446
  return '' if ($id =~ /F_[^S]\d/)  || ($id =~ /L_/); #cela ne provient pas d'une section et co
1447
  $id =~ s /(F|L)_//g;
1448
  $id =~ s/mainS(\d+)/&Roman($1)/e;
1449
  $id =~ s/S(\d+)S(\d+)S(\d+)/-$1-$2-$3/;
1450
  $id =~ s/S(\d+)S(\d+)/-$1-$2/;
1451
  $id =~ s/S(\d+)/-$1/;
1452
  $id;
1453
}
1454
 
1455
# permet de faire modifier quelque chose dans la table pour un tag
1456
#TODO j'ai rajouté l'option couleur, du coup je ne sais plus faire fonctionner le shif
1457
sub selection { my ($text, $couleur, @tag) = @_ ;
1458
  return '' if !defined($text);
1459
  for my $ta (@tag) {
6614 bpr 1460
    $text =~ s/XXXX="$ta">/li class="$couleur">/g;
1461
    $text =~ s/YYYY="$ta">/\/li>/g;
23 reyssat 1462
  };
1463
  $text;
1464
}
1465
 
734 bpr 1466
sub clean { my ($text, $ref) = @_;
23 reyssat 1467
  return '' if !defined($text);
6614 bpr 1468
   $text =~ s/<XXXX="\w*">/<li class="no_selected">/g;
1469
   $text =~ s/<YYYY="\w*">/<\/li>/g;
734 bpr 1470
   $text =~ s/ZZZZZ(\w+)/store_tip($1,$ref)/ge;
23 reyssat 1471
   $text;
1472
}
1473
 
734 bpr 1474
sub store_tip { my ($tag,$ref)=@_ ;
1475
  my $tip = $ref->{toctip}{$tag} ;
3552 bpr 1476
  my $title=$ref->{tittoc}{$tag} ;
6614 bpr 1477
  #$tip =~ s/'/\\\\'/g if ($tip) ; 
1478
  $title =~ s/'/\\\\'/g if ($title) ;
1479
  $ref->{toctip}{$tag} ? "<ul>$tip</ul>" : '' ;
734 bpr 1480
}
1481
 
23 reyssat 1482
sub chemin { my ($tag, $ref) = @_;
1483
  my $tagsup = $tag;
1484
  my $ch = $tag;
1485
  my $txt = ($ref->{tittoc}{$tagsup}) ? $ref->{tittoc}{$tagsup} : $ref->{titb}{$tagsup} ;
1486
  my $niv = 0;
1487
  while ($tagsup !~ /^main\b/) {
1488
    $niv++;
1489
    $tagsup = $ref->{upbl}{$tagsup};
6644 bpr 1490
    $ch  = "$tagsup,$ch"; if (!$ref->{tittoc}{$tagsup}) { $ref->{tittoc}{$tagsup}=$tagsup};
1491
    $txt = "\\link{$tagsup}{$ref->{tittoc}{$tagsup}} $FLECHE $txt" if ($tagsup) ; #if ($tagsup !~ /^main\b/);
23 reyssat 1492
  }
1493
  $ref->{chemin}{$tag} = $ch;
1494
  $ref->{niveau}{$tag} = $niv;
1495
  return $LOAD if (!$txt);
743 bpr 1496
  '<div class="wims_chemin">' . $LOAD . "$linkout  $txt" . '</div>';}
23 reyssat 1497
 
1498
sub sortuniq {
1499
  my $prev = "not $_[0]";
1500
  grep { $_ ne $prev && ($prev = $_, 1) } sort @_;
1501
}
1502
 
1503
 
1504
sub isotime {
178 bpr 1505
    my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
23 reyssat 1506
    $year += 1900;
178 bpr 1507
    $mon += 1 ; $mday += 1 ;
23 reyssat 1508
    $mday = sprintf("%02d", $mday);
1509
    $mon  = sprintf("%02d", $mon);
1510
    "$year-$mon-$mday $hour:$min:$sec";
1511
}
1512
 
1513
 sub usage {
1514
  print STDERR << "EOT"
1515
latex2wims [--style=style.css] [--macro=wims.sty] [--dir=dossier1] [--docdir=dossier2] [--embed=dossier3] [--verbose] file
1516
 
1517
  --style=style.css : style.css fichier de style css à utiliser
1518
     (peut aussi être mis dans le fichier file : \\wimsinclude{style.css})
1519
  --macro=wims.sty : wims.sty fichier de style à utiliser
1520
     (peut aussi être mis dans le fichier file : \\wimsinclude{wims.sty})
1521
  --dir=dossier1 : dossier1 est le répertoire où se trouvent tous les fichiers dont le fichier file
1522
  --docdir=dossier2 : dossier2 est le répertoire dans lequel sera créé le document
1523
     (un dossier dans le compte Modtool par exemple)
1524
  --embed=dossier3 : les fichiers de dossier3 sont appelés dans file
1525
     par la commande \embed{} (pour expert)
1526
  --verbose : détails
1527
EOT
1528
;
1529
  exit 1;
1530
}
1531
 
1532
##======================================================================##
1533
## Adapted from work by OZAWA Sakuro <ozawa@prince.pe.u-tokyo.ac.jp>
1534
## Copyright (c) 1995 OZAWA Sakuro.  All rights reserved.  This
1535
## program is free software; you can redistribute it and/or modify
1536
## it under the same terms as Perl itself.
1537
##======================================================================##
1538
 
1539
sub Roman { my($arg) = shift;
1540
  my %roman_digit = qw(1 IV 10 XL 100 CD 1000 MMMMMM);
1541
  my @figure = reverse sort keys %roman_digit;
1542
  grep($roman_digit{$_} = [split(//, $roman_digit{$_}, 2)], @figure);
1543
 
1544
  my($x, $roman);
1545
  foreach (@figure) {
1546
    my($digit, $i, $v) = (int($arg / $_), @{$roman_digit{$_}});
1547
    if (1 <= $digit and $digit <= 3) {
1548
      $roman .= $i x $digit;
1549
    } elsif ($digit == 4) {
1550
      $roman .= "$i$v";
1551
    } elsif ($digit == 5) {
1552
      $roman .= $v;
1553
    } elsif (6 <= $digit and $digit <= 8) {
1554
      $roman .= $v . $i x ($digit - 5);
1555
    } elsif ($digit == 9) {
1556
      $roman .= "$i$x";
1557
    }
1558
    $arg -= $digit * $_;
1559
    $x = $i;
1560
  }
1561
  $roman
1562
 }