latex.anubis 18.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

   
   
                           Making PDF file through Anubis
   
   
read tools/basis.anubis 
   
   
   La création d'un fichier PDF s'effectue via LaTeX. 
   
   
   2 possibilités sont offertes : 
   
     (1). en transmettant le  nom du fichier .tex: il ne s'agit  alors que d'effectuer une
          compilation LaTeX, puis de transfomer le fichier dvi en PDF
   
     (2).  en  transmettant le contenu du document  (mais pas seulement) afin  de créer le
          fichier  LaTeX  qui sera  alors  compilé  et transformé  en  PDF.  C'est le  cas
          lorsqu'il s"agit de créer un fichier PDF à partir d'un texte écrit en MAML.
   
   
   
   Dans les 2 cas, le résultat de l'opération sera sour la forme de : 
   
public type ResultMakePdf:
   cannot_create_tex,
   error_compilation (String),    // explanation
   cannot_create_pdf,
   error_moving_pdf,
   ok.

   
   
   (1) Création d'un fichier PDF à partir d'un fichier écrit en LaTeX
   ------------------------------------------------------------------
   
public define ResultMakePdf
   make_pdf_file
      (
        String          file_name,
        Maybe(String)   mb_execution_directory,
        Maybe(String)   mb_pdf_directory,
        Int32           compilation_number
      ).
   
   Avec: 
   'file_name'  est le  nom  du  fichier contenant  le  texte écrit  en  LaTeX, mais  sans
   l'extension '.tex'.;
   
   'mb_execution_directory'  est le nom  du répertoire  dans lequel  se trouve  le fichier
   'file_name'; s'il vaut failure, il s'agira du répertoire courant.
   
   'mb_pdf_directory' est le répertoire dans lequel sera déplacé le fichier pdf.
   
   'compilation_number' est le nombre de fois que doit être éxécuté la compilation LaTeX.
   
   
   
   
   (2) Création d'un document PDF avec le corps du document à compiler
   -------------------------------------------------------------------
   
   Le titre du document, dans la mesure où il en existe un, peut être défini de 2 façons : 
     .(1) en utilisant la commande \maketitle de LaTeX. 
     .(2) en définissant soi-même le contenu de la page 'titre'.
   
   Dans les deux cas,  la page titre constituera la première page  du document (un saut de
   page est effectué avant le reste du document).
   
   
public type LaTeX_Title:
   no_title,
   predefined   (String          title,          // le titre du document
                 Maybe(String)   mb_author),     // et, éventullement l'auteur
   free_title   (String).                        // contenu LaTeX libre de la première page
   
   
   
   La fonction permettant de créer le fichier PDF est : 
   
   
public define ResultMakePdf
   make_pdf_file
     (
       String          more_ahead, 
       LaTeX_Title     title,
       String          core,
       Bool            table_of_contents,
       String          colors,
       String          file_name,
       Maybe(String)   mb_execution_directory,
       Maybe(String)   mb_pdf_directory
     ).
   
   Avec :
   'more_ahead'        : figurera dans l'en tête, en sus des éléments de bases
   'title'             : cf ci-dessus
   'core'              : contenu (en LaTeX) qui doit se trouver entre \begin{document} et \end{document}
   'table_of_contents' : indicateur de la présence ou non d'un sommaire créé à partif de \makeindex
   'colors'            : les éventuels \\definecolor{}{}{} pour les couleurs utilisées dans 'core'
   
   Les trois autres composants sont identiques à la version précédente de 'make_pdf_file'.
   
   
   
   
 --- That's all for public part --------------------------------------------------------------------
   
   
   
   
   *** Special Characters

   
   Les  caractères  spéciaux  de  LaTeX   exigent  d'être  listés  afin  d'être  convertis
   correctement lors de la création du  fichier source LaTeX. Ces caractères spéciaux avec
   le traduction sont définis par le type SpecialChar. 
   
   
   
public type SpecialChar:
   schar  (Int8    schar,
           String  transform).   // into html or latex
   
   
   
   Liste des caractères spéciaux de LaTeX
   --------------------------------------
   
public define List(SpecialChar)
   latex_schar
     =
   [
     schar('<',     "\\textless"), 
     schar('>',     "\\textgreater"),
     schar('&',     "\\& "),  
     schar('#',     "\\# "),
     schar('{',     "\\{ "), 
     schar('}',     "\\} "),
     schar('_',     "\\_"),
     schar('\\',    "\\textbackslash "),
     schar('^',     "\\textasciicircum "),
     schar('~',     "\\textasciitilde "),
     schar('[',     "{[}"),
     schar('|',     "\\textbar"),
     schar('<',     "\\textless"),
     schar('>',     "\\textgreater"),
     schar('*',     "{*}")
   ].

   
   Déterminer si un caractère fait partie de la liste des caractères spéciaux
   -----------
   
public define Bool
   is_special_char
     (
       List(SpecialChar)  lsc,
       Int8               char
     ) =
   if lsc is 
     {
       []      then false,
       [h . t] then if char=schar(h) then true else is_special_char(t,char)
     }.

   
   Déterminer si un String contient des caractères spéciaux
   -----------
   
define Bool
   contains_schars
     (
       String          s,
       Int8 ->  Bool   is_special_char,
       Int32           n
     ) =
   if nth(n,s) is 
     {
       failure    then false,
       success(c) then if is_special_char(c) then true else contains_schars(s,is_special_char,n+1)
     }.
   
   
   Transformation des caractères spéciaux
   --------------------------------------
   
public define String
   transform_schar
     (
       Int8              c,
       List(SpecialChar) lsc
     ) =
   if lsc is 
     {
       []      then implode([c]),
       [h . t] then if schar(h)=c 
                      then transform(h)
                      else transform_schar(c,t)
     }.

   
   Lecture d'un String & caractères spéciaux
   ----------------------------------------
   
   Dans  un  premier,  il  est  examiné  si  le String  contient  ou  non  des  caractères
   spéciaux. Dans l'affirmative, ceux-ci sont alors transformés.
   
   
define String
   read_schar
     (
       List(Int8)          li,
       List(SpecialChar)   lsc
     ) =
   if li is 
     {
       []       then "",
       [h . t]  then transform_schar(h,lsc)+read_schar(t,lsc)
     }.
   
public define String
   read_schar
     (
       String              s,
       List(SpecialChar)   lsc
     ) =
   with all_schar = (List(Int8))map(schar,lsc),
   if contains_schars(s,(Int8 char) |-> member(all_schar,char),0)
     then read_schar(explode(s),lsc)
     else s.
   

   
   
   
   *** Fichier sources LaTeX
   
   
   
   En-tête du fichier LaTeX
   -------------------------
   
   Cet en-tête  comprend une partie génerique,  et une partie  optionnelle représentée par
   'more_ahead'.
   
define String
   latex_head
     (
       String    more_ahead
     ) =
     "\\documentclass[a4paper,twoside,10pt]{article}\n\n"
   //+ "\\batchmode\n"
   + "\\setlength{\\headheight}{15pt}\n"
   + "\\setlength{\\headsep}{0.5cm}\n"
   + "\\setlength{\\headheight}{15pt}\n"
   + "\\setlength{\\marginparwidth}{0pt}\n"
   + "\\setlength{\\oddsidemargin}{0pt}\n"
   + "\\setlength{\\evensidemargin}{0pt}\n"
   + "\\setlength{\\textwidth}{17cm}\n"
   + "\\setlength{\\textheight}{23.7cm}\n"
   + "\\setlength{\\parindent}{0pt}\n"
   //+ "\\usepackage[francais]{babel}\n"
   + "\\usepackage[utf8]{inputenc}\n"
   + "\\usepackage{color}\n"
   + "\\usepackage{graphics}\n"
   + "\\usepackage{longtable}\n"
   + "\\usepackage{fancyhdr}\n"
   + "\\fancyhead{}\n"
   + "\\fancyhead[LE]{\\thepage \\hfill \\textsl{Section \\leftmark}}\n"
   + "\\fancyhead[RO]{\\textsl{\\rightmark} \\hfill \\thepage}\n"
   + "\\fancyfoot{}\n"
   + "\\pagestyle{fancy}\n"
   + "\\renewcommand{\\headrulewidth}{0.4pt}\n\n"
   + more_ahead
   + "\n\n". 
   
   
   
   
   
   
   
   
   Définition du fichier LaTeX
   ---------------------------
   
public define String
   latex_page
     (
       String       more_ahead, 
       LaTeX_Title  title,
       String       text_from_maml,
       Bool         table_of_contents,
       String       colors,
     ) =
     latex_head(more_ahead
                + if title is 
                    {
                      no_title           then "",
                      predefined(t,mb_a) then   "\\title{"+t+"}\n"
                                              + if mb_a is 
                                                  {
                                                    failure    then "",
                                                    success(a) then "\\author{"+a+"}\n"
                                                  },
                      free_title(_)      then ""
                    })
   + "\\begin{document}\n\n"
   + colors
   + "\\begin{sloppypar}\n\n"
   + if title is 
       {
         no_title        then "",
         predefined(_,_) then "\\maketitle \n \\thispagestyle{empty} \n \\newpage \n ",
         free_title(t)   then t+" \n \\thispagestyle{empty} \n \\newpage \n "
       }
   + (if table_of_contents then "\\tableofcontents \n \\newpage \n" else "") 
   + text_from_maml
   + "\n\n"
   + "\\end{sloppypar}\n\n"
   + "\\end{document}\n\n".
   
   
   
   Compilation LaTeX (to PDF)
   --------------------------
   
  
define Bool
   execute_control
     (
       ExecuteControl    ec,
       Int32             max_time
     ) =
   checking every 2000 milliseconds,
   wait for true 
     then if read(process_stdout(ec),1000,10) is 
            {
              failure    then print("SO=failure \n"),
              success(s) then print("SO=["+to_string(s)+"]\n")
            };
          if read(process_stderr(ec),1000,10) is 
            {
              failure    then print("SE=failure \n"),
              success(s) then print("SE=["+to_string(s)+"]\n")
            };
          if check_execute_status(ec) is 
            {
              still_running        then 
                if now>max_time
                  then print("Time over \n"); false
                  else execute_control(ec,max_time),
              abnormal_termination then print("abnormal_termination \n"); false
              finished(i)          then 
                if i = 0 then true else print("Error: finished /= 0 \n"); false
            }.
   
   
define ResultMakePdf
   make_pdf_file
     (
       String          file_name,
       Maybe(String)   mb_execution_directory,
       Maybe(String)   mb_pdf_directory,
     ) =
   if (Maybe(ExecuteControl))execute(mb_execution_directory,"dvipdf",[file_name+".dvi"]) is 
     {
       failure     then print("Error execute making pdf file \n");
                        cannot_create_pdf,
       success(ec) then
         print("dvipdf : ");
         if execute_control(ec,now+30) 
           then print("OK \n");
             if mb_pdf_directory is 
                {
                  failure           then ok,
                  success(pdf_dir)  then 
             if (Maybe(ExecuteControl))execute(mb_execution_directory,"mv",[file_name+".pdf",pdf_dir]) is  
                { 
                  failure     then print("Error execute mv pdf \n"); error_moving_pdf,
                  success(mv) then 
                    print("mv pdf file -> pdf_directory : ");
                    if execute_control(mv,now+10) 
                      then print("OK \n"); ok
                      else  print("Error mv PDF \n");error_moving_pdf
               }}
                 
           else print("Error making pdf file \n");cannot_create_pdf
      }.
   
   
public define ResultMakePdf
   make_pdf_file
     (
       String          file_name,           // without extension
       Maybe(String)   mb_execution_directory,
       Maybe(String)   mb_pdf_directory,
       Int32           compilation_number   // 2 if table-of-contents is required
     ) =
   if (Maybe(ExecuteControl))
     execute(mb_execution_directory,"latex",[file_name+".tex"]) is 
     {
       failure     then 
         print("Error executing compilation n°"+compilation_number+"\n");
         error_compilation("Error executing compilation n°"+compilation_number),
       success(ec) then 
         print("Compilation n°"+compilation_number+" : ");
         if execute_control(ec,now+120) 
           then print("OK\n");
                if compilation_number = 1
                  then make_pdf_file(file_name,mb_execution_directory,mb_pdf_directory)
                   else make_pdf_file(file_name,mb_execution_directory,
                                       mb_pdf_directory,compilation_number-1)
           else print("Error compilation n°"+compilation_number+"\n");
                error_compilation("Error compilation n°"+compilation_number)
      }.
   
   
define ResultMakePdf
   make_pdf_file
     (
       String          more_ahead, 
       LaTeX_Title     title,
       String          core,
       Bool            table_of_contents,
       String          colors,
       String          file_name,
       Maybe(String)   mb_execution_directory,
       Maybe(String)   mb_pdf_directory
     ) =
   if write_to_file
       (if mb_execution_directory is 
          {
            failure    then "",
            success(s) then s
          }+"/"+file_name+".tex",
                    to_byte_array(latex_page(more_ahead,title,core,table_of_contents,colors))) is 
     {
       cannot_open_file then cannot_create_tex,
       write_error(_)   then cannot_create_tex,
       ok               then make_pdf_file(file_name,mb_execution_directory,mb_pdf_directory,
                                           if table_of_contents then 2 else 1)
     }.
   
   
   Résultat -> One
   ---------------

   
define One
   make_pdf_file
     (
       String          file_name,
       Maybe(String)   mb_execution_directory,
       Maybe(String)   mb_pdf_directory,
     ) =
   if (Maybe(ExecuteControl))execute(mb_execution_directory,"dvipdf",[file_name+".dvi"]) is 
     {
       failure     then print("Error execute making pdf file \n"),
       success(ec) then
         print("dvipdf : ");
         if execute_control(ec,now+30) 
           then print("OK \n");
             if mb_pdf_directory is 
                {
                  failure           then unique,
                  success(pdf_dir)  then 
             if (Maybe(ExecuteControl))execute(mb_execution_directory,"mv",[file_name+".pdf",pdf_dir]) is  
                { 
                  failure     then print("Error execute mv pdf \n"),
                  success(mv) then 
                    print("mv pdf file -> pdf_directory : ");
                    if execute_control(mv,now+10) 
                      then print("OK \n")
                      else  print("Error mv PDF \n")
               }}
                 
           else print("Error making pdf file \n")
      }.
   
   
 public define One
   latex_compilation
     (
       String          file_name,           // without extension
       Maybe(String)   mb_execution_directory,
       Maybe(String)   mb_pdf_directory,
       Int32           compilation_number   // 2 if table-of-contents is required
     ) =
   if (Maybe(ExecuteControl))
     execute(mb_execution_directory,"latex",[file_name+".tex"]) is 
     {
       failure     then print("Error executing compilation n°"+compilation_number+"\n"),
       success(ec) then 
         print("Compilation n°"+compilation_number+" : ");
         if execute_control(ec,now+60) 
           then print("OK\n");
                if compilation_number = 1
                  then make_pdf_file(file_name,mb_execution_directory,mb_pdf_directory)
                   else latex_compilation(file_name,mb_execution_directory,
                                          mb_pdf_directory,compilation_number-1)
           else print("Error compilation n°"+compilation_number+"\n")
      }.

   

   
   
   
   - Compilation par un 'execute produisant un Maybe(Int8))   
   --------------------------------------------------------
 define One
   make_pdf_file
     (
       String          file_name,
       Maybe(String)   mb_execution_directory,
       Maybe(String)   mb_pdf_directory,
     ) =
   if (Maybe(Int8))execute(mb_execution_directory,"dvipdf",[file_name+".dvi"]) is 
     {
       failure    then print("Error execute making pdf file \n"),
       success(s) then 
   if s = 0
     then if mb_pdf_directory is 
            {
              failure           then unique,
              success(pdf_dir)  then 
           if (Maybe(Int8))execute(mb_execution_directory,"mv",[file_name+".pdf",pdf_dir]) is  
            { 
              failure     then print("Error execute mv pdf \n"),
              success(mv) then 
                if mv = 0 then print("mv pdf = OK ") else print("Error mv PDF \n")
            }}
     else print("Error making pdf file \n")
      }.
   
   
public define One
   latex_compilation
     (
       String          file_name,           // without extension
       Maybe(String)   mb_execution_directory,
       Maybe(String)   mb_pdf_directory,
       Int32           compilation_number   // 2 if table-of-contents is required
     ) =
   if (Maybe(Int8))
     execute(mb_execution_directory,"latex",[file_name+".tex"]) is 
     {
       failure    then print("Error executing compilation n°"+compilation_number+"\n"),
       success(s) then 
         if s = 0
           then if compilation_number = 1
                  then make_pdf_file(file_name,mb_execution_directory,mb_pdf_directory)
                  else latex_compilation(file_name,mb_execution_directory,
                                         mb_pdf_directory,compilation_number-1)
           else print("Error compilation n°"+compilation_number+"\n")
      }.
   
public define One
   make_pdf_file
     (
       String          more_ahead, 
       LaTeX_Title     title,
       String          core,
       Bool            table_of_contents,
       String          colors,
       String          file_name,
       Maybe(String)   mb_execution_directory,
       Maybe(String)   mb_pdf_directory
     ) =
   if write_to_file
       (if mb_execution_directory is 
          {
            failure    then "",
            success(s) then s
          }+"/"+file_name+".tex",
                    to_byte_array(latex_page(more_ahead,title,core,table_of_contents,colors))) is 
     {
       cannot_open_file then print("COF \n"),
       write_error(_)   then print("WE \n"),
       ok then 
         latex_compilation(file_name,mb_execution_directory,mb_pdf_directory,
                                           if table_of_contents then 2 else 1)
     }.