Subversion Repositories wimsdev

Rev

Rev 7232 | Rev 7395 | 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='';
7387 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
7387 bpr 50
#$doc_DIR=$ENV{'w_docdir'};
23 reyssat 51
#$DIR=$ENV{'w_dir'};
7387 bpr 52
#$DIR =~ s/ +//;
53
push (@ARGV,split(' ', $ENV{'wims_exec_parm'})) if ($ENV{'wims_exec_parm'}) ;
23 reyssat 54
 
55
while ($_ = shift (@ARGV))
56
{
57
  last if (!/^--/);
7387 bpr 58
     if (/^--style=(.*)$/) { $STYLE   = $1; }
23 reyssat 59
  elsif (/^--macro=(.*)$/) { $MACRO   = $1; }
60
  elsif (/^--docdir=(.*)$/){ $doc_DIR = $1; }
61
  elsif (/^--dir=(.*)$/)   { $DIR     = $1; }
7232 bpr 62
  elsif (/^--subdir=(.*)$/){ $SUBDIR  = $1; }
7387 bpr 63
  elsif (/^--embed=(.*)$/) { $EMBED   = $1; }
23 reyssat 64
  elsif (/^--verbose$/)    { $verbose = 1; }
65
  elsif (/^--author=(.*)$/){ $author  = $1; }
66
  elsif (/^--email=(.*)$/) { $email   = $1; }
7387 bpr 67
  elsif (/^--worksheet=(.*)$/) { $worksheet   = $1; }
743 bpr 68
  elsif (/^--tooltip_prompt=(.*)$/) { $tooltip_prompt = $1; }
4627 bpr 69
  elsif (/^--linkout=(.*)$/) { $linkout = $1; }
7387 bpr 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;';
7387 bpr 87
$linkout = "\\doc{module=$linkout}" . $FLECHE if ($linkout) ;
743 bpr 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>
7387 bpr 94
#type =
23 reyssat 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
);
7387 bpr 115
my @liste_env_list = ('itemize', 'description', 'enumerate', 'trivlist') ;
23 reyssat 116
  $hash_environ{type}{'description_item'} = 'fold' ;
117
  $hash_environ{titre}{'description_item'} = ' ' ;
118
 
7387 bpr 119
my @liste_env_tabular = ('tabular') ;
734 bpr 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',
7387 bpr 124
  'displaymath', 'math', 'center', 'minipage', 'lstlisting', 'columns', 'column', 'figure');
23 reyssat 125
 
7387 bpr 126
my @liste_com_spec = ('paragraph', 'href', 'url', 'exercise', 'doc') ; #je ne m'en sers pas encore
23 reyssat 127
 
7387 bpr 128
#commandes par défaut : sont écrasés par un \def ou un \defwims
129
 
23 reyssat 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
 
7387 bpr 164
#type sert à repérer les fichiers embed ou fold.
23 reyssat 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 => {},
7387 bpr 178
  type   => {},
23 reyssat 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',
7387 bpr 215
 'ENDWHILE', 'REQUIRE','ENSURE','ENDFOR','STATE','UNTIL','THEN', 'RETURN') ;
23 reyssat 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
 
7387 bpr 259
# lit les fichiers wims.sty  puis les fichers .tex. pour éviter : un seul fichier tex, le premier ?
23 reyssat 260
 
261
my $TEXT = Init($FILE, \%hash_environ, \%hash_command, \%hash, \%hash_algo);
262
out1 ('sheet', $SHEET) ;
7387 bpr 263
$ABOUT = $hash{about}{main} ;
23 reyssat 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
 
7387 bpr 279
 
23 reyssat 280
# PASSE 2: ferme les \section & co
281
my $SEC_MIN_GLOBAL = 10; # = \infty
7387 bpr 282
#$SEC_MIN_GLOBAL = 3 ;
23 reyssat 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;
7387 bpr 292
if($SEC_MIN_GLOBAL == 10) {$SEC_MIN_GLOBAL = 0} ;
23 reyssat 293
my ($NIVEAU, $NIVEAU_max) = ($SEC_MIN_GLOBAL, $SEC_MIN_GLOBAL + $depth - 1);
294
 
295
 
7387 bpr 296
 
23 reyssat 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
}
7387 bpr 323
 
324
 
325
# PASSE 6: sort l'index mis à jour des ref à l'aide des labels créés auparavant  et rajoute
23 reyssat 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};
7387 bpr 330
  my $cle = 'prev|next|upbl|titb|keyw|datm';
23 reyssat 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;
7387 bpr 335
  $T =~ s/($macro)\s*([.*])?\{([^}]+)\}/store_ref($3, ($2 ? "$3: $2" : $3), $3, \%hash_bloc)/eg;
23 reyssat 336
  $T =~ s/\\($cle)\s*\{(\w*)\}/store_tag($1, $2, $tag, \%hash, \%hash_bloc)/eg;
337
  $hash{text}{$tag} = $T;
338
}
7387 bpr 339
my @ListIndex = sort {$a cmp $b} (keys %{$hash_index{page}}) ;
5664 bpr 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 ...
7387 bpr 348
#TODO : \embed{toto} : je suppose ici que le contenu de toto est du wims à ne pas
23 reyssat 349
#interpréter
7387 bpr 350
#pour l'instant je m'en suis servi pour stocker des programmes qui interviennent plusieurs fois.
23 reyssat 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
7387 bpr 354
#TODO option = chemin tout seul, toc à gauche + chemin, toc à gauche et à droite + chemin,
23 reyssat 355
#style selection droite, selection gauche
356
 
357
for my $tag (keys %{$hash{text}}) {
358
  complete ($tag, \%hash);
7387 bpr 359
  out ("$tag.hd", hd($tag,\%hash));
23 reyssat 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>": '' ;
7387 bpr 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) : '';
7387 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) {
7387 bpr 437
    if (!$ref->{toctip}{$Id}) {$ref->{toctip}{$Id}=($ref->{tittoc}{$id}) ?
438
      "<li>\\link{$id}{$ref->{tittoc}{$id}}</li>": '' ; }
6614 bpr 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) = @_;
7387 bpr 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
 
7387 bpr 473
sub class_index { my ($index,$level) = @_ ;
474
     my @a = split('!', $index) ;
475
     ($a[$level])? $a[$level] : $index ;
23 reyssat 476
 }
477
 #!(keys %{$ref_index->{page}}
7387 bpr 478
 #sort keys %{$ref_index->{page}} ;
23 reyssat 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) {
7387 bpr 485
      next if ($dejavu =~ /\b$index\b/) ;
23 reyssat 486
      my @list = sort {$a cmp $b} grep {class_index($_, $level) eq class_index($index, $level)} @L ;
7387 bpr 487
      my @l = split('!', $index) ;
23 reyssat 488
      if ($l[$level]) {
7387 bpr 489
          $TEXT .= "<li> " ;
23 reyssat 490
          for my $ind (split(',',$ref_index->{page}{$index})) {
7387 bpr 491
             if ($ind && !($dejavu =~ /\b$ind\b/)) {
23 reyssat 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
 }
7387 bpr 520
 
734 bpr 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
}
7387 bpr 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
7387 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
  }
7387 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\"" ;
7387 bpr 569
      $e_class= "\/ul" ;
23 reyssat 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'){
7387 bpr 575
              $b_class = "ul style=\"list-style:none;\"" ;
23 reyssat 576
              $e_class= "\/ul" ;
577
              $e_item = "\\end{$environ\_item}" ;
578
              $b_item = "\\begin{$environ\_item}"
579
              }
7387 bpr 580
        elsif ($environ eq 'trivlist') { $b_class = "ul style=\"list-style:none;\"" ;
23 reyssat 581
               $e_class= "\/ul" ;
582
        }
7387 bpr 583
      };
23 reyssat 584
  $TEXT =~ s/\\begin{$environ(\*)?}/<$environ>/g;
585
  $TEXT =~ s|\\end{$environ(\*)?}|<\/$environ>|g;
7387 bpr 586
 
23 reyssat 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>");
7387 bpr 592
  my $milieu = "<$environ>" . $u[4] . "<\/$environ>"   ;
23 reyssat 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;
7387 bpr 598
  $milieu =~ s|<$environ>|<$b_class>|g;
23 reyssat 599
  $decoup[0] . $milieu . traite_list ($u[1], $ref, $ref_env, $Id, $environ,$option);
600
}
7387 bpr 601
 
23 reyssat 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;
7387 bpr 605
 
23 reyssat 606
  my @decoup = split ("<$environ>", $TEXT);
607
 
608
  my $a = join ("<$environ>", @decoup[1..$#decoup]);
609
  return $TEXT if (!$a);
7387 bpr 610
 
23 reyssat 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);
7387 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" ;
7387 bpr 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
7387 bpr 654
#rajoute main pour ceux qui n'ont pas de parents.
23 reyssat 655
sub complete {my ($tag, $ref) = @_;
7387 bpr 656
  $ref->{datm}{$tag} = isotime() if  !($ref->{datm}{$tag}) ;
23 reyssat 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>';
7387 bpr 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;
7387 bpr 693
  } else  {
23 reyssat 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};
7387 bpr 702
  my $opt= ($ref_env->{type}{$rubrique} && $ref_env->{type}{$rubrique}=~ /fold/) ? 'bloc' : 'full' ;
743 bpr 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
 
7387 bpr 714
sub tabular { my ( $b, $style ) = @_;
715
  my @v = extract_bracketed ($b, '{}') ;
734 bpr 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="\\\\\\(|\\\\\\)" ;
7387 bpr 723
  my @dectab = split(/$par/, $b) ;
724
  $b = $dectab[0] ;
3334 bpr 725
  $b =~ s|\\\\|</td></tr><tr class=\"$stylerow\"><td class=\"$stylecell\">|g;
7387 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;
7387 bpr 730
     $b .=  $c . (($dectab[2*$cnt+1]) ? "\\(" . $dectab[2*$cnt+1] .  "\\)" : '' )  ;
731
     $cnt ++ ;
743 bpr 732
 };
7387 bpr 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 ) = @_;
7387 bpr 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
}
7387 bpr 743
 
744
sub minipage { my ( $b ) = @_;
4865 bpr 745
 my @v = extract_bracketed ($b, '[]') ;
7387 bpr 746
 my $option = $v[0] ;
6858 bpr 747
 if($option) {
7387 bpr 748
 $option=~ s/\[\s*b\s*\]/bottom/ ;
4866 bpr 749
 $option=~ s/\[\s*t\s*\]/top/ ;
7387 bpr 750
 $option=~ s/\[\s*c\s*\]/middle/ ;
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\">
7387 bpr 759
   $v[1]
4850 bpr 760
   </div>";
761
}
7387 bpr 762
sub columns { my ( $b ) = @_;
5289 bpr 763
  "<div class=\"columns\">$b</div>"
5286 bpr 764
}
765
 
7387 bpr 766
sub column { my ( $b ) = @_;
5286 bpr 767
 my @v = extract_bracketed ($b, '[]') ;
6104 bpr 768
 my $option = '';
7387 bpr 769
 $option= $v[0] ;
6104 bpr 770
 if($option) {
7387 bpr 771
  $option=~ s/\[\s*b\s*\]/bottom/ ;
6104 bpr 772
  $option=~ s/\[\s*t\s*\]/top/ ;
7387 bpr 773
  $option=~ s/\[\s*c\s*\]/middle/ ;
774
  $option=~s/\[\s*\]/middle/;
6104 bpr 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\">
7387 bpr 782
   $v[1]
5286 bpr 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 };
7387 bpr 806
  $b =~ s/\n{2,}/\n/;
23 reyssat 807
  '<div class="math">' . $b . '</div>' ;
808
}
809
 
810
sub align1 { my ( $b) = @_;
811
  $b = "\\(\\begin{matrix}  $b  \\end{matrix} \\)";
812
  if ($b =~ s/\\label{([^\}])}//) { $b = "\\label{$1}" . $b };
813
  '<div class="math">' . $b . '</div>' ;
814
}
815
sub align { my ( $b) = @_;
6169 bpr 816
  $b =  '<table class="wimscenter wimsnoborder tableau" style="width:100%"><tr><td>\\(' . $b . '\\\\</table>';
23 reyssat 817
  if ($b =~ s/\\label{([^\}])}//) { $b = "\\label{$1}" . $b };
818
  $b =~ s|\&|\\)&nbsp;</td><td>&nbsp;\\(|g;
819
  $b =~ s|\\\\\s*</table>|\\)</td></tr></table>|g;
820
  $b =~ s|\\\\|\\)</td></tr><tr><td>\\(|g;
5291 bpr 821
  $b =~ s|\\\(\s+\\\)||g;
822
  $b =~ s|\n\\\)|\\\)|g;
823
  $b =~ s|\\\(\n|\\\(|g;
23 reyssat 824
  '<div class="math">' . $b . '</div>' ;
825
}
826
 
827
sub pmatrix {"\\left ( \\begin{matrix} " . $_[0] . "\\end{matrix} \\right )" ;}
828
sub smallmatrix {"\\left ( \\begin{matrix} " . $_[0] . "\\end{matrix} \\right )" ;}
829
sub eqnarray {" <div class=\"math\">\\(\\begin{matrix} " . $_[0] . "\\end{matrix})</div> " ;}
830
sub center {" <div class=\"center\">" . $_[0] . "</div>"}
831
 
832
sub array {my ( $b ) = @_ ;
833
  my @v = extract_bracketed ($b, '{}');
834
  "\\begin{matrix} " . $v[1] . "\\end{matrix} ";
835
}
836
#TODO pour l'instant
5292 bpr 837
sub cases {"\\left \\lbrace\\begin{matrix} " . $_[0] . "\\end{matrix} \\right ." ; }
23 reyssat 838
 
839
sub gather { my ($b) = @_;
840
  my @decoup = split ('\\\\intertext', $b);
841
  my $cnt = 1;
842
  $b = equation($decoup[0]);
843
  while ($cnt <= $#decoup) {
844
    my @a = extract_bracketed ( $decoup[$cnt], '{}' );
845
    my $c = $a[0];
846
    $c =~ s/\{(.*)\}/$1/;
847
    $b .= $c . equation($a[1]);
848
    $cnt ++;
849
  }
850
  $b;
851
}
852
 
6169 bpr 853
sub displaymath {"<div class=\"math\">\\(\\displaystyle{ " . $_[0]. "}\\)</div>"; }
23 reyssat 854
sub math {" \\( " . $_[0]. "\\) "; }
855
 
7387 bpr 856
sub figure { my $caption='';
857
  if ( $_[0] =~ s/\\caption\{([^}]+)}//) {$caption=$1 };
858
  "<div class=\"figure\"> " .
859
  (($caption) ? "<div class=\"caption\">". $caption . "</div>":"")
860
    . $_[0] . "</div>" ;}
23 reyssat 861
 
862
sub thebibliography { my ( $b ) = @_;
863
  $b =~ s/\\bibitem{([^}]+)}/<\/li>\n<li>\[$1\]\\label{$1} /g;
864
  $b =~ s/\{\d+\}\s*<\/li>//;
7387 bpr 865
  '<h2 class="thebibliography">' . $hash{titb}{ref}
23 reyssat 866
  . "</h2>\n<ul class=\"thebibliography\">$b </li></ul>\n";
867
}
868
sub pspicture { '<p>dessin à faire dans wims</p>' ; }
869
sub picture { '<p>dessin à faire dans wims</p>' ; }
870
 
871
#decoupe ce qui se trouve à l'intérieur de \begin{wims} \end{wims} pour ne pas y toucher.
4851 bpr 872
# même pour verbatim, lstlisting
23 reyssat 873
sub traite_special { my ( $TEXT, $ref_spec, $ref, $environ ) = @_;
874
  $TEXT = recup_embed($TEXT, $ref) ;
875
  $TEXT =~ s/\\begin{$environ}/<$environ>/g;
876
  $TEXT =~ s/\\end{$environ}/<\/$environ>/g;
877
  $TEXT =~ s/\r\n/\n/gs ;
878
  my @decoup = split ("<$environ>", $TEXT);
879
  my $cnt = 1;
880
  if ($#decoup) {
881
    $TEXT = $decoup[0];
882
    while ($cnt <= $#decoup) {
883
      my @a = extract_tagged("<$environ>" . $decoup[$cnt], "<$environ>");
884
      die "Bug dans $environ insertion: $decoup[$cnt]" if (!defined($a[4]));
885
      $TEXT .= $environ . 'insertion' . $cnt . $a[1];
886
      $ref_spec->{$cnt} = $a[4];
887
      $cnt++;
888
    }
889
  }
890
  $TEXT;
891
}
892
 
893
sub latexonly { '' }
894
 
895
sub store_cmd { my ($def, $id, $narg, $val, $ref_command) = @_;
896
  if ($hash_secinv{$id}) {
897
    warn " ATTENTION : commande perso \"$id: argument invalide\n";
898
    return '';
899
  }
900
  my $origin = $ref_command->{origin}{$id};
901
  return '' if ($origin && $origin eq 'defwims' && $def ne 'defwims');
902
  $ref_command->{origin}{$id} = $def;
903
  $ref_command->{cnt_arg}{$id} = $narg;
904
  $ref_command->{definition}{$id} = $val;
905
  dbg("... commande perso \"$id\": argument: \"$narg\" definition: \"$val\"");
906
 '' ;
907
}
7387 bpr 908
 
23 reyssat 909
sub store_environ { my ($def, $cmd, $narg, $titre, $deb, $fin, $ref_env) = @_;
910
  my $origin = $ref_env->{origin}{$cmd};
911
  return '' if ($origin && $origin eq 'environmentwims' && $def ne 'environmentwims');
912
  $ref_env->{deb}{$cmd} = $deb;
913
  $ref_env->{fin}{$cmd} = $fin;
914
  $ref_env->{cnt_arg}{$cmd} = $narg;
7228 bpr 915
## one can have macro in definition of an environment - not general but useful for language
916
  if ($titre=~/\\(\w+)/) {
917
    $titre=$hash_command{definition}{$1} if $hash_command{definition}{$1}
918
  }
23 reyssat 919
  $ref_env->{titre}{$cmd} = $titre;
920
  $ref_env->{origin}{$cmd} = $def;
921
  my $style = $ref_env->{style}{$cmd} ;
7387 bpr 922
  $style = ($style)? $style : $cmd ;
734 bpr 923
  #if (!$ref_env->{deb}{$cmd}) { $ref_env->{deb}{$cmd} = "<div class=\"$style\"> "; }
924
  #if (!$ref_env->{fin}{$cmd}) { $ref_env->{fin}{$cmd} = "</div> "; }
23 reyssat 925
  dbg("... environnement perso \"$cmd\" argument: \"$narg\" titre: \"$titre\" style: \"$style\" debut: \"$deb\"  fin: \"$fin\"");
926
  '';
927
}
928
 
929
 
930
sub recup_command {my ($TEXT, $ref_command) = @_;
931
  my $DEF = '(defwims|def|newcommand|renewcommand)';
7387 bpr 932
  #FIXME ? mauvais pour def \def\toto#1#2 ... demander de le refaire avec defwims s'il y a plus de 3 arguments ?
23 reyssat 933
  $TEXT =~ s/\\$DEF\s*\\(\w*)#(\d)\{(.*)\}/store_cmd($1,$2,$3,$4,$ref_command)/eg;
934
  #2 arguments
935
  $TEXT =~ s/\\$DEF\s*\\(\w*)#(\d)#(\d)\{(.*)\}/store_cmd($1,$2,$4,$5,$ref_command)/eg;
936
  $TEXT =~ s/\\$DEF\s*\\(\w*)\s*\{(.*)\}/store_cmd($1,$2,0,$3,$ref_command)/eg;
937
  #3 arguments
938
  $TEXT =~ s/\\$DEF\s*\\(\w*)#(\d)#(\d)#(\d)\{(.*)\}/store_cmd($1,$2,$5,$6,$ref_command)/eg;
939
  # newcommand avec paramètres
940
  $TEXT =~ s/\\$DEF\s*\{\\(\w*)\}\s*\[(\d)\]\s*\{(.*)\}/store_cmd($1,$2,$3,$4,$ref_command)/eg;
941
  # newcommand sans paramètres
942
  $TEXT =~ s/\\$DEF\s*\{\\(\w*)\}\s*\{(.*)\}/store_cmd($1,$2,0,$3,$ref_command)/eg;
943
  $TEXT;
944
}
945
 
946
sub recup_config { my ($cmd, $arg, $ref_env) = @_;
947
  my @L = (split (',', $arg));
948
  my $style = $L[0];
949
  my $type = 'style';
950
  if ($cmd eq 'typefold') { $style = 'fold';  $type = 'type' };
951
  if ($cmd eq 'typelink') { $style = 'link' ; $type = 'type' };
734 bpr 952
  if ($cmd eq 'tablewims') { $type = 'tabular' ; };
23 reyssat 953
  if ($cmd eq 'listwims') {                   $type = 'list' };
954
  for my $rubrique (@L) {
955
    $ref_env->{$type}{$rubrique} = $style;
956
    dbg("... commande $rubrique de $type $style");
7387 bpr 957
    if ($cmd eq 'listwims') { $ref_env->{'style'}{$rubrique . '_item'} = $style . '_item' ;
23 reyssat 958
                              $ref_env->{'titre'}{$rubrique . '_item'} = '' }
959
  }
7387 bpr 960
  push @liste_env_tabular, (keys %{$ref_env->{tabular}}) ;
23 reyssat 961
 '';
962
}
963
sub recup_environ {my ($TEXT, $ref_env) = @_;
964
  my $pat = '\s*\{(.*)\}';
734 bpr 965
  $TEXT =~ s/\\(typefold|typelink|samestyle|listwims|tablewims)\{([^\}]+)\}/recup_config($1,$2, $ref_env)/eg;
23 reyssat 966
  # {nom}[#param]{titre}{debut}{fin}
967
  $TEXT =~ s/\\(environmentwims|[re]?newenvironment)\s*\{(\w*)\}\[(\d)\]$pat$pat$pat/store_environ($1,$2,$3,$4,$5,$6,$ref_env)/eg;
968
  # {nom}{titre}{debut}{fin}
969
  $TEXT =~ s/\\(environmentwims|[re]?newenvironment)\s*\{(\w*)\}$pat$pat/store_environ($1,$2,0,'', $3,$4,$ref_env)/eg;
970
 #\newtheorem{nom}[]?{titre}[]?
971
  $TEXT =~ s /(\\newtheorem\*?)\s*\{(\w*)\}\s*(\[[^\]]*\])?\s*\{([^\}]*)\}\s*(\[[^\]]*\])?\s*/store_environ($1,$2,0,$4,'','',$ref_env)/ge;
972
  $TEXT;
973
}
974
 
975
#on suppose que le texte commence par \command{}{}{}{} avec le bon nombre d'arguments
976
 
977
sub traite_command {my ($TEXT, $command, $ref_command) = @_;
978
  my $cnt_arg = $ref_command->{cnt_arg}{$command};
979
  my $com = $ref_command->{definition}{$command};
980
  if ($cnt_arg) {
981
    $TEXT =~ s/\\$command\s*\{/\{/;
982
    $TEXT = join ( "  ", subst($TEXT, $cnt_arg, $com));
983
  } else {
984
    $TEXT =~ s/\\$command\_/$com . '_'/ge;
985
    $TEXT =~ s/\\$command\b/$com/ge;
986
  }
987
  $TEXT;
988
}
989
#Texte = {}{}{} ou []{}{} qui sont les arguments
990
sub subst { my ($TEXT, $cnt_arg, $com, $environ, $ref_env ) = @_;
991
  my (@a,$u,$v);
992
  my $cnt = 0;
993
  while ($cnt < $cnt_arg) {
994
    @a = extract_tagged($TEXT, '{','}');
995
    ($u,$v) = ($a[1],$a[4]);
996
    if (!$a[0]) {
734 bpr 997
      @a = extract_tagged($TEXT, '\[','\]');
998
      ($u,$v) = ($a[1],$a[4]);
23 reyssat 999
    }
1000
    $TEXT = $u;
1001
    $cnt ++;
7387 bpr 1002
     my $sub="" ;
920 bpr 1003
    if ($v) {
1004
       $sub = $environ && $ref_env->{titre}{$environ} ? join (' ' , ( $ref_env->{titre}{$environ}, $v)) : $v;}
7387 bpr 1005
    else {$sub=''} ;
1006
     if (($com) && ("#$cnt")) { $com =~ s/#$cnt/$sub/ge ;  }  ;
23 reyssat 1007
  }
1008
  ($com, $TEXT);
1009
}
1010
 
1011
sub Traite_command { my ($TEXT, $command, $ref_command) = @_;
1012
  my $cnt_arg = $ref_command->{cnt_arg}{$command};
1013
  if ($cnt_arg) {
1014
    my @decoup = split ("\\\\$command\{", $TEXT );
1015
    my $cnt = 1;
1016
    $TEXT = $decoup[0];
1017
    while ($cnt <= $#decoup) {
1018
      $TEXT .= traite_command ('{' . $decoup[$cnt], $command, $ref_command);
1019
      $cnt++;
1020
    }
1021
  } else {
1022
    while( $TEXT =~ /\\$command\b/) {
1023
      $TEXT = traite_command ($TEXT, $command, $ref_command);
1024
    }
1025
  }
1026
  $TEXT;
1027
}
1028
 
1029
my %outagain;
1030
sub out { my ($bloc, $text) = @_;
1031
  warn "Écrase $bloc" if ($outagain{$bloc});
1032
  $outagain{$bloc} = 1;
1033
  open  (OUT, ">$BASE/$bloc") || die "ne peut pas créer $BASE/$bloc";
1034
  print OUT $text ; close OUT;
1035
}
1036
 
1037
sub out1 { my ($bloc, $text) = @_;
1038
  warn "Écrase $bloc" if ($outagain{$bloc});
178 bpr 1039
  $outagain{$bloc} = 1;
23 reyssat 1040
  open  (OUT, ">$doc_DIR$bloc") || die "ne peut pas créer $doc_DIR/$bloc";
1041
  print OUT $text ; close OUT;
1042
}
1043
 
1044
sub out_def { my ($bloc, $text) = @_;
1045
  open  (OUT, ">$BASE0/$bloc") || die "peut pas créer $BASE0/$bloc";
1046
  print OUT $text ; close OUT;
1047
}
1048
 
1049
# PASSE 1: développe 'input/include'
7387 bpr 1050
sub find_expand { my ($file) = @_;
23 reyssat 1051
  if (!open(IN, $DIR . $file)) { warn "$DIR$file n'existe pas"; return; }
1052
  dbg("... lecture de $file");
1053
  my $text = <IN>; close(IN);
743 bpr 1054
  $text =~ s/([^%]\s*\\end{document})[[:print:][:space:]]+/$1/;
1055
  $text =~ s/([^%])\s*\\endinput[[:print:][:space:]]+/$1/;
1056
  $text =~ s/\%\\(input|include|wimsinclude)([^\n]+)?//g;
1057
  $text =~ s/\\(input|include|wimsinclude)\s*{?([a-zA-Z0-9\-_\/]+)\.(sty|tex)\s*}?/find_expand("$2.$3")/eg;
4851 bpr 1058
  $text =~ s/\\lstinputlisting\s*\{([a-zA-Z0-9\-_\/\.]+)\s*\}/"\\begin\{lstlisting\}\n" . find_expand($1) . "\n\\end\{lstlisting\}"/eg;
7387 bpr 1059
 
23 reyssat 1060
  $text;
1061
}
1062
 
1063
sub open_close { my ($sec, $cnt, $entre) = @_;
1064
  my ($txt) = '';
1065
  my ($ind) = $hash_secinv{$sec};
1066
  #ferme
1067
  for (my $i = $#SECTIONS; $i >= $ind; $i--)
1068
  {
1069
    if ($cnt->[$i]) { $txt .= "</$SECTIONS[$i]>\n"; $cnt->[$i] = 0; }
1070
  }
1071
  #ouvre
1072
  if (!$entre) {
1073
    $txt .= "<$sec>"; $cnt->[$ind] = 1;
1074
    $SEC_MIN_GLOBAL = $ind if ($ind < $SEC_MIN_GLOBAL && $ind);
1075
  }
1076
  $txt;
1077
}
1078
 
7387 bpr 1079
sub cnt_section { my ($sec, $cnt) = @_ ;
23 reyssat 1080
  my ($txt) = '';
1081
  my ($ind) = $hash_secinv{$sec};
1082
  $SEC_MIN_GLOBAL = $ind if ($ind < $SEC_MIN_GLOBAL && $ind);
1083
  "<$sec>"
1084
 }
1085
 
7387 bpr 1086
 
23 reyssat 1087
sub store { my ($ref, $cle, $id, $text, $court) = @_ ;
1088
   $ref->{$cle}{$id}=$text ;
1089
   if (defined($court)) { $court =~ s/\[|\]//g ; $ref->{tittoc}{$id} = $court ; } ;
1090
   '';
1091
 }
1092
sub store_option { my ($A) = @_ ; $A = join(' ' , split(',', $A)) ;
734 bpr 1093
    if ($A =~ s/numero//)  { $NUMERO = 1 ;} ;
23 reyssat 1094
    if ($A =~ s/index//)   { $INDEX = 1 ;}
734 bpr 1095
    if ($A =~ s/tooltip//) { $TOOLTIP = 1 ;}
23 reyssat 1096
    if ($A =~ s/depth\s*=\s*([0-8])//) { $depth = $1 ; }
1097
    $OPTION .= $A ;
1098
   '' ;
1099
 }
1100
 
1101
sub store_include { my ($A) = @_ ; $A = join(' ' , split(',', $A)) ;
7387 bpr 1102
    if ($A =~ s/(\w*)\.(tex|sty)//) {
1103
         if ($MACRO) {$MACRO .= ",$1.$2" } else {$MACRO = "$1.$2" }
23 reyssat 1104
    };
1105
    if ($A =~ s/(\w*\.css)\b//) { if ($STYLE) {$STYLE .= ",$1" } else {$STYLE = $1 } };
1106
    if ($A =~ s/embed\s*=\s*([^}]+)//) { $EMBED = $1 ; }
1107
   '' ;
1108
 }
1109
 
1110
sub add { my ($a,$b)=@_ ;
7387 bpr 1111
    if ($a) {$a .= ",$b" } else {$a = $b }
23 reyssat 1112
 }
1113
 
1114
sub Init { my ($file, $ref_env, $ref_command, $ref, $ref_algo) = @_;
1115
  my ($total, $TEXT) = (0, find_expand($file));
4851 bpr 1116
  my %hash_spec = (wims =>{}, verbatim =>{}, lstlisting => {});
23 reyssat 1117
  my $ref_spec = \%hash_spec;
1118
  $TEXT = traite_special ($TEXT, $ref_spec->{wims}, $ref,'wims');
1119
  $TEXT = traite_special ($TEXT, $ref_spec->{verbatim}, $ref,'verbatim');
4851 bpr 1120
  $TEXT = traite_special ($TEXT, $ref_spec->{lstlisting}, $ref,'lstlisting');
23 reyssat 1121
  $TEXT = traitement_initial ($TEXT);
1122
  $TEXT =~ s/\\wimsoption\s*\{([^\}]+)\}/store_option($1)/eg ;
1123
  $TEXT =~ s/\\makeindex/store_option('index')/eg ;
1124
  $TEXT =~ s/\\wimsinclude\s*\{([^\}]+)\}/store_include($1)/eg ;
1125
  $TEXT =~ s/\\(title|email|author|about)\s*(\[[^\]]+\])?\s*\{([^\}]+)\}/store($ref,$1,'main',$3,$2)/eg ;
7387 bpr 1126
 
23 reyssat 1127
  $TEXT = traite_preambule ($TEXT, $ref_env, $ref_command, $ref);
1128
  for my $command (keys %{$ref_command->{definition}}) {
1129
    $TEXT = Traite_command ($TEXT, $command, $ref_command);
1130
  }
1131
  for my $A (@liste_voca) {
1132
   $ref_algo->{titre}{$A} =  $ref_command->{definition}{"algorithmic\L$A\E"}  if ($ref_command->{definition}{"algorithmic\L$A\E"}) ;
1133
} ;
4851 bpr 1134
  $TEXT =~ s/wimsinsertion(\d*)/$ref_spec->{'wims'}{$1}/g;
4858 bpr 1135
  $TEXT =~ s/verbatiminsertion(\d*)/verbatim($ref_spec->{'verbatim'}{$1},$1)/eg;
4851 bpr 1136
  $TEXT =~ s/lstlistinginsertion(\d*)/lstlisting($ref_spec->{'lstlisting'}{$1},$1)/eg;
23 reyssat 1137
  $TEXT;
1138
}
1139
 
1140
#sub store_makeindex { my ($txt) = @_ ;
1141
#    $txt =~ s/\\printindex/\link{index}{Index}/ ;
1142
#   }
1143
sub traitement_final { my ($TEXT) = @_;
1144
 #FIXME : je ne peux pas faire ca à cause des exercices de développement dont l'adresse
1145
 #contienne un ~. De toute facon
1146
 #ca ne devrait pas exister, mais quand même. ou les wims only
1147
#   $TEXT =~ s/~/&nbsp;/g;
6858 bpr 1148
   $TEXT =~ s:(<br class="spacer" />\s*<br class="spacer" />):<br class="spacer" />:g;
1149
   $TEXT =~ s:<br class="spacer" />\s*(</?div):$1:g;
1150
   $TEXT =~ s:(class="minipage">)\s*<br class="spacer" />:$1:g;
23 reyssat 1151
   $TEXT;
1152
}
1153
 
1154
sub traitemath {my ($txt) = @_;
1155
  my $test = 0;
1156
  while ($txt =~ /\$\$/) {
6169 bpr 1157
    $txt = $` . ($test == 0 ? '<div class="math">\\(\\displaystyle{' : '}\\)</div>') . $';
23 reyssat 1158
    $test = 1-$test;
1159
  }
1160
  $txt;
1161
}
1162
 
1163
sub traitement_initial { my ($TEXT) = @_;
1164
  $TEXT =~ s/\s*$//; # strip trailing whitespace
7387 bpr 1165
  $TEXT =~ s/\r\n/\n/gs ;
23 reyssat 1166
  $TEXT = traitemath($TEXT);
1167
  $TEXT =~ s/{\s*\\(bf|tt|it)\b/\\text$1\{/g;
1168
  $TEXT =~ s/\\begin\b\s*/\\begin/g;
1169
  $TEXT =~ s/\\end\b\s*/\\end/g;
1170
  #$TEXT =~ s/\\text\b/\\hbox/g;
1171
  $TEXT =~ s,\\\[,<p class="math">\\(,g;
1172
  $TEXT =~ s,\\\],\\)</p>,g;
1173
  $TEXT =~ s/\$([^\$]+)\$/\\( $1 \\)/g;
1174
 
1175
  $TEXT =~ s/\\`\s*{a}/à/g;
1176
  $TEXT =~ s/\\\^\s*{a}/â/g;
1177
  $TEXT =~ s/\\'\s*{a}/á/g;
1178
  $TEXT =~ s/\\'\s*{e}/é/g;
1179
  $TEXT =~ s/\\`\s*{e}/è/g;
1180
  $TEXT =~ s/\\\^\s*{e}/ê/g;
1181
  $TEXT =~ s/\\\^\s*{i}/î/g;
1182
  $TEXT =~ s/\\\`\s*{i}/ì/g;
1183
  $TEXT =~ s/\\\"\s*{i}/ï/g;
178 bpr 1184
  $TEXT =~ s/\\\"\s*{\\i}/ï/g;
23 reyssat 1185
  $TEXT =~ s/\\\^\s*{o}/ô/g;
1186
  $TEXT =~ s/\\\"\s*{o}/ö/g;
1187
  $TEXT =~ s/\\\`\s*{o}/ò/g;
1188
  $TEXT =~ s/\\\^\s*{u}/û/g;
1189
  $TEXT =~ s/\\`\s*{u}/ù/g;
1190
  $TEXT =~ s/\\c\s*\{c\}/ç/g;
1191
 
1192
  $TEXT =~ s/\\`\s*a/à/g;
1193
  $TEXT =~ s/\\\^\s*a/â/g;
1194
  $TEXT =~ s/\\'\s*a/á/g;
1195
  $TEXT =~ s/\\'\s*e/é/g;
1196
  $TEXT =~ s/\\`\s*e/è/g;
1197
  $TEXT =~ s/\\\^\s*e/ê/g;
3846 bpr 1198
  $TEXT =~ s/\\`\s*E/È/g;
1199
  $TEXT =~ s/\\\'\s*E/É/g;
1200
  $TEXT =~ s/\\\^\s*E/Ê/g;
23 reyssat 1201
  $TEXT =~ s/\\\^\s*i/î/g;
1202
  $TEXT =~ s/\\\`\s*i/ì/g;
1203
  $TEXT =~ s/\\\"\s*i/ï/g;
1204
  $TEXT =~ s/\\\^\s*o/ô/g;
1205
  $TEXT =~ s/\\\"\s*o/ö/g;
1206
  $TEXT =~ s/\\\`\s*o/ò/g;
1207
  $TEXT =~ s/\\\^\s*u/û/g;
1208
  $TEXT =~ s/\\`\s*u/ù/g;
1209
  $TEXT =~ s/\\c \s*c/ç/g;
7387 bpr 1210
 
23 reyssat 1211
  $TEXT =~ s/{\s*\\`\s*a\s*}/à/g;
1212
  $TEXT =~ s/{\s*\\\^\s*a\s*}/â/g;
1213
  $TEXT =~ s/{\s*\\'\s*a\s*}/á/g;
1214
  $TEXT =~ s/{\s*\\'\s*e\s*}/é/g;
1215
  $TEXT =~ s/{\s*\\`\s*e\s*}/è/g;
1216
  $TEXT =~ s/{\s*\\\^\s*e\s*}/ê/g;
1217
  $TEXT =~ s/{\s*\\\^\s*i\s*}/î/g;
1218
  $TEXT =~ s/{\s*\\\`\s*i\s*}/ì/g;
1219
  $TEXT =~ s/{\s*\\\"\s*i\s*}/ï/g;
1220
  $TEXT =~ s/{\s*\\\^\s*o\s*}/ô/g;
1221
  $TEXT =~ s/{\s*\\\"\s*o\s*}/ö/g;
1222
  $TEXT =~ s/{\s*\\\`\s*o\s*}/ò/g;
1223
  $TEXT =~ s/{\s*\\\^\s*u\s*}/û/g;
1224
  $TEXT =~ s/{\s*\\`\s*u\s*}/ù/g;
1225
  $TEXT =~ s/{\s*\\c \s*c\s*}/ç/g;
6693 bpr 1226
  $TEXT =~ s/\\maketitle//g;
3846 bpr 1227
  $TEXT =~ s/{\\textquotesingle}/'/g;
1228
  $TEXT =~ s/\\textquotesingle/'/g;
1229
  $TEXT =~ s/\\guillemotleft/<</g;
1230
  $TEXT =~ s/\\guillemotright/>>/g;
4627 bpr 1231
# MODIF YVES NOEL 19/09/2011 (debut)
1232
  $TEXT =~ s/>>/&gt;&gt;/g;
1233
  $TEXT =~ s/~~/&nbsp;&nbsp;/g;
1234
  $TEXT =~ s/\\onslide\+<[0-9]*>//g;
1235
  $TEXT =~ s/\\onslide<[0-9]*->//g;
1236
  $TEXT =~ s/\\onslide<[0-9]*>//g;
1237
  $TEXT =~ s/\\onslide\+<[0-9]*\-[0-9]*>//g;
1238
  $TEXT =~ s/\\onslide<[0-9]*\-[0-9]*->//g;
1239
  $TEXT =~ s/\\onslide<[0-9]*\-[0-9]*>//g;
6003 bpr 1240
  $TEXT =~ s/\[<\+->\]//g;
7387 bpr 1241
 
4627 bpr 1242
# MODIF YVES NOEL 19/09/2011 (fin)
3846 bpr 1243
  $TEXT =~ s/{}//g;
1244
  $TEXT =~ s/\\selectlanguage{french}\\sffamily//g;
7387 bpr 1245
 
23 reyssat 1246
 #$TEXT =~ s/([^\\])\%+/$1/g;
1247
  $TEXT =~ s/([^\\])\%.*/$1/g;
1248
  $TEXT =~ s/\n\%.*/\n/g;
1249
  $TEXT =~ s/\\\\\[\S*\]/\n\n/g;
1250
  $TEXT =~ s/\\(vspace|hspace|vskip|hskip)\**{\S*}//g;
1251
  $TEXT =~ s/\\parskip=*[a-z0-9 \.]+//g;
1252
  $TEXT =~ s/\\(vskip|hskip)\s*[a-z0-9 \.]+//g;
1253
  $TEXT =~ s/\\smallskip/\n/g;
1254
  $TEXT =~ s/\\(med|big)skip/\n\n/g;
1255
#  $TEXT =~ s/\\makebox\[(\w)cm\]{ }/\&nbsp\&nbsp\&nbsp/g;
1256
  $TEXT =~ s/~(:|;|\?|\!)/&nbsp;$1/g;
1257
 #utiliser verb uniquement dans le cas d'un mot
1258
#FIXME:  $TEXT =~ s/\verb"([^"]+)"/<tt class=verb>$1<\/tt>/g;
4852 bpr 1259
  $TEXT =~ s/\\includegraphics\s*\[(.*)\]\s*{(.*)}/includegraphics($2,$1)/eg;
1260
  $TEXT =~ s/\\includegraphics\s*{([^}]+)}/includegraphics($1)/eg;
23 reyssat 1261
  $TEXT =~ s/\\(begin|end){document}/\\document /g;
7387 bpr 1262
  $TEXT =~ s/\\exercise{module=([^\&]+)\&([^}]+)}{([^}]+)}/store_sheet($1,$2,$3,$worksheet)/eg ;
734 bpr 1263
  $TEXT =~ s/\\xspace//g;
7387 bpr 1264
  $TEXT = traite_beamer($TEXT) ;
23 reyssat 1265
  $TEXT;
1266
}
1267
 
5362 bpr 1268
sub traite_beamer {  my ($TEXT) = @_;
7387 bpr 1269
   $TEXT =~ s/\\uncover\s*(<([^>]+)>)?\s*{(.*)}/\\fold{.}{-->}{$3}/g ;
5362 bpr 1270
   $TEXT =~ s/\s*\\frametitle{([^}]+)}/store_frametitle($1)/ge;
6104 bpr 1271
   $TEXT =~ s/\\pause//g;
5362 bpr 1272
   $TEXT ;
1273
}
1274
 
1275
sub store_frametitle{ my ($TEXT)= @_ ;
1276
  "<div class=\"frametitle\">$TEXT</div>"
1277
}
4852 bpr 1278
sub linewidth { my ($line,$w)= @_ ;
4858 bpr 1279
  $line =~ s/1\.[0]\s*\\(line|text)width/100\%/g;
1280
  $line =~ s/0?\.([0-9])\s*\\(line|text)width/$1 0\%/g;
1281
  $line =~ s/0?\.([0-9]{2})[0-9]?\s*\\(line|text)width/$1\%/g;
4850 bpr 1282
  $line =~ s/ //g;
4852 bpr 1283
  $line = "$w=\"$line\"" if ($w) ;
7387 bpr 1284
  $line ;
4850 bpr 1285
}
178 bpr 1286
sub store_sheet { my ($ad1,$ad2,$titre,$worksheet) = @_ ;
7387 bpr 1287
   $ad2 =~ s/worksheet=(\d)+//g ;
178 bpr 1288
   $SHEET .= ":$ad1\n$ad2\n$titre\n\n" ;
7387 bpr 1289
   "\\exercise\{module=$ad1\&$ad2\&worksheet=$worksheet\}\{$titre\}" ;
23 reyssat 1290
 }
7387 bpr 1291
 
23 reyssat 1292
sub traite_preambule { my ($TEXT, $ref_env, $ref_command, $ref) = @_;
1293
  if ($TEXT=~ s/\\usepackage\[([^]]+)\]\{algorithmic\}//) {
1294
       $algo_noend = 1  if ($1 =~ /noend/);
7228 bpr 1295
  };
1296
  $TEXT = recup_command($TEXT, $ref_command);
1297
  $TEXT = traite_command($TEXT, $ref_command);
23 reyssat 1298
  $TEXT = recup_environ($TEXT, $ref_env);
7387 bpr 1299
 
1300
 
23 reyssat 1301
  $TEXT = recup_embed($TEXT, $ref) ;
1302
  for my $cmd ('ref','index') {
7387 bpr 1303
     $ref->{titb}{$cmd} = $ref_command->{definition}{$cmd . "name"}
23 reyssat 1304
  }
1305
  $TEXT;
1306
}
1307
 
1308
sub store_embed { my ($id, $titre, $ref) = @_ ;
1309
     $ref->{titb}{$id} = $titre ; $ref->{text}{$id} = `cat $EMBED/$id` ;
1310
     $ref->{upbl}{$id}='main'; $ref->{type}{$id}='embed';
1311
     "\\embed{$id}{$titre}" ;
1312
}
1313
 
1314
sub recup_embed { my ($TEXT, $ref) = @_ ;
1315
     $TEXT =~ s /\\embed\s*{([^}]+)}\s*{(.*)}/store_embed ($1, $2, $ref)/eg ;
1316
     $TEXT ;
1317
 }
1318
 
1319
#FIXME on ne peut prendre qu'un seul fichier de style
1320
#crée le fichier 1/.def
1321
sub def { my ($ref, @style) = @_;
1322
  my $tit = $ref->{title}{main};
1323
  my $aut = $ref->{author}{main};
1324
  my $mail= $ref->{email}{main};
1325
  my $datm= $ref->{datm}{main};
6644 bpr 1326
  my $header = '<link rel="stylesheet" href="html/themes/_css/l2w.css" type="text/css" />'  ;
23 reyssat 1327
  my $header_tmp ;
1328
  for my $file (@style){
1329
    if (!open(IN, $DIR . $file)) { die "le fichier $DIR$file n'existe pas";}
1330
    open (IN, $DIR . $file);
1331
    ($header_tmp = <IN>) =~ s/\n/\t/g;
1332
    $header .= "\t $header_tmp" ;
1333
  }
1334
  close IN;
6644 bpr 1335
  $header .= "\t<script type=\"text/javascript\">"
7387 bpr 1336
   . "jQuery(function() {jQuery( \"#left_toc\" ).menu();});"
6644 bpr 1337
   . "jQuery(function() {jQuery( \"#right_toc\" ).menu();});"
6619 bpr 1338
   . "</script>" if ($TOOLTIP==1);
7387 bpr 1339
 
23 reyssat 1340
"copyright=gnu
1341
docopen=yes
1342
dlang=fr
1343
tit=$tit
1344
author=$aut
1345
email=$mail
1346
header=$header
1347
datm=$datm";
1348
}
1349
#TODO en fait il faudrait renvoyer dans le cas ou le fichier est de type fold à la page en dessus
1350
# dépliée. Je ne suis pas sure de savoir faire ! sinon, on perd la table des matières.
1351
 
1352
sub store_ref { my ($link, $titre, $anchor, $ref_bloc) = @_;
1353
  my $txt = '' ;
7387 bpr 1354
  my @list = (split(',', $link)) ;
23 reyssat 1355
  for my $l (@list) {
1356
  dbg("... référence fichier: \"$l\" titre \"$l\"");
1357
  my $page = $ref_bloc->{fichier}{$l} ;
1358
  warn  "label $link n'existe pas" if !($page) ;
1359
  $txt .= ($#list) ? " \\link{$page}{$l}{$anchor}":
1360
             " \\link{$page}{$titre}{$anchor}";
1361
 }
1362
 $txt ;
1363
};
1364
 
1365
 
1366
#crée la page
1367
 
743 bpr 1368
sub toc_HTML {my ($text, $toc_g, $toc_d, $CHEMIN_up, $CHEMIN_down, $index) = @_ ;
7387 bpr 1369
   my $s='' ;
5362 bpr 1370
   $s= "l"  if($toc_g) ; $s .= "r" if($toc_d) ;
23 reyssat 1371
  if (($toc_g) || ($toc_d)) {
5362 bpr 1372
    $CHEMIN_up . '<div class="doc_latex2wims' . $s . '">'
7387 bpr 1373
   . (($toc_g) ? '<ul id="left_toc" class="left_toc">'. $toc_g
6614 bpr 1374
   . $index . '</ul>' : '')
5362 bpr 1375
   . '<div class="wimsdoc">'
23 reyssat 1376
   . $text
5362 bpr 1377
   . '</div>'
6614 bpr 1378
   . (($toc_d) ? '<ul id="right_toc" class="right_toc">'
23 reyssat 1379
   . $toc_d
5874 bpr 1380
   . '<div class="wimscenter">'
23 reyssat 1381
   . $LOAD
5874 bpr 1382
   . '</div></div>' : '')
7387 bpr 1383
   . $CHEMIN_down .
5664 bpr 1384
   '</div>';
1385
 
5362 bpr 1386
   }
1387
 
743 bpr 1388
   else {$CHEMIN_up . $text . $CHEMIN_down };
23 reyssat 1389
 }
1390
 
1391
 #################################
1392
 
1393
sub store_algo { my ($txt, $acc, $cmd, $comment, $indent) = @_ ;
1394
  $txt .= " " . $acc ;
1395
  $txt .= $hash_algo{titre}{$hash_algo{suite}{$cmd}} if  ($hash_algo{suite}{$cmd}) ;
1396
  $indent = $indent + $hash_algo{avant}{$cmd} ;
6472 bpr 1397
  if($hash_algo{titre}{$cmd}) {
1398
   $txt = "\n" . indent($indent) .  $hash_algo{titre}{$cmd} . $txt  if ( !($cmd =~ /END/) || $algo_noend == 0);
1399
  }
1400
  else
1401
  {
1402
   $txt = "\n" . indent($indent) . $txt  if ( !($cmd =~ /END/) || $algo_noend == 0);
1403
  }
1404
  $txt .=  indent('3') . "{<i>$comment</i>}" if ($comment) ;
23 reyssat 1405
  $txt .= "\n" ;
1406
  $txt =~ s/\n+/\n/ ;
1407
  $indent += $hash_algo{apres}{$cmd} ;
1408
  ($txt, $indent) ;
1409
}
1410
 
1411
sub algorithm { '<div class="algorithm">' . algorithmic (@_) . '</div>' } ;
1412
 
1413
sub algorithmic { my ($Text) = @_;
1414
  my $text ='' ; my $indent = 0 ;
1415
  my @T = split('\n', $Text);
1416
  for my $i (1..$#T) {
1417
    my $ligne = $T[$i];
1418
 #   $ligne =~ s/\$([^\$]+)\$/$1/g;
1419
  #  $ligne =~ s/\\\(//g;
1420
 #   $ligne =~ s/\\\)//g;
1421
    $ligne =~ s/\$([^\$]+)\$/\\( $1 \\)/g;
1422
    $ligne =~ s/^\[H\]//;
1423
    next if ($ligne =~ /\\begin\{algorithmic\}\[1\]/);
1424
    $ligne = "\n\n" if ($ligne =~ /\\end\{algorithmic\}/);
1425
    $ligne =~ s/\\caption\s*\{\s*([^\}]*)\}/<h4 class=\"algo_titre\"> $1 <\/h4>/;
1426
    $ligne =~ s/\\cdots/.../g;
1427
    $ligne =~ s/\\geq/>=/g;
1428
    $ligne =~ s/\\leq/<=/g;
1429
    $ligne =~ s/\\bmod/mod/g;
1430
#    $ligne =~ s/\[/<i>/g;
1431
#    $ligne =~ s/\]/<\/i>/g;
1432
    my $cle = 'FOR|WHILE|IF|UNTIL|ELSE|ELSIF|REQUIRE|ENSURE|STATE|ENDFOR|ENDWHILE|ENDIF' ;
1433
    if ($ligne =~ /\s*\\($cle)\s*(\[[^\]]+\]*)?\{([^\n]+)\}\s*([^\n]*)/) {
1434
         ($ligne,$indent) = store_algo($3, $4, $1, $2, $indent) ;
7387 bpr 1435
    }
23 reyssat 1436
    if ($ligne =~ /\s*\\($cle)\s*(\[[^\]]+\]*)?\s*([^\n]*)/) {($ligne,$indent) = store_algo('',$3, $1, $2, $indent);}
1437
  #TODO accepter des commentaires de plusieurs lignes ; présentation
1438
    #des commentaires ?
1439
    if ($ligne =~ /^\s*\\COMMENT/) { $ligne = "<i>$ligne</i>\n" };
1440
    $text .= "\n" . $ligne;
6472 bpr 1441
    $text =~ s/\n{2,}/\n/g;
1442
    $text =~ s/(<br\s*\/>)+/<br \/>/g;
1443
    $text =~ s/(<br>)+/<br \/>/g;
23 reyssat 1444
  }
6472 bpr 1445
  $text =~ s/\n/<br \/>/g;
23 reyssat 1446
  $text ;
1447
}
1448
 
1449
sub indent { my $espace = "&nbsp;" x  6 ; $espace x $_[0]; }
1450
 
1451
sub Numero { my ($id) = @_;
1452
  return '' if ($id =~ /F_[^S]\d/)  || ($id =~ /L_/); #cela ne provient pas d'une section et co
1453
  $id =~ s /(F|L)_//g;
1454
  $id =~ s/mainS(\d+)/&Roman($1)/e;
1455
  $id =~ s/S(\d+)S(\d+)S(\d+)/-$1-$2-$3/;
1456
  $id =~ s/S(\d+)S(\d+)/-$1-$2/;
1457
  $id =~ s/S(\d+)/-$1/;
1458
  $id;
1459
}
1460
 
1461
# permet de faire modifier quelque chose dans la table pour un tag
1462
#TODO j'ai rajouté l'option couleur, du coup je ne sais plus faire fonctionner le shif
1463
sub selection { my ($text, $couleur, @tag) = @_ ;
1464
  return '' if !defined($text);
1465
  for my $ta (@tag) {
6614 bpr 1466
    $text =~ s/XXXX="$ta">/li class="$couleur">/g;
1467
    $text =~ s/YYYY="$ta">/\/li>/g;
23 reyssat 1468
  };
1469
  $text;
1470
}
1471
 
734 bpr 1472
sub clean { my ($text, $ref) = @_;
23 reyssat 1473
  return '' if !defined($text);
6614 bpr 1474
   $text =~ s/<XXXX="\w*">/<li class="no_selected">/g;
1475
   $text =~ s/<YYYY="\w*">/<\/li>/g;
734 bpr 1476
   $text =~ s/ZZZZZ(\w+)/store_tip($1,$ref)/ge;
23 reyssat 1477
   $text;
1478
}
1479
 
7387 bpr 1480
sub store_tip { my ($tag,$ref)=@_ ;
734 bpr 1481
  my $tip = $ref->{toctip}{$tag} ;
3552 bpr 1482
  my $title=$ref->{tittoc}{$tag} ;
7387 bpr 1483
  #$tip =~ s/'/\\\\'/g if ($tip) ;
1484
  $title =~ s/'/\\\\'/g if ($title) ;
1485
  $ref->{toctip}{$tag} ? "<ul>$tip</ul>" : '' ;
734 bpr 1486
}
1487
 
23 reyssat 1488
sub chemin { my ($tag, $ref) = @_;
1489
  my $tagsup = $tag;
1490
  my $ch = $tag;
1491
  my $txt = ($ref->{tittoc}{$tagsup}) ? $ref->{tittoc}{$tagsup} : $ref->{titb}{$tagsup} ;
1492
  my $niv = 0;
1493
  while ($tagsup !~ /^main\b/) {
1494
    $niv++;
1495
    $tagsup = $ref->{upbl}{$tagsup};
6644 bpr 1496
    $ch  = "$tagsup,$ch"; if (!$ref->{tittoc}{$tagsup}) { $ref->{tittoc}{$tagsup}=$tagsup};
1497
    $txt = "\\link{$tagsup}{$ref->{tittoc}{$tagsup}} $FLECHE $txt" if ($tagsup) ; #if ($tagsup !~ /^main\b/);
23 reyssat 1498
  }
1499
  $ref->{chemin}{$tag} = $ch;
1500
  $ref->{niveau}{$tag} = $niv;
1501
  return $LOAD if (!$txt);
743 bpr 1502
  '<div class="wims_chemin">' . $LOAD . "$linkout  $txt" . '</div>';}
23 reyssat 1503
 
1504
sub sortuniq {
1505
  my $prev = "not $_[0]";
1506
  grep { $_ ne $prev && ($prev = $_, 1) } sort @_;
1507
}
1508
 
1509
 
1510
sub isotime {
178 bpr 1511
    my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
23 reyssat 1512
    $year += 1900;
178 bpr 1513
    $mon += 1 ; $mday += 1 ;
23 reyssat 1514
    $mday = sprintf("%02d", $mday);
1515
    $mon  = sprintf("%02d", $mon);
1516
    "$year-$mon-$mday $hour:$min:$sec";
1517
}
7387 bpr 1518
 
23 reyssat 1519
 sub usage {
1520
  print STDERR << "EOT"
1521
latex2wims [--style=style.css] [--macro=wims.sty] [--dir=dossier1] [--docdir=dossier2] [--embed=dossier3] [--verbose] file
1522
 
7387 bpr 1523
  --style=style.css : style.css fichier de style css à utiliser
23 reyssat 1524
     (peut aussi être mis dans le fichier file : \\wimsinclude{style.css})
7387 bpr 1525
  --macro=wims.sty : wims.sty fichier de style à utiliser
23 reyssat 1526
     (peut aussi être mis dans le fichier file : \\wimsinclude{wims.sty})
1527
  --dir=dossier1 : dossier1 est le répertoire où se trouvent tous les fichiers dont le fichier file
7387 bpr 1528
  --docdir=dossier2 : dossier2 est le répertoire dans lequel sera créé le document
23 reyssat 1529
     (un dossier dans le compte Modtool par exemple)
7387 bpr 1530
  --embed=dossier3 : les fichiers de dossier3 sont appelés dans file
1531
     par la commande \embed{} (pour expert)
1532
  --verbose : détails
23 reyssat 1533
EOT
1534
;
1535
  exit 1;
1536
}
7387 bpr 1537
 
23 reyssat 1538
##======================================================================##
1539
## Adapted from work by OZAWA Sakuro <ozawa@prince.pe.u-tokyo.ac.jp>
1540
## Copyright (c) 1995 OZAWA Sakuro.  All rights reserved.  This
1541
## program is free software; you can redistribute it and/or modify
1542
## it under the same terms as Perl itself.
1543
##======================================================================##
1544
 
1545
sub Roman { my($arg) = shift;
1546
  my %roman_digit = qw(1 IV 10 XL 100 CD 1000 MMMMMM);
1547
  my @figure = reverse sort keys %roman_digit;
1548
  grep($roman_digit{$_} = [split(//, $roman_digit{$_}, 2)], @figure);
1549
 
1550
  my($x, $roman);
1551
  foreach (@figure) {
1552
    my($digit, $i, $v) = (int($arg / $_), @{$roman_digit{$_}});
1553
    if (1 <= $digit and $digit <= 3) {
1554
      $roman .= $i x $digit;
1555
    } elsif ($digit == 4) {
1556
      $roman .= "$i$v";
1557
    } elsif ($digit == 5) {
1558
      $roman .= $v;
1559
    } elsif (6 <= $digit and $digit <= 8) {
1560
      $roman .= $v . $i x ($digit - 5);
1561
    } elsif ($digit == 9) {
1562
      $roman .= "$i$x";
1563
    }
1564
    $arg -= $digit * $_;
1565
    $x = $i;
1566
  }
1567
  $roman
1568
 }