maml3.anubis
89.1 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
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
The Anubis Project
The Minimal Anubis Markup Language.
(MAML)
Version 3
Copyright (c) Alain Prouté 2015.
define One xxx = todo("Handle the restricted use of marks for the web.").
This file obsoletes the following files in the same directory:
latex.anubis
maml.anubis
Author: Alain Prouté (reusing some code by Alain Prouté, David René and Olivier Duvernois)
MAML was first implemented in 2005 and was mainly to be used for writing texts within a
web page. It was called 'minimalist' since it was to be used by web site users, hence had
to remain very simple.
After some time (about 10 years !), a need for a more generic and flexible tool appeared.
This led to some modifications in the original MAML (this was version 2). However, I wanted
to make everything cleaner, more efficient concerning LaTeX code generation, and also easier
to use for the programer. Hence, this version 3.
MAML texts can be translated into HTML and PDF (via LaTeX). Notice that producing HTML output
also uses LaTeX if the $latex mark is used in the MAML source text.
This program requires that the following are installed if LaTeX is to be used:
- LaTeX2e (together with some LaTeX packages; see below)
- dvipng (used only for producing images for the HTML output in presence of the $latex mark)
LaTeX2e and dvipng are available under Linux and Windows (you can use MiKTeX under Windows).
The LaTeX packages used by MAML3 are:
- babel
- inputenc
- amsfonts
- amsmath
- amssymb
- latexsym
- [all]xy
- color
- graphicx
Normally, they should be all present in a standard distribution.
----------------------------- Contents -----------------------------------------------------
*** (1) How to write texts in MAML.
*** (2) Errors.
*** (3) Parsing options.
*** (4) Generating an HTML output.
*** (5) Generating LaTeX and PDF output.
*** (6) Managing options.
*** (7) Parsing MAML first.
--------------------------------------------------------------------------------------------
*** (1) How to write texts in MAML.
A user's guide for writing texts in MAML can be found in maml3_tutorial.maml in 'library/doc_tools'.
This is a maml file that you can process as follows (provided that the present file is first compiled):
anbexec maml maml3_tutorial.maml
This requires that LaTeX2e and dvipng are installed because, of course, the tutorial examplifies the
$latex mark. This produces maml3_tutorial.html and maml3_tutorial.pdf. Actually, these two files are already
part of the distribution. The HTML file refers to images which are also stored in 'library/doc_tools'.
The command line tool:
global define One maml (List(String) args).
*** (2) Errors.
Of course, syntax errors can be found during the parsing of the MAML text, and also later during
the output.
public type MAML_Pos: // position in a MAML text
position (Int line,
Int column,
Int offset).
public type MAML_Error:
//empty_mark_name (MAML_Pos pos),
unknown_mark (MAML_Pos pos, String name), // name of mark not recognized
too_few_arguments (MAML_Pos pos, String mark_name,
Int found,
Int expected),
input_file_not_found (String path),
in_input_file (String path, MAML_Error e),
error_in_verbatim_arg (MAML_Pos pos, String mark_name),
name_expected_in_define (MAML_Pos pos), // first argument of $define must be a name
number_expected_in_define (MAML_Pos pos), // second argument of $define must be a natural integer
unexpected_eof_in_argument (MAML_Pos pos, String mark_name),
cannot_create_file (String path),
cannot_write_file (String path),
cannot_execute_latex,
cannot_execute_pdflatex,
latex_error (Int return_code, String path_of_log_file),
cannot_execute_dvipng,
dvipng_error (Int return_code),
png_file_corrupted (String path).
The function below formats a MAML error into an English text.
public define String to_English (MAML_Error e).
*** (3) Parsing options.
Options can be transmitted to the MAML parser.
public type MAML_Parse_Option:
web.
The option 'web' should be used if maml is used for parsing texts comming from a web client. The effet of this
option is to inhibit all maml marks which could be dangerous for the server. These marks are
not documented in 'maml3_tutorial.maml'. They are the following:
$input(<file_path>) allows to input the content of a file (similar to LaTeX '\input', or Anubis 'read').
$output(<file path>)(text) allows to output text to a file.
*** (4) Generating an HTML output.
A MAML source text is read through the use of a 'stream' as defined in:
read tools/streams.anubis
This allows to read MAML texts from files, network connections and character strings (and more if you
define new sorts of streams). You can of course also use the stand-alone 'maml' module as shown above.
In order to produce an HTML or a PDF output, you need to provide some parameters. These parameters
are gathered into a datum of type 'MAML_HTML_Options' for HTML output and 'MAML_PDF_Options' for
PDF output.
public type MAML_HTML_Options:... (explained in section 5)
There is a default value:
public define MAML_HTML_Options default.
We use the type 'Text' for storing texts. This is a variant of 'Printable_tree'. Such data can
be printed.
public type Text:...
public define One print(WStream s, Text text).
In order to produce an HTML output, use the following:
public define Result(MAML_Error,Text)
to_HTML
(
List(MAML_Parse_Option) parse_options,
Stream maml_source,
MAML_HTML_Options options // you can put 'default' here
).
*** (5) Generating LaTeX and PDF output.
Again there are options and a default value:
public type MAML_PDF_Options:...
public define MAML_PDF_Options default.
public define Result(MAML_Error,Text)
to_PDF
(
List(MAML_Parse_Option) parse_options,
Stream maml_source,
Maybe((String,String)) pdf_output_path_and_name, // no output file if 'failure'
MAML_PDF_Options options // you can put 'default' here
).
Putting 'failure' for 'pdf_output_path_and_name' is used if one wants only the LaTeX text.
*** (6) Managing options.
If you are not satisfied by the default options, you can create your own by defining data of
these types:
public type MAML_HTML_Options:
options
(
// directories
String png_path_server, // the directory where dvipng generated images will be stored
// for example: site_directory/"public/png"
String png_path_client, // the same one but seen from the client viewpoint (i.e. included in the HTML text)
// in this case: "png" since site_directory/"public" is the root from the
// client viewpoint
String tmp_path, // the directory for temporary files on the server (.tex, .dvi, .aux, .log, ...)
Int font_size, // font size for main text (in pixels)
Int note_font_size, // font size for text in footnotes
Int text_width // width of text in pixels
).
public type MAML_PDF_Options:
options
(
// directories
String tmp_path, // the directory for temporary files (.tex, .dvi, .aux, .log, ...)
// LaTeX specific
String babel_language, // the language for the Babel package ("english", "french", ...)
String add_to_preambule, // what you want to add just before \begin{document}
Int page_width, // in millimeters
Int page_height // in millimeters
).
You can of course use the value "" for 'add_to_preambule'. If you want to include a table of contents,
use $ifpdf(\tableofcontents) in your MAML source, etc...
*** (7) Parsing MAML first.
Here is how to avoid parsing twice the MAML source text if you want to produce both outputs.
You can first parse the MAML source, which yields a datum of type:
public type MAML:...
public define Result(MAML_Error,MAML)
parse_MAML
(
List(MAML_Parse_Option) parse_options,
Stream s
).
and from this datum produce the HTML, LaTeX and PDF outputs:
public define Result(MAML_Error,Text)
to_HTML
(
MAML maml_text,
MAML_HTML_Options options // you can put 'default' here
).
public define Result(MAML_Error,Text)
to_PDF
(
MAML maml_text,
Maybe(String) pdf_output_path_and_name, // no output file if 'failure'
MAML_PDF_Options options // you can put 'default' here
).
--- That's all for the public part ! ------------------------------------------------------
read tools/basis.anubis
read tools/findstring.anubis
read system/files.anubis
---------------- Contents --------------------------------------------------------------
*** [1] Tools.
*** [1.1] Dividing a Word32 by a Float.
*** [1.2] The type 'Text'.
*** [1.3] Positions in MAML texts.
*** [1.4] The type 'MAML' of MAML texts.
*** [1.5] Tools for handling special characters.
*** [1.5] Reading the argument of $verbatim and of $latex.
*** [2] Parsing MAML.
*** [2.1] Informations about a MAML mark.
*** [2.2] Getting the number of arguments from the name of a mark.
*** [2.3] Reading 'chunks' of text.
*** [2.4] Reading the name of a mark.
*** [2.5] Reading the arguments of a mark.
*** [2.6] Reading a complete mark.
*** [2.7] managing levels of parentheses.
*** [2.8] Parsing MAML texts.
*** [3] Handling special marks.
*** [3.1] verbatim.
*** [3.2] define.
*** [3.3] latex.
*** [3.3.1] LaTeX preambule and postambule for png images.
*** [3.3.2] LaTeX preambule and postambule for PDF output.
*** [4] Producing HTML output.
*** [4.1] Formating colors for HTML.
*** [4.2] A convenient return type 'To_HTML_Result'.
*** [4.3] Kleisli-like functions for handling errors.
*** [4.4] Translating MAML to HTML.
*** [4.5] The public tool 'to_HTML'.
*** [4.6] Default options for HTML.
*** [5] Producing LaTeX output.
*** [5.1] Formating text sises (for $big).
*** [5.2] Preparing text of arguments of $code and $tt.
*** [5.3] Translating MAML to LaTeX.
*** [6] Producing PDF output.
*** [7] The command line tool 'maml'.
*** [7.1] Formating positions.
*** [7.2] Translating error messages to English.
*** [7.3] The command line tool.
----------------------------------------------------------------------------------------
*** [1] Tools.
*** [1.1] Dividing a Word32 by a Float.
define Word32
Word32 x / Float f
=
if to_Float(x) / f is
{
failure then x,
success(q) then integral_part_to_Word32(q)
}.
define Float
Float f / Int n
=
if f/to_Float(n) is
{
failure then f,
success(q) then q
}.
*** [1.2] The type 'Text'.
For performance reasons, instead of using character strings (of type 'String') for
representing texts, we use the following type.
public type Text:
t, // empty text
txt_str(Text,String), // text followed by a string
txt_txt(Text,Text). // text followed by a text
Convenience functions:
public define Text Text t - String s = txt_str(t,s).
public define Text Text t - Text u = txt_txt(t,u).
so that the text 'Are you okay ?' may be written like this:
t - " Are" - " you" - " okay ?"
This is useful for easily concatenating pieces of text (without copying anything).
We provide a conversion function (avoid it as much as possible):
public define String
to_string
(
Text text
) =
if text is
{
t then "",
txt_str(a,b) then to_string(a)+b,
txt_txt(a,b) then to_string(a)+to_string(b)
}.
and a printing function:
public define One
print
(
WStream file,
Text text
) =
if text is
{
t then unique,
txt_str(_0,_1) then print(file,_0); print(file,_1),
txt_txt(_0,_1) then print(file,_0); print(file,_1)
}.
*** [1.3] Positions in MAML texts.
define MAML_Pos
get_pos
(
Stream s
) =
position(current_line(s),current_column(s),current_offset(s)).
*** [1.4] The type 'MAML' of MAML texts.
type MAML:
end,
var (Int), // MAML variable $1, $2, ...
text (Text), // ordinary text
mark (String name, List(MAML) args), // mark
MAML + MAML. // compound MAML
Reformating a MAML as a string (used by $input for getting the file name).
define String
to_string
(
MAML m
) =
if m is
{
end then "",
var(v) then "$"+abs_to_decimal(v),
text(txt) then to_string(txt),
mark(name,args) then if args is [] then "$"+name+" " else
"$" + name + concat(map((MAML a) |-> "("+to_string(a)+")" ,args)),
a+b then to_string(a) + to_string(b)
}.
*** [1.5] Tools for handling special characters.
If the MAML text contains the character '<' for example, it must be transformed into '<' in the
HTML output, and so on...
public type SpecialChar:
schar (Word8 c,
List(Word8) transform). // into html or latex
define List(SpecialChar)
html_schar
=
[
schar('<', explode("<")),
schar('>', explode(">")),
schar('&', explode("&")),
schar('~', explode(" "))
].
define List(SpecialChar)
latex_schar
=
[
schar('<', explode("\\textless ")),
schar('>', explode("\\textgreater ")),
schar('&', explode("\\& ")),
schar('#', explode("\\# ")),
schar('{', explode("\\{ ")),
schar('}', explode("\\} ")),
schar('_', explode("\\_")),
schar('\\', explode("\\textbackslash ")),
schar('^', explode("\\textasciicircum ")),
schar('~', explode("\\textasciitilde ")),
schar('[', explode("{[}")),
schar('|', explode("\\textbar ")),
schar('*', explode("{*}")),
schar('%', explode("\\%"))
].
public define Maybe(List(Word8))
is_special_char
(
Word8 char,
List(SpecialChar) lsc
) =
if lsc is
{
[ ] then failure,
[h . t] then since h is schar(c,val),
if c = char
then success(val)
else is_special_char(char,t)
}.
define List(Word8)
handle_special_chars
(
List(Word8) s,
List(SpecialChar) lsc
) =
if s is
{
[ ] then [ ],
[h . t] then if is_special_char(h,lsc) is
{
failure then [h . handle_special_chars(t,lsc)],
success(val) then val + handle_special_chars(t,lsc)
}
}.
define Text
handle_special_chars
(
Text text,
List(SpecialChar) lsc
) =
if text is
{
t then t,
txt_str(te,s) then handle_special_chars(te,lsc) - implode(handle_special_chars(explode(s),lsc)),
txt_txt(t1,t2) then handle_special_chars(t1,lsc) - handle_special_chars(t2,lsc)
}.
*** [1.6] Reading several bytes.
define List(Word8)
read_bytes // read at most n bytes
(
Stream s,
Int n
) =
if n =< 0 then [ ] else
if read_byte(s) is
{
failure then [ ],
success(c) then [c . read_bytes(s,n-1)]
}.
define Int
update_level
(
Int level,
List(Word8) cs
) =
if cs is
{
[ ] then level,
[h . t] then
if h = '('
then 1 + update_level(level,t)
else if h = ')'
then (-1) + update_level(level,t)
else update_level(level,t)
}.
*** [1.7] Reading the argument of $verbatim and of $latex.
We must not interpret anything except $lpar and $rpar, and we must take care of levels of parentheses.
define Result(MAML_Error,String)
read_latex_argument
(
Stream s,
String mark_name,
Int level,
List(Word8) so_far,
) =
if read_byte(s) is
{
failure then error(error_in_verbatim_arg(get_pos(s),mark_name)),
success(c) then
if c = '('
then read_latex_argument(s,mark_name,level+1,[c . so_far])
else if c = ')'
then if level = 0
then ok(implode(reverse(so_far)))
else read_latex_argument(s,mark_name,level-1,[c . so_far])
else read_latex_argument(s,mark_name,level,[c . so_far])
}.
define Result(MAML_Error,String)
read_latex_argument
(
Stream s,
String mark_name
) =
skip_blanks(s);
if read_byte(s) is
{
failure then error(error_in_verbatim_arg(get_pos(s),mark_name)),
success(c) then
if c = '(' // the opening parenthese which delimits the argument
then read_latex_argument(s,mark_name,0,[])
else error(error_in_verbatim_arg(get_pos(s),mark_name))
}.
define Result(MAML_Error,String)
read_verbatim_argument
(
Stream s,
String mark_name,
Int level,
List(Word8) so_far,
) =
if read_byte(s) is
{
failure then error(error_in_verbatim_arg(get_pos(s),mark_name)),
success(c) then
if c = '('
then read_verbatim_argument(s,mark_name,level+1,[c . so_far])
else if c = ')'
then if level = 0
then ok(implode(reverse(so_far)))
else read_verbatim_argument(s,mark_name,level-1,[c . so_far])
else if c = '$'
then with bs = read_bytes(s,4),
ibs = implode(bs),
//print("--------->"+ibs+"<--------\n");
//print("update level = "+update_level(level,bs)+"\n");
if update_level(level,bs) < 0
then (unput_bytes(reverse(bs),s); read_verbatim_argument(s,mark_name,level,[c . so_far]))
else if length(bs) = 4
then if ibs = "lpar"
then read_verbatim_argument(s,mark_name,level,['(' . so_far])
else if ibs = "rpar"
then read_verbatim_argument(s,mark_name,level,[')' . so_far])
else (unput_bytes(reverse(bs),s); read_verbatim_argument(s,mark_name,level,[c . so_far]))
else (unput_bytes(reverse(bs),s); read_verbatim_argument(s,mark_name,level,[c . so_far]))
else read_verbatim_argument(s,mark_name,level,[c . so_far])
}.
define Result(MAML_Error,String)
read_verbatim_argument
(
Stream s,
String mark_name
) =
skip_blanks(s);
if read_byte(s) is
{
failure then error(error_in_verbatim_arg(get_pos(s),mark_name)),
success(c) then
if c = '(' // the opening parenthese which delimits the argument
then read_verbatim_argument(s,mark_name,0,[])
else error(error_in_verbatim_arg(get_pos(s),mark_name))
}.
*** [2] Parsing MAML.
*** [2.1] Informations about a MAML mark.
We need a notion of 'context' for storing the definition established by $define.
type MAML_Macro:
macro (String name,
Int numargs,
MAML body).
A predefined MAML context:
define List(MAML_Macro)
predefined
=
[
macro("red", 1, mark("rgb",[text(t-"220,0,0"), var(1)])),
macro("yellow", 1, mark("rgb",[text(t-"180,180,0"), var(1)])),
macro("green", 1, mark("rgb",[text(t-"0,180,0"), var(1)])),
macro("blue", 1, mark("rgb",[text(t-"0,0,200"), var(1)])),
macro("grey", 1, mark("rgb",[text(t-"90,90,90"), var(1)]))
].
public type MAMLMark:
mark(String name,
Int number_of_arguments).
The list af all these informations is available in the list below. This list grows when a
$define is encountered. See 'maml3_tutorial.maml.pdf' for explanations about each mark.
public define List(MAMLMark)
all_maml_marks
=
[
mark("lq", 0), // left quotation mark <<
mark("rq", 0), // right quotation mark >>
mark("~", 0),
mark("tbgc", 2), // text background color: $tbgc(color)(text)
mark("note", 1),
//mark("input", 1),
mark("title", 1),
mark("section", 1),
mark("subsection", 1),
mark("subsubsection", 1),
mark("ifhtml", 1),
mark("ifpdf", 1),
mark("define", 3), // defining a MAML macro: $define(name)(2)(body ... $1 ... $2 ...)
mark("dollar", 0), // the caracter $ itself
mark("", 0), // same as "dollar"
mark("lpar", 0), // left parenthesis
mark("rpar", 0), // right parenthesis
mark("par", 0), // new paragraph
mark("item", 0), // used within lists
mark("bold", 1),
mark("italic", 1),
mark("rgb", 2), // $rgb(r,g,b)(text)
mark("big", 1),
mark("image", 2), // $image(width)(file)
mark("code", 2), // used to display computer code: $code(bgcolor)(text)
mark("output", 2),
mark("verbatim", 1), // used to display raw text
mark("center", 1),
mark("box", 2),
mark("label", 1),
mark("ref", 2),
mark("tlink", 2), // hypertext link with text
mark("comment", 1), // ignored by the MAML parser
mark("mailto", 2), // mail
mark("list", 1),
mark("sub", 1), // indices
mark("sup", 1), // exponents
mark("tt", 1), // use typewriter font
mark("latex", 1) // used for inserting LaTeX text
] + map((MAML_Macro mac) |-> if mac is macro(name,n,body) then mark(name,n), predefined).
*** [2.2] Getting the number of arguments from the name of a mark.
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(Int)
number_of_arguments
(
String name,
List(MAMLMark) l
) =
if l is
{
[ ] then failure,
[h . t] then if h is mark(n,i) then
if name = n
then success(i)
else number_of_arguments(name,t)
}.
*** [2.3] Reading 'chunks' 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(Word8) so_far, // characters already read in reverse order
Int 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: "+word8_to_Word32(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).
*** [2.4] Reading the name of a mark.
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
(
Word8 c
) =
('a' +=< c & c +=< 'z') | ('A' +=< c & c +=< 'Z') | ('0' +=< c & c +=< '9') | (c = '~').
define Bool
is_blank_char
(
Word8 c
) =
member(c," \n\r\t").
define String
read_mark_name_aux // '$' already read
(
Stream s,
List(Word8) 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_aux(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
}.
type ReadMarkNameResult:
error (MAML_Error),
mark (String name), // a true mark name $name
var (Int name). // the name of a variable $1
define ReadMarkNameResult
read_mark_name
(
Stream s
) =
with name = read_mark_name_aux(s,[]),
if name = "" then mark("") else
if decimal_scan(name) is
{
failure then mark(name),
success(n) then var(n)
}.
*** [2.5] Reading the arguments of a mark.
Forward declaration of 'parse_MAML':
define Result(MAML_Error,(MAML,List(MAMLMark))) // returns the MAML and the new list of marks
parse_MAML
(
List(MAML_Parse_Option) parse_options,
Stream s,
List(MAMLMark) maml_marks,
Maybe(Int) 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(MAML_Error,(MAML,List(MAMLMark)))
read_argument
(
List(MAML_Parse_Option) parse_options,
Stream s,
String mark_name,
Int naar, // number of arguments already read
Int expected, // number of arguments expected
List(MAMLMark) maml_marks,
) =
skip_blanks(s);
if read_byte(s) is
{
failure then // opening parenthese not found
error(too_few_arguments(get_pos(s),mark_name,naar,expected)),
success(c) then
if c = '('
then parse_MAML(parse_options,s,maml_marks,success(0)) // terminal call
else error(too_few_arguments(get_pos(s),mark_name,naar,expected))
}.
define Result(MAML_Error,(List(MAML),List(MAMLMark)))
read_arguments // other than those already read
(
List(MAML_Parse_Option) parse_options,
Stream s,
String mark_name,
Int naar, // number of arguments already read
Int expected, // number of arguments expected
List(MAMLMark) maml_marks,
) =
if naar >= expected then ok(([],maml_marks)) else
if read_argument(parse_options,s,mark_name,naar,expected,maml_marks) is
{
error(msg) then error(msg),
ok(p) then
since p is (first,new_marks),
if read_arguments(parse_options,s,mark_name,naar+1,expected,new_marks) is
{
error(msg) then error(msg),
ok(p2) then since p2 is (others,final_marks),
ok(([first . others],final_marks))
}
}.
*** [2.6] Reading a complete mark.
'$' character is already read.
define Result(MAML_Error,(MAML,List(MAMLMark)))
read_mark
(
List(MAML_Parse_Option) parse_options,
Stream s,
List(MAMLMark) maml_marks
) =
if read_mark_name(s) is
{
error(msg) then error(msg),
mark(name) then
if name = "end" then ok((end,maml_marks)) else
if name = "verbatim" then if read_verbatim_argument(s,"verbatim") is
{
error(msg) then error(msg),
ok(verbatim_body) then ok((mark(name,[text(t-verbatim_body)]),maml_marks))
} else
if name = "latex" then if read_latex_argument(s,"latex") is
{
error(msg) then error(msg),
ok(latex_body) then ok((mark(name,[text(t-latex_body)]),maml_marks))
} else
if (name = "input" & not(web:parse_options)) then if read_argument(parse_options,s,name,0,1,maml_marks) is
{
error(msg) then error(msg),
ok(p) then since p is (arg,new_marks),
with file_path = to_string(arg),
if file(file_path,read) is
{
failure then error(input_file_not_found(file_path)),
success(fp1) then if parse_MAML(parse_options,make_stream(fp1),new_marks,success(0)) is
{
error(msg) then error(in_input_file(file_path,msg)),
ok(p2) then ok(p2)
}
}
} else
if number_of_arguments(name,maml_marks) is
{
failure then error(unknown_mark(get_pos(s),name)),
success(n) then if read_arguments(parse_options,s,name,0,n,maml_marks) is
{
error(msg) then error(msg),
ok(p) then since p is (args,new_marks), ok((mark(name,args),new_marks))
}
},
var(i) then
ok((var(i),maml_marks))
}.
*** [2.7] managing levels of parentheses.
Levels of type 'Maybe(Int)' must be incremented and decremented.
define Maybe(Int)
Maybe(Int) mb_level + Int 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(Int) 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.
}.
define Maybe(String)
is_pure_text
(
MAML m
) =
if m is
{
end then failure,
var(_) then failure,
text(txt) then success(to_string(txt)),
mark(_,_) then failure,
m1 + m2 then
if is_pure_text(m1) is
{
failure then failure,
success(txt1) then if is_pure_text(m2) is
{
failure then failure,
success(txt2) then success(txt1+txt2)
}
}
}.
define Maybe(Int)
decimal_scan
(
MAML m
) =
if is_pure_text(m) is
{
failure then failure,
success(t) then decimal_scan(t)
}.
define Maybe(String)
is_mark_name
(
MAML m
) =
if is_pure_text(m) is
{
failure then failure,
success(s) then
if mapand((Word8 c) |-> member(c,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~"),explode(s))
then success(s)
else failure
}.
*** [2.8] Parsing MAML texts.
define Result(MAML_Error,List(MAMLMark))
update_mark_list
(
Stream s,
MAML m,
List(MAMLMark) list
) =
if m is mark(name,args)
then if name = "define"
then if args is // $define has 3 arguments: $define(name)(num_args)(value)
{
[ ] then should_not_happen(ok(list)),
[a1 . t1] then if t1 is
{
[ ] then should_not_happen(ok(list)),
[a2 . t2] then
if decimal_scan(a2) is
{
failure then error(number_expected_in_define(get_pos(s))),
success(i) then
if is_mark_name(a1) is
{
failure then error(name_expected_in_define(get_pos(s)))
success(the_name) then ok([mark(the_name,i) . list])
}
}
}
}
else ok(list)
else ok(list).
Ok, now we are ready to parse the MAML.
define Result(MAML_Error,(MAML,List(MAMLMark)))
parse_MAML
(
List(MAML_Parse_Option) parse_options,
Stream s,
List(MAMLMark) maml_marks,
Maybe(Int) mb_level
) =
with prefix = read_chunks(s,t), // t is the empty 'Text'
if read_byte(s) is
{
failure then ok((text(prefix),maml_marks)),
success(c) then
if c = '$'
then if read_mark(parse_options,s,maml_marks) is
{
error(msg) then error(msg),
ok(p) then since p is (m,new_marks),
if m is end then ok((text(prefix),maml_marks)) else
if update_mark_list(s,m,new_marks) is // add a new mark if m is $define()()()
{
error(msg) then error(msg),
ok(maml_marks1) then if parse_MAML(parse_options,s,maml_marks1,mb_level) is
{
error(msg) then error(msg),
ok(p2) then since p2 is (rest,final_marks),
ok((text(prefix)+m+rest,final_marks))
}
}
}
else if c = '('
then if parse_MAML(parse_options,s,maml_marks,mb_level+1) is
{
error(msg) then error(msg),
ok(p) then since p is (rest,new_marks),
ok((text(prefix)+text(t-"(")+rest,new_marks))
}
else if c = ')'
then if must_stop_reading(mb_level)
then ok((text(prefix),maml_marks))
else if parse_MAML(parse_options,s,maml_marks,mb_level+(-1)) is
{
error(msg) then error(msg),
ok(p) then since p is (rest,new_marks),
ok((text(prefix)+text(t-")")+rest,new_marks))
}
else should_not_happen(error(unknown_mark(get_pos(s),"")))
}.
public define Result(MAML_Error,(MAML,List(MAMLMark)))
parse_MAML
(
List(MAML_Parse_Option) parse_options,
Stream s
) =
parse_MAML(parse_options,s,all_maml_marks,failure).
define One e2 = todo "handle the case of $1, $2, ... found outside third argument of $define".
*** [3] Handling special marks.
*** [3.1] 'must_be_text'.
define String
must_be_text // used by $verbatim and $latex
(
MAML m
) =
if m is
{
end then "",
var(i) then "$"+i,
text(s) then to_string(s),
mark(name,args) then "$"+name+concat(map((MAML m1) |-> "("+must_be_text(m1)+")",args),""),
m1 + m2 then must_be_text(m1)+must_be_text(m2)
}.
*** [3.2] define.
Extending the context with a new definition:
define List(MAML_Macro)
make_macro
(
MAML name,
MAML numargs,
MAML body,
List(MAML_Macro) ctxt
) =
if is_pure_text(name) is
{
failure then should_not_happen(ctxt),
success(name1) then if is_pure_text(numargs) is
{
failure then should_not_happen(ctxt),
success(na) then if decimal_scan(na) is
{
failure then should_not_happen(ctxt),
success(n) then [macro(name1,n,body) . ctxt]
}
}
}.
Getting the definition of a defined mark:
define Maybe(MAML)
is_defined
(
String name,
List(MAML_Macro) ctxt
) =
if ctxt is
{
[ ] then failure,
[h . ctxt1] then since h is macro(na,nu,body),
if name = na
then success(body)
else is_defined(name,
ctxt1)
}.
Replacing formal arguments of defined marks by their values.
define MAML
replace_args
(
MAML body,
List(MAML) values
) =
if body is
{
end then end,
var(i) then
if nth(i-1,values) is
{
failure then should_not_happen(body),
success(val) then val
},
text(_) then body,
mark(name,args) then
mark(name,map((MAML m) |-> replace_args(m,values), args)),
m1 + m2 then replace_args(m1,values) + replace_args(m2,values)
}.
*** [3.3] latex.
read web/web_arg_encode.anubis (used for creating file names)
read graphism/png_size.anubis ('png_image_size' needed)
*** [3.3.1] LaTeX preambule and postambule for png images.
define String
latex_png_preambule
=
"\\documentclass[12pt]{article}
\\usepackage[english]{babel}
\\usepackage[utf8]{inputenc}
\\usepackage[T1]{fontenc}
\\usepackage{amsfonts,amsmath,amssymb}
\\usepackage{latexsym}
\\usepackage[all]{xy}
\\usepackage{color}
\\pagestyle{empty}
\\textwidth20cm
\\textheight30cm
\\begin{document}
{\\Huge
".
\\usepackage{tikz}
\\usetikzlibrary{shapes,arrows}
define String
latex_png_postambule
=
"
}
\\end{document}
".
*** [3.3.2] LaTeX preambule and postambule for PDF output.
define String
latex_pdf_preambule
(
String add_to_prea
) =
"\\documentclass[11pt]{article}
\\usepackage[french,english]{babel}
\\usepackage[utf8]{inputenc}
\\usepackage[T1]{fontenc}
\\usepackage{amsfonts,amsmath,amssymb}
\\usepackage{latexsym}
\\usepackage[all]{xy}
\\usepackage{tabularx}
\\usepackage{array}
\\usepackage{color,colortbl}
\\usepackage{graphicx}
\\pagestyle{empty}
\\textwidth16cm
\\textheight22cm
\\oddsidemargin0mm
\\evensidemargin0mm
\\parindent0mm
\\renewcommand{\familydefault}{pnc}
\\newenvironment{liste}{\\begin{list}{$\\bullet$}{\\itemsep-1mm \\topsep0mm
\\partopsep0mm}}{\\end{list}}
\\author{MAML3}
"+add_to_prea+
"
\\begin{document}
".
\\usepackage{tikz}
\\usetikzlibrary{shapes,arrows}
\\usepackage[pdftex,colorlinks=true,linkcolor=blue,pdfencoding=auto,psdextra]{hyperref}
\\PassOptionsToPackage{unicode}{hyperref}
\\PassOptionsToPackage{naturalnames}{hyperref}
define String
latex_pdf_postambule
=
"
\\end{document}
".
Check if a LaTeX body contains a display $$....$$ (starting at n-th byte)
define Bool
has_a_display
(
String text,
Int n
) =
if nth(n,text) is
{
failure then false,
success(c) then
if c = '$'
then if nth(n+1,text) is
{
failure then false,
success(d) then
if d = '$'
then true
else has_a_display(text,n+2)
}
else has_a_display(text,n+1)
}.
type ResultPNG_height:
error (MAML_Error),
png_file_height (String fname, Word32 height), // height of image in pixels
log_file (String fname).
public define ResultPNG_height
make_png_file
(
String text, // LaTeX body
String fname, // a common name for the files
Bool true_png_file, // false for the temporary png file
MAML_HTML_Options opts
) =
since opts is options(spath,cpath,tpath,fsize,fnsize,twidth),
with fnametex = fname+".tex", // actual name of the .tex file
fnamedvi = fname+".dvi", // idem .dvi file
fnamepng = fname+".png", // idem .png file
if write_to_file(tpath/fnametex,
to_byte_array(latex_png_preambule+text+latex_png_postambule)) is
{
cannot_open_file then error(cannot_create_file(tpath/fnametex)),
write_error(_) then error(cannot_write_file(tpath/fnametex)),
ok then if (Maybe(Word8))execute(success(tpath),"latex",
["-no-shell-escape", // avoid security problems
"-interaction=batchmode", // quiet mode
//"nonstopmode", // avoid stopping
fnametex]) is
{
failure then error(cannot_execute_latex),
success(rcode1) then if rcode1 /= 0
then
(
/* return the error code and path of log file */
error(latex_error(to_Int([rcode1]),(tpath/fname)+".log"))
)
else
// The DVI file is okay. Convert it to PNG.
if (Maybe(Word8))execute(success(tpath),"dvipng",
["-T","tight","-Q","8","-bg","Transparent","-o",fnamepng,fnamedvi]) is
{
failure then error(cannot_execute_dvipng),
success(rcode2) then
if rcode2 /= 0
then error(dvipng_error(to_Int([rcode2])))
else if png_image_size(tpath/fnamepng) is
{
failure then error(png_file_corrupted(tpath/fnamepng)),
success(p) then
(if tpath = spath
then unique
else if true_png_file
then forget(copy_file(tpath/fnamepng,spath/fnamepng))
else unique);
since p is (w,h), png_file_height(cpath/fnamepng,h)
}
}
}
}.
type ResultPNG_down:
error (MAML_Error),
png_file_down (String fname,
Word32 width, // of image
Word32 height,
Word32 down), // number of pixels the image must be shifted down in the HTML page
log_file (String fname).
define ResultPNG_down // returns the name of the png file or of the log file
latex_to_png
(
String text, // a LaTex body
MAML_HTML_Options opts
) =
with hash = sha1(text), // create a unique name for all our files
corefname = web_arg_encode(hash),
spath = png_path_server(opts),
if file_exists(spath/("/f"+corefname+".png"))
then with dims = if png_image_size(spath+"/f"+corefname+".png") is
{
failure then (0,0),
success(p) then p
},
since dims is (w,h),
png_file_down(spath/("f"+corefname+".png"),w,h,
if png_image_size(spath+"/t"+corefname+".png") is
{
failure then 0,
success(p) then if p is (w1,h1) then
if h1 +=< 100 then 0 else h1-100
})
else if make_png_file(text,"f"+corefname,true,opts) is
{
error(msg) then error(msg),
png_file_height(true_fnamepng,true_height) then
with dims = if png_image_size(spath+"/f"+corefname+".png") is
{
failure then (0,0),
success(p) then p
},
since dims is (w,h),
if has_a_display(text,0)
then png_file_down(true_fnamepng,w,h,0)
else if make_png_file("\\rule{1mm}{1in}"+text,"t"+corefname,true,opts) is
{
error(msg) then error(msg),
png_file_height(_,fake_height) then
png_file_down(true_fnamepng,w,h,if fake_height +=< 100 then 0 else fake_height-100),
log_file(fname) then log_file(fname)
},
log_file(fname) then log_file(fname)
}.
*** [4] Producing HTML output.
*** [4.1] Formating colors for HTML.
define String
html_format
(
RGB color
) =
if color is rgb(r,g,b) then
"#" + to_hexa(r) + to_hexa(g) + to_hexa(b).
*** [4.2] A convenient return type 'To_HTML_Result'.
type Output:
outfile(String name,
WStream fp).
type To_HTML_Result:
error (MAML_Error),
ok (Text text,
List(MAML_Macro) ctxt,
List(Output) outs).
*** [4.3] Kleisli-like functions for handling errors.
define To_HTML_Result
Text txt - To_HTML_Result r
=
if r is
{
error(msg) then error(msg),
ok(text,ctxt,outs) then ok(txt - text,ctxt,outs)
}.
define To_HTML_Result
To_HTML_Result r - String s
=
if r is
{
error(msg) then error(msg),
ok(text,ctxt,outs,) then ok(text - s,ctxt,outs)
}.
define To_HTML_Result
ok
(
To_HTML_Result r,
List(MAML_Macro) ctxt,
List(Output) outs
) =
if r is
{
error(msg) then error(msg),
ok(text,_,_) then ok(text,ctxt,outs)
}.
define To_HTML_Result
To_HTML_Result r1 - To_HTML_Result r2
=
if r1 is
{
error(msg) then error(msg),
ok(t1,c1,o1) then if r2 is
{
error(msg) then error(msg),
ok(t2,c2,o2) then ok(t1 - t2,c2,o2)
}
}.
define Bool
has_a_display
(
To_HTML_Result r
) =
if r is
{
error(_) then false,
ok(txt,_,_) then has_a_display(to_string(txt),0)
}.
*** [4.4] Translating MAML to HTML.
define Bool
is_in
(
String name,
List(String) stack
) =
name:stack.
define String
get_code_bg_color
(
List(MAML_Macro) l
) =
if l is
{
[ ] then "rgb(0,0,0)",
[h . t] then since h is macro(name,numargs,body),
if name = "codebgcolor" & numargs = 1 // $codebgcolor(r,g,b)
then "rgb("+must_be_text(body)+")"
else get_code_bg_color(t)
}.
define List(Output)
do_output
(
String filename,
Text text,
List(Output) outs,
List(Output) all_outs
) =
if outs is
{
[ ] then if file(filename,new) is
{
failure then print("Cannot create file '"+filename+"'.\n"); all_outs,
success(fp) then print(weaken(fp),to_string(text)); [outfile(filename,weaken(fp)) . all_outs]
},
[h . t] then since h is outfile(name,p),
if name = filename
then (print(p,to_string(text)); all_outs)
else do_output(filename,text,t,all_outs)
}.
define List(Output)
do_output
(
String filename,
To_HTML_Result t,
List(Output) all_outs
) =
if t is
{
error(_) then all_outs,
ok(text,_,outs) then do_output(filename,text,outs,outs)
}.
define To_HTML_Result
to_HTML
(
MAML m,
List(MAML_Macro) ctxt,
MAML_HTML_Options opts,
List(String) stack,
Maybe(WStream) tocfile,
List(Output) outs,
Var(Int) lab
) =
since opts is options(spath,cpath,tpath,fsize,fnsize,twidth),
if m is
{
end then ok(t,ctxt,outs),
var(i) then should_not_happen(ok(t,ctxt,outs)),
text(txt) then ok(handle_special_chars(txt,html_schar),ctxt,outs),
mark(name,args) then
if args is
{
[ ] then
// no operand
if name = "lq" then ok(t-"«",ctxt,outs) else
if name = "rq" then ok(t-"»",ctxt,outs) else
if name = "~" then ok(t-"~",ctxt,outs) else
if name = "dollar" then ok(t-"$",ctxt,outs) else
if name = "" then ok(t-"$",ctxt,outs) else
if name = "lpar" then ok(t-"(",ctxt,outs) else
if name = "rpar" then ok(t-")",ctxt,outs) else
if name = "par" then ok(t-"<br>",ctxt,outs) else
if name = "item" then ok(t-"<li>",ctxt,outs) else
if is_defined(name,ctxt) is
{
failure then should_not_happen(ok(t,ctxt,outs)),
success(body) then // found $name with no argument
to_HTML(body,ctxt,opts,stack,tocfile,outs,lab)
},
[a1 . t1] then if t1 is
{
[ ] then
// one operand
if name = "note" then ok(t-"<div class=\"dropdown\">
<span>(<sup>note</sup>)</span>
<div class=\"drp-content\" style=\"font-size:"-to_decimal(fnsize)
-"px; width:"-to_decimal(twidth-100)-"; padding: 6px 6px 6px 6px;\">"
-to_HTML(a1,ctxt,opts,["note" . stack],tocfile,outs,lab)-"</div></div>",ctxt,outs) else
if name = "title" then ok(t-"<center><h1>"-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"</h1></center>",ctxt,outs) else
if name = "section" then lab <- *lab+1;
if tocfile is
{
failure then unique,
success(tocf) then print(tocf,"$tocsec(toclab"+to_decimal(*lab)+")("+to_string(a1)+")\n")
};
ok(t-"<a id=toclab"-to_decimal(*lab)-"></a><h2>"-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"</h2>",ctxt,outs) else
if name = "subsection" then lab <- *lab+1;
if tocfile is
{
failure then unique,
success(tocf) then print(tocf,"$tocsubsec(toclab"+to_decimal(*lab)+")("+to_string(a1)+")\n")
};
ok(t-"<a id=toclab"-to_decimal(*lab)-"></a><h3>"-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"</h3>",ctxt,outs) else
if name = "subsubsection" then lab <- *lab+1;
if tocfile is
{
failure then unique,
success(tocf) then print(tocf,"$tocsubsubsec(toclab"+to_decimal(*lab)+")("+to_string(a1)+")\n")
};
ok(t-"<a id=toclab"-to_decimal(*lab)-"></a><h4>"-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"</h4>",ctxt,outs) else
if name = "ifhtml" then to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab) else
if name = "ifpdf" then ok(t,ctxt,outs) else
if name = "label" then ok(t-"<a id=\""-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"\"></a>",ctxt,outs) else
if name = "bold" then ok(t-"<strong>"-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"</strong>",ctxt,outs) else
if name = "italic" then ok(t-"<em>"-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"</em>",ctxt,outs) else
if name = "big" then ok(t-"<big>"-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"</big>",ctxt,outs) else
if name = "verbatim" then ok(t-"<br><pre>"
-must_be_text(a1)-"</pre><br>",ctxt,outs) else
if name = "center" then ok(t-"<br><center><table style=\"color: inherit; font-size: inherit;\"><tr><td>"
-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"</td></tr></table></center><br>",ctxt,outs) else
if name = "comment" then ok(t,ctxt,outs) else
if name = "list" then ok(t-"<span><ul>"-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"</ul></span>",ctxt,outs) else
if name = "sub" then ok(t-"<sub>"-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"</sub>",ctxt,outs) else
if name = "sup" then ok(t-"<sup>"-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"</sup>",ctxt,outs) else
if name = "tt" then ok(t-"<span style=\"font-family:'Lucida Console', monospace; font-size:inherit; font-style: normal;\">"
-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"</span>",ctxt,outs) else
if name = "latex" then with red_factor = if is_in("note",stack) then (30.0/fnsize) else (26.4/fsize),
with disp = has_a_display(must_be_text(a1),0),
if latex_to_png(must_be_text(a1),opts) is
{
error(msg) then error(msg),
png_file_down(fname,w,h,down) then
//print("=================== fname = "+fname+"\n");
ok(t-(if disp then "<br><center>" else "")
-"<img src=\""-fname-"\" width=\""-to_decimal(w/red_factor)
-"\" height=\""-to_decimal(h/red_factor)
-"\" style=\"vertical-align: -"-
to_decimal((down/red_factor))-"px\">"-(if disp then "</center><br>" else ""),ctxt,outs),
log_file(fname) then
ok(t-"<a href=\""-fname-"\">LaTeX error</a>",ctxt,outs)
} else
if is_defined(name,ctxt) is
{
failure then should_not_happen(ok(t,ctxt,outs)),
success(body) then // found $name with 1 argument
to_HTML(replace_args(body,[a1]),ctxt,opts,stack,tocfile,outs,lab)
},
[a2 . t2] then if t2 is
{
[ ] then
// two operands
if name = "output" then with outs1 = do_output(must_be_text(a1),t - must_be_text(a2),outs,outs),
ok(t,ctxt,outs1) else
if name = "code" then ok(t-"<pre style=\"color: inherit; font-size: inherit; background-color: rgb("
-must_be_text(a1)-"); width: "-to_decimal(text_width(opts))-"px;\">"-
to_HTML(a2,ctxt,opts,stack,tocfile,outs,lab)-"</pre>",ctxt,outs) else
if name = "tbgc" then ok(t-"<div style=\"display: inline; background-color: rgb("-must_be_text(a1)-"); \">"-
to_HTML(a2,ctxt,opts,stack,tocfile,outs,lab)-"</div>",ctxt,outs) else
if name = "image" then ok(t-"<img src=\""-to_HTML(a2,ctxt,opts,stack,tocfile,outs,lab)-"\" width=\""
-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"\">",ctxt,outs) else
if name = "rgb" then ok(t-"<div style=\"display: inline; color: rgb("-must_be_text(a1)-")\">"
-to_HTML(a2,ctxt,opts,stack,tocfile,outs,lab)-"</div>",ctxt,outs) else
if name = "box" then ok(t-"<div align=\"left\" style=\"display: inline-block; width: "-
to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"px;\">"
-to_HTML(a2,ctxt,opts,stack,tocfile,outs,lab)-"</div>",ctxt,outs) else
if name = "ref" then ok(t-"<a rel=\"tag\" href=\"#"-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"\">"
-to_HTML(a2,ctxt,opts,stack,tocfile,outs,lab)-"</a>",ctxt,outs) else
if name = "tlink" then with n = to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab),
c = to_HTML(a2,ctxt,opts,stack,tocfile,outs,lab),
ok(t-"<a href=\""-c-"\" target=\"w"-(""+to_ascii(sha1(a1)))-"\">"-n-"</a>",ctxt,outs) else
if name = "mailto" then ok(t-"<a href=\"mailto:"-to_HTML(a1,ctxt,opts,stack,tocfile,outs,lab)-"\">"
-to_HTML(a2,ctxt,opts,stack,tocfile,outs,lab)- "</a>",ctxt,outs) else
if is_defined(name,ctxt) is
{
failure then should_not_happen(ok(t,ctxt,outs)),
success(body) then // found $name with 2 argument
to_HTML(replace_args(body,[a1,a2]),ctxt,opts,stack,tocfile,outs,lab)
},
[a3 . t3] then if t3 is
{
[ ] then
// three operands
if name = "define" then ok(t,make_macro(a1,
a2,
a3,
ctxt),outs) else
if is_defined(name,ctxt) is
{
failure then should_not_happen(ok(t,ctxt,outs)),
success(body) then // found $name with 3 argument
to_HTML(replace_args(body,[a1,a2,a3]),ctxt,opts,stack,tocfile,outs,lab)
},
[_ . _] then
// at least four operands
if is_defined(name,ctxt) is
{
failure then should_not_happen(ok(t,ctxt,outs)),
success(body) then // found $name with at least 4 argument
to_HTML(replace_args(body,args),ctxt,opts,stack,tocfile,outs,lab)
},
}
}
}
}
m1 + m2 then
if to_HTML(m1,ctxt,opts,stack,tocfile,outs,lab) is
{
error(msg) then error(msg),
ok(text1,ctxt1,outs1) then if to_HTML(m2,ctxt1,opts,stack,tocfile,outs1,lab) is
{
error(msg) then error(msg),
ok(text2,ctxt2,outs2) then ok(text1-text2,ctxt2,outs2)
}
}
}.
*** [4.5] The public tool 'to_HTML'.
public define Result(MAML_Error,Text)
to_HTML
(
List(MAML_Parse_Option) parse_options,
Stream maml_source,
MAML_HTML_Options opts, // you can put 'default' here
Maybe(WStream) tocfile
) =
if (Result(MAML_Error,(MAML,List(MAMLMark))))parse_MAML(parse_options,maml_source) is
{
error(msg) then error(msg),
ok(p) then since p is (m,_),
if to_HTML(m,predefined,opts,[],tocfile,[],var((Int)0)) is
{
error(msg) then error(msg),
ok(text,ctxt,outs) then ok(text)
}
}.
public define Result(MAML_Error,Text)
to_HTML
(
MAML maml_text,
MAML_HTML_Options opts, // you can put 'default' here
Maybe(WStream) tocfile
) =
since opts is options(spath,cpath,tpath,fsize,fnsize,twidth),
make_directories(".",split_path(spath));
make_directories(".",split_path(tpath));
if to_HTML(maml_text,predefined,opts,[],tocfile,[],var((Int)0)) is
{
error(msg) then error(msg),
ok(text,ctxt,outs) then ok(text)
}.
*** [4.6] Default options for HTML.
public define MAML_HTML_Options
default
=
options(
"png", // png_path_server
"png", // png_path_client
"tmp", // tmp_path
12, // font size
10, // footnote font size
600 // width of text (pixels)
).
*** [5] Producing LaTeX output.
*** [5.1] Formating text sises (for $big).
define String
latex_format_size
(
Int n
) =
if n = 0 then " " else
if n = 1 then "\\large " else
if n = 2 then "\\Large " else
if n = 3 then "\\LARGE " else
if n = 4 then "\\huge " else
"\\Huge ".
*** [5.2] Preparing the text for LaTeX.
define List(Word8)
latex_prepare
(
List(Word8) text,
Bool ic, // true if within the argument of $tt or $code
Bool il // within $latex or $verbatim
) =
if il then text else
if text is
{
[ ] then [ ],
[c . t] then
if c = '-' then ['{', '-', '}' . latex_prepare(t,ic,il)] else
if c = '_' then ['\\', '_' . latex_prepare(t,ic,il)] else
if c = '{' then ['\\', '{' . latex_prepare(t,ic,il)] else
if c = '}' then ['\\', '}' . latex_prepare(t,ic,il)] else
if c = '_' then ['\\', '_' . latex_prepare(t,ic,il)] else
if c = '^' then if ic
then explode("\\texttt{\\symbol{94}}") + latex_prepare(t,ic,il)
else ['\\', '^', '{', '}' . latex_prepare(t,ic,il)] else
if c = '&' then ['\\', '&' . latex_prepare(t,ic,il)] else
if c = '%' then ['\\', '%' . latex_prepare(t,ic,il)] else
if c = '#' then ['\\', '#' . latex_prepare(t,ic,il)] else
if c = '\\' then if ic
then explode("{\\texttt{\\symbol{92}}}") + latex_prepare(t,ic,il)
else explode("{\\textbackslash}") + latex_prepare(t,ic,il) else
if c = ' ' then if ic
then ['\\', ' ' . latex_prepare(t,ic,il)]
else [c . latex_prepare(t,ic,il)] else
if c = '\n' then if ic
then explode("\\\\\n") + latex_prepare(t,ic,il)
else [c . latex_prepare(t,ic,il)] else
if c = '\r' then latex_prepare(t,ic,il) else
//if c = '$' then explode("{\\textbackslash}") + latex_prepare(t,ic,il) else
// '$' can appear in MAML only in the argument of $verbatim and should not be changed
// since \begin{verbatim}\end{verbatim} will render it as $.
[c . latex_prepare(t,ic,il)]
}.
define Text
latex_prepare
(
Text t,
Bool ic,
Bool il
) =
if t is
{
t then t,
txt_str(a,b) then txt_str(latex_prepare(a,ic,il),implode(latex_prepare(explode(b),ic,il))),
txt_txt(a,b) then txt_txt(latex_prepare(a,ic,il),latex_prepare(b,ic,il))
}.
define MAML
latex_prepare
(
MAML m,
Bool ic,
Bool il
) =
if m is
{
end then end,
var(i) then m,
text(txt) then text(latex_prepare(txt,ic,il)),
mark(name,args) then if (name = "tt" | name = "code")
then mark(name,map((MAML u) |-> latex_prepare(u,true,il) ,args))
else if (name = "latex" | name = "verbatim")
then mark(name,map((MAML u) |-> latex_prepare(u,ic,true) ,args))
else mark(name,map((MAML u) |-> latex_prepare(u,ic,il) ,args)),
m1 + m2 then latex_prepare(m1,ic,il) + latex_prepare(m2,ic,il)
}.
define String
scale_to_1
(
Int n
) =
if (Maybe(Float))(to_Float(n)/255) is
{
failure then should_not_happen("0.0"),
success(q) then float_to_string(q,3)
}.
define String
force_sub_string
(
String s,
Int start,
Int len
) =
if sub_string(s,start,len) is
{
failure then "0",
success(s1) then s1
}.
define Maybe((Int,Int,Int))
decompose_color
(
String c
) =
//print("c = "+c+"\n");
if find_string(c,",",0) is
{
failure then failure,
success(comma1) then //print("comma1 = "+comma1+"\n");
if find_string(c,",",comma1+1) is
{
failure then failure,
success(comma2) then //print("comma2 = "+comma2+"\n");
with r = force_sub_string(c,0,comma1),
g = force_sub_string(c,comma1+1,comma2-comma1-1),
b = force_sub_string(c,comma2+1,length(c)-(comma2+1)),
//print(r+"|"+g+"|"+b+"\n");
if decimal_scan(r) is
{
failure then failure,
success(r1) then if decimal_scan(g) is
{
failure then failure,
success(g1) then if decimal_scan(b) is
{
failure then failure,
success(b1) then
success((r1,g1,b1))
}
}
}
}
}.
define String
scale_color
(
String c
) =
if decompose_color(c) is
{
failure then "0.0,0.0,0.0",
success(t) then since t is (r,g,b),
scale_to_1(r)+","+scale_to_1(g)+","+scale_to_1(b)
}.
*** [5.3] Translating MAML to LaTeX.
define (Text,List(MAML_Macro))
to_LaTeX
(
MAML m,
List(MAML_Macro) ctxt,
Int size, // used for nested $big
Bool ic // if true we are currently within $tt() or $code()
) =
if m is
{
end then (t,ctxt),
var(i) then (t,ctxt),
text(txt) then (txt,ctxt),
mark(name,args) then
if args is
{
[ ] then // zero argument
if name = "lq" then (t-"\\og{}",ctxt) else
if name = "rq" then (t-"\\fg{}",ctxt) else
if name = "~" then (t-"\\textasciitilde{}",ctxt) else
if name = "dollar" then (t-"\\$",ctxt) else
if name = "" then (t-"\\$",ctxt) else
if name = "lpar" then (t-"(",ctxt) else
if name = "rpar" then (t-")",ctxt) else
if name = "par" then (t-"\\rule{1mm}{0mm}\n\n",ctxt) else
if name = "item" then (t-"\\item ",ctxt) else
if is_defined(name,ctxt) is
{
failure then should_not_happen((t,ctxt)),
success(body) then to_LaTeX(body,ctxt,size,ic)
},
[a1 . t1] then if t1 is
{
[ ] then // 1 argument
if name = "big" then since to_LaTeX(a1,ctxt,size+1,ic) is (_arg1,_nctxt),
(t-"{"-latex_format_size(size)-_arg1-"}",_nctxt) else
if name = "tt" then since to_LaTeX(if ic then a1 else latex_prepare(a1,true,false),ctxt,size,true) is (_arg1,_nctxt),
(t-"{\\tt{"-_arg1-"}}",_nctxt) else
if name = "latex" then since to_LaTeX(latex_prepare(a1,false,true),ctxt,size,ic) is (_arg1,_nctxt),
(t-"{}"-_arg1-"{}",_nctxt) else
since to_LaTeX(a1,ctxt,size,ic) is (arg1,nctxt),
if name = "note" then (t-"{\\rm(\\footnote{"-arg1-"})}",nctxt) else
if name = "title" then (t-"\\begin{center}\\Huge\\bf{}"-arg1-"\end{center}",nctxt) else
if name = "section" then (t-"\\section{"-arg1-"}",nctxt) else
if name = "subsection" then (t-"\\subsection{"-arg1-"}",nctxt) else
if name = "subsubsection" then (t-"\\subsubsection{"-arg1-"}",nctxt) else
if name = "ifhtml" then (t,nctxt) else
if name = "ifpdf" then (arg1,nctxt) else
if name = "label" then (t-"\\label{"-arg1-"}",nctxt) else
if name = "bold" then (t-"{\\bf{}"-arg1-"}",nctxt) else
if name = "italic" then (t-"{\\it{}"-arg1-"}",nctxt) else
if name = "verbatim" then (t-"\\begin{verbatim}"-must_be_text(a1)-"\\end{verbatim}",nctxt) else
if name = "center" then (t-"\\begin{center}"-arg1-"\\end{center}",nctxt) else
if name = "comment" then (t,nctxt) else
if name = "list" then (t-"\\begin{liste}"-arg1-"\\end{liste}",nctxt) else
if name = "sub" then (t-"{\\raisebox{-3pt}{\small{}"-arg1-"}}",nctxt) else
if name = "sup" then (t-"{\\raisebox{3pt}{\small{}"-arg1-"}}",nctxt) else
if is_defined(name,ctxt) is
{
failure then should_not_happen((t,ctxt)),
success(body) then to_LaTeX(replace_args(body,[a1]),ctxt,size,ic)
},
[a2 . t2] then if t2 is
{
[ ] then // 2 arguments
since to_LaTeX(a1,ctxt,size,ic) is (arg1,nctxt1),
since to_LaTeX(a2,nctxt1,size,ic) is (arg2,nctxt2),
if name = "output" then (t,nctxt2) else
if name = "code" then since to_LaTeX(if ic then a2
else latex_prepare(a2,true,false),ctxt,size,true) is (_arg2,_nctxt),
(t-"\n\n"
-"{\\tt\\begin{tabularx}{\\textwidth}{>{\\columncolor[rgb]{"-scale_color(to_string(arg1))
-"}}X}{}"-_arg2-"\\hfill{}"-
"\\end{tabularx}}\n\n",_nctxt) else
if name = "tbgc" then (t-"{\\definecolor{bgcol}{rgb}{"-scale_color(to_string(arg1))-"}\\colorbox{bgcol}{"-arg2-"}}",nctxt2) else
if name = "image" then (t-"{\\includegraphics[width="-to_string(arg1)-"pt]{"-arg2-"}}",nctxt2) else
if name = "rgb" then (t-"{\\color[rgb]{"-scale_color(to_string(arg1))-"}"-arg2-"}",nctxt2) else
// if name = "rgb" then (t-"{\\definecolor{txtcol}{rgb}{"-scale_color(to_string(arg1))-"}{\\color{txtcol}"-arg2-"}}",nctxt2) else
if name = "box" then (t-"\\makebox["-arg1-"px][l]{"-arg2-"}",nctxt2) else
if name = "ref" then (t-arg2-" (page \\pageref{"-arg1-"})",nctxt2) else
if name = "tlink" then (t-arg1-" ({\\tt{}"-arg2-"})",nctxt2) else
if name = "mailto" then (t-"{"-arg2-"} ({\\verb+"-arg1-"+})",nctxt2) else
if is_defined(name,ctxt) is
{
failure then should_not_happen((t,ctxt)),
success(body) then to_LaTeX(replace_args(body,[a1,a2]),ctxt,size,ic)
},
[a3 . t3] then if t3 is
{
[ ] then // 3 arguments
since to_LaTeX(a1,ctxt,size,ic) is (arg1,nctxt1),
since to_LaTeX(a2,nctxt1,size,ic) is (arg2,nctxt2),
since to_LaTeX(a3,nctxt2,size,ic) is (arg3,nctxt3),
if name = "define" then (t,make_macro(a1,a2,a3,nctxt3)) else
if is_defined(name,ctxt) is
{
failure then should_not_happen((t,ctxt)),
success(body) then to_LaTeX(replace_args(body,[a1,a2,a3]),ctxt,size,ic)
},
[_ . _] then // at least 4 arguments
if is_defined(name,ctxt) is
{
failure then should_not_happen((t,ctxt)),
success(body) then to_LaTeX(replace_args(body,args),ctxt,size,ic)
}
}
}
}
},
m1 + m2 then
if to_LaTeX(m1,ctxt,size,ic) is (txt1,ctxt1) then
if to_LaTeX(m2,ctxt1,size,ic) is (txt2,ctxt2) then
(txt1 - txt2, ctxt2)
}.
*** [5.4] Default options for LaTeX/PDF.
public define MAML_PDF_Options
default
=
options(".", // tmp_path
"english", // babel_language
"", // add_to_preambule
160, // page_width (millimeters)
240 // page_height (millimeters)
).
*** [6] Producing PDF output.
*** [6.1] Stream tool.
public define Result(MAML_Error,Text)
to_PDF
(
MAML maml_text,
Maybe((String,String)) pdf_output_path_and_name, // no output file if 'failure'
MAML_PDF_Options opts // you can put 'default' here
) =
since opts is options(tmp_path,babel,add,_,_),
since to_LaTeX(maml_text,predefined,0,false) is (txt,_),
if pdf_output_path_and_name is
{
failure then ok(txt),
success(p) then since p is (pdf_path,name),
if file(tmp_path/(name+".tex"),new) is
{
failure then error(cannot_create_file(tmp_path/(name+".tex"))),
success(texfile) then
forget(print(weaken(texfile),
t-latex_pdf_preambule(add_to_preambule(opts))-"\n"-txt-latex_pdf_postambule));
forget(flush(texfile));
if (Maybe(Word8))execute(success(tmp_path),"pdflatex",
[
"-no-shell-escape", // avoid security problems
"-interaction",
"nonstopmode", // avoid stopping
tmp_path/(name+".tex")]) is
{
failure then error(cannot_execute_pdflatex),
success(code) then
forget(move_file(tmp_path/(name+".pdf"),pdf_path/(name+".pdf")));
if code = 0
then ok(txt)
else error(latex_error(to_Int([code]),tmp_path/(name+".log")))
}
}
}.
*** [6.2] MAML tool.
public define Result(MAML_Error,Text)
to_PDF
(
List(MAML_Parse_Option) parse_options,
Stream maml_source,
Maybe((String,String)) pdf_output_path_and_name, // no output file if 'failure'
MAML_PDF_Options opts // you can put 'default' here
) =
if (Result(MAML_Error,(MAML,List(MAMLMark))))parse_MAML(parse_options,maml_source) is
{
error(msg) then error(msg)
ok(p) then since p is (m,_),
to_PDF(m,pdf_output_path_and_name,opts)
}.
*** [7] The command line tool 'maml'.
*** [7.1] Formating positions.
define String
format_without_offset
(
MAML_Pos pos
) =
if pos is position(line,column,offset) then
"line "+abs_to_decimal(line)+", column "+abs_to_decimal(column).
*** [7.2] Translating error messages to English.
public define String
to_English
(
MAML_Error e
) =
if e is
{
//empty_mark_name(pos) then
// "Empty mark name at "+format_without_offset(pos)+".",
unknown_mark(pos,name) then
"Unknown mark '$"+name+"' at "+format_without_offset(pos)+".",
too_few_arguments(pos,name,found,expected) then
"Too few arguments to $"+name+" at "+format_without_offset(pos)+
" found: "+abs_to_decimal(found)+
" expected: "+abs_to_decimal(expected)+".",
input_file_not_found(path) then
"Input file '"+path+"' not found.",
in_input_file(path,err) then
"In input file '"+path+"': "+to_English(err),
error_in_verbatim_arg(pos,mark_name) then
"Error at "+format_without_offset(pos)+" in argument of '$"+mark_name+"'.",
name_expected_in_define(pos) then
"First argument of $define et "+format_without_offset(pos)+" not an acceptable mark name.",
number_expected_in_define(pos) then
"Second argument of $define et "+format_without_offset(pos)+" must be a natural integer.",
unexpected_eof_in_argument(pos,name) then
"Unexpected end of text in argument of '$"+name+"' at "+format_without_offset(pos)+".",
cannot_create_file(path) then
"Cannot create file '"+path+"'.",
cannot_write_file(path) then
"Cannot write into file '"+path+"'.",
cannot_execute_latex then
"Cannot execute 'latex'.",
cannot_execute_pdflatex then
"Cannot execute 'pdflatex'.",
latex_error(code,path) then
"LaTeX error, return code = "+code+", path of log file: '"+path+"'.",
cannot_execute_dvipng then
"Cannot execute 'dvipng'.",
dvipng_error(code) then
"dvipng error, code = "+code+".",
png_file_corrupted(path) then
"PGN file '"+path+"' corrupted."
}.
*** [7.3] The command line tool.
define String
html_head
(
MAML_HTML_Options opts
) =
since opts is options(spath,cpath,tpath,fsize,fnsize,twidth),
"<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<style>
body { counter-reset: section;
font-size: "+fsize+"px; }
p { align: justify; width: "+twidth+"px; }
h2 { counter-reset: subsection; }
h2::before { counter-increment: section;
content: counter(section) \". \"; }
h3 { counter-reset: subsubsection; }
h3::before { counter-increment: subsection;
content: counter(section) \".\" counter(subsection) \" \"; }
h4::before { counter-increment: subsubsection;
content: counter(section) \".\" counter(subsection) \".\" counter(subsubsection) \" \"; }
.dropdown { position: relative;
display: inline-block; }
.drp-content { display: none;
position: fixed;
bottom: 10px;
right: 10px;
background-color: #ffcc00;
max-width: "+(twidth-100)+"px;
box-shadow: 0px 16px 16px 0px rgba(0,0,0,0.6);
padding: 0px 6px;
z-index: 0; }
.dropdown:hover .drp-content { display: block;
z-index: 2; }
</style>
</head>".
define Maybe(WStream)
weaken
(
Maybe(RWStream) s
) =
if s is
{
failure then failure,
success(f) then success(weaken(f))
}.
type Option:
pdf. // generate a PDF file
define List(Option)
get_options
(
List(String) args
) =
if args is
{
[ ] then [ ],
[h . t] then
if h = "-pdf" then [pdf . get_options(t)] else
get_options(t)
}.
global define One
maml
(
List(String) args
) =
with options = get_options(args),
if args is
{
[ ] then print("Usage: anbexec maml <filename> <option> ... <option>\n"+
" Options:\n"+
" -pdf generate a PDF (and LaTeX) output\n"),
[h . _] then if file(h,read) is
{
failure then print("File '"+h+"' not found.\n"),
success(fp) then print("Compiling '"+h+"' ...\n");
if file(h+".mamltoc",read) is // create an empty .mamltoc file if it doesn't exist
{
failure then print("Creating an empty '"+h+".mamltoc' file.\n");
if file(h+".mamltoc",new) is
{
failure then print("Cannot create file '"+h+".mamltoc'.\n"),
success(fp2) then print(weaken(fp2),"\n")
},
success(_) then unique
};
with s = make_stream(fp),
if (Result(MAML_Error,(MAML,List(MAMLMark))))parse_MAML([],s) is
{
error(msg) then print(to_English(msg)+"\n"),
ok(p) then since p is (m,_),
(
since (MAML_HTML_Options)default is options(spath,cpath,tpath,fsize,fnsize,twidth),
make_directories(".",split_path(spath));
make_directories(".",split_path(tpath))
);
if to_HTML(m,predefined,default,[],weaken(file(h+".mamltoc",new)),[],var((Int)0)) is
{
error(msg) then print(to_English(msg)+"\n"),
ok(html_text,_,_) then
// output the HTML
if file(h+".html",new) is
{
failure then print("Cannot create file '"+h+".html'.\n"),
success(f) then forget(print(weaken(f),
t-"<html>"-html_head(default)-
"<body><center><table style=\"width: 600px; font-size:12px;\"><tr><td>"-
"<div style=\"text-align: justify; text-justify: inter-word; width: "-to_decimal(text_width(default))-"px;\">"
-html_text-"</div></td></tr></table></center></body></html>"))
};
// output the LaTeX
if pdf:options
then (if to_LaTeX(m,predefined,0,false) is (latex_text,_) then
if file(h+".tex",new) is
{
failure then print("Cannot create file '"+h+".tex'.\n"),
success(f) then forget(print(weaken(f),
t-latex_pdf_preambule("")-latex_text-latex_pdf_postambule));
forget(flush(f));
if (Maybe(Word8))execute(success("."),"pdflatex",
["-no-shell-escape", // avoid security problems
"-interaction", "nonstopmode", // avoid stopping
h+".tex"]) is
{
failure then print("Cannot execute pdflatex.\n"),
success(_) then unique
}
})
else unique
}
}
}
}.