maml.anubis 35.5 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 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098

   
                          The Minimal Anubis Markup Language. 
                                        (MAML)

   
   
   
   This is version 2 of this tool. 
   
   
   For web sites, we need a 'markup  language', which is something like HTML or LaTeX, but
   much more simple. Here, we define such  a language, called 'MAML'. We provide tools for
   reading  texts  written in  MAML,  and  producing either  a  translation  in HTML  (for
   inclusion into web pages), or in LaTex (for generation of PDF files).
   
   Typically, a  text in MAML is  entered manually into a  'text area' in a  web page. The
   text is interpreted  as pure text until the character $  is encountered. This character
   begins a 'mark' to be interpreted. The  character $ is immediately followed by a symbol
   (non empty  sequence of letters  from 'a'  to 'z'), and  a certain number  of arguments
   enclosed in parentheses. For example, if there are two arguments:
   
                             $name(... arg 1 ...)(... arg 2 ...)

   where 'name' is the name of the mark.  The number of arguments depends on that name. It
   may be zero.
   
   Informations on a mark are stored as:
   
public type MAMLMark:
   mark(String         name, 
        Int32          number_of_arguments). 
   
   The list af all these informations is available as:
   
public define List(MAMLMark)   
   maml_marks
     =
   [
   mark("dollar",      0),
   mark("lpar",        0), 
   mark("rpar",        0),
   mark("par",         0),
   mark("item",        0), 
   mark("bold",        1), 
   mark("italic",      1), 
   mark("yellow",      1), 
   mark("red",         1), 
   mark("grey",        1), 
   mark("green",       1), 
   mark("blue",        1), 
   mark("big",         1), 
   mark("image",       1), 
   mark("code",        1), 
   mark("center",      1), 
   mark("link",        1), 
   mark("list",        1),
   mark("sub",         1), 
   mark("sup",         1), 
   mark("tt",          1)
   ]. 
   
   
   For  performance reasons, instead  of using  character strings  (of type  'String') for
   representing texts, we  use the following type. The MAML  parser never includes strings
   of more than 80 characters in such a datum.
   
public type Text:
   t,                   // empty text
   Text - String,       // text followed by a string
   Text - Text.         // text followed by a text   

   We provide a conversion function:
   
public define String       to_string    (Text text).   
   
   
   The MAML  text to  be parsed  may come from  any source  of bytes. Such  a source  is a
   'Stream' as defined in 'tools/stream.anubis'.
   
read tools/streams.anubis   
   
   The function 'parse_MAML'  below parses a MAML  text from a stream, and  returns, if no
   error occurs, a 'MAML parse tree' of the following type:
   
public type MAML:
   text  (Text),                            // simple text containing no mark
   mark  (String name),                     // mark with no argument
   mark  (String name, MAML),               // mark with 1 argument
   mark  (String name, MAML, MAML),         // mark with 2 arguments
   mark  (String name, MAML, MAML, MAML),   // mark with 3 arguments
   MAML + MAML.                             // tree made of two pieces of MAML
   
   
public define Result(Int32,MAML)     parse_MAML(Stream s). 
   
   The result  is either 'error(n)', where  'n' is the byte  position of the  error in the
   source, or 'ok(m)' where 'm' is a MAML parse tree.
   
   
   MAML  parse  trees  may  be  translated  into various  formats.   
   
public define Text        to_HTML      (MAML m).
public define Text        to_LaTeX     (MAML m,String site_name,String public_dir).

   'public_dir' is the directory where images are stored.
   
   
   For compatibility with maml.anubis version 1 we provide the following functions:
   
public define Result(Int32,String)
   convert_MAML_to_HTML
     (
       String maml
     ).
   
public define Result(Int32,String)
   convert_MAML_to_LaTeX
     (
       String maml,
       String public_dir
     ). 
   
   
   --- That's all for the public part ! --------------------------------------------------
   
   
   
   
read tools/basis.anubis   
read tools/latex.anubis

   
   
   *** The MAML Parser. 

   
   Getting  the number of  arguments corresponding  to a  given mark  name. The  result is
   'failure' if the name is not recognized, and 'success(n)' if it is recognized, with 'n'
   the number of arguments.
   
define Maybe(Int32)
   number_of_arguments
     (
       String          name, 
       List(MAMLMark)  l
     ) =
   if l is 
     {
       [ ] then //print("MAML mark name "+name+" not regognized.\n"); 
                failure, 
   
       [h . t] then if h is mark(n,i) then 
         if name = n
         then (
                //print("MAML mark "+name+" has "+i+" arguments.\n"); 
                success(i)
              )
         else number_of_arguments(name,t)
     }.

define Maybe(Int32)
   number_of_arguments
     (
       String name
     ) =
   number_of_arguments(name,maml_marks). 
   
   
   
   
   Reading a 'chunk' of text. We read the  text by chunks of at most 80 characters. When a
   character '$',  '(' or ')' is encountered,  the chunk is terminated.  This function may
   return an empty chunk.
   
define String
   read_chunk
     (
       Stream        s,
       List(Int8)    so_far,       // characters already read in reverse order
       Int32         n             // length of 'so_far'
     ) =
   //print("Reading a chunk. ["+n+"]\n"); 
   if n >= 80
   then (with r = implode(reverse(so_far)), //print("chunk read: "+r+"\n"); 
                                            r)
   else if read_byte(s) is 
     {
       failure then //print("Cannot read byte from source.\n");
                    implode(reverse(so_far)),
   
       success(c) then //print("byte read: "+int8_to_int32(c)+"\n"); 
         if member(c,"$()")
         then (unput_byte(c,s); implode(reverse(so_far)))
         else read_chunk(s,[c . so_far],n+1)
     }.
   
   
   Now, we want to read chunks until one  of the special characters '$', '(' or ')' or the
   end of input is found.
      
define Text
   read_chunks
     (
       Stream   s,
       Text     so_far        // chunks already read
     ) =
   with next_chunk = read_chunk(s,[],0), 
   if next_chunk = ""
   then so_far 
   else read_chunks(s,so_far - next_chunk). 


   When we encounter a '$' in the MAML source text, we must read the name of the mark, and
   as  many  arguments as  this  name  requires. Reading  the  arguments  is performed  by
   'parse_MAML' because each argument (not including  the first and last parentheses) is a
   regular  MAML text. Hence,  the two  functions 'parse_MAML'  and 'read_mark'  are cross
   recursive.
   
   The name of a MAML mark is right delimited either by a blank character or by an opening
   parenthese. In the case of a blank  character, this character is considered part of the
   name,  but a  subsequent blank  character is  not. This  feature enables  to  write for
   example:
   
                      $dollar T             -->     $T
                      $dollar  blabla       -->     $ blabla
   
   Hence we need a special function for reading the name of the mark. 
   
define Bool
   is_name_char
     (
       Int8 c
     ) =
   with n = int8_to_int32(c), 
   if n >= 'a' then n =< 'z' else false. 
   
define Bool
   is_blank_char
     (
       Int8 c
     ) =
   member(c," \n\r\t").
   
define String
   read_mark_name      // '$' already read
     (
       Stream       s, 
       List(Int8)   so_far        // characters already read
     ) =
   if read_byte(s) is 
     {
       failure then implode(reverse(so_far)),
       success(c) then 
         if is_name_char(c)
         then read_mark_name(s,[c . so_far])                    // continue reading the name
         else if is_blank_char(c)
              then implode(reverse(so_far))                     // eat the first blank character
              else unput_byte(c,s); implode(reverse(so_far))    // don't eat if not blank
     }.
   
   
   We declare 'parse_MAML':
   
define Result(Int32,MAML)
   parse_MAML
     (
       Stream                    s, 
       Maybe(Int32)              level  // may use a parenthese level to detect the end
     ). 
   
   If we use  a parenthese level to detect  the end, the closing parenthese  is eaten (and
   not part of the result).   
   
   
   When we must read an argument, we  first skip blank characters until we find an opening
   parenthese. Then we call 'parse_MAML' in  order to read until the corresponding closing
   parenthese. 
   
define Result(Int32,MAML)
   read_argument
     (
       Stream                    s
     ) =
   skip_blanks(s); 
   if read_byte(s) is 
     {
       failure then // opening parenthese not found
         error(get_bytes_read(s)), 
       success(c) then 
         if c = '('
         then parse_MAML(s,success(0))    // terminal call
         else error(get_bytes_read(s))
     }.
   
   
   
   Reading a complete mark ('$' character already read):
   
define Result(Int32,MAML)   
   read_mark
     (
       Stream                    s
     ) =
   with name = read_mark_name(s,[]), 
   if number_of_arguments(name) is 
     {
       failure then error(get_bytes_read(s)), 
       success(n) then 
         if n = 0 then ok(mark(name)) else
         if n = 1 then if read_argument(s) is 
                    {
                      error(i) then error(i), 
                      ok(a1) then ok(mark(name,a1))
                    } else
         if n = 2 then if read_argument(s) is 
                    {
                      error(i) then error(i),
                      ok(a1) then if read_argument(s) is 
                        {
                          error(i) then error(i),
                          ok(a2) then ok(mark(name,a1,a2))
                        }
                    } else
         if n = 3 then if read_argument(s) is 
                    {
                      error(i) then error(i),
                      ok(a1) then if read_argument(s) is 
                        {
                          error(i) then error(i),
                          ok(a2) then if read_argument(s) is 
                            {
                              error(i) then error(i), 
                              ok(a3) then ok(mark(name,a1,a2,a3))
                            }
                        }
                    } else alert
     }.
       
   
   
   Levels of type 'Maybe(Int32)' must be incremented and decremented. 
   
define Maybe(Int32)
   Maybe(Int32) mb_level + Int32 n
     =
   if mb_level is 
     {
       failure then failure, 
       success(l) then success(l+n)
     }.
   
   Depending of the level, we may need to stop reading.
   
define Bool
   must_stop_reading
     (
       Maybe(Int32)    mb_level
     ) =
   if mb_level is 
     {
       failure then false,       // no level => don't stop reading
       success(n) then n =< 0    // stop reading if level is 0 or less. 
     }. 
   
   
   
   Ok, now we are ready to parse the MAML. 
   
define Result(Int32,MAML)     
   parse_MAML
     (
       Stream                      s,
       Maybe(Int32)                mb_level
     ) =
   with prefix = read_chunks(s,t), 
   if read_byte(s) is 
     {
       failure then ok(text(prefix)), 
       success(c) then 
         if c = '$'
         then if read_mark(s) is
                {
                  error(i) then error(i),
                  ok(m) then if parse_MAML(s,mb_level) is 
                    {
                      error(i) then error(i), 
                      ok(rest) then ok(text(prefix)+m+rest)
                    }
                }
         else if c = '('
              then if parse_MAML(s,mb_level+1) is 
                     {
                       error(i) then error(i),
                       ok(rest) then ok(text(prefix)+text(t-"(")+rest)
                     }
              else if c = ')'
                   then if must_stop_reading(mb_level)
                        then ok(text(prefix)) 
                        else if parse_MAML(s,mb_level+(-1)) is 
                               {
                                 error(i) then error(i), 
                                 ok(rest) then ok(text(prefix)+text(t-")")+rest)
                               }
                   else alert  // should never happen (see 'read_chunks')
     }.
   
   
   
public define Result(Int32,MAML)
   parse_MAML
     (
       Stream s
     ) =
   parse_MAML(s,failure). 
   
   
   
   

   
   *** Special characters

   
   HTML  et LaTeX utilisent  des caractères  spéciaux qu'il  convient de  transformer pour
   l'édition/
   
   Pour ces  deux formats, il est défini  dans un premier temps  une List(SpecialChar) qui
   recense les caractères spéciaux utilisés.
   
   Il suffira alors d'appeler la fonction :
   
        to_special_characters
   
   avec  comme arguements  le  Text à  examiner  et éventuellement  à  transformer, et  la
   List(SpecialChar)
   
   
   (Cf  le tools/latex.anubis  pour  la liste  LaTeX et  les  fonctions de  lecture et  de
   transformation).
   
 

define Text
   to_special_characters
     (
       Text                text,
       List(SpecialChar)   lsc
     ) =
   if text is 
     {
       t                   then t,
       Text te - String s  then to_special_characters(te,lsc) - read_schar(s,lsc),
       Text t1 - Text t2   then to_special_characters(t1,lsc) - to_special_characters(t2,lsc)
     }.
   
   
   
   
   *** Translating to HTML. 
   
   
define List(SpecialChar)
   html_schar
     =
   [
     schar('<',     "&lt;"),
     schar('>',     "&gt;"),
     schar('&',     "&amp;")
   ].
   
public define Text
   to_HTML
     (
       MAML m
     ) =
   if m is 
     {
       text(Text t) then to_special_characters(t,html_schar), 
   
       mark(String name) then 
         if name = "dollar" then t-"$"       else
         if name = "lpar"   then t-"("       else
         if name = "rpar"   then t-")"       else
         if name = "par"    then t-"<br>"    else
         if name = "item"   then t-"<li>"    else
         alert,
   
       mark(String name,MAML a1) then 
         if name = "bold"   then t-"<strong>"-to_HTML(a1)-"</strong>"                                 else
         if name = "italic" then t-"<em>"-to_HTML(a1)-"</em>"                                         else
         if name = "yellow" then t-"<span style=\"{color: rgb(180,180,0)}\">"-  to_HTML(a1)-"</span>" else
         if name = "red"    then t-"<span style=\"{color: rgb(180,0,0)}\">"-    to_HTML(a1)-"</span>" else
         if name = "grey"   then t-"<span style=\"{color: rgb(130,130,130)}\">"-to_HTML(a1)-"</span>" else
         if name = "green"  then t-"<span style=\"{color: rgb(0,180,0)}\">"-    to_HTML(a1)-"</span>" else
         if name = "blue"   then t-"<span style=\"{color: rgb(0,0,180)}\">"-    to_HTML(a1)-"</span>" else
         if name = "big"    then t-"<big>"-to_HTML(a1)-"</big>"                                       else
         if name = "image"  then t-"<img src=\""-to_HTML(a1)-"\">"                                    else
         if name = "code"   then t-"<table><tr><td><span style=\"{color: rgb(0,130,130)}\"><pre>"
                                      -to_HTML(a1)-"</pre></span></td></tr></table>"                  else
         if name = "center" then t-"<span><center>"-to_HTML(a1)-"</center></span>"                    else
         if name = "link"   then with c = to_HTML(a1), 
                                 t-"<a href=\""-c-"\" target=\"w"-(""+now)-"\">"-c-"</a>"             else
         if name = "list"   then t-"<span><ul>"-to_HTML(a1)-"</ul></span>"                            else
         if name = "sub"    then t-"<sub>"-to_HTML(a1)-"</sub>"                                       else
         if name = "sup"    then t-"<sup>"-to_HTML(a1)-"</sup>"                                       else
         if name = "tt"     then t-"<span style=\"{color: rgb(0,130,130)}\"><tt>"
                                     -to_HTML(a1)-"</tt></span>"                                      else
         alert, 
   
       mark(String name,MAML a1,MAML a2) then 
         alert,
   
       mark(String name,MAML a1,MAML a2,MAML a3) then 
         alert, 
   
       m1 + m2 then to_HTML(m1) - to_HTML(m2)
     }. 
   

   
   *** Translating to LaTeX. 
   
  
   Pour retrouver dans  le source LaTeX le texte  même du source MAML et  des modalités de
   mise en forme et de mise en page, il convient de :
   
   . tenir compte de la syntaxe particulière des caractères spéciaux de LaTeX
   . de retrouver dans le sources LaTeX  la mise en forme particulière de la balise 'code'
   de MAML. Si des transformations MAML ->  LaTeX sont communes pour du texte en dehors et
   dans  de la  balise 'code',  celles-ci pourront  différer selon  les règles  propres de
   compilation de LaTeX.
   
   
   
   Transformation en Text commune à toutes les balises MAML
   --------------------------------------------------------
   
define Text
   to_text
     (
       String name
     ) =
   if name = "dollar" then t-"\\$"                  else 
   if name = "lpar"   then t-"("                    else 
   if name = "rpar"   then t-")"                    else 
   if name = "par"    then t-"\n\n \\smallskip \n"  else
   if name = "item"   then t-"\\item "              else 
   alert.

   
   Définition de couleurs
   ----------------------
   
   Pour définir une couleur RGB, donner un nombre en 0 et 1 pour chacune des couleurs de base
   Pour définir un gris, donner un nombre entre 0 (noir) et 1 (blanc)
   
public define String
   maml_LaTeX_colors
     =
     //"\\definecolor{my_green}{rgb}{0.27,0.51,0.27}\n"
     "\\definecolor{my_green}{rgb}{0.09,0.66,0.09}\n"
   + "\\definecolor{my_grey}{gray}{0.50}\n"
   + "\\definecolor{code_color}{rgb}{0.05,0.41,0.64}\n".
   + "\\definecolor{code_color}{rgb}{0.61,0.50,0}\n".
   + "\\definecolor{code_color}{rgb}{0.43,0.43,0.12}\n".
   + "\\definecolor{code_color}{rgb}{0.54,0.54,0.15}\n".
   + "\\definecolor{code_color}{rgb}{0.60,0.60,0.16}\n".
   + "\\definecolor{code_color}{rgb}{0.77,0.77,0.29}\n".
   + "\\definecolor{code_color}{rgb}{0.53,0.50,0.21}\n".
     "\\definecolor{my_green}{rgb}{0.15,0.54,0.15}\n"
   
   
   Prise en compte de la balise 'big' de MAML
   ------------------------------------------
   
   Avec MAML, il est possible d'avoir une séquence de 'big' imbriqués: 
      $big($big(xxx))
   
   Pour la transformation en LaTeX, il  est nécessaire de compter le nombre d'imbrications
   afin de déterminer la taille de caractères qui seront utilisée.
   
define String
   big_to_LaTeX
     (
       Int32 bigger
     ) =
   if bigger = 1 then "\\large" else
   if bigger = 2 then "\\Large" else
   if bigger = 3 then "\\LARGE" else
   if bigger = 4 then "\\huge"  else
   "\\Huge".
   
   
define (String,MAML)
   big_to_LaTeX
     (
       Int32   bigger,
       MAML    next
     ) =
   if next is mark(String name,MAML m1)
     then if name = "big"
            then big_to_LaTeX(bigger+1,m1)
            else (big_to_LaTeX(bigger),next)
     else (big_to_LaTeX(bigger),next).
   
   

   
   Balise 'image'
   -------------

   Le nom de l'image est sous la forme : 
       "http://"+site_name+"/"+image_path

   
define String 
   get_eps_name
     (
       String    image_path,
       String    site_name,
     ) =
   with lsn = length(site_name)+8,
        l = length(image_path),
        img_name = substr(image_path,lsn,l-lsn),
        li = length(img_name),
        ext = substr(img_name,li-3,3),
   if member(["gif","GIF","jpg","JPG"],ext)
     then (substr(img_name,0,li-3)+"ps")
     else img_name.
   
define Text
   image_to_LaTeX
     (
       MAML            m,
       MAML -> Text    to_LaTeX,
       String          site_name,
       String          public_dir
     ) =
   if m is text(te) 
     then if te is Text _ - String s 
            then (t-("\\includegraphics{"+public_dir+"/"+get_eps_name(s,site_name)+"}"))
            else to_LaTeX(m)
     else to_LaTeX(m).
   
   
   
   Prise en compte de la balise 'code' de MAML
   -------------------------------------------

   La balise  'code' de  MAML détermine  un format particulier  d'affichage des  lignes de
   codes qu'elle contient. Aussi, quand la  fonction générique de tranformation du MAML en
   Text rencontrera  cette balise  'code', il  sera fait appel  la fonction  spécifique de
   transformation 'code_to_LaTeX'.
   
   
   
   
   Le code est écrit dans un environnement longtable
   
   Chaque ' ' contenu dans la balise  est signifiant et doit donc être explicitement écrit
   par LaTeX ( -> "\ ").
   
   Par ailleurs, le passage à la ligne "\\" étant impossible dans l'environnement \tabular
   à  l'intérieur d'une  'boîte' (ex  \textcolor{}{}), la  fonction 'format_code_to_LaTeX'
   prendra en argument, outre la liste des caractères, la commande de la dite boîte.
   
   Ainsi, si  dans le code MAML, '\n'  est rencontré, la transformation  suivra les étapes
   suivantes :
     .1. fermeture de la boîte préalablement ouverte
     .2. passage à la ligne
     .3. réouverture de la boîte

   La commande de la boîte sera donnée à la lecture de chacune des balises concernées lors
   de la lecture du MAML par 'code_to_LaTeX()'

   
   Par ailleurs,  pour obtenir le  même effet, le  code LaTeX est susceptible  de différer
   selon les  environnements. Il  est donc nécessaire,  à ces  moments, de se  souvenir de
   l'environnement  actif.   La  fonction  de  transformation en  LaTeX  aura  donc  comme
   composant   une  List(Environment)   qui  permettra   d'adapter  le   codage   LaTeX  à
   l'environnement actif.
   
public type Environment:
   center,
   textcolor,
   itemize.
   
   
   
   itemize :
   --------
   
   Le format à l'intérieur de l'environnement 'itemize' est défini par : 
   
define String
   itemize_format
     =
     "\\setlength{\\topsep}{5mm} "
   + "\\setlength{\\itemindent}{5mm} "
   + "\\setlength{\\itemsep}{1mm} ".
   
   L'environnement 'itemize' doit  être suivi d'un espacement vertical.   Si 'itemize' est
   inclu  dans  un \textcolor,  il  ne  faut pas  mettre  cet  espacement  juste après  le
   \end{itemize} mais après l'accolade fermant le texcolor. Mais si c'est le textcolor qui
   est inclu dans le itemize, il n'y pas lieu d'ajouter un cet espacement.
   
   
   L'ajout éventuel de \vspace après le \textcolor s'effectue par : 
   
define String
   textcolor_end
     (
       List(Environment) lenv
     ) =
   if lenv is 
     {
       []       then "",
       [h . t]  then 
         // tester lequel des environnements textcolor ou itemize est inclu dans l'autre. 
         // l'inclu est le plus haut dans la liste.
         if h is 
           {
             center    then textcolor_end(t),
             textcolor then if member(t,itemize) then "\\vspace{2mm}" else "",
             itemize   then ""
           }
     }.
   
 define String
   textcolor_close
     (
       List(Environment) lenv
     ) =
   if member(lenv,code)
     then ""
     else "}".

   
   et de la même façon après le \end{itemize} :
   
define String
   itemize_end
     (
       List(Environment) lenv
     ) =
   if member(lenv,textcolor) then "" else "\\vspace{5mm}\n".
   

   
   Code, couleur (\textcolor) et paragraphe
   ----------------------------------------
   
   La balise 'code' de MAML se traduit par l'écriture du texte en gris. Mais à l'intérieur
   du  code, une  autre couleur  peut-être employé.  La compilation  de LaTeX  interdit le
   passage d'un paragraphe à un autre dans  \textcolor. Aussi, à la lecture du source MAML
   le code de passage à un nouveau paragraphe, il faudra :
   
      . fermer le \textcolor,
      . fermer, au besoin, le sous-environnement ouvert dans le \texcolor (\bf, par exemple),
      . écrire le code de passage au paragraphe suivant
      . réouvrir le \textcolor
      . réouvrir le sous-environnement.
   
   
define String
   format_code_with_color2
     (
       List(Int8)         l,
       Maybe(String)      sub_environment,
       String             begin_code_color,
       String             end_code_color,
     ) =
   if l is 
     {
       []      then end_code_color,
       [h . t] then 
         (if h = ' '  then "\\ " else
          if h = '\n' then 
             if sub_environment is // {
               {
                 failure    then end_code_color+(if t is [] then "" else "\\\\ ")+begin_code_color,
                 success(s) then end_code_color+"} "+(if t is [] then "" else "\\\\ ")+begin_code_color+s
               } else
           //if h ='|'   then "\\raisebox{-2.8pt}{\\rule{0.5pt}{11pt}}" else 
           //if h = '-'  then "$-$"
            else transform_schar(h,latex_schar))
          + format_code_with_color2(t,sub_environment,begin_code_color,end_code_color)
     }.
   
define String
   format_code_with_color
     (
       List(Int8)         l,
       Maybe(String)      sub_environment,
       String             begin_code_color,
       String             end_code_color,
     ) =
   if l is 
     {
       []      then end_code_color,
       [h . t] then 
         if h=13  //h = '\n' //| h = 13
           then format_code_with_color(t,sub_environment,begin_code_color,end_code_color)
           else format_code_with_color2(l,sub_environment,begin_code_color,end_code_color)
     }.   
   
   
define Text
   format_code
     (
       Text               text,
       Maybe(String)      sub_environment,
       List(Environment)  lenv
     ) = 
   if text is 
     {
       t                  then t,
       Text te - String s then 
            format_code(te,sub_environment,lenv)
          - if member(lenv,textcolor)
              then format_code_with_color(explode(s),sub_environment,"","")
              else ("\\textcolor{code_color}{"
                   + format_code_with_color(explode(s),sub_environment," \\textcolor{code_color}{","}")),
       Text t1 - Text t2  then 
          format_code(t1,sub_environment,lenv) - format_code(t2,sub_environment,lenv)  
     }.
   
   

   
define Text
   code_to_LaTeX
     (
       MAML               m,
       Maybe(String)      sub_environment,
       String             site_name,
       String             public_dir,
       List(Environment)  lenv
     ) = 
   if m is 
     {
       text(Text t)                           then format_code(t,sub_environment,lenv),
       mark(String name)                      then 
          t-"\\textcolor{code_color}{"-to_text(name)-"}",
       mark(String name,MAML m1)              then 
         if name = "bold"    then 
           t-"{\\bf "
            -code_to_LaTeX(m1,success("\\bf"),site_name,public_dir,lenv)-"}" else 
         if name = "italic"  then 
           t-"{\\it "
            -code_to_LaTeX(m1,success("\\it"),site_name,public_dir,lenv)-"}" else
         if name = "yellow"  then 
           t-"\\textcolor{yellow}{"
            -code_to_LaTeX(m1,success("\\textcolor{yellow}{"),site_name,public_dir,[textcolor . lenv])
            -("}"+textcolor_end(lenv))              else
   
         if name = "red"     then 
           t-"\\textcolor{red}{"
            -code_to_LaTeX(m1,success("\\textcolor{red}{"),site_name,public_dir,[textcolor . lenv])
            -("}"+textcolor_end(lenv))           else
   
         if name = "grey"    then
           t-"\\textcolor{my_grey}{"
            -code_to_LaTeX(m1,success("\\textcolor{my_grey}{"),site_name,public_dir,[textcolor . lenv])
            -("}"+textcolor_end(lenv))    else
   
         if name = "green"   then 
           t-"\\textcolor{my_green}{"
            -code_to_LaTeX(m1,success("\\textcolor{my_green}{"),site_name,public_dir,[textcolor . lenv])
            -("}"+textcolor_end(lenv))   else
   
         if name = "blue"    then
           t-"\\textcolor{blue}{"
            -code_to_LaTeX(m1,success("\\textcolor{blue}{"),site_name,public_dir,[textcolor . lenv])
            -("}"+textcolor_end(lenv))   else
   
         if name = "big"     then (if big_to_LaTeX(1,m1) is (string_latex,next_maml) then 
              t-("{"+string_latex)
               -code_to_LaTeX(next_maml,success(string_latex),site_name,public_dir,lenv)-"}")   else
   
         if name = "image"   then 
            image_to_LaTeX(m1,(MAML ma) |-> code_to_LaTeX(ma,failure,site_name,public_dir,lenv),
                           site_name,public_dir)                                                  else
   
         if name = "code"    then 
           t-("\\vspace{2mm}\n {\\tt \\setlongtables "
              +"\\begin{longtable}["+(if member(lenv,center)then "c" else "l")+"]{l}\n")
            -code_to_LaTeX(m1,sub_environment,site_name,public_dir,lenv)
            -" \\end{longtable}}\n\n \\vspace{3mm}"                                                else
   
         if name = "center"  then 
           t-"\\begin{center} "
            -code_to_LaTeX(m1,sub_environment,site_name,public_dir,[center . lenv])
            -"\\end{center}"                                                                  else
   
         if name = "link"    then 
           t-"{\\tt "
            -code_to_LaTeX(m1,sub_environment,site_name,public_dir,lenv)-"}"   else
   
         if name = "list"    then 
           t-("\\begin{itemize}\n "+itemize_format)
            -code_to_LaTeX(m1,sub_environment,site_name,public_dir,[itemize . lenv])
            -("\\end{itemize} "+itemize_end(lenv))                                 else
   
         if name = "sub"     then 
           t-"\\mbox{$_{"-code_to_LaTeX(m1,sub_environment,site_name,public_dir,lenv)-"}$}"       else
   
         if name = "sup"     then
           t-"\\mbox{\\textsuperscript{"
            -code_to_LaTeX(m1,sub_environment,site_name,public_dir,lenv)-"}}" else 
   
         if name = "tt"      then
           t-"{\\tt "-code_to_LaTeX(m1,success("\\tt"),site_name,public_dir,lenv)-"}"       else
   
         alert,
       mark(String n,MAML m1,MAML m2)         then alert,
       mark(String n,MAML m1,MAML m2,MAML m3) then alert,
       m1 + m2                                then 
          code_to_LaTeX(m1,sub_environment,site_name,public_dir,lenv)
        - code_to_LaTeX(m2,sub_environment,site_name,public_dir,lenv)
     }.
   
   
   
   
   
   Fonction générique MAML -> Text
   -------------------------------

define Bool
   next_maml_is
    ( 
      MAML   next,
      String name
    ) = 
   if next is 
     {
       text(_)  then false,
       mark(n)  then false,
       mark(n,_) then (n=name),
       mark(String n,MAML a1,MAML a2) then false,
       mark(String n,MAML a1,MAML a2,MAML a3) then false,
       m1 + m2 then (next_maml_is(m1,name) | next_maml_is(m2,name))
     }.
   
public define Text
   to_LaTeX
     (
       MAML               m,
       String             site_name,
       String             public_dir,
       List(Environment)  lenv,
       One -> String      s_en,         // select_english
       One -> String      us_en         // unselect_english
     ) =
   if m is 
     {
       text(Text t)       then to_special_characters(t,latex_schar),

       mark(String name)  then to_text(name),
   
       mark(String name,MAML a1) then 
         if name = "bold"    then 
           t-"{\\bf "-to_LaTeX(a1,site_name,public_dir,lenv,s_en,us_en)-"}"                else 
   
         if name = "italic"  then 
           t-"{\\it "-to_LaTeX(a1,site_name,public_dir,lenv,s_en,us_en)-"}"                else
   
         if name = "yellow"  then
           t-"\\textcolor{yellow}{" 
            -to_LaTeX(a1,site_name,public_dir,[textcolor . lenv],s_en,us_en) 
            -("}"+textcolor_end(lenv))                                         else
   
         if name = "red"     then 
           if next_maml_is(a1,"code")
           then to_LaTeX(a1,site_name,public_dir,lenv,s_en,us_en) 
           else (t-"\\textcolor{red}{"
                  -to_LaTeX(a1,site_name,public_dir,[textcolor . lenv],s_en,us_en) 
                  -("}"+textcolor_end(lenv)))                                  else

         if name = "grey"    then 
           t-"\\textcolor{my_grey}{"
            -to_LaTeX(a1,site_name,public_dir,[textcolor . lenv],s_en,us_en)
            -("}"+textcolor_end(lenv))                                         else

         if name = "green"   then 
           t-"\\textcolor{my_green}{"
            -to_LaTeX(a1,site_name,public_dir,[textcolor . lenv],s_en,us_en)
            -("}"+textcolor_end(lenv))                                         else

         if name = "blue"    then 
           t-"\\textcolor{blue}{"
            -to_LaTeX(a1,site_name,public_dir,[textcolor . lenv],s_en,us_en)
            -("}"+textcolor_end(lenv))                                         else

         if name = "big"     then 
           (if big_to_LaTeX(1,a1) is (string_latex,next_maml) then 
            t-("{"+string_latex)-to_LaTeX(next_maml,site_name,public_dir,lenv,s_en,us_en)-"}") else

         if name = "image"   then 
           image_to_LaTeX
             (a1,(MAML ma) |-> to_LaTeX(ma,site_name,public_dir,lenv,s_en,us_en),site_name,public_dir) 
                else

         if name = "code"    then 
           t-(s_en(unique)+"\n {\\tt \\setlongtables "
              + "\\begin{longtable}["+(if member(lenv,center) then "c" else "l")+"]{l}\n")
            -code_to_LaTeX(a1,failure,site_name,public_dir,[])
            -(" \\end{longtable}} \n"+us_en(unique)+" \n\n ")                         else

         if name = "center"  then 
           t-"\\begin{center} "
            -to_LaTeX(a1,site_name,public_dir,[center . lenv],s_en,us_en)
            -"\\end{center}"                                                         else

         if name = "link"    then 
           t-"{\\tt "-to_LaTeX(a1,site_name,public_dir,lenv,s_en,us_en)-"}"          else

         if name = "list"    then 
           t-(s_en(unique)+"\n \\begin{itemize}\n"+itemize_format)
            -to_LaTeX(a1,site_name,public_dir,[itemize . lenv],s_en,us_en)
            -("\\end{itemize} "+itemize_end(lenv)+"\n "+us_en(unique))                       else

         if name = "sub"     then 
           t-"\\mbox{$_{"-to_LaTeX(a1,site_name,public_dir,lenv,s_en,us_en)-"}$}"            else

         if name = "sup"     then 
           t-"\\mbox{\\textsuperscript{"-to_LaTeX(a1,site_name,public_dir,lenv,s_en,us_en)-"}}"  else 

         if name = "tt"      then 
           t-"{\\tt "-to_LaTeX(a1,site_name,public_dir,lenv,s_en,us_en)-"}"               else

         alert,
   
       mark(String name,MAML a1,MAML a2) then alert,
       mark(String name,MAML a1,MAML a2,MAML a3) then alert,
       m1 + m2 then   to_LaTeX(m1,site_name,public_dir,lenv,s_en,us_en)
                    - to_LaTeX(m2,site_name,public_dir,lenv,s_en,us_en)
     }.
   
   
public define Text
   to_LaTeX
     (
       MAML           m,
       String         site_name,
       String         public_dir,
       One -> String  select_english,
       One -> String  unselect_english
     ) =
   to_LaTeX(m,site_name,public_dir,[],select_english,unselect_english).
   
   
   
   
   *** Compatibility with maml.anubis version 1.  
   
   
public define String
   to_string
     (
       Text text
     ) =
   if text is 
     {
       t      then "",
       u - s  then to_string(u)+s, 
       u - v  then to_string(u)+to_string(v)
     }.
   
public define Result(Int32,String)
   convert_MAML_to_HTML
     (
       String s
     ) =
   if parse_MAML(make_stream(s)) is 
     {
       error(i) then error(i), 
       ok(m)    then ok(to_string(to_HTML(m)))
     }.
   
public define Result(Int32,String)
   convert_MAML_to_LaTeX
     (
       String         s,
       String         site_name,            // sommething like : "www.my_beautiful_site.com"
       String         public_dir,
       One -> String  select_english,
       One -> String  unselect_english
     ) =
   if parse_MAML(make_stream(s)) is 
     {
       error(i) then error(i), 
       ok(m)    then ok(to_string(to_LaTeX(m,site_name,public_dir,select_english,unselect_english)))
     }.