Subversion Repositories wimsdev

Rev

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