package omd
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file parser.ml
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
open Ast open Compat module Sub : sig type t val of_string : string -> t val to_string : t -> string val offset : int -> t -> t val lexbuf : t -> Lexing.lexbuf val contains : string -> t -> bool val print : Format.formatter -> t -> unit val head : ?rev:unit -> t -> char option val tail : ?rev:unit -> t -> t val heads : int -> t -> char list val tails : int -> t -> t val for_all : (char -> bool) -> t -> bool val exists : (char -> bool) -> t -> bool val is_empty : t -> bool val length : t -> int val sub : len:int -> t -> t end = struct type t = { base : string ; off : int ; len : int } let of_string base = { base; off = 0; len = String.length base } let to_string { base; off; len } = String.sub base off len let print ppf s = Format.fprintf ppf "%S" (to_string s) let length { len; _ } = len let offset n { base; off; len } = if n < 0 then invalid_arg "offset"; let rec loop n base off len = if n = 0 || len = 0 then { base; off; len } else match base.[off] with | '\t' -> let ts = ((off + 4) / 4 * 4) - off in let b = Buffer.create len in Buffer.add_substring b base 0 off; for _ = 1 to ts do Buffer.add_char b ' ' done; Buffer.add_substring b base (off + 1) (len - 1); loop n (Buffer.contents b) off (len + ts - 1) | _ -> loop (n - 1) base (off + 1) (len - 1) in loop n base off len let lexbuf s = Lexing.from_string (to_string s) let contains s1 { base; off; len } = let rec loop off = if off + String.length s1 > len then false else s1 = String.sub base off (String.length s1) || loop (off + 1) in loop off let head ?rev s = match (rev, s) with | _, { len = 0; _ } -> None | None, { base; off; _ } -> Some base.[off] | Some (), { base; off; len } -> Some base.[off + len - 1] let tail ?rev s = match (rev, s) with | _, { len = 0; _ } -> s | None, { base; off; len } -> { base; off = succ off; len = pred len } | Some (), { base; off; len } -> { base; off; len = pred len } let heads n s = if n < 0 then invalid_arg "heads"; let rec loop n s = if n = 0 || length s = 0 then [] else match head s with | Some c -> c :: loop (pred n) (tail s) | None -> [] in loop n s let tails n s = if n < 0 then invalid_arg "tails"; let rec loop n s = if n = 0 then s else loop (pred n) (tail s) in loop n s let is_empty s = length s = 0 let exists f s = let rec loop s i = if i >= s.len then false else if f s.base.[s.off + i] then true else loop s (succ i) in loop s 0 let for_all f s = not (exists (fun c -> not (f c)) s) let sub ~len s = if len > s.len then invalid_arg "sub"; { s with len } end exception Fail module P : sig type state type 'a t = state -> 'a val of_string : string -> state val peek : char option t val peek_exn : char t val pos : state -> int val range : state -> int -> int -> string val set_pos : state -> int -> unit val junk : unit t val char : char -> unit t val next : char t val ( ||| ) : 'a t -> 'a t -> 'a t val ws : unit t val sp : unit t val ws1 : unit t val ( >>> ) : unit t -> 'a t -> 'a t val ( <<< ) : 'a t -> unit t -> 'a t val protect : 'a t -> 'a t val peek_before : char -> state -> char val peek_after : char -> state -> char val pair : 'a t -> 'b t -> ('a * 'b) t end = struct type state = { str : string ; mutable pos : int } let of_string str = { str; pos = 0 } type 'a t = state -> 'a let char c st = if st.pos >= String.length st.str then raise Fail; if st.str.[st.pos] <> c then raise Fail; st.pos <- st.pos + 1 let next st = if st.pos >= String.length st.str then raise Fail else let c = st.str.[st.pos] in st.pos <- st.pos + 1; c let peek_exn st = if st.pos >= String.length st.str then raise Fail else st.str.[st.pos] let peek st = if st.pos >= String.length st.str then None else Some st.str.[st.pos] let peek_before c st = if st.pos = 0 then c else st.str.[st.pos - 1] let peek_after c st = if st.pos + 1 >= String.length st.str then c else st.str.[st.pos + 1] let pos st = st.pos let range st pos n = String.sub st.str pos n let set_pos st pos = st.pos <- pos let junk st = if st.pos < String.length st.str then st.pos <- st.pos + 1 let protect p st = let off = pos st in try p st with | e -> set_pos st off; raise e let ( ||| ) p1 p2 st = try protect p1 st with | Fail -> p2 st let ws st = let rec loop () = match peek_exn st with | ' ' | '\t' | '\010' .. '\013' -> junk st; loop () | _ -> () in try loop () with | Fail -> () let sp st = let rec loop () = match peek_exn st with | ' ' | '\t' -> junk st; loop () | _ -> () in try loop () with | Fail -> () let ws1 st = match peek_exn st with | ' ' | '\t' | '\010' .. '\013' -> junk st; ws st | _ -> raise Fail let ( >>> ) p q st = p st; q st let ( <<< ) p q st = let x = p st in q st; x let pair p q st = let x = p st in let y = q st in (x, y) end type html_kind = | Hcontains of string list | Hblank type code_block_kind = | Tilde | Backtick type t = | Lempty | Lblockquote of Sub.t | Lthematic_break | Latx_heading of int * string * attributes | Lsetext_heading of int * int | Lfenced_code of int * int * code_block_kind * (string * string) * attributes | Lindented_code of Sub.t | Lhtml of bool * html_kind | Llist_item of list_type * int * Sub.t | Lparagraph | Ldef_list of string let sp3 s = match Sub.head s with | Some ' ' -> ( let s = Sub.tail s in match Sub.head s with | Some ' ' -> ( let s = Sub.tail s in match Sub.head s with | Some ' ' -> (3, Sub.tail s) | Some _ | None -> (2, s)) | Some _ | None -> (1, s)) | Some _ | None -> (0, s) let ( ||| ) p1 p2 s = try p1 s with | Fail -> p2 s let rec ws ?rev s = match Sub.head ?rev s with | Some (' ' | '\t' | '\010' .. '\013') -> ws ?rev (Sub.tail ?rev s) | None | Some _ -> s let is_empty s = Sub.is_empty (ws s) let thematic_break s = match Sub.head s with | Some (('*' | '_' | '-') as c) -> let rec loop n s = match Sub.head s with | Some c1 when c = c1 -> loop (succ n) (Sub.tail s) | Some (' ' | '\t' | '\010' .. '\013') -> loop n (Sub.tail s) | Some _ -> raise Fail | None -> if n < 3 then raise Fail; Lthematic_break in loop 1 (Sub.tail s) | Some _ | None -> raise Fail let setext_heading s = match Sub.head s with | Some (('-' | '=') as c) -> let rec loop n s = match Sub.head s with | Some c1 when c = c1 -> loop (succ n) (Sub.tail s) | Some _ | None -> if not (Sub.is_empty (ws s)) then raise Fail; if c = '-' && n = 1 then raise Fail; (* can be interpreted as an empty list item *) Lsetext_heading ( (if c = '-' then 2 else 1) , n ) in loop 1 (Sub.tail s) | Some _ | None -> raise Fail let is_punct = function | '!' | '"' | '#' | '$' | '%' | '&' | '\'' | '(' | ')' | '*' | '+' | ',' | '-' | '.' | '/' | ':' | ';' | '<' | '=' | '>' | '?' | '@' | '[' | '\\' | ']' | '^' | '_' | '`' | '{' | '|' | '}' | '~' -> true | _ -> false let parse_attributes = function | None -> [] | Some s -> ( let attributes = String.split_on_char ' ' s in let f (id, classes, acc) s = if s = "" then (id, classes, acc) else match s.[0] with | '#' -> (Some (String.sub s 1 (String.length s - 1)), classes, acc) | '.' -> (id, String.sub s 1 (String.length s - 1) :: classes, acc) | _ -> ( let attr = String.split_on_char '=' s in match attr with | [] -> (id, classes, acc) | h :: t -> (id, classes, (h, String.concat "=" t) :: acc)) in let id, classes, acc = List.fold_left f (None, [], []) attributes in let acc = List.rev acc in let acc = if classes <> [] then ("class", String.concat " " (List.rev classes)) :: acc else acc in match id with | Some id -> ("id", id) :: acc | None -> acc) let attribute_string s = let buf = Buffer.create 64 in let rec loop s = match Sub.head s with | None -> (Sub.of_string (Buffer.contents buf), None) | Some ('\\' as c) -> ( let s = Sub.tail s in match Sub.head s with | Some c when is_punct c -> Buffer.add_char buf c; loop (Sub.tail s) | Some _ | None -> Buffer.add_char buf c; loop s) | Some '{' -> let buf' = Buffer.create 64 in let rec loop' s = match Sub.head s with | Some '}' -> ( let s = Sub.tail s in match Sub.head s with | Some _ -> Buffer.add_char buf '{'; Buffer.add_buffer buf buf'; Buffer.add_char buf '}'; loop s | None -> ( Sub.of_string (Buffer.contents buf) , Some (Buffer.contents buf') )) | None -> Buffer.add_char buf '{'; Buffer.add_buffer buf buf'; (Sub.of_string (Buffer.contents buf), None) | Some '{' -> Buffer.add_char buf '{'; Buffer.add_buffer buf buf'; Buffer.reset buf'; loop' (Sub.tail s) | Some c -> Buffer.add_char buf' c; loop' (Sub.tail s) in loop' (Sub.tail s) | Some c -> Buffer.add_char buf c; loop (Sub.tail s) in let s', a = loop (ws s) in (s', parse_attributes a) let atx_heading s = let rec loop n s = if n > 6 then raise Fail; match Sub.head s with | Some '#' -> loop (succ n) (Sub.tail s) | Some (' ' | '\t' | '\010' .. '\013') -> let s, a = match Sub.head ~rev:() s with | Some '}' -> attribute_string s | _ -> (s, []) in let s = ws ~rev:() (ws s) in let rec loop t = match Sub.head ~rev:() t with | Some '#' -> loop (Sub.tail ~rev:() t) | Some (' ' | '\t' | '\010' .. '\013') | None -> ws ~rev:() t | Some _ -> s in Latx_heading (n, Sub.to_string (ws (loop s)), a) | Some _ -> raise Fail | None -> Latx_heading (n, Sub.to_string s, []) in loop 0 s let entity s = match Sub.heads 2 s with | '#' :: ('x' | 'X') :: _ -> let rec loop m n s = if m > 8 then raise Fail; match Sub.head s with | Some ('a' .. 'f' as c) -> loop (succ m) ((n * 16) + Char.code c - Char.code 'a' + 10) (Sub.tail s) | Some ('A' .. 'F' as c) -> loop (succ m) ((n * 16) + Char.code c - Char.code 'A' + 10) (Sub.tail s) | Some ('0' .. '9' as c) -> loop (succ m) ((n * 16) + Char.code c - Char.code '0') (Sub.tail s) | Some ';' -> if m = 0 then raise Fail; let u = if n = 0 || not (Uchar.is_valid n) then Uchar.rep else Uchar.of_int n in ([ u ], Sub.tail s) | Some _ | None -> raise Fail in loop 0 0 (Sub.tails 2 s) | '#' :: _ -> let rec loop m n s = if m > 8 then raise Fail; match Sub.head s with | Some ('0' .. '9' as c) -> loop (succ m) ((n * 10) + Char.code c - Char.code '0') (Sub.tail s) | Some ';' -> if m = 0 then raise Fail; let u = if n = 0 || not (Uchar.is_valid n) then Uchar.rep else Uchar.of_int n in ([ u ], Sub.tail s) | Some _ | None -> raise Fail in loop 0 0 (Sub.tail s) | ('a' .. 'z' | 'A' .. 'Z') :: _ -> let rec loop len t = match Sub.head t with | Some ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9') -> loop (succ len) (Sub.tail t) | Some ';' -> ( let name = Sub.to_string (Sub.sub ~len s) in match Entities.f name with | [] -> raise Fail | cps -> (cps, Sub.tail t)) | Some _ | None -> raise Fail in loop 1 (Sub.tail s) | _ -> raise Fail let info_string c s = let buf = Buffer.create 17 in let s, a = match Sub.head ~rev:() s with | Some '}' -> attribute_string s | _ -> (s, []) in let s = ws ~rev:() (ws s) in let rec loop s = match Sub.head s with | Some (' ' | '\t' | '\010' .. '\013') | None -> if c = '`' && Sub.exists (function | '`' -> true | _ -> false) s then raise Fail; ((Buffer.contents buf, Sub.to_string (ws s)), a) | Some '`' when c = '`' -> raise Fail | Some ('\\' as c) -> ( let s = Sub.tail s in match Sub.head s with | Some c when is_punct c -> Buffer.add_char buf c; loop (Sub.tail s) | Some _ | None -> Buffer.add_char buf c; loop s) | Some ('&' as c) -> ( let s = Sub.tail s in match entity s with | ul, s -> List.iter (Buffer.add_utf_8_uchar buf) ul; loop s | exception Fail -> Buffer.add_char buf c; loop s) | Some c -> Buffer.add_char buf c; loop (Sub.tail s) in loop (ws s) let fenced_code ind s = match Sub.head s with | Some (('`' | '~') as c) -> let rec loop n s = match Sub.head s with | Some c1 when c = c1 -> loop (succ n) (Sub.tail s) | Some _ | None -> if n < 3 then raise Fail; let s, a = info_string c s in let c = if c = '`' then Backtick else Tilde in Lfenced_code (ind, n, c, s, a) in loop 1 (Sub.tail s) | Some _ | None -> raise Fail let indent s = let rec loop n s = match Sub.head s with | Some ' ' -> loop (n + 1) (Sub.tail s) | Some '\t' -> loop (n + 4) (Sub.tail s) | Some _ | None -> n in loop 0 s let unordered_list_item ind s = match Sub.head s with | Some (('+' | '-' | '*') as c) -> let s = Sub.tail s in if is_empty s then Llist_item (Bullet c, 2 + ind, s) else let n = indent s in if n = 0 then raise Fail; let n = if n <= 4 then n else 1 in Llist_item (Bullet c, n + 1 + ind, Sub.offset n s) | Some _ | None -> raise Fail let ordered_list_item ind s = let rec loop n m s = match Sub.head s with | Some ('0' .. '9' as c) -> if n >= 9 then raise Fail; loop (succ n) ((m * 10) + Char.code c - Char.code '0') (Sub.tail s) | Some (('.' | ')') as c) -> let s = Sub.tail s in if is_empty s then Llist_item (Ordered (m, c), n + 1 + ind, s) else let ind' = indent s in if ind' = 0 then raise Fail; let ind' = if ind' <= 4 then ind' else 1 in Llist_item (Ordered (m, c), n + ind + ind' + 1, Sub.offset ind' s) | Some _ | None -> raise Fail in loop 0 0 s let tag_name s0 = match Sub.head s0 with | Some ('a' .. 'z' | 'A' .. 'Z') -> let rec loop len s = match Sub.head s with | Some ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '-') -> loop (succ len) (Sub.tail s) | Some _ | None -> (Sub.to_string (Sub.sub s0 ~len), s) in loop 1 (Sub.tail s0) | Some _ | None -> raise Fail let = [ "address" ; "aside" ; "base" ; "basefont" ; "blockquote" ; "body" ; "caption" ; "center" ; "col" ; "colgroup" ; "dd" ; "details" ; "dialog" ; "dir" ; "div" ; "dl" ; "dt" ; "fieldset" ; "figcaption" ; "figure" ; "footer" ; "form" ; "frame" ; "frameset" ; "h1" ; "h2" ; "h3" ; "h4" ; "h5" ; "h6" ; "head" ; "header" ; "hr" ; "html" ; "iframe" ; "legend" ; "li" ; "link" ; "main" ; "menu" ; "menuitem" ; "meta" ; "nav" ; "noframes" ; "ol" ; "optgroup" ; "option" ; "p" ; "param" ; "section" ; "source" ; "summary" ; "table" ; "tbody" ; "td" ; "tfoot" ; "th" ; "thead" ; "title" ; "tr" ; "track" ; "ul" ] let = [ "script"; "pre"; "style" ] let known_tag s = let s = String.lowercase_ascii s in List.mem s known_tags let special_tag s = let s = String.lowercase_ascii s in List.mem s special_tags let closing_tag s = let s = ws s in match Sub.head s with | Some '>' -> if not (is_empty (Sub.tail s)) then raise Fail; Lhtml (false, Hblank) | Some _ | None -> raise Fail let special_tag tag s = if not (special_tag tag) then raise Fail; match Sub.head s with | Some (' ' | '\t' | '\010' .. '\013' | '>') | None -> Lhtml (true, Hcontains [ "</script>"; "</pre>"; "</style>" ]) | Some _ -> raise Fail let known_tag tag s = if not (known_tag tag) then raise Fail; match Sub.heads 2 s with | (' ' | '\t' | '\010' .. '\013') :: _ | [] | '>' :: _ | '/' :: '>' :: _ -> Lhtml (true, Hblank) | _ -> raise Fail let ws1 s = match Sub.head s with | Some (' ' | '\t' | '\010' .. '\013') -> ws s | Some _ | None -> raise Fail let attribute_name s = match Sub.head s with | Some ('a' .. 'z' | 'A' .. 'Z' | '_' | ':') -> let rec loop s = match Sub.head s with | Some ('a' .. 'z' | 'A' .. 'Z' | '_' | '.' | ':' | '0' .. '9') -> loop (Sub.tail s) | Some _ | None -> s in loop s | Some _ | None -> raise Fail let attribute_value s = match Sub.head s with | Some (('\'' | '"') as c) -> let rec loop s = match Sub.head s with | Some c1 when c = c1 -> Sub.tail s | Some _ -> loop (Sub.tail s) | None -> raise Fail in loop (Sub.tail s) | Some _ -> let rec loop first s = match Sub.head s with | Some (' ' | '\t' | '\010' .. '\013' | '"' | '\'' | '=' | '<' | '>' | '`') | None -> if first then raise Fail; s | Some _ -> loop false (Sub.tail s) in loop true s | None -> raise Fail let attribute s = let s = ws1 s in let s = attribute_name s in let s = ws s in match Sub.head s with | Some '=' -> let s = ws (Sub.tail s) in attribute_value s | Some _ | None -> s let attributes s = let rec loop s = match attribute s with | s -> loop s | exception Fail -> s in loop s let open_tag s = let s = attributes s in let s = ws s in let n = match Sub.heads 2 s with | '/' :: '>' :: _ -> 2 | '>' :: _ -> 1 | _ -> raise Fail in if not (is_empty (Sub.tails n s)) then raise Fail; Lhtml (false, Hblank) let raw_html s = match Sub.heads 10 s with | '<' :: '?' :: _ -> Lhtml (true, Hcontains [ "?>" ]) | '<' :: '!' :: '-' :: '-' :: _ -> Lhtml (true, Hcontains [ "-->" ]) | '<' :: '!' :: '[' :: 'C' :: 'D' :: 'A' :: 'T' :: 'A' :: '[' :: _ -> Lhtml (true, Hcontains [ "]]>" ]) | '<' :: '!' :: _ -> Lhtml (true, Hcontains [ ">" ]) | '<' :: '/' :: _ -> let tag, s = tag_name (Sub.tails 2 s) in (known_tag tag ||| closing_tag) s | '<' :: _ -> let tag, s = tag_name (Sub.tails 1 s) in (special_tag tag ||| known_tag tag ||| open_tag) s | _ -> raise Fail let blank s = if not (is_empty s) then raise Fail; Lempty let tag_string s = let buf = Buffer.create 17 in let s, a = match Sub.head ~rev:() s with | Some '}' -> attribute_string s | _ -> (s, []) in let s = ws ~rev:() (ws s) in let rec loop s = match Sub.head s with | Some (' ' | '\t' | '\010' .. '\013') | None -> (Buffer.contents buf, a) | Some c -> Buffer.add_char buf c; loop (Sub.tail s) in loop (ws s) let def_list s = let s = Sub.tail s in match Sub.head s with | Some (' ' | '\t' | '\010' .. '\013') -> Ldef_list (String.trim (Sub.to_string s)) | _ -> raise Fail let indented_code ind s = if indent s + ind < 4 then raise Fail; Lindented_code (Sub.offset (4 - ind) s) let parse s0 = let ind, s = sp3 s0 in match Sub.head s with | Some '>' -> let s = Sub.offset 1 s in let s = if indent s > 0 then Sub.offset 1 s else s in Lblockquote s | Some '=' -> setext_heading s | Some '-' -> (setext_heading ||| thematic_break ||| unordered_list_item ind) s | Some '_' -> thematic_break s | Some '#' -> atx_heading s | Some ('~' | '`') -> fenced_code ind s | Some '<' -> raw_html s | Some '*' -> (thematic_break ||| unordered_list_item ind) s | Some '+' -> unordered_list_item ind s | Some '0' .. '9' -> ordered_list_item ind s | Some ':' -> def_list s | Some _ -> (blank ||| indented_code ind) s | None -> Lempty let parse s = try parse s with | Fail -> Lparagraph open P let is_empty st = let off = pos st in try let rec loop () = match next st with | ' ' | '\t' | '\010' .. '\013' -> loop () | _ -> set_pos st off; false in loop () with | Fail -> set_pos st off; true let inline_attribute_string s = let ppos = pos s in ws s; let a = match peek s with | Some '{' -> let buf = Buffer.create 64 in let rec loop s pos = match peek s with | Some '}' -> junk s; Some (Buffer.contents buf) | None | Some '{' -> set_pos s pos; None | Some c -> Buffer.add_char buf c; junk s; loop s pos in junk s; loop s (pos s) | _ -> None in let attr = parse_attributes a in if attr = [] then set_pos s ppos; attr let entity buf st = let p = pos st in if next st <> '&' then raise Fail; match peek st with | Some '#' -> ( junk st; match peek st with | Some ('x' | 'X') -> junk st; let rec aux n m = if n > 8 then Buffer.add_string buf (range st p (pos st - p)) else match peek st with | Some ('0' .. '9' as c) -> junk st; aux (succ n) ((m * 16) + Char.code c - Char.code '0') | Some ('a' .. 'f' as c) -> junk st; aux (succ n) ((m * 16) + Char.code c - Char.code 'a' + 10) | Some ('A' .. 'F' as c) -> junk st; aux (succ n) ((m * 16) + Char.code c - Char.code 'A' + 10) | Some ';' -> junk st; if n = 0 then Buffer.add_string buf (range st p (pos st - p)) else let u = if Uchar.is_valid m && m <> 0 then Uchar.of_int m else Uchar.rep in Buffer.add_utf_8_uchar buf u | Some _ | None -> Buffer.add_string buf (range st p (pos st - p)) in aux 0 0 | Some '0' .. '9' -> let rec aux n m = if n > 8 then Buffer.add_string buf (range st p (pos st - p)) else match peek st with | Some ('0' .. '9' as c) -> junk st; aux (succ n) ((m * 10) + Char.code c - Char.code '0') | Some ';' -> junk st; if n = 0 then Buffer.add_string buf (range st p (pos st - p)) else let u = if Uchar.is_valid m && m <> 0 then Uchar.of_int m else Uchar.rep in Buffer.add_utf_8_uchar buf u | Some _ | None -> Buffer.add_string buf (range st p (pos st - p)) in aux 0 0 | Some _ | None -> Buffer.add_string buf (range st p (pos st - p))) | Some ('0' .. '9' | 'a' .. 'z' | 'A' .. 'Z') -> let q = pos st in let rec aux () = match peek st with | Some ('0' .. '9' | 'a' .. 'z' | 'A' .. 'Z') -> junk st; aux () | Some ';' -> ( let name = range st q (pos st - q) in junk st; match Entities.f name with | [] -> Buffer.add_string buf (range st p (pos st - p)) | _ :: _ as cps -> List.iter (Buffer.add_utf_8_uchar buf) cps) | Some _ | None -> Buffer.add_string buf (range st p (pos st - p)) in aux () | Some _ | None -> Buffer.add_string buf (range st p (pos st - p)) module Pre = struct type delim = | Ws | Punct | Other type emph_style = | Star | Underscore type link_kind = | Img | Url type t = | Bang_left_bracket | Left_bracket of link_kind | Emph of delim * delim * emph_style * int | R of attributes inline let concat = function | [ x ] -> x | l -> Concat ([], l) let left_flanking = function | Emph (_, Other, _, _) | Emph ((Ws | Punct), Punct, _, _) -> true | _ -> false let right_flanking = function | Emph (Other, _, _, _) | Emph (Punct, (Ws | Punct), _, _) -> true | _ -> false let is_opener = function | Emph (pre, _, Underscore, _) as x -> left_flanking x && ((not (right_flanking x)) || pre = Punct) | Emph (_, _, Star, _) as x -> left_flanking x | _ -> false let is_closer = function | Emph (_, post, Underscore, _) as x -> right_flanking x && ((not (left_flanking x)) || post = Punct) | Emph (_, _, Star, _) as x -> right_flanking x | _ -> false let classify_delim = function | '!' | '"' | '#' | '$' | '%' | '&' | '\'' | '(' | ')' | '*' | '+' | ',' | '-' | '.' | '/' | ':' | ';' | '<' | '=' | '>' | '?' | '@' | '[' | '\\' | ']' | '^' | '_' | '`' | '{' | '|' | '}' | '~' -> Punct | ' ' | '\t' | '\010' .. '\013' | '\160' -> Ws | _ -> Other let to_r = function | Bang_left_bracket -> Text ([], "![") | Left_bracket Img -> Text ([], "![") | Left_bracket Url -> Text ([], "[") | Emph (_, _, Star, n) -> Text ([], String.make n '*') | Emph (_, _, Underscore, n) -> Text ([], String.make n '_') | R x -> x let rec parse_emph = function | (Emph (pre, _, q1, n1) as x) :: xs when is_opener x -> let rec loop acc = function | (Emph (_, post, q2, n2) as x) :: xs when is_closer x && q1 = q2 -> let xs = if n1 >= 2 && n2 >= 2 then if n2 > 2 then Emph (Punct, post, q2, n2 - 2) :: xs else xs else if n2 > 1 then Emph (Punct, post, q2, n2 - 1) :: xs else xs in let r = let il = concat (List.map to_r (parse_emph (List.rev acc))) in if n1 >= 2 && n2 >= 2 then R (Strong ([], il)) :: xs else R (Emph ([], il)) :: xs in let r = if n1 >= 2 && n2 >= 2 then if n1 > 2 then Emph (pre, Punct, q1, n1 - 2) :: r else r else if n1 > 1 then Emph (pre, Punct, q1, n1 - 1) :: r else r in parse_emph r | (Emph _ as x) :: xs1 as xs when is_opener x -> let xs' = parse_emph xs in if xs' = xs then loop (x :: acc) xs1 else loop acc xs' | x :: xs -> loop (x :: acc) xs | [] -> x :: List.rev acc in loop [] xs | x :: xs -> x :: parse_emph xs | [] -> [] let parse_emph xs = concat (List.map to_r (parse_emph xs)) end let escape buf st = if next st <> '\\' then raise Fail; match peek st with | Some c when is_punct c -> junk st; Buffer.add_char buf c | Some _ | None -> Buffer.add_char buf '\\' let link_label allow_balanced_brackets st = if peek_exn st <> '[' then raise Fail; junk st; let buf = Buffer.create 17 in let rec loop n nonempty = match peek_exn st with | ']' when n = 0 -> junk st; if not nonempty then raise Fail; Buffer.contents buf | ']' as c -> assert (n > 0); junk st; Buffer.add_char buf c; loop (pred n) true | '\\' as c -> junk st; Buffer.add_char buf c; begin match peek_exn st with | c when is_punct c -> junk st; Buffer.add_char buf c | _ -> () end; loop n true | '[' when not allow_balanced_brackets -> raise Fail | '[' as c -> junk st; Buffer.add_char buf c; loop (succ n) true | (' ' | '\t' | '\010' .. '\013') as c -> junk st; Buffer.add_char buf c; loop n nonempty | _ as c -> junk st; Buffer.add_char buf c; loop n true in loop 0 false let normalize s = let buf = Buffer.create (String.length s) in let rec loop start seen_ws i = if i >= String.length s then Buffer.contents buf else match s.[i] with | ' ' | '\t' | '\010' .. '\013' -> loop start true (succ i) | _ as c -> if (not start) && seen_ws then Buffer.add_char buf ' '; Buffer.add_char buf (Char.lowercase_ascii c); loop false false (succ i) in loop true false 0 let tag_name st = match peek_exn st with | 'a' .. 'z' | 'A' .. 'Z' -> junk st; let rec loop () = match peek st with | Some ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '-') -> junk st; loop () | Some _ | None -> () in loop () | _ -> raise Fail let ws_buf buf st = let rec loop () = match peek st with | Some ((' ' | '\t' | '\010' .. '\013') as c) -> Buffer.add_char buf c; junk st; loop () | Some _ | None -> () in loop () let closing_tag st = let start = pos st in if next st <> '<' then raise Fail; if next st <> '/' then raise Fail; tag_name st; ws st; if next st <> '>' then raise Fail; range st start (pos st - start) let list p st = let rec loop () = match protect p st with | () -> loop () | exception Fail -> () in loop () let single_quoted_attribute st = if next st <> '\'' then raise Fail; let rec loop () = match peek_exn st with | '\'' -> junk st (* | '&' -> *) (* entity buf st; loop () *) | _ -> junk st; loop () in loop () let double_quoted_attribute st = if next st <> '"' then raise Fail; let rec loop () = match peek_exn st with | '"' -> junk st (* | '&' -> *) (* entity buf st; loop () *) | _ -> junk st; loop () in loop () let unquoted_attribute st = let rec loop n = match peek_exn st with | ' ' | '\t' | '\010' .. '\013' | '"' | '\'' | '=' | '<' | '>' | '`' -> if n = 0 then raise Fail (* | '&' -> *) (* entity buf st; loop () *) | _ -> junk st; loop (succ n) in loop 0 let attribute_value st = match peek_exn st with | '\'' -> single_quoted_attribute st | '"' -> double_quoted_attribute st | _ -> unquoted_attribute st let attribute_name st = match peek_exn st with | 'a' .. 'z' | 'A' .. 'Z' | '_' | ':' -> junk st; let rec loop () = match peek st with | Some ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' | '.' | ':' | '-') -> junk st; loop () | Some _ | None -> () in loop () | _ -> raise Fail let option d p st = match protect p st with | r -> r | exception Fail -> d let some p st = Some (p st) let attribute_value_specification = ws >>> char '=' >>> ws >>> attribute_value let ws1_buf buf st = match peek st with | Some (' ' | '\t' | '\010' .. '\013') -> ws_buf buf st | Some _ | None -> raise Fail let attribute st = ws1 st; attribute_name st; option () attribute_value_specification st let open_tag st = let start = pos st in if next st <> '<' then raise Fail; tag_name st; list attribute st; ws st; begin match peek_exn st with | '/' -> junk st | _ -> () end; if next st <> '>' then raise Fail; range st start (pos st - start) let html_comment st = let buf = Buffer.create 17 in if next st <> '<' then raise Fail; if next st <> '!' then raise Fail; if next st <> '-' then raise Fail; if next st <> '-' then raise Fail; Buffer.add_string buf "<!--"; let rec loop start = match peek_exn st with | '-' as c -> ( junk st; match peek_exn st with | '-' -> junk st; if next st <> '>' then raise Fail; Buffer.add_string buf "-->"; Buffer.contents buf | '>' when start -> raise Fail | _ -> Buffer.add_char buf c; loop false) | '>' when start -> raise Fail | '&' -> entity buf st; loop false | _ as c -> junk st; Buffer.add_char buf c; loop false in loop true let processing_instruction st = let buf = Buffer.create 17 in if next st <> '<' then raise Fail; if next st <> '?' then raise Fail; Buffer.add_string buf "<?"; let rec loop () = match peek_exn st with | '?' as c -> ( junk st; match peek_exn st with | '>' -> junk st; Buffer.add_string buf "?>"; Buffer.contents buf | _ -> Buffer.add_char buf c; loop ()) | '&' -> entity buf st; loop () | _ as c -> junk st; Buffer.add_char buf c; loop () in loop () let cdata_section st = let buf = Buffer.create 17 in if next st <> '<' then raise Fail; if next st <> '!' then raise Fail; if next st <> '[' then raise Fail; if next st <> 'C' then raise Fail; if next st <> 'D' then raise Fail; if next st <> 'A' then raise Fail; if next st <> 'T' then raise Fail; if next st <> 'A' then raise Fail; if next st <> '[' then raise Fail; Buffer.add_string buf "<![CDATA["; let rec loop () = match peek_exn st with | ']' as c -> ( junk st; match peek_exn st with | ']' as c1 -> ( junk st; match peek_exn st with | '>' -> junk st; Buffer.add_string buf "]]>"; Buffer.contents buf | _ -> Buffer.add_char buf c; Buffer.add_char buf c1; loop ()) | _ -> Buffer.add_char buf c; loop ()) | '&' -> entity buf st; loop () | _ as c -> junk st; Buffer.add_char buf c; loop () in loop () let declaration st = let buf = Buffer.create 17 in if next st <> '<' then raise Fail; if next st <> '!' then raise Fail; Buffer.add_string buf "<!"; match peek_exn st with | 'A' .. 'Z' -> let rec loop () = match peek_exn st with | 'A' .. 'Z' as c -> junk st; Buffer.add_char buf c; loop () | ' ' | '\t' | '\010' .. '\013' -> ws1_buf buf st; let rec loop () = match peek_exn st with | '>' as c -> junk st; Buffer.add_char buf c; Buffer.contents buf | '&' -> entity buf st; loop () | _ as c -> junk st; Buffer.add_char buf c; loop () in loop () | _ -> raise Fail in loop () | _ -> raise Fail let link_destination st = let buf = Buffer.create 17 in match peek_exn st with | '<' -> junk st; let rec loop () = match peek_exn st with | '>' -> junk st; Buffer.contents buf | '\010' .. '\013' | '<' -> raise Fail | '\\' -> escape buf st; loop () | '&' -> entity buf st; loop () | _ as c -> junk st; Buffer.add_char buf c; loop () in loop () | _ -> let rec loop n = match peek st with | Some ('(' as c) -> junk st; Buffer.add_char buf c; loop (succ n) | Some ')' when n = 0 -> if Buffer.length buf = 0 then raise Fail; Buffer.contents buf | Some (')' as c) -> junk st; Buffer.add_char buf c; loop (pred n) | Some '\\' -> escape buf st; loop n | Some '&' -> entity buf st; loop n | Some (' ' | '\t' | '\x00' .. '\x1F' | '\x7F' | '\x80' .. '\x9F') | None -> if n > 0 || Buffer.length buf = 0 then raise Fail; Buffer.contents buf | Some c -> junk st; Buffer.add_char buf c; loop n in loop 0 let eol st = match peek st with | Some '\n' -> junk st | Some _ -> raise Fail | None -> () let link_title st = let buf = Buffer.create 17 in match peek_exn st with | ('\'' | '"') as c -> junk st; let rec loop () = match peek_exn st with | '\\' -> escape buf st; loop () | '&' -> entity buf st; loop () | _ as c1 when c = c1 -> junk st; Buffer.contents buf | _ as c1 -> junk st; Buffer.add_char buf c1; loop () in loop () | '(' -> junk st; let rec loop () = match peek_exn st with | '\\' -> escape buf st; loop () | '&' -> entity buf st; loop () | ')' -> junk st; Buffer.contents buf | _ as c -> junk st; Buffer.add_char buf c; loop () in loop () | _ -> raise Fail let space st = match peek_exn st with | ' ' -> junk st | _ -> raise Fail let many p st = try while true do p st done with | Fail -> () let scheme st = match peek_exn st with | 'a' .. 'z' | 'A' .. 'Z' -> let rec loop n = if n < 32 then match peek st with | Some ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '+' | '.' | '-') -> junk st; loop (succ n) | Some _ | None -> n else n in let n = loop 0 in if n < 2 then raise Fail | _ -> raise Fail let absolute_uri st = let p = pos st in scheme st; if next st <> ':' then raise Fail; let rec loop () = match peek st with | Some ( ' ' | '\t' | '\010' .. '\013' | '\x00' .. '\x1F' | '\x7F' .. '\x9F' | '<' | '>' ) | None -> let txt = range st p (pos st - p) in (txt, txt) | Some _ -> junk st; loop () in loop () let email_address st = let p = pos st in let rec loop n = match peek_exn st with | 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '.' | '!' | '#' | '$' | '%' | '&' | '\'' | '*' | '+' | '/' | '=' | '?' | '^' | '_' | '`' | '{' | '|' | '}' | '~' | '-' -> junk st; loop (succ n) | '@' -> junk st; let label st = let let_dig st = match peek_exn st with | 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' -> junk st; false | '-' -> junk st; true | _ -> raise Fail in if let_dig st then raise Fail; let rec loop last = match let_dig st with | r -> loop r | exception Fail -> if last then raise Fail in loop false in label st; list (char '.' >>> label) st; let txt = range st p (pos st - p) in (txt, "mailto:" ^ txt) | _ -> raise Fail in loop 0 let autolink st = match peek_exn st with | '<' -> junk st; let label, destination = (absolute_uri ||| email_address) st in if next st <> '>' then raise Fail; { Ast.label = Text ([], label); destination; title = None } | _ -> raise Fail let inline_link = char '(' >>> ws >>> option ("", None) (pair link_destination (option None (ws1 >>> some link_title))) <<< ws <<< char ')' let get_buf buf = let s = Buffer.contents buf in Buffer.clear buf; s let text buf acc = if Buffer.length buf = 0 then acc else Pre.R (Text ([], get_buf buf)) :: acc let inline_pre buf acc st = let pos = pos st in let rec gobble_open_backtick n = match peek st with | Some '`' -> junk st; gobble_open_backtick (succ n) | Some _ -> let acc = text buf acc in let bufcode = Buffer.create 17 in let finish () = let content = Buffer.contents bufcode in let content = if String.length content >= 2 && content.[0] = ' ' && content.[String.length content - 1] = ' ' (* FIXME not fully whitespace *) then String.sub content 1 (String.length content - 2) else content in let attr = inline_attribute_string st in Pre.R (Code (attr, content)) :: acc in let rec gobble_body start m = match peek st with | Some '`' -> junk st; gobble_body start (succ m) | _ when m = n -> finish () | Some ((' ' | '\t' | '\010' .. '\013') as c) -> if m > 0 then Buffer.add_string bufcode (String.make m '`'); Buffer.add_char bufcode (if c = '\010' then ' ' else c); junk st; gobble_body (start && m = 0) 0 | Some c -> junk st; (* if seen_ws then Buffer.add_char bufcode ' '; *) if m > 0 then Buffer.add_string bufcode (String.make m '`'); Buffer.add_char bufcode c; gobble_body false 0 | None -> Buffer.add_string buf (range st pos n); set_pos st (pos + n); acc in gobble_body true 0 | None -> Buffer.add_string buf (String.make n '`'); acc in gobble_open_backtick 0 let rec inline defs st = let buf = Buffer.create 0 in let text acc = text buf acc in let rec reference_link kind acc st = let off0 = pos st in match protect (link_label true) st with | lab -> ( let off1 = pos st in let lab1 = inline defs (of_string lab) in let reflink lab' = let s = normalize lab' in match List.find_opt (fun ({ label; _ } : attributes link_def) -> label = s) defs with | Some { label = _; destination; title; attributes = attr } -> let r = let def = { label = lab1; destination; title } in match kind with | Pre.Img -> Image (attr, def) | Url -> Link (attr, def) in loop (Pre.R r :: text acc) st | None -> if kind = Img then Buffer.add_char buf '!'; Buffer.add_char buf '['; let acc = Pre.R lab1 :: text acc in Buffer.add_char buf ']'; set_pos st off1; loop acc st in match peek st with | Some '[' -> ( if peek_after '\000' st = ']' then ( junk st; junk st; reflink lab ) else match protect (link_label false) st with | _ -> set_pos st off0; junk st; loop (Left_bracket kind :: text acc) st | exception Fail -> reflink lab) | Some '(' -> ( match protect inline_link st with | _ -> set_pos st off0; junk st; loop (Left_bracket kind :: text acc) st | exception Fail -> reflink lab) | Some _ | None -> reflink lab) | exception Fail -> junk st; loop (Left_bracket kind :: text acc) st and loop acc st = match peek_exn st with | '<' as c -> ( match protect autolink st with | def -> let attr = inline_attribute_string st in loop (Pre.R (Link (attr, def)) :: text acc) st | exception Fail -> match protect (closing_tag ||| open_tag ||| html_comment ||| declaration ||| cdata_section ||| processing_instruction) st with | tag -> loop (Pre.R (Html ([], tag)) :: text acc) st | exception Fail -> junk st; Buffer.add_char buf c; loop acc st) | '\n' -> junk st; sp st; loop (Pre.R (Soft_break []) :: text acc) st | ' ' as c -> ( junk st; match peek st with | Some ' ' -> ( match protect (many space >>> char '\n' >>> many space) st with | () -> loop (Pre.R (Hard_break []) :: text acc) st | exception Fail -> junk st; Buffer.add_string buf " "; loop acc st) | Some '\n' -> loop acc st | Some _ | None -> Buffer.add_char buf c; loop acc st) | '`' -> loop (inline_pre buf acc st) st | '\\' as c -> ( junk st; match peek st with | Some '\n' -> junk st; loop (Pre.R (Hard_break []) :: text acc) st | Some c when is_punct c -> junk st; Buffer.add_char buf c; loop acc st | Some _ | None -> Buffer.add_char buf c; loop acc st) | '!' as c -> ( junk st; match peek st with | Some '[' -> reference_link Img (text acc) st | Some _ | None -> Buffer.add_char buf c; loop acc st) | '&' -> entity buf st; loop acc st | ']' -> junk st; let acc = text acc in let rec aux seen_link xs = function | Pre.Left_bracket Url :: _ when seen_link -> Buffer.add_char buf ']'; loop acc st | Left_bracket k :: acc' -> ( match peek st with | Some '(' -> ( match protect inline_link st with | destination, title -> let attr = inline_attribute_string st in let r = let label = Pre.parse_emph xs in let def = { label; destination; title } in match k with | Img -> Image (attr, def) | Url -> Link (attr, def) in loop (Pre.R r :: acc') st | exception Fail -> Buffer.add_char buf ']'; loop acc st) | Some '[' -> ( let label = Pre.parse_emph xs in let off1 = pos st in match link_label false st with | lab -> ( let s = normalize lab in match List.find_opt (fun ({ label; _ } : attributes link_def) -> label = s) defs with | Some { label = _; destination; title; attributes = attr } -> let def = { label; destination; title } in let r = match k with | Img -> Image (attr, def) | Url -> Link (attr, def) in loop (Pre.R r :: acc') st | None -> if k = Img then Buffer.add_char buf '!'; Buffer.add_char buf '['; let acc = Pre.R label :: text acc' in Buffer.add_char buf ']'; set_pos st off1; loop acc st) | exception Fail -> if k = Img then Buffer.add_char buf '!'; Buffer.add_char buf '['; let acc = Pre.R label :: text acc in Buffer.add_char buf ']'; set_pos st off1; loop acc st) | Some _ | None -> Buffer.add_char buf ']'; loop acc st) | (Pre.R (Link _) as x) :: acc' -> aux true (x :: xs) acc' | x :: acc' -> aux seen_link (x :: xs) acc' | [] -> Buffer.add_char buf ']'; loop acc st in aux false [] acc | '[' -> reference_link Url acc st | ('*' | '_') as c -> let pre = peek_before ' ' st in let f post n st = let pre = pre |> Pre.classify_delim in let post = post |> Pre.classify_delim in let e = if c = '*' then Pre.Star else Pre.Underscore in loop (Pre.Emph (pre, post, e, n) :: text acc) st in let rec aux n = match peek st with | Some c1 when c1 = c -> junk st; aux (succ n) | Some c1 -> f c1 n st | None -> f ' ' n st in aux 0 | _ as c -> junk st; Buffer.add_char buf c; loop acc st | exception Fail -> Pre.parse_emph (List.rev (text acc)) in loop [] st let sp3 st = match peek_exn st with | ' ' -> ( junk st; match peek_exn st with | ' ' -> ( junk st; match peek_exn st with | ' ' -> junk st; 3 | _ -> 2 | exception Fail -> 2) | _ -> 1 | exception Fail -> 1) | _ -> 0 | exception Fail -> 0 let link_reference_definition st : attributes Ast.link_def = let ws st = let rec loop seen_nl = match peek st with | Some (' ' | '\t' | '\011' .. '\013') -> junk st; loop seen_nl | Some '\n' when not seen_nl -> junk st; loop true | Some _ | None -> () in loop false in let ws1 st = match next st with | ' ' | '\t' | '\010' .. '\013' -> ws st | _ -> raise Fail in ignore (sp3 st); let label = link_label false st in if next st <> ':' then raise Fail; ws st; let destination = link_destination st in let attributes = inline_attribute_string st in match protect (ws1 >>> link_title <<< sp <<< eol) st with | title -> { label; destination; title = Some title; attributes } | exception Fail -> (sp >>> eol) st; { label; destination; title = None; attributes } let link_reference_definitions st = let rec loop acc = match protect link_reference_definition st with | def -> loop (def :: acc) | exception Fail -> (acc, pos st) in loop []