File: bk_html.c

package info (click to toggle)
halibut 1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,084 kB
  • sloc: ansic: 58,429; makefile: 294; perl: 194; lisp: 76; sh: 21
file content (3238 lines) | stat: -rw-r--r-- 90,952 bytes parent folder | download | duplicates (2)
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
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
/*
 * HTML backend for Halibut
 */

/*
 * TODO:
 * 
 *  - I'm never entirely convinced that having a fragment link to
 *    come in at the start of the real text in the file is
 *    sensible. Perhaps for the topmost section in the file, no
 *    fragment should be used? (Though it should probably still be
 *    _there_ even if unused.)
 * 
 *  - In HHK index mode: subsidiary hhk entries (as in replacing
 *    `foo, bar' with `foo\n\tbar') can be done by embedding
 *    sub-<UL>s in the hhk file. This requires me getting round to
 *    supporting that idiom in the rest of Halibut, but I thought
 *    I'd record how it's done here in case I turn out to have
 *    forgotten when I get there.
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include "halibut.h"
#include "winchm.h"

#define is_heading_type(type) ( (type) == para_Title || \
				(type) == para_Chapter || \
				(type) == para_Appendix || \
				(type) == para_UnnumberedChapter || \
				(type) == para_Heading || \
				(type) == para_Subsect)

#define heading_depth(p) ( (p)->type == para_Subsect ? (p)->aux + 1 : \
			   (p)->type == para_Heading ? 1 : \
			   (p)->type == para_Title ? -1 : 0 )

typedef struct {
    int number_at_all, just_numbers;
    wchar_t *number_suffix;
} sectlevel;

typedef struct {
    int nasect;
    sectlevel achapter, *asect;
    int *contents_depths;	       /* 0=main, 1=chapter, 2=sect etc */
    int ncdepths;
    int address_section, visible_version_id;
    int leaf_contains_contents, leaf_smallest_contents;
    int navlinks;
    int rellinks;
    char *contents_filename;
    char *index_filename;
    char *template_filename;
    char *single_filename;
    char *chm_filename, *hhp_filename, *hhc_filename, *hhk_filename;
    char **template_fragments;
    int ntfragments;
    char **chm_extrafiles, **chm_extranames;
    int nchmextrafiles, chmextrafilesize;
    char *head_end, *body_start, *body_end, *addr_start, *addr_end;
    char *body_tag, *nav_attr;
    wchar_t *author, *description;
    wchar_t *index_text, *contents_text, *preamble_text, *title_separator;
    wchar_t *nav_prev_text, *nav_next_text, *nav_up_text, *nav_separator;
    wchar_t *index_main_sep, *index_multi_sep;
    wchar_t *pre_versionid, *post_versionid;
    int restrict_charset, output_charset;
    enum {
	HTML_3_2, HTML_4, ISO_HTML,
	XHTML_1_0_TRANSITIONAL, XHTML_1_0_STRICT
    } htmlver;
    wchar_t *lquote, *rquote;
    int leaf_level;
} htmlconfig;

#define contents_depth(conf, level) \
    ( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 )

#define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL)

typedef struct htmlfile htmlfile;
typedef struct htmlsect htmlsect;

struct htmlfile {
    htmlfile *next;
    char *filename;
    int last_fragment_number;
    int min_heading_depth;
    htmlsect *first, *last;	       /* first/last highest-level sections */
    /*
     * The `temp' field is available for use in individual passes
     * over the file list. For example, the HHK index generation
     * uses it to ensure no index term references the same file
     * more than once.
     */
    int temp;
    /*
     * CHM section structure, if we're generating a CHM.
     */
    struct chm_section *chmsect;
};

struct htmlsect {
    htmlsect *next, *parent;
    htmlfile *file;
    paragraph *title, *text;
    enum { NORMAL, TOP, INDEX } type;
    int contents_depth;
    char **fragments;
};

typedef struct {
    htmlfile *head, *tail;
    htmlfile *single, *index;
    tree234 *frags;
    tree234 *files;
} htmlfilelist;

typedef struct {
    htmlsect *head, *tail;
} htmlsectlist;

typedef struct {
    htmlfile *file;
    char *fragment;
} htmlfragment;

typedef struct {
    int nrefs, refsize;
    word **refs;
} htmlindex;

typedef struct {
    htmlsect *section;
    char *fragment;
    int generated, referenced;
} htmlindexref;

typedef struct {
    /*
     * This level deals with charset conversion, starting and
     * ending tags, and writing to the file. It's the lexical
     * level.
     */
    void *write_ctx;
    void (*write)(void *write_ctx, const char *data, int len);
    int charset, restrict_charset;
    charset_state cstate;
    int ver;
    enum {
	HO_NEUTRAL, HO_IN_TAG, HO_IN_EMPTY_TAG, HO_IN_TEXT
    } state;
    int hackflags;		       /* used for icky .HH* stuff */
    int hacklimit;		       /* text size limit, again for .HH* */
    /*
     * Stuff beyond here deals with the higher syntactic level: it
     * tracks how many levels of <ul> are currently open when
     * producing a contents list, for example.
     */
    int contents_level;
} htmloutput;

void ho_write_ignore(void *write_ctx, const char *data, int len)
{
    IGNORE(write_ctx);
    IGNORE(data);
    IGNORE(len);
}
void ho_write_file(void *write_ctx, const char *data, int len)
{
    FILE *fp = (FILE *)write_ctx;
    if (len == -1)
        fclose(fp);
    else
        fwrite(data, 1, len, fp);
}
void ho_write_stdio(void *write_ctx, const char *data, int len)
{
    /* same as write_file, but we don't close the file */
    FILE *fp = (FILE *)write_ctx;
    if (len > 0)
        fwrite(data, 1, len, fp);
}
void ho_setup_file(htmloutput *ho, const char *filename)
{
    FILE *fp = fopen(filename, "w");
    if (fp) {
        ho->write = ho_write_file;
        ho->write_ctx = fp;
    } else {
        err_cantopenw(filename);
        ho->write = ho_write_ignore; /* saves conditionalising rest of code */
    }
}
void ho_setup_stdio(htmloutput *ho, FILE *fp)
{
    ho->write = ho_write_stdio;
    ho->write_ctx = fp;
}

struct chm_output {
    struct chm *chm;
    char *filename;
    rdstringc rs;
};
void ho_write_chm(void *write_ctx, const char *data, int len)
{
    struct chm_output *co = (struct chm_output *)write_ctx;
    if (len == -1) {
        chm_add_file(co->chm, co->filename, co->rs.text, co->rs.pos);
        sfree(co->filename);
        sfree(co->rs.text);
        sfree(co);
    } else {
        rdaddsn(&co->rs, data, len);
    }
}
void ho_setup_chm(htmloutput *ho, struct chm *chm, const char *filename)
{
    struct chm_output *co = snew(struct chm_output);

    co->chm = chm;
    co->rs = empty_rdstringc;
    co->filename = dupstr(filename);

    ho->write_ctx = co;
    ho->write = ho_write_chm;
}

void ho_write_rdstringc(void *write_ctx, const char *data, int len)
{
    rdstringc *rs = (rdstringc *)write_ctx;
    if (len > 0)
        rdaddsn(rs, data, len);
}
void ho_setup_rdstringc(htmloutput *ho, rdstringc *rs)
{
    ho->write_ctx = rs;
    ho->write = ho_write_rdstringc;
}

void ho_string(htmloutput *ho, const char *string)
{
    ho->write(ho->write_ctx, string, strlen(string));
}
void ho_finish(htmloutput *ho)
{
    ho->write(ho->write_ctx, NULL, -1);
}

/*
 * Nasty hacks that modify the behaviour of htmloutput files. All
 * of these are flag bits set in ho.hackflags. HO_HACK_QUOTEQUOTES
 * has the same effect as the `quote_quotes' parameter to
 * html_text_limit_internal, except that it's set globally on an
 * entire htmloutput structure; HO_HACK_QUOTENOTHING suppresses
 * quoting of any HTML special characters (for .HHP files);
 * HO_HACK_OMITQUOTES completely suppresses the generation of
 * double quotes at all (turning them into single quotes, for want
 * of a better idea).
 */
#define HO_HACK_QUOTEQUOTES 1
#define HO_HACK_QUOTENOTHING 2
#define HO_HACK_OMITQUOTES 4

static int html_fragment_compare(void *av, void *bv)
{
    htmlfragment *a = (htmlfragment *)av;
    htmlfragment *b = (htmlfragment *)bv;
    int cmp;

    if ((cmp = strcmp(a->file->filename, b->file->filename)) != 0)
	return cmp;
    else
	return strcmp(a->fragment, b->fragment);
}

static int html_filename_compare(void *av, void *bv)
{
    char *a = (char *)av;
    char *b = (char *)bv;

    return strcmp(a, b);
}

static void html_file_section(htmlconfig *cfg, htmlfilelist *files,
			      htmlsect *sect, int depth);

static htmlfile *html_new_file(htmlfilelist *list, char *filename);
static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title,
			       htmlconfig *cfg);

/* Flags for html_words() flags parameter */
#define NOTHING 0x00
#define MARKUP 0x01
#define LINKS 0x02
#define INDEXENTS 0x04
#define ALL 0x07
static void html_words(htmloutput *ho, word *words, int flags,
		       htmlfile *file, keywordlist *keywords, htmlconfig *cfg);
static void html_codepara(htmloutput *ho, word *words);

static void element_open(htmloutput *ho, char const *name);
static void element_close(htmloutput *ho, char const *name);
static void element_empty(htmloutput *ho, char const *name);
static void element_attr(htmloutput *ho, char const *name, char const *value);
static void element_attr_w(htmloutput *ho, char const *name,
			   wchar_t const *value);
static void html_text(htmloutput *ho, wchar_t const *str);
static void html_text_nbsp(htmloutput *ho, wchar_t const *str);
static void html_text_limit(htmloutput *ho, wchar_t const *str, int maxlen);
static void html_text_limit_internal(htmloutput *ho, wchar_t const *text,
				     int maxlen, int quote_quotes, int nbsp);
static void html_nl(htmloutput *ho);
static void html_raw(htmloutput *ho, char *text);
static void html_raw_as_attr(htmloutput *ho, char *text);
static void cleanup(htmloutput *ho);

static void html_href(htmloutput *ho, htmlfile *thisfile,
		      htmlfile *targetfile, char *targetfrag);
static void html_fragment(htmloutput *ho, char const *fragment);

static char *html_format(paragraph *p, char *template_string);
static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file,
				    char *text);
static char *html_sanitise_filename(htmlfilelist *files, char *text);

static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s,
				htmlfile *thisfile, keywordlist *keywords,
				htmlconfig *cfg);
static void html_section_title(htmloutput *ho, htmlsect *s,
			       htmlfile *thisfile, keywordlist *keywords,
			       htmlconfig *cfg, int real);

static htmlconfig html_configure(paragraph *source, int chm_mode)
{
    htmlconfig ret;
    paragraph *p;

    /*
     * Defaults.
     */
    ret.leaf_level = chm_mode ? -1 /* infinite */ : 2;
    ret.achapter.just_numbers = FALSE;
    ret.achapter.number_at_all = TRUE;
    ret.achapter.number_suffix = L": ";
    ret.nasect = 1;
    ret.asect = snewn(ret.nasect, sectlevel);
    ret.asect[0].just_numbers = TRUE;
    ret.asect[0].number_at_all = TRUE;
    ret.asect[0].number_suffix = L" ";
    ret.ncdepths = 0;
    ret.contents_depths = 0;
    ret.visible_version_id = TRUE;
    ret.address_section = chm_mode ? FALSE : TRUE;
    ret.leaf_contains_contents = FALSE;
    ret.leaf_smallest_contents = 4;
    ret.navlinks = chm_mode ? FALSE : TRUE;
    ret.rellinks = TRUE;
    ret.single_filename = dupstr("Manual.html");
    ret.contents_filename = dupstr("Contents.html");
    ret.index_filename = dupstr("IndexPage.html");
    ret.template_filename = dupstr("%n.html");
    if (chm_mode) {
        ret.chm_filename = dupstr("output.chm");
        ret.hhc_filename = dupstr("contents.hhc");
        ret.hhk_filename = dupstr("index.hhk");
        ret.hhp_filename = NULL;
    } else {
        ret.chm_filename = ret.hhp_filename = NULL;
        ret.hhc_filename = ret.hhk_filename = NULL;
    }
    ret.ntfragments = 1;
    ret.template_fragments = snewn(ret.ntfragments, char *);
    ret.template_fragments[0] = dupstr("%b");
    ret.chm_extrafiles = ret.chm_extranames = NULL;
    ret.nchmextrafiles = ret.chmextrafilesize = 0;
    ret.head_end = ret.body_tag = ret.body_start = ret.body_end =
	ret.addr_start = ret.addr_end = ret.nav_attr = NULL;
    ret.author = ret.description = NULL;
    ret.restrict_charset = CS_UTF8;
    ret.output_charset = CS_ASCII;
    ret.htmlver = HTML_4;
    ret.index_text = L"Index";
    ret.contents_text = L"Contents";
    ret.preamble_text = L"Preamble";
    ret.title_separator = L" - ";
    ret.nav_prev_text = L"Previous";
    ret.nav_next_text = L"Next";
    ret.nav_up_text = L"Up";
    ret.nav_separator = L" | ";
    ret.index_main_sep = L": ";
    ret.index_multi_sep = L", ";
    ret.pre_versionid = L"[";
    ret.post_versionid = L"]";
    /*
     * Default quote characters are Unicode matched single quotes,
     * falling back to ordinary ASCII ".
     */
    ret.lquote = L"\x2018\0\x2019\0\"\0\"\0\0";
    ret.rquote = uadv(ret.lquote);

    /*
     * Two-pass configuration so that we can pick up global config
     * (e.g. `quotes') before having it overridden by specific
     * config (`html-quotes'), irrespective of the order in which
     * they occur.
     */
    for (p = source; p; p = p->next) {
	if (p->type == para_Config) {
	    if (!ustricmp(p->keyword, L"quotes")) {
		if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) {
		    ret.lquote = uadv(p->keyword);
		    ret.rquote = uadv(ret.lquote);
		}
	    } else if (!ustricmp(p->keyword, L"index")) {
		ret.index_text = uadv(p->keyword);
	    } else if (!ustricmp(p->keyword, L"contents")) {
		ret.contents_text = uadv(p->keyword);
	    }
	}
    }

    for (p = source; p; p = p->next) {
	if (p->type == para_Config) {
	    wchar_t *k = p->keyword;
            int generic = FALSE;

            if (!chm_mode && !ustrnicmp(k, L"html-", 5)) {
                k += 5;
            } else if (!chm_mode && !ustrnicmp(k, L"xhtml-", 6)) {
                k += 6;
            } else if (chm_mode && !ustrnicmp(k, L"chm-", 4)) {
                k += 4;
            } else if (!ustrnicmp(k, L"htmlall-", 8)) {
                k += 8;
                /* In this mode, only accept directives that don't
                 * vary completely between the HTML and CHM output
                 * types. */
                generic = TRUE;
            } else {
                continue;
            }

	    if (!ustricmp(k, L"restrict-charset")) {
		ret.restrict_charset = charset_from_ustr(&p->fpos, uadv(k));
	    } else if (!ustricmp(k, L"output-charset")) {
		ret.output_charset = charset_from_ustr(&p->fpos, uadv(k));
	    } else if (!ustricmp(k, L"version")) {
		wchar_t *vername = uadv(k);
		static const struct {
		    const wchar_t *name;
		    int ver;
		} versions[] = {
		    {L"html3.2", HTML_3_2},
		    {L"html4", HTML_4},
		    {L"iso-html", ISO_HTML},
		    {L"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL},
		    {L"xhtml1.0strict", XHTML_1_0_STRICT}
		};
		int i;

		for (i = 0; i < (int)lenof(versions); i++)
		    if (!ustricmp(versions[i].name, vername))
			break;

		if (i == lenof(versions))
		    err_htmlver(&p->fpos, vername);
		else
		    ret.htmlver = versions[i].ver;
	    } else if (!ustricmp(k, L"single-filename")) {
		sfree(ret.single_filename);
		ret.single_filename = dupstr(adv(p->origkeyword));
	    } else if (!ustricmp(k, L"contents-filename")) {
		sfree(ret.contents_filename);
		ret.contents_filename = dupstr(adv(p->origkeyword));
	    } else if (!ustricmp(k, L"index-filename")) {
		sfree(ret.index_filename);
		ret.index_filename = dupstr(adv(p->origkeyword));
	    } else if (!ustricmp(k, L"template-filename")) {
		sfree(ret.template_filename);
		ret.template_filename = dupstr(adv(p->origkeyword));
	    } else if (!ustricmp(k, L"template-fragment")) {
		char *frag = adv(p->origkeyword);
		if (*frag) {
		    while (ret.ntfragments--)
			sfree(ret.template_fragments[ret.ntfragments]);
		    sfree(ret.template_fragments);
		    ret.template_fragments = NULL;
		    ret.ntfragments = 0;
		    while (*frag) {
			ret.ntfragments++;
			ret.template_fragments =
			    sresize(ret.template_fragments,
				    ret.ntfragments, char *);
			ret.template_fragments[ret.ntfragments-1] =
			    dupstr(frag);
			frag = adv(frag);
		    }
		} else
		    err_cfginsufarg(&p->fpos, p->origkeyword, 1);
	    } else if (!ustricmp(k, L"chapter-numeric")) {
		ret.achapter.just_numbers = utob(uadv(k));
	    } else if (!ustricmp(k, L"chapter-shownumber")) {
		ret.achapter.number_at_all = utob(uadv(k));
	    } else if (!ustricmp(k, L"suppress-navlinks")) {
		ret.navlinks = !utob(uadv(k));
	    } else if (!ustricmp(k, L"rellinks")) {
		ret.rellinks = utob(uadv(k));
	    } else if (!ustricmp(k, L"chapter-suffix")) {
		ret.achapter.number_suffix = uadv(k);
	    } else if (!ustricmp(k, L"leaf-level")) {
		wchar_t *u = uadv(k);
		if (!ustricmp(u, L"infinite") ||
		    !ustricmp(u, L"infinity") ||
		    !ustricmp(u, L"inf"))
		    ret.leaf_level = -1;   /* represents infinity */
		else
		    ret.leaf_level = utoi(u);
	    } else if (!ustricmp(k, L"section-numeric")) {
		wchar_t *q = uadv(k);
		int n = 0;
		if (uisdigit(*q)) {
		    n = utoi(q);
		    q = uadv(q);
		}
		if (n >= ret.nasect) {
		    int i;
		    ret.asect = sresize(ret.asect, n+1, sectlevel);
		    for (i = ret.nasect; i <= n; i++)
			ret.asect[i] = ret.asect[ret.nasect-1];
		    ret.nasect = n+1;
		}
		ret.asect[n].just_numbers = utob(q);
	    } else if (!ustricmp(k, L"section-shownumber")) {
		wchar_t *q = uadv(k);
		int n = 0;
		if (uisdigit(*q)) {
		    n = utoi(q);
		    q = uadv(q);
		}
		if (n >= ret.nasect) {
		    int i;
		    ret.asect = sresize(ret.asect, n+1, sectlevel);
		    for (i = ret.nasect; i <= n; i++)
			ret.asect[i] = ret.asect[ret.nasect-1];
		    ret.nasect = n+1;
		}
		ret.asect[n].number_at_all = utob(q);
	    } else if (!ustricmp(k, L"section-suffix")) {
		wchar_t *q = uadv(k);
		int n = 0;
		if (uisdigit(*q)) {
		    n = utoi(q);
		    q = uadv(q);
		}
		if (n >= ret.nasect) {
		    int i;
		    ret.asect = sresize(ret.asect, n+1, sectlevel);
		    for (i = ret.nasect; i <= n; i++) {
			ret.asect[i] = ret.asect[ret.nasect-1];
		    }
		    ret.nasect = n+1;
		}
		ret.asect[n].number_suffix = q;
	    } else if (!ustricmp(k, L"contents-depth") ||
		       !ustrnicmp(k, L"contents-depth-", 15)) {
		/*
		 * Relic of old implementation: this directive used
		 * to be written as \cfg{html-contents-depth-3}{2}
		 * rather than the usual Halibut convention of
		 * \cfg{html-contents-depth}{3}{2}. We therefore
		 * support both.
		 */
		wchar_t *q = k[14] ? k+15 : uadv(k);
		int n = 0;
		if (uisdigit(*q)) {
		    n = utoi(q);
		    q = uadv(q);
		}
		if (n >= ret.ncdepths) {
		    int i;
		    ret.contents_depths =
			sresize(ret.contents_depths, n+1, int);
		    for (i = ret.ncdepths; i <= n; i++) {
			ret.contents_depths[i] = i+2;
		    }
		    ret.ncdepths = n+1;
		}
		ret.contents_depths[n] = utoi(q);
	    } else if (!ustricmp(k, L"head-end")) {
		ret.head_end = adv(p->origkeyword);
	    } else if (!ustricmp(k, L"body-tag")) {
		ret.body_tag = adv(p->origkeyword);
	    } else if (!ustricmp(k, L"body-start")) {
		ret.body_start = adv(p->origkeyword);
	    } else if (!ustricmp(k, L"body-end")) {
		ret.body_end = adv(p->origkeyword);
	    } else if (!ustricmp(k, L"address-start")) {
		ret.addr_start = adv(p->origkeyword);
	    } else if (!ustricmp(k, L"address-end")) {
		ret.addr_end = adv(p->origkeyword);
	    } else if (!ustricmp(k, L"navigation-attributes")) {
		ret.nav_attr = adv(p->origkeyword);
	    } else if (!ustricmp(k, L"author")) {
		ret.author = uadv(k);
	    } else if (!ustricmp(k, L"description")) {
		ret.description = uadv(k);
	    } else if (!ustricmp(k, L"suppress-address")) {
		ret.address_section = !utob(uadv(k));
	    } else if (!ustricmp(k, L"versionid")) {
		ret.visible_version_id = utob(uadv(k));
	    } else if (!ustricmp(k, L"quotes")) {
		if (*uadv(k) && *uadv(uadv(k))) {
		    ret.lquote = uadv(k);
		    ret.rquote = uadv(ret.lquote);
		}
	    } else if (!ustricmp(k, L"leaf-contains-contents")) {
		ret.leaf_contains_contents = utob(uadv(k));
	    } else if (!ustricmp(k, L"leaf-smallest-contents")) {
		ret.leaf_smallest_contents = utoi(uadv(k));
	    } else if (!ustricmp(k, L"index-text")) {
		ret.index_text = uadv(k);
	    } else if (!ustricmp(k, L"contents-text")) {
		ret.contents_text = uadv(k);
	    } else if (!ustricmp(k, L"preamble-text")) {
		ret.preamble_text = uadv(k);
	    } else if (!ustricmp(k, L"title-separator")) {
		ret.title_separator = uadv(k);
	    } else if (!ustricmp(k, L"nav-prev-text")) {
		ret.nav_prev_text = uadv(k);
	    } else if (!ustricmp(k, L"nav-next-text")) {
		ret.nav_next_text = uadv(k);
	    } else if (!ustricmp(k, L"nav-up-text")) {
		ret.nav_up_text = uadv(k);
	    } else if (!ustricmp(k, L"nav-separator")) {
		ret.nav_separator = uadv(k);
	    } else if (!ustricmp(k, L"index-main-separator")) {
		ret.index_main_sep = uadv(k);
	    } else if (!ustricmp(k, L"index-multiple-separator")) {
		ret.index_multi_sep = uadv(k);
	    } else if (!ustricmp(k, L"pre-versionid")) {
		ret.pre_versionid = uadv(k);
	    } else if (!ustricmp(k, L"post-versionid")) {
		ret.post_versionid = uadv(k);
	    } else if (!generic && !ustricmp(
                           k, chm_mode ? L"filename" : L"mshtmlhelp-chm")) {
		sfree(ret.chm_filename);
		ret.chm_filename = dupstr(adv(p->origkeyword));
	    } else if (!generic && !ustricmp(
                           k, chm_mode ? L"contents-name" :
                           L"mshtmlhelp-contents")) {
		sfree(ret.hhc_filename);
		ret.hhc_filename = dupstr(adv(p->origkeyword));
	    } else if (!generic && !ustricmp(
                           k, chm_mode ? L"index-name" :
                           L"mshtmlhelp-index")) {
		sfree(ret.hhk_filename);
		ret.hhk_filename = dupstr(adv(p->origkeyword));
	    } else if (!generic && !chm_mode &&
                       !ustricmp(k, L"mshtmlhelp-project")) {
		sfree(ret.hhp_filename);
		ret.hhp_filename = dupstr(adv(p->origkeyword));
	    } else if (!generic && chm_mode &&
                       !ustricmp(k, L"extra-file")) {
                char *diskname, *chmname;

                diskname = adv(p->origkeyword);
                if (*diskname) {
                    chmname = adv(diskname);
                    if (!*chmname)
                        chmname = diskname;

                    if (chmname[0] == '#' || chmname[0] == '$')
                        err_chm_badname(&p->fpos, chmname);

                    if (ret.nchmextrafiles >= ret.chmextrafilesize) {
                        ret.chmextrafilesize = ret.nchmextrafiles * 5 / 4 + 32;
                        ret.chm_extrafiles = sresize(
                            ret.chm_extrafiles, ret.chmextrafilesize, char *);
                        ret.chm_extranames = sresize(
                            ret.chm_extranames, ret.chmextrafilesize, char *);
                    }
                    ret.chm_extrafiles[ret.nchmextrafiles] = dupstr(diskname);
                    ret.chm_extranames[ret.nchmextrafiles] =
                        dupstr(chmname);
                    ret.nchmextrafiles++;
                }
	    }
	}
    }

    if (!chm_mode) {
        /*
         * If we're in HTML mode but using the old-style options to
         * output HTML Help Workshop auxiliary files, do some
         * consistency checking.
         */

        /*
         * Enforce that the CHM and HHP filenames must either be both
         * present or both absent. If one is present but not the other,
         * turn both off.
         */
        if (!ret.chm_filename ^ !ret.hhp_filename) {
            err_chmnames();
            sfree(ret.chm_filename); ret.chm_filename = NULL;
            sfree(ret.hhp_filename); ret.hhp_filename = NULL;
        }
        /*
         * And if we're not generating an HHP, there's no need for HHC
         * or HHK.
         */
        if (!ret.hhp_filename) {
            sfree(ret.hhc_filename); ret.hhc_filename = NULL;
            sfree(ret.hhk_filename); ret.hhk_filename = NULL;
        }
    }

    /*
     * Now process fallbacks on quote characters.
     */
    while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) &&
	   (!cvt_ok(ret.restrict_charset, ret.lquote) ||
	    !cvt_ok(ret.restrict_charset, ret.rquote))) {
	ret.lquote = uadv(ret.rquote);
	ret.rquote = uadv(ret.lquote);
    }

    return ret;
}

paragraph *html_config_filename(char *filename)
{
    /*
     * If the user passes in a single filename as a parameter to
     * the `--html' command-line option, then we should assume it
     * to imply _two_ config directives:
     * \cfg{html-single-filename}{whatever} and
     * \cfg{html-leaf-level}{0}; the rationale being that the user
     * wants their output _in that file_.
     */
    paragraph *p, *q;

    p = cmdline_cfg_simple("html-single-filename", filename, NULL);
    q = cmdline_cfg_simple("html-leaf-level", "0", NULL);
    p->next = q;
    return p;
}

paragraph *chm_config_filename(char *filename)
{
    return cmdline_cfg_simple("chm-filename", filename, NULL);
}

static void html_backend_common(paragraph *sourceform, keywordlist *keywords,
                                indexdata *idx, int chm_mode)
{
    paragraph *p;
    htmlsect *topsect;
    htmlconfig conf;
    htmlfilelist files = { NULL, NULL, NULL, NULL, NULL, NULL };
    htmlsectlist sects = { NULL, NULL }, nonsects = { NULL, NULL };
    struct chm *chm = NULL;
    int has_index, hhk_needed = FALSE;

    conf = html_configure(sourceform, chm_mode);

    /*
     * We're going to make heavy use of paragraphs' private data
     * fields in the forthcoming code. Clear them first, so we can
     * reliably tell whether we have auxiliary data for a
     * particular paragraph.
     */
    for (p = sourceform; p; p = p->next)
	p->private_data = NULL;

    files.frags = newtree234(html_fragment_compare);
    files.files = newtree234(html_filename_compare);

    /*
     * Start by figuring out into which file each piece of the
     * document should be put. We'll do this by inventing an
     * `htmlsect' structure and stashing it in the private_data
     * field of each section paragraph; we also need one additional
     * htmlsect for the document index, which won't show up in the
     * source form but needs to be consistently mentioned in
     * contents links.
     * 
     * While we're here, we'll also invent the HTML fragment name(s)
     * for each section.
     */
    {
	htmlsect *sect;
	int d;

	topsect = html_new_sect(&sects, NULL, &conf);
	topsect->type = TOP;
	topsect->title = NULL;
	topsect->text = sourceform;
	topsect->contents_depth = contents_depth(conf, 0);
	html_file_section(&conf, &files, topsect, -1);

	for (p = sourceform; p; p = p->next)
	    if (is_heading_type(p->type)) {
		d = heading_depth(p);

		if (p->type == para_Title) {
		    topsect->title = p;
		    continue;
		}

		sect = html_new_sect(&sects, p, &conf);
		sect->text = p->next;

		sect->contents_depth = contents_depth(conf, d+1) - (d+1);

		if (p->parent) {
		    sect->parent = (htmlsect *)p->parent->private_data;
		    assert(sect->parent != NULL);
		} else
		    sect->parent = topsect;
		p->private_data = sect;

		html_file_section(&conf, &files, sect, d);

		{
		    int i;
		    for (i=0; i < conf.ntfragments; i++) {
			sect->fragments[i] =
			    html_format(p, conf.template_fragments[i]);
			sect->fragments[i] =
			    html_sanitise_fragment(&files, sect->file,
						   sect->fragments[i]);
		    }
		}
	    }

	/*
	 * And the index, if we have one. Note that we don't output
	 * an index as an HTML file if we're outputting one as a
	 * .HHK (in either of the HTML or CHM output modes).
	 */
	has_index = (count234(idx->entries) > 0);
	if (has_index && !chm_mode && !conf.hhk_filename) {
	    sect = html_new_sect(&sects, NULL, &conf);
	    sect->text = NULL;
	    sect->type = INDEX;
	    sect->parent = topsect;
            sect->contents_depth = 0;
	    html_file_section(&conf, &files, sect, 0);   /* peer of chapters */
	    sect->fragments[0] = utoa_dup(conf.index_text, CS_ASCII);
	    sect->fragments[0] = html_sanitise_fragment(&files, sect->file,
							sect->fragments[0]);
	    files.index = sect->file;
	}
    }

    /*
     * Go through the keyword list and sort out fragment IDs for
     * all the potentially referenced paragraphs which _aren't_
     * headings.
     */
    {
	int i;
	keyword *kw;
	htmlsect *sect;

	for (i = 0; (kw = index234(keywords->keys, i)) != NULL; i++) {
	    paragraph *q, *p = kw->para;

	    if (!is_heading_type(p->type)) {
		htmlsect *parent;

		/*
		 * Find the paragraph's parent htmlsect, to
		 * determine which file it will end up in.
		 */
		q = p->parent;
		if (!q) {
		    /*
		     * Preamble paragraphs have no parent. So if we
		     * have a non-heading with no parent, it must
		     * be preamble, and therefore its parent
		     * htmlsect must be the preamble one.
		     */
		    assert(sects.head &&
			   sects.head->type == TOP);
		    parent = sects.head;
		} else
		    parent = (htmlsect *)q->private_data;

		/*
		 * Now we can construct an htmlsect for this
		 * paragraph itself, taking care to put it in the
		 * list of non-sections rather than the list of
		 * sections (so that traverses of the `sects' list
		 * won't attempt to add it to the contents or
		 * anything weird like that).
		 */
		sect = html_new_sect(&nonsects, p, &conf);
		sect->file = parent->file;
		sect->parent = parent;
		p->private_data = sect;

		/*
		 * Fragment IDs for these paragraphs will simply be
		 * `p' followed by an integer.
		 */
		sect->fragments[0] = snewn(40, char);
		sprintf(sect->fragments[0], "p%d",
			sect->file->last_fragment_number++);
		sect->fragments[0] = html_sanitise_fragment(&files, sect->file,
							    sect->fragments[0]);
	    }
	}
    }

    /*
     * Reset the fragment numbers in each file. I've just used them
     * to generate `p' fragment IDs for non-section paragraphs
     * (numbered list elements, bibliocited), and now I want to use
     * them for `i' fragment IDs for index entries.
     */
    {
	htmlfile *file;
	for (file = files.head; file; file = file->next)
	    file->last_fragment_number = 0;
    }

    /*
     * Now sort out the index. This involves:
     * 
     * 	- For each index term, we set up an htmlindex structure to
     * 	  store all the references to that term.
     * 
     * 	- Then we make a pass over the actual document, finding
     * 	  every word_IndexRef; for each one, we actually figure out
     * 	  the HTML filename/fragment pair we will use to reference
     * 	  it, store that information in the private data field of
     * 	  the word_IndexRef itself (so we can recreate it when the
     * 	  time comes to output our HTML), and add a reference to it
     * 	  to the index term in question.
     */
    {
	int i;
	indexentry *entry;
	htmlsect *lastsect;
	word *w;

	/*
	 * Set up the htmlindex structures.
	 */

	for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
	    htmlindex *hi = snew(htmlindex);

	    hi->nrefs = hi->refsize = 0;
	    hi->refs = NULL;

	    entry->backend_data = hi;
	}

	/*
	 * Run over the document inventing fragments. Each fragment
	 * is of the form `i' followed by an integer.
	 */
	lastsect = sects.head;	       /* this is always the top section */
	for (p = sourceform; p; p = p->next) {
	    if (is_heading_type(p->type) && p->type != para_Title)
		lastsect = (htmlsect *)p->private_data;

	    for (w = p->words; w; w = w->next)
		if (w->type == word_IndexRef) {
		    htmlindexref *hr = snew(htmlindexref);
		    indextag *tag;
		    int i;

		    hr->referenced = hr->generated = FALSE;
		    hr->section = lastsect;
		    {
			char buf[40];
			sprintf(buf, "i%d",
				lastsect->file->last_fragment_number++);
			hr->fragment = dupstr(buf);
			hr->fragment =
			    html_sanitise_fragment(&files, hr->section->file,
						   hr->fragment);
		    }
		    w->private_data = hr;

		    tag = index_findtag(idx, w->text);
		    if (!tag)
			break;

		    for (i = 0; i < tag->nrefs; i++) {
			indexentry *entry = tag->refs[i];
			htmlindex *hi = (htmlindex *)entry->backend_data;

			if (hi->nrefs >= hi->refsize) {
			    hi->refsize += 32;
			    hi->refs = sresize(hi->refs, hi->refsize, word *);
			}

			hi->refs[hi->nrefs++] = w;
		    }
		}
	}
    }

    if (chm_mode)
        chm = chm_new();

    /*
     * Now we're ready to write out the actual HTML files.
     * 
     * For each file:
     * 
     *  - we open that file and write its header
     *  - we run down the list of sections
     * 	- for each section directly contained within that file, we
     * 	  output the section text
     * 	- for each section which is not in the file but which has a
     * 	  parent that is, we output a contents entry for the
     * 	  section if appropriate
     *  - finally, we output the file trailer and close the file.
     */
    {
	htmlfile *f, *prevf;
	htmlsect *s;
	paragraph *p;

	prevf = NULL;

	for (f = files.head; f; f = f->next) {
	    htmloutput ho;
	    int displaying;
	    enum LISTTYPE { NOLIST, UL, OL, DL };
	    enum ITEMTYPE { NOITEM, LI, DT, DD };
	    struct stackelement {
		struct stackelement *next;
		enum LISTTYPE listtype;
		enum ITEMTYPE itemtype;
	    } *stackhead;

#define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
#define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )

            if (chm)
                ho_setup_chm(&ho, chm, f->filename);
	    else if (!strcmp(f->filename, "-"))
                ho_setup_stdio(&ho, stdout);
            else
                ho_setup_file(&ho, f->filename);

	    ho.charset = conf.output_charset;
	    ho.restrict_charset = conf.restrict_charset;
	    ho.cstate = charset_init_state;
	    ho.ver = conf.htmlver;
	    ho.state = HO_NEUTRAL;
	    ho.contents_level = 0;
	    ho.hackflags = 0;	       /* none of these thankyouverymuch */
	    ho.hacklimit = -1;

	    /* <!DOCTYPE>. */
	    switch (conf.htmlver) {
	      case HTML_3_2:
                ho_string(&ho, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
                          "HTML 3.2 Final//EN\">\n");
		break;
	      case HTML_4:
                ho_string(&ho, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
                          " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
                          "strict.dtd\">\n");
		break;
	      case ISO_HTML:
                ho_string(&ho, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
                          "15445:2000//DTD HTML//EN\">\n");
		break;
	      case XHTML_1_0_TRANSITIONAL:
                ho_string(&ho, "<?xml version=\"1.0\" encoding=\"");
                ho_string(&ho, charset_to_mimeenc(conf.output_charset));
                ho_string(&ho, "\"?>\n"
                          "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
                          " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
                          "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
		break;
	      case XHTML_1_0_STRICT:
                ho_string(&ho, "<?xml version=\"1.0\" encoding=\"");
                ho_string(&ho, charset_to_mimeenc(conf.output_charset));
                ho_string(&ho, "\"?>\n"
                          "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
                          " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
                          "DTD/xhtml1-strict.dtd\">\n");
		break;
	    }

	    element_open(&ho, "html");
	    if (is_xhtml(conf.htmlver)) {
		element_attr(&ho, "xmlns", "http://www.w3.org/1999/xhtml");
	    }
	    html_nl(&ho);

	    element_open(&ho, "head");
	    html_nl(&ho);

	    element_empty(&ho, "meta");
	    element_attr(&ho, "http-equiv", "content-type");
	    {
		char buf[200];
		sprintf(buf, "text/html; charset=%.150s",
			charset_to_mimeenc(conf.output_charset));
		element_attr(&ho, "content", buf);
	    }
	    html_nl(&ho);

	    if (conf.author) {
		element_empty(&ho, "meta");
		element_attr(&ho, "name", "author");
		element_attr_w(&ho, "content", conf.author);
		html_nl(&ho);
	    }

	    if (conf.description) {
		element_empty(&ho, "meta");
		element_attr(&ho, "name", "description");
		element_attr_w(&ho, "content", conf.description);
		html_nl(&ho);
	    }

	    element_open(&ho, "title");
	    if (f->first && f->first->title) {
		html_words(&ho, f->first->title->words, NOTHING,
			   f, keywords, &conf);

		assert(f->last);
		if (f->last != f->first && f->last->title) {
		    html_text(&ho, conf.title_separator);
		    html_words(&ho, f->last->title->words, NOTHING,
			       f, keywords, &conf);
		}
	    }
	    element_close(&ho, "title");
	    html_nl(&ho);

	    if (conf.rellinks) {

		if (prevf) {
		    element_empty(&ho, "link");
		    element_attr(&ho, "rel", "previous");
		    element_attr(&ho, "href", prevf->filename);
		    html_nl(&ho);
		}

		if (f != files.head) {
		    element_empty(&ho, "link");
		    element_attr(&ho, "rel", "ToC");
		    element_attr(&ho, "href", files.head->filename);
		    html_nl(&ho);
		}

		if (conf.leaf_level > 0) {
		    htmlsect *p = f->first->parent;
		    assert(p == f->last->parent);
		    if (p) {
			element_empty(&ho, "link");
			element_attr(&ho, "rel", "up");
			element_attr(&ho, "href", p->file->filename);
			html_nl(&ho);
		    }
		}

		if (has_index && files.index && f != files.index) {
		    element_empty(&ho, "link");
		    element_attr(&ho, "rel", "index");
		    element_attr(&ho, "href", files.index->filename);
		    html_nl(&ho);
		}

		if (f->next) {
		    element_empty(&ho, "link");
		    element_attr(&ho, "rel", "next");
		    element_attr(&ho, "href", f->next->filename);
		    html_nl(&ho);
		}

	    }

	    if (conf.head_end)
		html_raw(&ho, conf.head_end);

	    /*
	     * Add any <head> data defined in specific sections
	     * that go in this file. (This is mostly to allow <meta
	     * name="AppleTitle"> tags for Mac online help.)
	     */
	    for (s = sects.head; s; s = s->next) {
		if (s->file == f && s->text) {
		    for (p = s->text;
			 p && (p == s->text || p->type == para_Title ||
			       !is_heading_type(p->type));
			 p = p->next) {
			if (p->type == para_Config) {
			    if (!ustricmp(p->keyword, L"html-local-head")) {
				html_raw(&ho, adv(p->origkeyword));
			    }
			}
		    }
		}
	    }

	    element_close(&ho, "head");
	    html_nl(&ho);

	    if (conf.body_tag)
		html_raw(&ho, conf.body_tag);
	    else
		element_open(&ho, "body");
	    html_nl(&ho);

	    if (conf.body_start)
		html_raw(&ho, conf.body_start);

	    /*
	     * Write out a nav bar. Special case: we don't do this
	     * if there is only one file.
	     */
	    if (conf.navlinks && files.head != files.tail) {
		element_open(&ho, "p");
		if (conf.nav_attr)
		    html_raw_as_attr(&ho, conf.nav_attr);

		if (prevf) {
		    element_open(&ho, "a");
		    element_attr(&ho, "href", prevf->filename);
		}
		html_text(&ho, conf.nav_prev_text);
		if (prevf)
		    element_close(&ho, "a");

		html_text(&ho, conf.nav_separator);

		if (f != files.head) {
		    element_open(&ho, "a");
		    element_attr(&ho, "href", files.head->filename);
		}
		html_text(&ho, conf.contents_text);
		if (f != files.head)
		    element_close(&ho, "a");

		/* We don't bother with "Up" links for leaf-level 1,
		 * as they would be identical to the "Contents" links. */
		if (conf.leaf_level >= 2) {
		    htmlsect *p = f->first->parent;
		    assert(p == f->last->parent);
		    html_text(&ho, conf.nav_separator);
		    if (p) {
			element_open(&ho, "a");
			element_attr(&ho, "href", p->file->filename);
		    }
		    html_text(&ho, conf.nav_up_text);
		    if (p) {
			element_close(&ho, "a");
		    }
		}

		if (has_index && files.index) {
		    html_text(&ho, conf.nav_separator);
		    if (f != files.index) {
			element_open(&ho, "a");
			element_attr(&ho, "href", files.index->filename);
		    }
		    html_text(&ho, conf.index_text);
		    if (f != files.index)
			element_close(&ho, "a");
		}

		html_text(&ho, conf.nav_separator);

		if (f->next) {
		    element_open(&ho, "a");
		    element_attr(&ho, "href", f->next->filename);
		}
		html_text(&ho, conf.nav_next_text);
		if (f->next)
		    element_close(&ho, "a");

		element_close(&ho, "p");
		html_nl(&ho);
	    }
	    prevf = f;

	    /*
	     * Special case: for single-file mode, we output the top
	     * section title before the TOC.
	     */
	    if (files.head == files.tail && sects.head->type == TOP) {
		element_open(&ho, "h1");

		/*
		 * Provide anchor(s) for cross-links to target.
		 */
		{
		    int i;
		    for (i=0; i < conf.ntfragments; i++)
			if (sects.head->fragments[i])
			    html_fragment(&ho, sects.head->fragments[i]);
		}

		html_section_title(&ho, sects.head, f, keywords, &conf, TRUE);

		element_close(&ho, "h1");
	    }

	    /*
	     * Write out a prefix TOC for the file (if a leaf file).
	     * 
	     * We start by going through the section list and
	     * collecting the sections which need to be added to
	     * the contents. On the way, we also test to see if
	     * this file is a leaf file (defined as one which
	     * contains all descendants of any section it
	     * contains), because this will play a part in our
	     * decision on whether or not to _output_ the TOC.
	     */
	    {
		int ntoc = 0, tocsize = 0, tocstartidx = 0;
		htmlsect **toc = NULL;
		int leaf = TRUE;

		for (s = sects.head; s; s = s->next) {
		    htmlsect *a, *ac;
		    int depth, adepth;

		    /*
		     * Search up from this section until we find
		     * the highest-level one which belongs in this
		     * file.
		     */
		    depth = adepth = 0;
		    a = NULL;
		    for (ac = s; ac; ac = ac->parent) {
			if (ac->file == f) {
			    a = ac;
			    adepth = depth;
			}
			depth++;
		    }

		    if (s->file != f && a != NULL)
			leaf = FALSE;

		    if (a) {
			if (adepth <= a->contents_depth) {
			    if (ntoc >= tocsize) {
				tocsize += 64;
				toc = sresize(toc, tocsize, htmlsect *);
			    }
			    toc[ntoc++] = s;
			}
		    }
		}

		/*
		 * Special case: for single-file mode, we don't output
		 * the first section TOC entry if it's the top since we
		 * have already output a section title for it above the
		 * TOC, and since we don't output the top TOC entry, we
		 * reduce the level of the remaining TOC entries by one
		 * so that they are output correctly one level up from
		 * where they would have been.
		 */
		tocstartidx = (files.head == files.tail && ntoc > 0 &&
			       toc[0]->type == TOP) ? 1 : 0;
		if (leaf && conf.leaf_contains_contents &&
		    ntoc >= conf.leaf_smallest_contents &&
		    tocstartidx < ntoc) {
		    int i;

		    for (i = tocstartidx; i < ntoc; i++) {
			htmlsect *s = toc[i];
			int hlevel = (s->type == TOP ? -1 :
				      s->type == INDEX ? 0 :
				      heading_depth(s->title))
			    - tocstartidx - f->min_heading_depth + 1;

			assert(hlevel >= 1);
			html_contents_entry(&ho, hlevel, s,
					    f, keywords, &conf);
		    }
		    html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
		}
	    }

	    /*
	     * Now go through the document and output some real
	     * text.
	     */
	    displaying = FALSE;
	    for (s = sects.head; s; s = s->next) {
		if (s->file == f) {
		    /*
		     * This section belongs in this file.
		     * Display it.
		     */
		    displaying = TRUE;
		} else {
		    /*
		     * Doesn't belong in this file, but it may be
		     * a descendant of a section which does, in
		     * which case we should consider it for the
		     * main TOC of this file (for non-leaf files).
		     */
		    htmlsect *a, *ac;
		    int depth, adepth;

		    displaying = FALSE;

		    /*
		     * Search up from this section until we find
		     * the highest-level one which belongs in this
		     * file.
		     */
		    depth = adepth = 0;
		    a = NULL;
		    for (ac = s; ac; ac = ac->parent) {
			if (ac->file == f) {
			    a = ac;
			    adepth = depth;
			}
			depth++;
		    }

		    if (a != NULL) {
			/*
			 * This section does not belong in this
			 * file, but an ancestor of it does. Write
			 * out a contents table entry, if the depth
			 * doesn't exceed the maximum contents
			 * depth for the ancestor section.
			 */
			if (adepth <= a->contents_depth) {
			    html_contents_entry(&ho, adepth, s,
						f, keywords, &conf);
			}
		    }
		}

		if (displaying) {
		    int hlevel;
		    char htag[3];

		    html_contents_entry(&ho, 0, NULL, f, keywords, &conf);

		    /*
		     * Display the section heading.
		     *
		     * Special case: for single-file mode, we don't
		     * output the top section title since we have
		     * already output it before the TOC.
		     */
		    if (files.head != files.tail || s->type != TOP) {
			hlevel = (s->type == TOP ? -1 :
				  s->type == INDEX ? 0 :
				  heading_depth(s->title))
			    - f->min_heading_depth + 1;
			assert(hlevel >= 1);
			/* HTML headings only go up to <h6> */
			if (hlevel > 6)
			    hlevel = 6;
			htag[0] = 'h';
			htag[1] = '0' + hlevel;
			htag[2] = '\0';
			element_open(&ho, htag);

			/*
			 * Provide anchor(s) for cross-links to target.
			 * 
			 * (Also we'll have to do this separately in
			 * other paragraph types - NumberedList and
			 * BiblioCited.)
			 */
			{
			    int i;
			    for (i=0; i < conf.ntfragments; i++)
				if (s->fragments[i])
				    html_fragment(&ho, s->fragments[i]);
			}

			html_section_title(&ho, s, f, keywords, &conf, TRUE);

			element_close(&ho, htag);
		    }

		    /*
		     * Now display the section text.
		     */
		    if (s->text) {
			stackhead = snew(struct stackelement);
			stackhead->next = NULL;
			stackhead->listtype = NOLIST;
			stackhead->itemtype = NOITEM;

			for (p = s->text;; p = p->next) {
			    enum LISTTYPE listtype;
			    struct stackelement *se;

			    /*
			     * Preliminary switch to figure out what
			     * sort of list we expect to be inside at
			     * this stage.
			     *
			     * Since p may still be NULL at this point,
			     * I invent a harmless paragraph type for
			     * it if it is.
			     */
			    switch (p ? p->type : para_Normal) {
			      case para_Rule:
			      case para_Normal:
			      case para_Copyright:
			      case para_BiblioCited:
			      case para_Code:
			      case para_QuotePush:
			      case para_QuotePop:
			      case para_Chapter:
			      case para_Appendix:
			      case para_UnnumberedChapter:
			      case para_Heading:
			      case para_Subsect:
			      case para_LcontPop:
				listtype = NOLIST;
				break;

			      case para_Bullet:
				listtype = UL;
				break;

			      case para_NumberedList:
				listtype = OL;
				break;

			      case para_DescribedThing:
			      case para_Description:
				listtype = DL;
				break;

			      case para_LcontPush:
				se = snew(struct stackelement);
				se->next = stackhead;
				se->listtype = NOLIST;
				se->itemtype = NOITEM;
				stackhead = se;
				continue;

			      default:     /* some totally non-printing para */
				continue;
			    }

			    html_nl(&ho);

			    /*
			     * Terminate the most recent list item, if
			     * any. (We left this until after
			     * processing LcontPush, since in that case
			     * the list item won't want to be
			     * terminated until after the corresponding
			     * LcontPop.)
			     */
			    if (stackhead->itemtype != NOITEM) {
				element_close(&ho, itemname(stackhead->itemtype));
				html_nl(&ho);
			    }
			    stackhead->itemtype = NOITEM;

			    /*
			     * Terminate the current list, if it's not
			     * the one we want to be in.
			     */
			    if (listtype != stackhead->listtype &&
				stackhead->listtype != NOLIST) {
				element_close(&ho, listname(stackhead->listtype));
				html_nl(&ho);
			    }

			    /*
			     * Leave the loop if our time has come.
			     */
			    if (!p || (is_heading_type(p->type) &&
				       p->type != para_Title))
				break;     /* end of section text */

			    /*
			     * Start a fresh list if necessary.
			     */
			    if (listtype != stackhead->listtype &&
				listtype != NOLIST)
				element_open(&ho, listname(listtype));

			    stackhead->listtype = listtype;

			    switch (p->type) {
			      case para_Rule:
				element_empty(&ho, "hr");
				break;
			      case para_Code:
				html_codepara(&ho, p->words);
				break;
			      case para_Normal:
			      case para_Copyright:
				element_open(&ho, "p");
				html_nl(&ho);
				html_words(&ho, p->words, ALL,
					   f, keywords, &conf);
				html_nl(&ho);
				element_close(&ho, "p");
				break;
			      case para_BiblioCited:
				element_open(&ho, "p");
				if (p->private_data) {
				    htmlsect *s = (htmlsect *)p->private_data;
				    int i;
				    for (i=0; i < conf.ntfragments; i++)
					if (s->fragments[i])
					    html_fragment(&ho, s->fragments[i]);
				}
				html_nl(&ho);
				html_words(&ho, p->kwtext, ALL,
					   f, keywords, &conf);
				html_text(&ho, L" ");
				html_words(&ho, p->words, ALL,
					   f, keywords, &conf);
				html_nl(&ho);
				element_close(&ho, "p");
				break;
			      case para_Bullet:
			      case para_NumberedList:
				element_open(&ho, "li");
				if (p->private_data) {
				    htmlsect *s = (htmlsect *)p->private_data;
				    int i;
				    for (i=0; i < conf.ntfragments; i++)
					if (s->fragments[i])
					    html_fragment(&ho, s->fragments[i]);
				}
				html_nl(&ho);
				stackhead->itemtype = LI;
				html_words(&ho, p->words, ALL,
					   f, keywords, &conf);
				break;
			      case para_DescribedThing:
				element_open(&ho, "dt");
				html_nl(&ho);
				stackhead->itemtype = DT;
				html_words(&ho, p->words, ALL,
					   f, keywords, &conf);
				break;
			      case para_Description:
				element_open(&ho, "dd");
				html_nl(&ho);
				stackhead->itemtype = DD;
				html_words(&ho, p->words, ALL,
					   f, keywords, &conf);
				break;

			      case para_QuotePush:
				element_open(&ho, "blockquote");
				break;
			      case para_QuotePop:
				element_close(&ho, "blockquote");
				break;

			      case para_LcontPop:
				se = stackhead;
				stackhead = stackhead->next;
				assert(stackhead);
				sfree(se);
				break;
			    }
			}

			assert(stackhead && !stackhead->next);
			sfree(stackhead);
		    }
		    
		    if (s->type == INDEX) {
			indexentry *entry;
			int i;

			/*
			 * This section is the index. I'll just
			 * render it as a single paragraph, with a
			 * colon between the index term and the
			 * references, and <br> in between each
			 * entry.
			 */
			element_open(&ho, "p");

			for (i = 0; (entry =
				     index234(idx->entries, i)) != NULL; i++) {
			    htmlindex *hi = (htmlindex *)entry->backend_data;
			    int j;

			    if (i > 0)
				element_empty(&ho, "br");
			    html_nl(&ho);

			    html_words(&ho, entry->text, MARKUP|LINKS,
				       f, keywords, &conf);

			    html_text(&ho, conf.index_main_sep);

			    for (j = 0; j < hi->nrefs; j++) {
				htmlindexref *hr =
				    (htmlindexref *)hi->refs[j]->private_data;
				paragraph *p = hr->section->title;

				if (j > 0)
				    html_text(&ho, conf.index_multi_sep);

				html_href(&ho, f, hr->section->file,
					  hr->fragment);
				hr->referenced = TRUE;
				if (p && p->kwtext)
				    html_words(&ho, p->kwtext, MARKUP|LINKS,
					       f, keywords, &conf);
				else if (p && p->words)
				    html_words(&ho, p->words, MARKUP|LINKS,
					       f, keywords, &conf);
				else {
				    /*
				     * If there is no title at all,
				     * this must be because our
				     * target section is the
				     * preamble section and there
				     * is no title. So we use the
				     * preamble_text.
				     */
				    html_text(&ho, conf.preamble_text);
				}
				element_close(&ho, "a");
			    }
			}
			element_close(&ho, "p");
		    }
		}
	    }

	    html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
	    html_nl(&ho);

	    {
		/*
		 * Footer.
		 */
		int done_version_ids = FALSE;

		if (conf.address_section)
		    element_empty(&ho, "hr");

		if (conf.body_end)
		    html_raw(&ho, conf.body_end);

		if (conf.address_section) {
		    int started = FALSE;
		    if (conf.htmlver == ISO_HTML) {
			/*
			 * The ISO-HTML validator complains if
			 * there isn't a <div> tag surrounding the
			 * <address> tag. I'm uncertain of why this
			 * should be - there appears to be no
			 * mention of this in the ISO-HTML spec,
			 * suggesting that it doesn't represent a
			 * change from HTML 4, but nonetheless the
			 * HTML 4 validator doesn't seem to mind.
			 */
			element_open(&ho, "div");
		    }
		    element_open(&ho, "address");
		    if (conf.addr_start) {
			html_raw(&ho, conf.addr_start);
			html_nl(&ho);
			started = TRUE;
		    }
		    if (conf.visible_version_id) {
			for (p = sourceform; p; p = p->next)
			    if (p->type == para_VersionID) {
				if (started)
				    element_empty(&ho, "br");
				html_nl(&ho);
				html_text(&ho, conf.pre_versionid);
				html_words(&ho, p->words, NOTHING,
					   f, keywords, &conf);
				html_text(&ho, conf.post_versionid);
				started = TRUE;
			    }
			done_version_ids = TRUE;
		    }
		    if (conf.addr_end) {
			if (started)
			    element_empty(&ho, "br");
			html_raw(&ho, conf.addr_end);
		    }
		    element_close(&ho, "address");
		    if (conf.htmlver == ISO_HTML)
			element_close(&ho, "div");
		}

		if (!done_version_ids) {
		    /*
		     * If the user didn't want the version IDs
		     * visible, I think we still have a duty to put
		     * them in an HTML comment.
		     */
		    int started = FALSE;
		    for (p = sourceform; p; p = p->next)
			if (p->type == para_VersionID) {
			    if (!started) {
				html_raw(&ho, "<!-- version IDs:\n");
				started = TRUE;
			    }
			    html_words(&ho, p->words, NOTHING,
				       f, keywords, &conf);
			    html_nl(&ho);
			}
		    if (started)
			html_raw(&ho, "-->\n");
		}
	    }

	    element_close(&ho, "body");
	    html_nl(&ho);
	    element_close(&ho, "html");
	    html_nl(&ho);
	    cleanup(&ho);
	}
    }

    /*
     * Before we start outputting the HTML Help files, check
     * whether there's even going to _be_ an index file: we omit it
     * if the index contains nothing.
     */
    if (chm_mode || conf.hhk_filename) {
	int ok = FALSE;
	int i;
	indexentry *entry;

	for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
	    htmlindex *hi = (htmlindex *)entry->backend_data;

	    if (hi->nrefs > 0) {
		ok = TRUE;	       /* found an index entry */
		break;
	    }
	}

	if (ok)
	    hhk_needed = TRUE;
    }

    /*
     * If we're doing direct CHM output, tell winchm.c all the things
     * it will need to know aside from the various HTML files'
     * contents.
     */
    if (chm) {
        chm_contents_filename(chm, conf.hhc_filename);
        if (has_index)
            chm_index_filename(chm, conf.hhk_filename);
        chm_default_window(chm, "main");

        {
            htmloutput ho;
            rdstringc rs = {0, 0, NULL};

            ho.charset = CS_CP1252; /* as far as I know, CHM is */
            ho.restrict_charset = CS_CP1252; /* hardwired to this charset */
            ho.cstate = charset_init_state;
            ho.ver = HTML_4;	       /* *shrug* */
            ho.state = HO_NEUTRAL;
            ho.contents_level = 0;
            ho.hackflags = HO_HACK_QUOTENOTHING;

            ho_setup_rdstringc(&ho, &rs);

            ho.hacklimit = 255;
            html_words(&ho, topsect->title->words, NOTHING,
                       NULL, keywords, &conf);

            rdaddc(&rs, '\0');
            chm_title(chm, rs.text);

            chm_default_topic(chm, files.head->filename);

            chm_add_window(chm, "main", rs.text,
                           conf.hhc_filename, conf.hhk_filename,
                           files.head->filename,
                           /* This first magic number is
                            * fsWinProperties, controlling Navigation
                            * Pane options and the like. Constants
                            * HHWIN_PROP_* in htmlhelp.h. */
                           0x62520,
                           /* This second number is fsToolBarFlags,
                            * mainly controlling toolbar buttons.
                            * Constants HHWIN_BUTTON_*. NOTE: there
                            * are two pairs of bits for Next/Previous
                            * buttons: 7/8 (which do nothing useful),
                            * and 21/22 (which work). (Neither of
                            * these are exposed in the HHW UI, but
                            * they work fine in HH.) We use the
                            * latter. */
                           0x70304e);

            sfree(rs.text);
        }

        {
            htmlfile *f;

            for (f = files.head; f; f = f->next)
                f->chmsect = NULL;
            for (f = files.head; f; f = f->next) {
                htmlsect *s = f->first;
                htmloutput ho;
                rdstringc rs = {0, 0, NULL};

                ho.charset = CS_CP1252;
                ho.restrict_charset = CS_CP1252;
                ho.cstate = charset_init_state;
                ho.ver = HTML_4;	       /* *shrug* */
                ho.state = HO_NEUTRAL;
                ho.contents_level = 0;
                ho.hackflags = HO_HACK_QUOTENOTHING;

                ho_setup_rdstringc(&ho, &rs);
                ho.hacklimit = 255;

                if (f->first->title)
                    html_words(&ho, f->first->title->words, NOTHING,
                               NULL, keywords, &conf);
                else if (f->first->type == INDEX)
                    html_text(&ho, conf.index_text);
                rdaddc(&rs, '\0');
                
                while (s && s->file == f)
                    s = s->parent;

                /*
                 * Special case, as below: the TOP file is not
                 * considered to be the parent of everything else.
                 */
                if (s && s->type == TOP)
                    s = NULL;

                f->chmsect = chm_add_section(chm, s ? s->file->chmsect : NULL,
                                             rs.text, f->filename);

                sfree(rs.text);
            }
        }

        {
            int i;

            for (i = 0; i < conf.nchmextrafiles; i++) {
                const char *fname = conf.chm_extrafiles[i];
                FILE *fp;
                long size;
                char *data;

                fp = fopen(fname, "rb");
                if (!fp) {
                    err_cantopen(fname);
                    continue;
                }

                fseek(fp, 0, SEEK_END);
                size = ftell(fp);
                rewind(fp);

                data = snewn(size, char);
                size = fread(data, 1, size, fp);
                fclose(fp);

                chm_add_file(chm, conf.chm_extranames[i], data, size);
                sfree(data);
            }
        }
    }

    /*
     * Output the MS HTML Help supporting files, if requested.
     *
     * A good unofficial reference for these is <http://chmspec.nongnu.org/>.
     */
    if (conf.hhp_filename) {
	htmlfile *f;
	htmloutput ho;

	ho.charset = CS_CP1252;	       /* as far as I know, HHP files are */
	ho.restrict_charset = CS_CP1252;   /* hardwired to this charset */
	ho.cstate = charset_init_state;
	ho.ver = HTML_4;	       /* *shrug* */
	ho.state = HO_NEUTRAL;
	ho.contents_level = 0;
	ho.hackflags = HO_HACK_QUOTENOTHING;

        ho_setup_file(&ho, conf.hhp_filename);

	ho_string(&ho,
                  "[OPTIONS]\n"
                  /* Binary TOC required for Next/Previous nav to work */
                  "Binary TOC=Yes\n"
                  "Compatibility=1.1 or later\n"
                  "Compiled file=");
        ho_string(&ho, conf.chm_filename);
	ho_string(&ho, "\n"
                  "Default Window=main\n"
                  "Default topic=");
        ho_string(&ho, files.head->filename);
	ho_string(&ho, "\n"
                  "Display compile progress=Yes\n"
                  "Full-text search=Yes\n"
                  "Title=");

	ho.hacklimit = 255;
	html_words(&ho, topsect->title->words, NOTHING,
		   NULL, keywords, &conf);

	ho_string(&ho, "\n");

	/*
	 * These two entries don't seem to be remotely necessary
	 * for a successful run of the help _compiler_, but
	 * omitting them causes the GUI Help Workshop to behave
	 * rather strangely if you try to load the help project
	 * into that and edit it.
	 */
	if (conf.hhc_filename) {
            ho_string(&ho, "Contents file=");
            ho_string(&ho, conf.hhc_filename);
            ho_string(&ho, "\n");
        }
	if (hhk_needed) {
            ho_string(&ho, "Index file=");
            ho_string(&ho, conf.hhk_filename);
            ho_string(&ho, "\n");
        }

        ho_string(&ho, "\n[WINDOWS]\nmain=\"");

	ho.hackflags |= HO_HACK_OMITQUOTES;
	ho.hacklimit = 255;
	html_words(&ho, topsect->title->words, NOTHING,
		   NULL, keywords, &conf);

        ho_string(&ho, "\",\"");
        if (conf.hhc_filename)
            ho_string(&ho, conf.hhc_filename);
        ho_string(&ho, "\",\"");
        if (hhk_needed)
            ho_string(&ho, conf.hhk_filename);
        ho_string(&ho, "\",\"");
        ho_string(&ho, files.head->filename);
        ho_string(&ho, "\",,,,,,"
                  /* This first magic number is fsWinProperties, controlling
                   * Navigation Pane options and the like.
                   * Constants HHWIN_PROP_* in htmlhelp.h. */
                  "0x62520,,"
                  /* This second number is fsToolBarFlags, mainly controlling
                   * toolbar buttons. Constants HHWIN_BUTTON_*.
                   * NOTE: there are two pairs of bits for Next/Previous
                   * buttons: 7/8 (which do nothing useful), and 21/22
                   * (which work). (Neither of these are exposed in the HHW
                   * UI, but they work fine in HH.) We use the latter. */
                  "0x70304e,,,,,,,,0\n");

	/*
	 * The [FILES] section is also not necessary for
	 * compilation (hhc appears to build up a list of needed
	 * files just by following links from the given starting
	 * points), but useful for loading the project into HHW.
	 */
	ho_string(&ho, "\n[FILES]\n");
	for (f = files.head; f; f = f->next) {
	    ho_string(&ho, f->filename);
	    ho_string(&ho, "\n");
        }

        ho_finish(&ho);
    }
    if (chm || conf.hhc_filename) {
	htmlfile *f;
	htmlsect *s, *a;
	htmloutput ho;
	int currdepth = 0;

	ho.charset = CS_CP1252;	       /* as far as I know, HHC files are */
	ho.restrict_charset = CS_CP1252;   /* hardwired to this charset */
	ho.cstate = charset_init_state;
	ho.ver = HTML_4;	       /* *shrug* */
	ho.state = HO_NEUTRAL;
	ho.contents_level = 0;
	ho.hackflags = HO_HACK_QUOTEQUOTES;

        if (chm)
            ho_setup_chm(&ho, chm, conf.hhc_filename);
        else
            ho_setup_file(&ho, conf.hhc_filename);

	/*
	 * Magic DOCTYPE which seems to work for .HHC files. I'm
	 * wary of trying to change it!
	 */
	ho_string(&ho, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
                  "<HTML><HEAD>\n"
                  "<META HTTP-EQUIV=\"Content-Type\" "
                  "CONTENT=\"text/html; charset=");
        ho_string(&ho, charset_to_mimeenc(conf.output_charset));
        ho_string(&ho, "\">\n"
                  "</HEAD><BODY><UL>\n");

	for (f = files.head; f; f = f->next) {
	    /*
	     * For each HTML file, write out a contents entry.
	     */
	    int depth, leaf = TRUE;

	    /*
	     * Determine the depth of this file in the contents
	     * tree.
	     * 
	     * If the file contains no sections, it is assumed to
	     * have depth zero.
	     */
	    depth = 0;
	    if (f->first)
		for (a = f->first->parent; a && a->type != TOP; a = a->parent)
		    depth++;

	    /*
	     * Determine if this file is a leaf file, by
	     * trawling the section list to see if there's any
	     * section with an ancestor in this file but which
	     * is not itself in this file.
	     *
	     * Special case: for contents purposes, the TOP
	     * file is not considered to be the parent of the
	     * chapter files, so it's always a leaf.
	     * 
	     * A file with no sections in it is also a leaf.
	     */
	    if (f->first && f->first->type != TOP) {
		for (s = f->first; s; s = s->next) {
		    htmlsect *a;

		    if (leaf && s->file != f) {
			for (a = s; a; a = a->parent)
			    if (a->file == f) {
				leaf = FALSE;
				break;
			    }
		    }
		}
	    }

	    /*
	     * Now write out our contents entry.
	     */
	    while (currdepth < depth) {
		ho_string(&ho, "<UL>\n");
		currdepth++;
	    }
	    while (currdepth > depth) {
		ho_string(&ho, "</UL>\n");
		currdepth--;
	    }
            ho_string(&ho, "<LI><OBJECT TYPE=\"text/sitemap\">"
                      "<PARAM NAME=\"Name\" VALUE=\"");
	    ho.hacklimit = 255;
	    if (f->first->title)
		html_words(&ho, f->first->title->words, NOTHING,
			   NULL, keywords, &conf);
	    else if (f->first->type == INDEX)
		html_text(&ho, conf.index_text);
            ho_string(&ho, "\"><PARAM NAME=\"Local\" VALUE=\"");
            ho_string(&ho, f->filename);
            ho_string(&ho, "\"><PARAM NAME=\"ImageNumber\" VALUE=\"");
            ho_string(&ho, leaf ? "11" : "1");
            ho_string(&ho, "\"></OBJECT>\n");
	}

	while (currdepth > 0) {
	    ho_string(&ho, "</UL>\n");
	    currdepth--;
	}

	ho_string(&ho, "</UL></BODY></HTML>\n");

	cleanup(&ho);
    }
    if (hhk_needed) {
	htmlfile *f;
	htmloutput ho;
	indexentry *entry;
	int i;

	/*
	 * First make a pass over all HTML files and set their
	 * `temp' fields to zero, because we're about to use them.
	 */
	for (f = files.head; f; f = f->next)
	    f->temp = 0;

	ho.charset = CS_CP1252;	       /* as far as I know, HHK files are */
	ho.restrict_charset = CS_CP1252;   /* hardwired to this charset */
	ho.cstate = charset_init_state;
	ho.ver = HTML_4;	       /* *shrug* */
	ho.state = HO_NEUTRAL;
	ho.contents_level = 0;
	ho.hackflags = HO_HACK_QUOTEQUOTES;

        if (chm)
            ho_setup_chm(&ho, chm, conf.hhk_filename);
        else
            ho_setup_file(&ho, conf.hhk_filename);

	/*
	 * Magic DOCTYPE which seems to work for .HHK files. I'm
	 * wary of trying to change it!
	 */
        ho_string(&ho, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
                  "<HTML><HEAD>\n"
                  "<META HTTP-EQUIV=\"Content-Type\" "
                  "CONTENT=\"text/html; charset=");
        ho_string(&ho, charset_to_mimeenc(conf.output_charset));
        ho_string(&ho, "\">\n"
                  "</HEAD><BODY><UL>\n");

	/*
	 * Go through the index terms and output each one.
	 */
	for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
	    htmlindex *hi = (htmlindex *)entry->backend_data;
	    int j;

	    if (hi->nrefs > 0) {
		ho_string(&ho, "<LI><OBJECT TYPE=\"text/sitemap\">\n"
                          "<PARAM NAME=\"Name\" VALUE=\"");
		ho.hacklimit = 255;
		html_words(&ho, entry->text, NOTHING,
			   NULL, keywords, &conf);
		ho_string(&ho, "\">\n");

		for (j = 0; j < hi->nrefs; j++) {
		    htmlindexref *hr =
			(htmlindexref *)hi->refs[j]->private_data;

		    /*
		     * Use the temp field to ensure we don't
		     * reference the same file more than once.
		     */
		    if (!hr->section->file->temp) {
			ho_string(&ho, "<PARAM NAME=\"Local\" VALUE=\"");
                        ho_string(&ho, hr->section->file->filename);
                        ho_string(&ho, "\">\n");
			hr->section->file->temp = 1;
		    }

		    hr->referenced = TRUE;
		}

		ho_string(&ho, "</OBJECT>\n");

		/*
		 * Now go through those files and re-clear the temp
		 * fields ready for the _next_ index term.
		 */
		for (j = 0; j < hi->nrefs; j++) {
		    htmlindexref *hr =
			(htmlindexref *)hi->refs[j]->private_data;
		    hr->section->file->temp = 0;
		}
	    }
	}

	ho_string(&ho, "</UL></BODY></HTML>\n");
	cleanup(&ho);
    }

    if (chm) {
        /*
         * Finalise and write out the CHM file.
         */
        const char *data;
        int len;
        FILE *fp;

        fp = fopen(conf.chm_filename, "wb");
        if (!fp) {
            err_cantopenw(conf.chm_filename);
        } else {
            data = chm_build(chm, &len);
            fwrite(data, 1, len, fp);
            fclose(fp);
        }

        chm_free(chm);
    }

    /*
     * Go through and check that no index fragments were referenced
     * without being generated, or indeed vice versa.
     * 
     * (When I actually get round to freeing everything, this can
     * probably be the freeing loop as well.)
     */
    for (p = sourceform; p; p = p->next) {
	word *w;
	for (w = p->words; w; w = w->next)
	    if (w->type == word_IndexRef) {
		htmlindexref *hr = (htmlindexref *)w->private_data;

		assert(!hr->referenced == !hr->generated);
	    }
    }

    /*
     * Free all the working data.
     */
    {
	htmlfragment *frag;
	while ( (frag = (htmlfragment *)delpos234(files.frags, 0)) != NULL ) {
	    /*
	     * frag->fragment is dynamically allocated, but will be
	     * freed when we process the htmlsect structure which
	     * it is attached to.
	     */
	    sfree(frag);
	}
	freetree234(files.frags);
    }
    /*
     * The strings in files.files are all owned by their containing
     * htmlfile structures, so there's no need to free them here.
     */
    freetree234(files.files);
    {
	htmlsect *sect, *tmp;
	sect = sects.head;
	while (sect) {
	    int i;
	    tmp = sect->next;
	    for (i=0; i < conf.ntfragments; i++)
		sfree(sect->fragments[i]);
	    sfree(sect->fragments);
	    sfree(sect);
	    sect = tmp;
	}
	sect = nonsects.head;
	while (sect) {
	    int i;
	    tmp = sect->next;
	    for (i=0; i < conf.ntfragments; i++)
		sfree(sect->fragments[i]);
	    sfree(sect->fragments);
	    sfree(sect);
	    sect = tmp;
	}
    }
    {
	htmlfile *file, *tmp;
	file = files.head;
	while (file) {
	    tmp = file->next;
	    sfree(file->filename);
	    sfree(file);
	    file = tmp;
	}
    }
    {
	int i;
	indexentry *entry;
	for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
	    htmlindex *hi = (htmlindex *)entry->backend_data;
	    sfree(hi);
	}
    }
    {
	paragraph *p;
	word *w;
	for (p = sourceform; p; p = p->next)
	    for (w = p->words; w; w = w->next)
		if (w->type == word_IndexRef) {
		    htmlindexref *hr = (htmlindexref *)w->private_data;
		    assert(hr != NULL);
		    sfree(hr->fragment);
		    sfree(hr);
		}
    }
    sfree(conf.asect);
    sfree(conf.single_filename);
    sfree(conf.contents_filename);
    sfree(conf.index_filename);
    sfree(conf.template_filename);
    while (conf.ntfragments--)
	sfree(conf.template_fragments[conf.ntfragments]);
    sfree(conf.template_fragments);
    while (conf.nchmextrafiles--) {
	sfree(conf.chm_extrafiles[conf.nchmextrafiles]);
	sfree(conf.chm_extranames[conf.nchmextrafiles]);
    }
    sfree(conf.chm_extrafiles);
}

void html_backend(paragraph *sourceform, keywordlist *keywords,
                  indexdata *idx, void *unused)
{
    IGNORE(unused);
    html_backend_common(sourceform, keywords, idx, FALSE);
}

void chm_backend(paragraph *sourceform, keywordlist *keywords,
                 indexdata *idx, void *unused)
{
    IGNORE(unused);
    html_backend_common(sourceform, keywords, idx, TRUE);
}

static void html_file_section(htmlconfig *cfg, htmlfilelist *files,
			      htmlsect *sect, int depth)
{
    htmlfile *file;
    int ldepth;

    /*
     * `depth' is derived from the heading_depth() macro at the top
     * of this file, which counts title as -1, chapter as 0,
     * heading as 1 and subsection as 2. However, the semantics of
     * cfg->leaf_level are defined to count chapter as 1, heading
     * as 2 etc. So first I increment depth :-(
     */
    ldepth = depth + 1;

    if (cfg->leaf_level == 0) {
	/*
	 * leaf_level==0 is a special case, in which everything is
	 * put into a single file.
	 */
	if (!files->single)
	    files->single = html_new_file(files, cfg->single_filename);

	file = files->single;
    } else {
	/*
	 * If the depth of this section is at or above leaf_level,
	 * we invent a fresh file and put this section at its head.
	 * Otherwise, we put it in the same file as its parent
	 * section.
	 * 
	 * Another special value of cfg->leaf_level is -1, which
	 * means infinity (i.e. it's considered to always be
	 * greater than depth).
	 */
	if (cfg->leaf_level > 0 && ldepth > cfg->leaf_level) {
	    /*
	     * We know that sect->parent cannot be NULL. The only
	     * circumstance in which it can be is if sect is at
	     * chapter or appendix level, i.e. ldepth==1; and if
	     * that's the case, then we cannot have entered this
	     * branch unless cfg->leaf_level==0, in which case we
	     * would be in the single-file case above and not here
	     * at all.
	     */
	    assert(sect->parent);

	    file = sect->parent->file;
	} else {
	    if (sect->type == TOP) {
		file = html_new_file(files, cfg->contents_filename);
	    } else if (sect->type == INDEX) {
		file = html_new_file(files, cfg->index_filename);
	    } else {
		char *title;

		assert(ldepth > 0 && sect->title);
		title = html_format(sect->title, cfg->template_filename);
		file = html_new_file(files, title);
		sfree(title);
	    }
	}
    }

    sect->file = file;

    if (file->min_heading_depth > depth) {
	/*
	 * This heading is at a higher level than any heading we
	 * have so far placed in this file; so we set the `first'
	 * pointer.
	 */
	file->min_heading_depth = depth;
	file->first = sect;
    }

    if (file->min_heading_depth == depth)
	file->last = sect;
}

static htmlfile *html_new_file(htmlfilelist *list, char *filename)
{
    htmlfile *ret = snew(htmlfile);

    ret->next = NULL;
    if (list->tail)
	list->tail->next = ret;
    else
	list->head = ret;
    list->tail = ret;

    ret->filename = html_sanitise_filename(list, dupstr(filename));
    add234(list->files, ret->filename);
    ret->last_fragment_number = 0;
    ret->min_heading_depth = INT_MAX;
    ret->first = ret->last = NULL;

    return ret;
}

static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title,
			       htmlconfig *cfg)
{
    htmlsect *ret = snew(htmlsect);

    ret->next = NULL;
    if (list->tail)
	list->tail->next = ret;
    else
	list->head = ret;
    list->tail = ret;

    ret->title = title;
    ret->file = NULL;
    ret->parent = NULL;
    ret->type = NORMAL;

    ret->fragments = snewn(cfg->ntfragments, char *);
    {
	int i;
	for (i=0; i < cfg->ntfragments; i++)
	    ret->fragments[i] = NULL;
    }

    return ret;
}

static void html_words(htmloutput *ho, word *words, int flags,
		       htmlfile *file, keywordlist *keywords, htmlconfig *cfg)
{
    word *w;
    char *c, *c2, *p, *q;
    int style, type;

    for (w = words; w; w = w->next) switch (w->type) {
      case word_HyperLink:
	if (flags & LINKS) {
	    element_open(ho, "a");
	    c = utoa_dup(w->text, CS_ASCII);
	    c2 = snewn(1 + 10*strlen(c), char);
	    for (p = c, q = c2; *p; p++) {
		if (*p == '&')
		    q += sprintf(q, "&amp;");
		else if (*p == '<')
		    q += sprintf(q, "&lt;");
		else if (*p == '>')
		    q += sprintf(q, "&gt;");
		else
		    *q++ = *p;
	    }
	    *q = '\0';
	    element_attr(ho, "href", c2);
	    sfree(c2);
	    sfree(c);
	}
	break;
      case word_UpperXref:
      case word_LowerXref:
	if (flags & LINKS) {
	    keyword *kwl = kw_lookup(keywords, w->text);
	    paragraph *p;
	    htmlsect *s;

            /* kwl should be non-NULL in any sensible case, but if the
             * input contained an unresolvable xref then it might be
             * NULL as an after-effect of that */
	    if (kwl) {
                p = kwl->para;
                s = (htmlsect *)p->private_data;

                assert(s);

                html_href(ho, file, s->file, s->fragments[0]);
            } else {
                /* If kwl is NULL, we must still open an href to
                 * _somewhere_, because it's easier than remembering
                 * which one not to close when we come to the
                 * XrefEnd */
                html_href(ho, file, file, NULL);
            }
	}
	break;
      case word_HyperEnd:
      case word_XrefEnd:
	if (flags & LINKS)
	    element_close(ho, "a");
	break;
      case word_IndexRef:
	if (flags & INDEXENTS) {
	    htmlindexref *hr = (htmlindexref *)w->private_data;
	    html_fragment(ho, hr->fragment);
	    hr->generated = TRUE;
	}
	break;
      case word_Normal:
      case word_Emph:
      case word_Strong:
      case word_Code:
      case word_WeakCode:
      case word_WhiteSpace:
      case word_EmphSpace:
      case word_StrongSpace:
      case word_CodeSpace:
      case word_WkCodeSpace:
      case word_Quote:
      case word_EmphQuote:
      case word_StrongQuote:
      case word_CodeQuote:
      case word_WkCodeQuote:
	style = towordstyle(w->type);
	type = removeattr(w->type);
	if (style == word_Emph &&
	    (attraux(w->aux) == attr_First ||
	     attraux(w->aux) == attr_Only) &&
	    (flags & MARKUP))
	    element_open(ho, "em");
	else if (style == word_Strong &&
	    (attraux(w->aux) == attr_First ||
	     attraux(w->aux) == attr_Only) &&
	    (flags & MARKUP))
	    element_open(ho, "strong");
	else if ((style == word_Code || style == word_WeakCode) &&
		 (attraux(w->aux) == attr_First ||
		  attraux(w->aux) == attr_Only) &&
		 (flags & MARKUP))
	    element_open(ho, "code");

	if (type == word_WhiteSpace)
	    html_text(ho, L" ");
	else if (type == word_Quote) {
	    if (quoteaux(w->aux) == quote_Open)
		html_text(ho, cfg->lquote);
	    else
		html_text(ho, cfg->rquote);
	} else {
	    if (!w->alt || cvt_ok(ho->restrict_charset, w->text))
		html_text_nbsp(ho, w->text);
	    else
		html_words(ho, w->alt, flags, file, keywords, cfg);
	}

	if (style == word_Emph &&
	    (attraux(w->aux) == attr_Last ||
	     attraux(w->aux) == attr_Only) &&
	    (flags & MARKUP))
	    element_close(ho, "em");
	else if (style == word_Strong &&
	    (attraux(w->aux) == attr_Last ||
	     attraux(w->aux) == attr_Only) &&
	    (flags & MARKUP))
	    element_close(ho, "strong");
	else if ((style == word_Code || style == word_WeakCode) &&
		 (attraux(w->aux) == attr_Last ||
		  attraux(w->aux) == attr_Only) &&
		 (flags & MARKUP))
	    element_close(ho, "code");

	break;
    }
}

static void html_codepara(htmloutput *ho, word *words)
{
    element_open(ho, "pre");
    element_open(ho, "code");
    for (; words; words = words->next) if (words->type == word_WeakCode) {
	char *open_tag;
	wchar_t *t, *e;

	t = words->text;
	if (words->next && words->next->type == word_Emph) {
	    e = words->next->text;
	    words = words->next;
	} else
	    e = NULL;

	while (e && *e && *t) {
	    int n;
	    int ec = *e;

	    for (n = 0; t[n] && e[n] && e[n] == ec; n++);

	    open_tag = NULL;
	    if (ec == 'i')
		open_tag = "em";
	    else if (ec == 'b')
		open_tag = "b";
	    if (open_tag)
		element_open(ho, open_tag);

	    html_text_limit(ho, t, n);

	    if (open_tag)
		element_close(ho, open_tag);

	    t += n;
	    e += n;
	}
	html_text(ho, t);
	html_nl(ho);
    }
    element_close(ho, "code");
    element_close(ho, "pre");
}

static void html_charset_cleanup(htmloutput *ho)
{
    char outbuf[256];
    int bytes;

    bytes = charset_from_unicode(NULL, NULL, outbuf, lenof(outbuf),
				 ho->charset, &ho->cstate, NULL);
    if (bytes > 0)
        ho->write(ho->write_ctx, outbuf, bytes);
}

static void return_mostly_to_neutral(htmloutput *ho)
{
    if (ho->state == HO_IN_EMPTY_TAG && is_xhtml(ho->ver)) {
        ho_string(ho, " />");
    } else if (ho->state == HO_IN_EMPTY_TAG || ho->state == HO_IN_TAG) {
        ho_string(ho, ">");
    }

    ho->state = HO_NEUTRAL;
}

static void return_to_neutral(htmloutput *ho)
{
    if (ho->state == HO_IN_TEXT) {
	html_charset_cleanup(ho);
    }

    return_mostly_to_neutral(ho);
}

static void element_open(htmloutput *ho, char const *name)
{
    return_to_neutral(ho);
    ho_string(ho, "<");
    ho_string(ho, name);
    ho->state = HO_IN_TAG;
}

static void element_close(htmloutput *ho, char const *name)
{
    return_to_neutral(ho);
    ho_string(ho, "</");
    ho_string(ho, name);
    ho_string(ho, ">");
    ho->state = HO_NEUTRAL;
}

static void element_empty(htmloutput *ho, char const *name)
{
    return_to_neutral(ho);
    ho_string(ho, "<");
    ho_string(ho, name);
    ho->state = HO_IN_EMPTY_TAG;
}

static void html_nl(htmloutput *ho)
{
    return_to_neutral(ho);
    ho_string(ho, "\n");
}

static void html_raw(htmloutput *ho, char *text)
{
    return_to_neutral(ho);
    ho_string(ho, text);
}

static void html_raw_as_attr(htmloutput *ho, char *text)
{
    assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG);
    ho_string(ho, " ");
    ho_string(ho, text);
}

static void element_attr(htmloutput *ho, char const *name, char const *value)
{
    html_charset_cleanup(ho);
    assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG);
    ho_string(ho, " ");
    ho_string(ho, name);
    ho_string(ho, "=\"");
    ho_string(ho, value);
    ho_string(ho, "\"");
}

static void element_attr_w(htmloutput *ho, char const *name,
			   wchar_t const *value)
{
    html_charset_cleanup(ho);
    ho_string(ho, " ");
    ho_string(ho, name);
    ho_string(ho, "=\"");
    html_text_limit_internal(ho, value, 0, TRUE, FALSE);
    html_charset_cleanup(ho);
    ho_string(ho, "\"");
}

static void html_text(htmloutput *ho, wchar_t const *text)
{
    return_mostly_to_neutral(ho);
    html_text_limit_internal(ho, text, 0, FALSE, FALSE);
}

static void html_text_nbsp(htmloutput *ho, wchar_t const *text)
{
    return_mostly_to_neutral(ho);
    html_text_limit_internal(ho, text, 0, FALSE, TRUE);
}

static void html_text_limit(htmloutput *ho, wchar_t const *text, int maxlen)
{
    return_mostly_to_neutral(ho);
    html_text_limit_internal(ho, text, maxlen, FALSE, FALSE);
}

static void html_text_limit_internal(htmloutput *ho, wchar_t const *text,
				     int maxlen, int quote_quotes, int nbsp)
{
    int textlen = ustrlen(text);
    char outbuf[256];
    int bytes, err;

    if (ho->hackflags & (HO_HACK_QUOTEQUOTES | HO_HACK_OMITQUOTES))
	quote_quotes = TRUE;	       /* override the input value */

    if (maxlen > 0 && textlen > maxlen)
	textlen = maxlen;
    if (ho->hacklimit >= 0) {
	if (textlen > ho->hacklimit)
	    textlen = ho->hacklimit;
	ho->hacklimit -= textlen;
    }

    while (textlen > 0) {
	/* Scan ahead for characters we really can't display in HTML. */
	int lenbefore, lenafter;
	for (lenbefore = 0; lenbefore < textlen; lenbefore++)
	    if (text[lenbefore] == L'<' ||
		text[lenbefore] == L'>' ||
		text[lenbefore] == L'&' ||
		(text[lenbefore] == L'"' && quote_quotes) ||
		(text[lenbefore] == L' ' && nbsp))
		break;
	lenafter = lenbefore;
	bytes = charset_from_unicode(&text, &lenafter, outbuf, lenof(outbuf),
				     ho->charset, &ho->cstate, &err);
	textlen -= (lenbefore - lenafter);
	if (bytes > 0)
            ho->write(ho->write_ctx, outbuf, bytes);
	if (err) {
	    /*
	     * We have encountered a character that cannot be
	     * displayed in the selected output charset. Therefore,
	     * we use an HTML numeric entity reference.
	     */
            char buf[40];
	    assert(textlen > 0);
            sprintf(buf, "&#%ld;", (long int)*text);
            ho_string(ho, buf);
	    text++, textlen--;
	} else if (lenafter == 0 && textlen > 0) {
	    /*
	     * We have encountered a character which is special to
	     * HTML.
	     */
            if (*text == L'"' && (ho->hackflags & HO_HACK_OMITQUOTES)) {
                ho_string(ho, "'");
            } else if (ho->hackflags & HO_HACK_QUOTENOTHING) {
                char c = *text;
                ho->write(ho->write_ctx, &c, 1);
            } else {
                if (*text == L'<')
                    ho_string(ho, "&lt;");
                else if (*text == L'>')
                    ho_string(ho, "&gt;");
                else if (*text == L'&')
                    ho_string(ho, "&amp;");
                else if (*text == L'"')
                    ho_string(ho, "&quot;");
                else if (*text == L' ') {
                    assert(nbsp);
                    ho_string(ho, "&nbsp;");
                } else
                    assert(!"Can't happen");
            }
	    text++, textlen--;
	}
    }
}

static void cleanup(htmloutput *ho)
{
    return_to_neutral(ho);
    ho_finish(ho);
}

static void html_href(htmloutput *ho, htmlfile *thisfile,
		      htmlfile *targetfile, char *targetfrag)
{
    rdstringc rs = { 0, 0, NULL };
    char *url;

    if (targetfile != thisfile)
	rdaddsc(&rs, targetfile->filename);
    if (targetfrag) {
	rdaddc(&rs, '#');
	rdaddsc(&rs, targetfrag);
    }
    url = rs.text;

    element_open(ho, "a");
    element_attr(ho, "href", url);
    sfree(url);
}

static void html_fragment(htmloutput *ho, char const *fragment)
{
    element_open(ho, "a");
    element_attr(ho, "name", fragment);
    if (is_xhtml(ho->ver))
	element_attr(ho, "id", fragment);
    element_close(ho, "a");
}

static char *html_format(paragraph *p, char *template_string)
{
    char *c, *t;
    word *w;
    wchar_t *ws, wsbuf[2];
    rdstringc rs = { 0, 0, NULL };

    t = template_string;
    while (*t) {
	if (*t == '%' && t[1]) {
	    int fmt;

	    t++;
	    fmt = *t++;

	    if (fmt == '%') {
		rdaddc(&rs, fmt);
		continue;
	    }

	    w = NULL;
	    ws = NULL;

	    if (p->kwtext && fmt == 'n')
		w = p->kwtext;
	    else if (p->kwtext2 && fmt == 'b') {
		/*
		 * HTML fragment names must start with a letter, so
		 * simply `1.2.3' is not adequate. In this case I'm
		 * going to cheat slightly by prepending the first
		 * character of the first word of kwtext, so that
		 * we get `C1' for chapter 1, `S2.3' for section
		 * 2.3 etc.
		 */
		if (p->kwtext && p->kwtext->text[0]) {
		    ws = wsbuf;
		    wsbuf[1] = '\0';
		    wsbuf[0] = p->kwtext->text[0];
		}
		w = p->kwtext2;
	    } else if (p->keyword && *p->keyword && fmt == 'k')
		ws = p->keyword;
	    else
		/* %N comes here; also failure cases of other fmts */
		w = p->words;

	    if (ws) {
		c = utoa_dup(ws, CS_ASCII);
		rdaddsc(&rs,c);
		sfree(c);
	    }

	    while (w) {
		if (removeattr(w->type) == word_Normal) {
		    c = utoa_dup(w->text, CS_ASCII);
		    rdaddsc(&rs,c);
		    sfree(c);
		}
		w = w->next;
	    }
	} else {
	    rdaddc(&rs, *t++);
	}
    }

    return rdtrimc(&rs);
}

static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file,
				    char *text)
{
    /*
     * The HTML 4 spec's strictest definition of fragment names (<a
     * name> and "id" attributes) says that they `must begin with a
     * letter and may be followed by any number of letters, digits,
     * hyphens, underscores, colons, and periods'.
     * 
     * So here we unceremoniously rip out any characters not
     * conforming to this limitation.
     */
    char *p = text, *q = text;

    while (*p && !((*p>='A' && *p<='Z') || (*p>='a' && *p<='z')))
	p++;
    if ((*q++ = *p++) != '\0') {
	while (*p) {
	    if ((*p>='A' && *p<='Z') ||
		(*p>='a' && *p<='z') ||
		(*p>='0' && *p<='9') ||
		*p=='-' || *p=='_' || *p==':' || *p=='.')
		*q++ = *p;
	    p++;
	}

	*q = '\0';
    }

    /* If there's nothing left, make something valid up */
    if (!*text) {
	static const char anonfrag[] = "anon";
	text = sresize(text, lenof(anonfrag), char);
	strcpy(text, anonfrag);
    }

    /*
     * Now we check for clashes with other fragment names, and
     * adjust this one if necessary by appending a hyphen followed
     * by a number.
     */
    {
	htmlfragment *frag = snew(htmlfragment);
	int len = 0;		       /* >0 indicates we have resized */
	int suffix = 1;

	frag->file = file;
	frag->fragment = text;

	while (add234(files->frags, frag) != frag) {
	    if (!len) {
		len = strlen(text);
		frag->fragment = text = sresize(text, len+20, char);
	    }

	    sprintf(text + len, "-%d", ++suffix);
	}
    }

    return text;
}

static char *html_sanitise_filename(htmlfilelist *files, char *text)
{
    /*
     * Unceremoniously rip out any character that might cause
     * difficulty in some filesystem or another, or be otherwise
     * inconvenient.
     * 
     * That doesn't leave much punctuation. I permit alphanumerics
     * and +-.=_ only.
     */
    char *p = text, *q = text;

    while (*p) {
	if ((*p>='A' && *p<='Z') ||
	    (*p>='a' && *p<='z') ||
	    (*p>='0' && *p<='9') ||
	    *p=='-' || *p=='_' || *p=='+' || *p=='.' || *p=='=')
	    *q++ = *p;
	p++;
    }
    *q = '\0';

    /* If there's nothing left, make something valid up */
    if (!*text) {
	static const char anonfrag[] = "anon.html";
	text = sresize(text, lenof(anonfrag), char);
	strcpy(text, anonfrag);
    }

    /*
     * Now we check for clashes with other filenames, and adjust
     * this one if necessary by appending a hyphen followed by a
     * number just before the file extension (if any).
     */
    {
	int len, extpos;
	int suffix = 1;

	p = NULL;

	while (find234(files->files, text, NULL)) {
	    if (!p) {
		len = strlen(text);
		p = text;
		text = snewn(len+20, char);

		for (extpos = len; extpos > 0 && p[extpos-1] != '.'; extpos--);
		if (extpos > 0)
		    extpos--;
		else
		    extpos = len;
	    }

	    sprintf(text, "%.*s-%d%s", extpos, p, ++suffix, p+extpos);
	}

	if (p)
	    sfree(p);
    }

    return text;
}

static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s,
				htmlfile *thisfile, keywordlist *keywords,
				htmlconfig *cfg)
{
    if (ho->contents_level >= depth && ho->contents_level > 0) {
	element_close(ho, "li");
	html_nl(ho);
    }

    while (ho->contents_level > depth) {
	element_close(ho, "ul");
	ho->contents_level--;
	if (ho->contents_level > 0) {
	    element_close(ho, "li");
	}
	html_nl(ho);
    }

    while (ho->contents_level < depth) {
	html_nl(ho);
	element_open(ho, "ul");
	html_nl(ho);
	ho->contents_level++;
    }

    if (!s)
	return;

    element_open(ho, "li");
    html_href(ho, thisfile, s->file, s->fragments[0]);
    html_section_title(ho, s, thisfile, keywords, cfg, FALSE);
    element_close(ho, "a");
    /* <li> will be closed by a later invocation */
}

static void html_section_title(htmloutput *ho, htmlsect *s, htmlfile *thisfile,
			       keywordlist *keywords, htmlconfig *cfg,
			       int real)
{
    if (s->title) {
	sectlevel *sl;
	word *number;
	int depth = heading_depth(s->title);

	if (depth < 0)
	    sl = NULL;
	else if (depth == 0)
	    sl = &cfg->achapter;
	else if (depth <= cfg->nasect)
	    sl = &cfg->asect[depth-1];
	else
	    sl = &cfg->asect[cfg->nasect-1];

	if (!sl || !sl->number_at_all)
	    number = NULL;
	else if (sl->just_numbers)
	    number = s->title->kwtext2;
	else
	    number = s->title->kwtext;

	if (number) {
	    html_words(ho, number, MARKUP,
		       thisfile, keywords, cfg);
	    html_text(ho, sl->number_suffix);
	}

	html_words(ho, s->title->words, real ? ALL : MARKUP,
		   thisfile, keywords, cfg);
    } else {
	assert(s->type != NORMAL);
	/*
	 * If we're printing the full document title for _real_ and
	 * there isn't one, we don't want to print `Preamble' at
	 * the top of what ought to just be some text. If we need
	 * it in any other context such as TOCs, we need to print
	 * `Preamble'.
	 */
	if (s->type == TOP && !real)
	    html_text(ho, cfg->preamble_text);
	else if (s->type == INDEX)
	    html_text(ho, cfg->index_text);
    }
}