This file is indexed.

/usr/share/pyshared/HTMLgen/HTMLgen.py is in python-htmlgen 2.2.2-12ubuntu3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
#'$Id: HTMLgen.py,v 2.5 1999/04/20 03:29:14 friedric Exp friedric $'

# COPYRIGHT (C) 1996-9  ROBIN FRIEDRICH  email:Robin.Friedrich@pdq.net
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear in
# supporting documentation.
# THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

"""A class library for the generation of HTML documents.

Each HTML tag type has a supporting class which is responsible for
emitting itself as valid HTML formatted text. An attempt is made to
provide classes for newer HTML 3.2 and proposed tag elements.  The
definitive reference for HTML tag elements can be found at
[W3C].  Also, I used the HTML book by Musciano and
Kennedy from [O Reilly] (2nd. Ed.) as the guiding reference.

The Document classes are container objects which act as a focal point
to populate all the contents of a particular web page. It also can
enforce consistent document formating according to the guidelines from
the [Yale Web Style Manual].

Features include customization of document template graphics / colors
through use of resource files, minimizing the need for modifying or
subclassing from the module source code. Support for tables, frames,
forms (persistent and otherwise) and client-side imagemaps are included.

A newer implementation for the Table support is now included,
TableLite().  In support of this there are new tag classes TD, TH, TR
and Caption.  These class instances can be assembled in any way to
populate the TableLite container object. 

.. [W3C] http://www.W3.org/TR/REC-html32.html
.. [O Reilly] http://www.oreilly.com/catalog/html3/index.html
.. [Yale Web Style Manual] http://info.med.yale.edu/caim/manual/contents.html
"""

import string, re, time, os
import UserList, copy
from imgsize import imgsize

__author__ = 'Robin Friedrich   friedrich@pythonpros.com'
__version__ = '2.2.2'

StringType = type('s')
IntType    = type(3)
ListType   = type([1])
TupleType  = type((1,2))
InstanceType = type(UserList.UserList())
CONTYPE = 'Content-Type: text/html\n\n'
DOCTYPE = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">\n<HTML>\n'
PRINTECHO = 1


#################
# CLASS LIBRARY #
#################


#======= NEW CLASS STRUCTURE ============
# HTMLgen 1       HTMLgen 2
# --------------- ------------------
# Document        SeriesDocument
# MinimalDocument SimpleDocument
#                 BasicDocument (base class)
#                 TemplateDocument

class BasicDocument:
    """Base class to define an HTML document.

    Non-keyword arguments are taken as the initial contents for this object.

    Keyword arguments:
        title -- HTML TITLE attribute for document
        bgcolor -- background color expressed in hex-triplet or names from HTMLcolors.
        background -- background image filename
        cgi -- flag to indicate if this is used in CGI context (1 if it is)
        textcolor -- color to use for normal text
        linkcolor -- color to use for hyperlinks
        vlinkcolor -- color to use for visited hyperlinks
        alinkcolor -- color to use when hyperlink is active
    """
    title = ''
    cgi = None
    bgcolor = None
    background = None
    textcolor = None
    linkcolor = None
    vlinkcolor = None
    alinkcolor = None
    
    def __init__(self, *args, **kw):
        self.contents = list(args)
        for name, value in kw.items():
            setattr(self, name, value)

    def __str__(self):
        s = []
        if self.cgi:
            s.append('Content-Type: text/html\n\n' + DOCTYPE)
        else:
            s.append(DOCTYPE)
        s.append('\n<!-- This file generated using Python HTMLgen module. -->\n')

        # build the HEAD and BODY tags
        s.append(self.html_head())
        s.append(self.html_body_tag())

        # DOCUMENT CONTENT SECTION and FOOTER added on
        bodystring = '%s\n' * len(self.contents)
        s.append((bodystring % tuple(self.contents)))

        # CLOSE the document
        s.append('\n</BODY> </HTML>\n')
        return string.join(s, '')

    def html_head(self):
        """Generate the HEAD, TITLE and BODY tags.
        """
        return '<HEAD>\n  <META NAME="GENERATOR" CONTENT="HTMLgen %s">\n\
        <TITLE>%s</TITLE> </HEAD>\n' % (__version__, self.title)
    
    def html_body_tag(self):
        """Return BODY tag with attributes.
        """
        s = ['<BODY']
        if self.bgcolor:    s.append(' BGCOLOR="%s"' % self.bgcolor)
        if self.background: s.append(' BACKGROUND="%s"' % self.background)
        if self.textcolor:  s.append(' TEXT="%s"' % self.textcolor)
        if self.linkcolor:  s.append(' LINK="%s"' % self.linkcolor)
        if self.vlinkcolor: s.append(' VLINK="%s"' % self.vlinkcolor)
        if self.alinkcolor: s.append(' ALINK="%s"' % self.alinkcolor)
        s.append('>\n')
        return string.join(s, '')

    def append_file(self, filename, marker_function = None):
        """Add the contents of a file to the document.

        filename -- the filename of the file to be read [string]
        marker_function -- a callable object which the text read from
          the file will be passed through before being added to the
          document.
        """
        f = open(mpath(filename), 'r')
        if marker_function:
            self.append(marker_function(f.read()))
        else:
            self.append(f.read())
        f.close()
        
    def append(self, *items):
        """Add content to the Document object.
        
        Arg *items* can be plain text or objects; multiple arguments supported.
        """
        for item in items:
            self.contents.append(item)

    def prepend(self, *items):
        """Add content to the beginning of the Document object.
        
        Arg *items* can be plain text or objects; multiple arguments supported.
        """
        for item in items:
            self.contents.insert(0, item)

    def copy(self):
        """Return a complete copy of the current Document object.
        """
        return copy.deepcopy(self)

    def write(self, filename = None):
        """Emit the Document HTML to a file or standard output.
        
        In Unix you can use environment variables in filenames.
        Will print to stdout if no argument.
        """
        if filename:
            f = open(mpath(filename), 'w')
            f.write(str(self))
            f.close()
            if PRINTECHO: print 'wrote: "'+filename+'"'
        else:
            import sys
            sys.stdout.write(str(self))

class FramesetDocument(BasicDocument):
    """A minimal document suitable for entering Framesets.

    Arguments are for contents **NOT** a document resource file.

    Keyword Parameters
    
        title -- string to be used as the document title.
        base  -- object of the Base class
        meta  -- object of the Meta class
        cgi   -- if non zero will issue a mime type of text/html
        script -- a single or list of Script objects to be included in the <HEAD>
    
    No <body> markup. Instead add Frameset(s) with the constructor or
    append method.
    """
    base = None
    meta = None
    cgi = None
    script = None

    def __str__(self):
        s = []
        if self.cgi:
            s.append('Content-Type: text/html\n\n' + DOCTYPE)
        else:
            s.append(DOCTYPE)
        s.append('\n<!-- This file generated using Python HTMLgen module. -->\n')

        # build the HEAD tag
        s.append(self.html_head())

        # DOCUMENT CONTENT SECTION
        bodystring = '%s\n' * len(self.contents)
        s.append((bodystring % tuple(self.contents)))

        # CLOSE the document
        s.append('\n</HTML>')        
        return string.join(s, '')


class SimpleDocument(BasicDocument):
    """Supports all features of a self contained document.

    This includes support for CSS1, meta and base tags, and embedded
    scripts.

    First constructor argument is resource file containing document
    attribute settings.
    """
    base = None
    style = None
    stylesheet = None
    meta = None
    onLoad = None
    onUnload = None
    script = None
    
    def __init__(self, resource = None, **kw):
        self.contents = []
        # Read attributes from resource file into instance namespace
        if resource: execfile(mpath(resource), self.__dict__)
        for name, value in kw.items():
            setattr(self, name, value)

    def __str__(self):
        s = []
        if self.cgi:
            s.append('Content-Type: text/html\n\n' + DOCTYPE)
        else:
            s.append(DOCTYPE)
        s.append('\n<!-- This file generated using Python HTMLgen module. -->\n')

        # build the HEAD and BODY tags
        s.append(self.html_head())
        s.append(self.html_body_tag())

        # DOCUMENT CONTENT SECTION and FOOTER added on
        bodystring = '%s\n' * len(self.contents)
        s.append((bodystring % tuple(self.contents)))

        s.append('\n</BODY> </HTML>\n') # CLOSE the document
        return string.join(s, '')

    def html_head(self):
        """Generate the HEAD TITLE and BODY tags.
        """
        s = ['<HEAD>\n  <META NAME="GENERATOR" CONTENT="HTMLgen %s">\n\
        <TITLE>%s</TITLE>\n' % (__version__, self.title)]
        if self.meta: s.append(str(self.meta))
        if self.base: s.append(str(self.base))
        if self.stylesheet:
            s.append('\n <LINK rel=stylesheet href="%s" type="text/css" title="%s">\n' \
                     % (self.stylesheet, self.stylesheet))
        if self.style:
            s.append('\n<STYLE>\n<!--\n%s\n-->\n</style>\n' % self.style)
        if self.script: # for javascripts
            if type(self.script) in (TupleType, ListType):
                for script in self.script:
                    s.append(str(script))
            else:
                s.append(str(self.script))
        s.append('</HEAD>\n')
        return string.join(s, '')

    def html_body_tag(self):
        """Return BODY tag with attributes.
        """
        s = ['<BODY']
        if self.bgcolor:    s.append(' BGCOLOR="%s"' % self.bgcolor)
        if self.background: s.append(' BACKGROUND="%s"' % self.background)
        if self.textcolor:  s.append(' TEXT="%s"' % self.textcolor)
        if self.linkcolor:  s.append(' LINK="%s"' % self.linkcolor)
        if self.vlinkcolor: s.append(' VLINK="%s"' % self.vlinkcolor)
        if self.alinkcolor: s.append(' ALINK="%s"' % self.alinkcolor)
        if self.onLoad:     s.append(' onLoad="%s"' % self.onLoad)
        if self.onUnload:   s.append(' onUnload="%s"' % self.onUnload)
        s.append('>\n')
        return string.join(s, '')


class SeriesDocument(SimpleDocument):
    """Primary container class for an HTML document as part of a series.

    Formerly known as Document().

    Navigation mechanisms are provided.

    Single optional string argument for the path to a resource file
    used to specify document parameters. This helps minimize the need
    for subclassing from this class. Keyword parameters may be used
    for any of the following class attributes. See *HTMLtest.py* for
    example usage.

    Class instance attributes and keyword arguments
    
        base -- object of the Base class
        meta -- object of the Meta class
        cgi  -- if non zero will issue a mime type of text/html
        logo -- ('filename', width, height)  All images are specified
                 with a tuple of string, int, int. If the size of the
                 graphic is unknown, use 0, 0.  This one is the little
                 graphic on the footer of each page.
        banner -- ('filename', width, height) Banner graphic at
                 the top of page. Can also be set to a string filename
                 or an Image object. Can be autosized if it's a GIF.
        title --  string to be used as the document title.
        subtitle -- string to be used as the document subtitle.
                 If non-nil, this string will be used for the doc title
                 instead of title.
        author -- String used in the copyright notice
        email -- Email address for feedback mailto: tag
        zone -- string used to label the time zone if datetime
                 is used. By default not used.
        bgcolor -- Color string (can use variables from
                 HTMLcolors.py)
        background -- string filename of a graphic used as the
                 doc background.
        textcolor -- Color string used for text.  (can use
                 variables from HTMLcolors.py)
        linkcolor -- Color string used for hyperlinked text. 
        vlinkcolor -- Color string used for visited hypertext.
        alinkcolor -- Color string used for active hypertext.
        place_nav_buttons -- Flag to enable/disable the use of
                 navigation buttons.
                 Default is on. Set to 0 to disable.
        blank -- Image tuple for the transparent spacer gif
        prev -- Image tuple for the Previous Page button
        next -- Image tuple for the Next Page button
        top -- Image tuple for the Top of Manual button
        home -- Image tuple for the site Home Page button
        goprev -- URL string for the prev button
        gonext -- URL string for the next button
        gotop  -- URL string for the top button
        gohome -- URL string for the home button
        script -- a single or list of Script objects to be included in the <HEAD>
        onLoad -- Script, which is executed when the document is loaded
        onUnload -- Script, which is executed when the document is unloaded
    """
    subtitle = None
    banner = ('/image/banner.gif', 472, 30)
    logo = ('/image/logo.gif', 36, 35)
    author = 'Micky Mouse'
    email = 'micky@disney.com'
    zone = ' Central US'
    place_nav_buttons = 'yes'
    blank = ('../image/blank.gif', 71, 19)
    prev = ('../image/BTN_PrevPage.gif', 71, 19)
    next = ('../image/BTN_NextPage.gif', 71, 19)
    top = ('../image/BTN_ManualTop.gif', 74, 19)
    home = ('../image/BTN_HomePage.gif', 74, 19)
    goprev = None # URLs for above navigation buttons
    gonext = None
    gotop  = None
    gohome = None

    def __str__(self):
        s = []
        if self.cgi:
            s.append(CONTYPE + DOCTYPE)
        else:
            s.append(DOCTYPE)
        s.append('\n<!-- This file generated using Python HTMLgen module. -->\n')
        # build the HEAD and BODY tags
        s.append(self.html_head())
        s.append(self.html_body_tag())
        # HEADER SECTION
        s.append(self.header())

        # DOCUMENT CONTENT SECTION and FOOTER added on
        bodystring = '%s\n' * len(self.contents)
        s.append((bodystring % tuple(self.contents)))
        
        s.append(self.footer())
        s.append('\n</BODY> </HTML>\n') # CLOSE the document
        return string.join(s, '')
    
    def header(self):
        """Generate the standard header markups.
        """
        # HEADER SECTION - overload this if you don't like mine.
        s = []
        if self.banner:
            bannertype = type(self.banner)
            if bannertype in (TupleType, StringType):
                s.append(str(Image(self.banner, border=0)) + '<BR>\n')
            elif bannertype == InstanceType:
                s.append(str(self.banner) + '<BR>\n')
            else:
                raise TypeError, 'banner must be either a tuple, instance, or string.'
        if self.place_nav_buttons:
            s.append(self.nav_buttons())
        s.append(str(Heading(3,self.title)))
        if self.subtitle:
            s.append('<H2>%s</H2>\n' % self.subtitle)
        s.append('<HR>\n\n')
        return string.join(s, '')

    def nav_buttons(self):
        """Generate hyperlinked navigation buttons.

        If a self.go* attribute is null that corresponding button is
        replaced with a transparent gif to properly space the remaining
        buttons.
        """
        s = []
        if self.goprev: # place an image button for previous page
            btn = Image(self.prev, border=0, alt='Previous')
            link = Href(self.goprev, str(btn))
            s.append(str(link) + ' \n')
        else: # place a blank gif as spacer
            #btn = Image(self.blank)
            s.append('<span style="width: 60px"></span> \n')
        if self.gonext: # place an image button for next page
            btn = Image(self.next, border=0, alt='Next')
            link = Href(self.gonext, str(btn))
            s.append(str(link) + ' \n')
        else: # place a blank gif as spacer
            btn = Image(self.blank)
            s.append(str(btn) + ' \n')
        if self.gotop: # place an image button for top of manual page
            btn = Image(self.top, border=0, alt='Top of Manual')
            link = Href(self.gotop, str(btn))
            s.append(str(link) + ' \n')
        else: # place a blank gif as spacer
            btn = Image(self.blank)
            s.append(str(btn) + ' \n')
        if self.gohome: # place an image button for site home page
            btn = Image(self.home, border=0, alt='Home Page')
            link = Href(self.gohome, str(btn))
            s.append(str(link) + ' \n')
        else: # place a blank gif as spacer
            btn = Image(self.blank)
            s.append(str(btn) + ' \n')
        return string.join(s, '')
 
    def footer(self):
        """Generate the standard footer markups.
        """
        # FOOTER SECTION - overload this if you don't like mine.
        t = time.localtime(time.time())
        #self.datetime = time.strftime("%c %Z", t)    #not available in JPython
        self.datetime = time.asctime(t)
        #self.date = time.strftime("%A %B %d, %Y", t)
        x = string.split(self.datetime)
        self.date = x[0] + ' ' + x[1] + ' ' + x[2] + ', ' + x[4]
        s =  ['\n<P><HR>\n']
        if self.place_nav_buttons:
            s.append(self.nav_buttons())
        s.append('<BR>' + str(Image(self.logo, align='bottom')))
        s.append('\n<FONT SIZE="-1"><P>Copyright &#169 %s<BR>All Rights Reserved<BR>\n' \
            % self.author)
        s.append('\nComments to author: ' + str(MailTo(self.email)) )
        s.append('<br>\nGenerated: %s <BR>' % self.date) # can use self.datetime here instead
        s.append('<hr>\n</FONT>')
        return string.join(s, '')

# Aliases for backward compatability with HTMLgen 1.2
Document = SeriesDocument
MinimalDocument = SimpleDocument


class StringTemplate:
    """Generate documents based on a template and a substitution mapping.

    Must use Python 1.5 or newer. Uses re and the get method on dictionaries.

    Usage:
       T = TemplateDocument('Xfile')
       T.substitutions = {'month': ObjectY, 'town': 'Scarborough'}
       T.write('Maine.html')

    A dictionary, or object that behaves like a dictionary, is assigned to the
    *substitutions* attribute which has symbols as keys to objects. Upon every
    occurance of these symbols surrounded by braces {} in the source template,
    the corresponding value is converted to a string and substituted in the output.

    For example, source text which looks like:
     I lost my heart at {town} Fair.
    becomes:
     I lost my heart at Scarborough Fair.

    Symbols in braces which do not correspond to a key in the dictionary remain
    unchanged.

    An optional third argument to the class is a list or two strings to be
    used as the delimiters instead of { } braces. They must be of the same
    length; for example ['##+', '##'] is invalid.
    """
    def __init__(self, template, substitutions=None, **kw):
        self.delimiters = ['{', '}']
        self.__dict__.update(kw)
        if len(self.delimiters) != 2:
            raise ValueError("delimiter argument must be a pair of strings")
        self.delimiter_width = len(self.delimiters[0])
        delimiters = map(re.escape, self.delimiters)
        self.subpatstr = delimiters[0] + "[\w_]+" + delimiters[1]
        self.subpat = re.compile(self.subpatstr)
        self.substitutions = substitutions or {}
        self.set_template(template)

    def set_template(self, template):
        self.source = template
    
    def keys(self):
        return self.substitutions.keys()

    def __setitem__(self, name, value):
        self.substitutions[name] = value
        
    def __getitem__(self, name):
        return self.substitutions[name]
      
    def __str__(self):
        return self._sub(self.source)

    def _sub(self, source, subs=None):
        """Perform source text substitutions.

        *source* string containing template source text
        *subs* mapping of symbols to replacement values
        """
        substitutions = subs or self.substitutions
        dw = self.delimiter_width
        i = 0
        output = []
        matched = self.subpat.search(source[i:])
        while matched:
            a, b = matched.span()
            output.append(source[i:i+a])
            # using the new get method for dicts in 1.5
            output.append(str(substitutions.get(
                   source[i+a+dw:i+b-dw], source[i+a:i+b])))
            i = i + b
            matched = self.subpat.search(source[i:])
        else:
            output.append(source[i:])
        return string.join(output, '')
    
    def write(self, filename = None):
        """Emit the Document HTML to a file or standard output.
        
        Will not overwrite file is it exists and is textually the same.
        In Unix you can use environment variables in filenames.
        Will print to stdout if no argument given.
        """
        if filename:
            filename = mpath(filename)
            if os.path.exists(filename):
                s = str(self)
                if compare_s2f(s, filename):
                    f = open(filename, 'w')
                    f.write(str(self))
                    f.close()
                    if PRINTECHO: print 'wrote: "'+filename+'"'
                else:
                    if PRINTECHO: print 'file unchanged: "'+filename+'"'
            else:
                f = open(filename, 'w')
                f.write(str(self))
                f.close()
                if PRINTECHO: print 'wrote: "'+filename+'"'
        else:
            import sys
            sys.stdout.write(str(self))

class TemplateDocument(StringTemplate):
    """Generate documents based on a template and a substitution mapping.

    Must use Python 1.5 or newer. Uses re and the get method on dictionaries.

    Usage:
       T = TemplateDocument('Xfile')
       T.substitutions = {'month': ObjectY, 'town': 'Scarborough'}
       T.write('Maine.html')

    A dictionary, or object that behaves like a dictionary, is assigned to the
    *substitutions* attribute which has symbols as keys to objects. Upon every
    occurance of these symbols surrounded by braces {} in the source template,
    the corresponding value is converted to a string and substituted in the output.

    For example, source text which looks like:
     I lost my heart at {town} Fair.
    becomes:
     I lost my heart at Scarborough Fair.

    Symbols in braces which do not correspond to a key in the dictionary remain
    unchanged.

    An optional third argument to the class is a list or two strings to be
    used as the delimiters instead of { } braces. They must be of the same
    length; for example ['##+', '##'] is invalid.
    """
    def set_template(self, template):
        f = open(mpath(template))
        self.source = f.read()
        f.close()

class AutoStringTemplate(StringTemplate):
    marker_begin = '<!--{%s}Begin-->'
    marker_end   = '<!--{%s}End-->'
    R = re.compile(r"<!--{(?P<key>[\w_]+)}Begin-->(?P<text>.*?)<!--{\1}End-->", re.S)
    
    def set_template(self, template):
        """Set template string and normalize by extracting comment tokens.
        """
        self.source = template
        self.extract_template()
        
    def extract_template(self, source=None):
        """Convert comment-marked regions to a regular {tokens}.
        
        Updates the substitution dictionary with the text from the region.
        """
        source = source or self.source
        a = 0
        newsubs = {}
        newtemplate = []
        d1, d2 = self.delimiters
        while 1:
            m = self.R.search(source, a)
            if m:
                start, end = m.span()
                newtemplate.append(source[a:start])
                a = end
                newsubs[m.group('key')] = m.group('text')
                newtemplate.append(d1+m.group('key')+d2)
            else:
                newtemplate.append(source[a:])
                break
        self.source = string.join(newtemplate, '')
        self.substitutions.update(newsubs)

    def _sub(self, source, subs=None):
        """Perform source text substitutions.

        *source* string containing template source text
        *subs* mapping of symbols to replacement values
        """
        substitutions = subs or self.substitutions
        dw = self.delimiter_width
        i = 0
        output = []
        matched = self.subpat.search(source[i:])
        while matched:
            a, b = matched.span()
            output.append(source[i:i+a])
            #implant comments to mark the location of the tokens
            output.append(self.marker_begin % source[i+a+dw:i+b-dw])
            # using the new get method for dicts in 1.5
            output.append(str(substitutions.get(
                   source[i+a+dw:i+b-dw], source[i+a:i+b])))
            output.append(self.marker_end % source[i+a+dw:i+b-dw])
            i = i + b
            matched = self.subpat.search(source[i:])
        else:
            output.append(source[i:])
        return string.join(output, '')

class AutoTemplateDocument(AutoStringTemplate):
    """Generate documents based on a template and a substitution mapping.
    
    The primary difference between AutoTemplateDocument and TemplateDocument
    is that the Auto version can read through an HTML file previously
    generated with this class and identify the regions of text that were
    substituted. It then extracts that text into the substitution dictionary
    and can then be updated. The intent is to eliminate the need to 
    maintain separate content files for insertion into templates. The HTML
    output file can double as a template for future use.
    Output from this class have their filled regions marked by comments:
        ...gets <!--{wz}Begin-->glued,<!--{wz}End--> in place...
    Which came from ...gets {wz} in place... in old style template syntax.
    
    AutoTemplateDocument is a functional superset of TemplateDocument and should
    be compatible.

    Usage:
       T = AutoTemplateDocument('Maine.html')
       T.substitutions = {'month': ObjectY, 'town': 'Scarborough'}
       or
       T['month'] = ObjectY ; T['town'] = 'Scarborough'
       T.write('Maine.html')
    
    A dictionary, or object that behaves like a dictionary, is assigned to the
    *substitutions* attribute which has symbols as keys to objects. Upon every
    occurance of these symbols surrounded by braces {} in the source template,
    the corresponding value is converted to a string and substituted in the output.

    For example, source text which looks like:
     I lost my heart at {town} Fair.
    becomes:
     I lost my heart at Scarborough Fair.

    Symbols in braces which do not correspond to a key in the dictionary remain
    unchanged.

    An optional third argument to the class is a list or two strings to be
    used as the delimiters instead of { } braces. They must be of the same
    length; for example ['##+', '##'] is invalid.
    """
    def set_template(self, template):
        f = open(mpath(template))
        self.source = f.read()
        f.close()


class Container:
    """A holder for a list of HTMLgen objects.    
    """

    def __init__(self, *args, **kw):
        self.contents = list(args)
        for name, value in kw.items():
            setattr(self, name, value)

    def __str__(self):
        bodystring = '%s\n' * len(self.contents)
        return bodystring % tuple(self.contents)

    def __add__(self, other):
        new = self.__class__()
        new.contents = self.contents + other.contents
        return new
        
    def append_file(self, filename, marker_function = None):
        """Add the contents of a file to the document.

        filename -- the filename of the file to be read [string]
        marker_function -- a callable object which the text read from
          the file will be passed through before being added to the
          document.
        """
        f = open(mpath(filename), 'r')
        if marker_function:
            self.append(marker_function(f.read()))
        else:
            self.append(f.read())
        f.close()
        
    def append(self, *items):
        """Add content to the Document object.
        
        Arg *items* can be plain text or objects; multiple arguments supported.
        """
        for item in items:
            self.contents.append(item)

    def prepend(self, *items):
        """Add content to the beginning of the Document object.
        
        Arg *items* can be plain text or objects; multiple arguments supported.
        """
        li = len(items)
        for i in range(li-1, 0, -1):
            self.contents.insert(0, items[i])

    def copy(self):
        """Return a complete copy of the current Container object.
        """
        return copy.deepcopy(self)

#===================
        
class Meta:
    """Set document Meta-information.

    The META element is used within the HEAD element to embed
    document meta-information not defined by other HTML elements.
    
    Keywords supported
 
        name  -- NAME element attribute (default: 'keywords')
        equiv  -- will map to the HTTP-EQUIV attribute
        content -- mandatory attribute (default: 'python,HTMLgen')
        url -- URL naturally
    
    Example:

        Meta( name='keywords', content='eggs,spam,beans' )
    """
    def __init__(self, **kw):
        self.equiv = 'keywords'
        self.name  = ''
        self.content = 'python,HTMLgen'
        self.url = ''
        for item in kw.keys():
            self.__dict__[item] = kw[item]

    def __str__(self):
        s = ['<META']
        if self.equiv: s.append(' HTTP-EQUIV="%s"' % self.equiv)
        if self.name:  s.append(' NAME="%s"' % self.name)
        if self.content: s.append(' CONTENT="%s"' % self.content)
        if self.url: s.append(' URL="%s"' % self.url)
        s.append('>\n')
        return string.join(s, '')

##### Client-side Imagemap Support #####

class Map:
    """Used to name and describe a client-side image map.

    The *areas* argument is a list of Area objects.
    Keyword arg is supported for *name*, which defines the map name
    to be used with the usemap attribute of an Image class instance.
    """
    def __init__(self, areas = None, **kw):
        self.areas = areas or []
        self.name = ''
        for item in kw.keys():
            self.__dict__[item] = kw[item]

    def __str__(self):
        s = ['\n<MAP NAME="%s">\n' % self.name]
        for area in self.areas:
            s.append(str(area))
        s.append('</MAP>\n')
        return string.join(s, '')


class Href:
    """Generate a hyperlink.

    Argument 1 is the URL and argument 2 is the hyperlink text.

    Keyword arguments

        target -- is an optional target symbol 
        onClick --  is the script-code which is executed when link is clicked.
        onMouseOver -- the script-code which is executed when the mouse
                       moves over the link.
        onMouseOut -- the script-code which is executed when the mouse
                       moves off the link.
    """
    def __init__(self, url='', text='', **kw):
        self.target = None
        self.onClick = None
        self.onMouseOver = None
        self.onMouseOut = None
        self.url = url
        self.text = text
        for item in kw.keys():
            if self.__dict__.has_key(item):
                self.__dict__[item] = kw[item]
            else:
                raise KeyError, `item`+' not a valid parameter for this class.'

    def __str__(self):
        s = ['<A HREF="%s"' % self.url]
        if self.target: s.append(' TARGET="%s"' % self.target)
        if self.onClick: s.append(' onClick="%s"' % self.onClick)
        if self.onMouseOver: s.append(' onMouseOver="%s"' % self.onMouseOver)
        if self.onMouseOut: s.append(' onMouseOut="%s"' % self.onMouseOut)
        s.append('>%s</A>' % self.text)
        return string.join(s, '')

    def append(self, content):
        self.text = self.text + str(content)
        

A = HREF = Href # alias

class Name(Href):
    """Generate a named anchor.

    Arg *url* is a string or URL object,
    Arg *text* is optional string or object to be highlighted as the anchor.
    """
    def __str__(self):
        return '<A NAME="%s">%s</A>' % (self.url, self.text)

NAME = Name # alias

class MailTo:
    """A Mailto href

    First argument is an email address, optional second argument is
    the text shown as the underlined hyperlink. Default is the email
    address. Optional third argument is a Subject: for the email.
    """
    def __init__(self, address='', text=None, subject=None):
        self.address = address
        self.text = text or address
        self.subject = subject

    def __str__(self):
        if self.subject: self.address = "%s?subject=%s" % (self.address, self.subject)
        return '<A HREF="mailto:%s">%s</A>' % (self.antispam(self.address), self.text)

    def antispam(self, address):
        """Process a string with HTML encodings to defeat address spiders.
        """
        from random import choice
        buffer = map(None, address)
        for i in range(0, len(address), choice((2,3,4))):
            buffer[i] = '&#%d;' % ord(buffer[i])
        return string.join(buffer,'')

MAILTO = Mailto = MailTo # aliases

class P:
    """Just echo a <P> tag."""
    def __str__(self):
        return '\n<P>\n'

# List constructs

class List(UserList.UserList):
    """Will generate a bulleted list given a list argument.

    Now supports rendering a list into multiple columns by setting the
    *columns* attribute to a number greater than one. This is
    implemented using tables and you can also set a background color
    for the list itself by using the *bgcolor* attribute.

    Supports nested lists, i.e. lists of lists. Each time a list is
    encountered in a list it will indent those contents w.r.t. the
    prior list entry. This can continue indefinitely through nested
    lists although there are only three different bullets provided by
    the browser (typically).
    
    Optional keyword *indent* can be used to indicate whether you want
    the list to start left justified or indented. *indent=0* will make
    it left justified. The default is to indent.

    Optional keyword *type* can be set to either disk, circle, or
    square to specify what kind of symbol is used for each list item's
    bullet. (Netscape extension)
    
    Since we inherit from the UserList class any normal list
    operations work on instances of this class.  Any list contents
    will do. Each of the items will be emitted in html if they are
    themselves objects from this module.
    Aliases: UL, BulletList
    """

    I_am_a_list = 1
    tagname = 'UL'
    attrs = ('type','align','class','id','style')
    flags = ('compact',)
    columns = 1
    bgcolor = ''
    pad = '    '
    indent = 1
    def __init__(self, list = None, **kw):
        self.data = []
        self.lvl = 0
        if list:
            if type(list) == type(self.data):
                self.data[:] = list
            else:
                self.data[:] = list.data[:]
        for item in kw.keys():
            self.__dict__[string.lower(item)] = kw[item]

    def __getslice__(self, i, j):
        newlist = copy.copy(self)
        newlist.data = self.data[i:j]
        newlist.columns = 1 # don't forget that the copy will carry
                            # the old attribute value. set to 1
        return newlist

    def multi_column_table(self):
        """Return a table containing the list sliced into columns.
        """
        slices = self.column_slices(self.columns)
        table = TableLite(border=0, cellpadding=3)
        if self.bgcolor: table.bgcolor = self.bgcolor
        for begin, end in slices:
            column = TD(self[begin:end], valign='top', html_escape='no')
            table.append(column)
        return table

    def column_slices(self, columns=1):
        """Calculate a list of index pairs bounding column slices.
        """
        list_len = len(self.data)
        col_len, remainder = divmod(list_len, columns)
        if remainder: col_len = col_len + 1
        indexpairs = []
        if columns > 1:
            for column in range(columns):
                col_end = (column+1)*col_len
                if col_end < list_len:
                    indexpairs.append((column*col_len, col_end))
                else:
                    indexpairs.append((column*col_len, list_len))
        else:
            indexpairs.append((0, list_len))
        return indexpairs
        
    def __str__(self):
        if self.columns > 1: # go to the new multicolumn feature
            return str(self.multi_column_table())
        # same as before
        self.firstitem = 1
        self.s = []
        if self.indent:
            self.s.append(self.pad*self.lvl + self.start_element())
        for item in self.data: #start processing main list
            itemtype = type(item)
            if itemtype == InstanceType: 
                try: # in case it's a nested list object
                    if item.I_am_a_list:
                        itemtype = ListType
                except AttributeError:
                    pass
            if itemtype == ListType: #process the sub list
                self.sub_list(item)
            else:
                self.s.append(self.render_list_item(item))

        if self.indent: #close out this level of list
            self.s.append(self.pad*self.lvl + self.end_element())
        self.lvl = 0
        return string.join(self.s, '')

    def sub_list(self, list):
        """Recursive method for generating a subordinate list
        """
        self.lvl = self.lvl + 1
        if type(list) == InstanceType:
            try:
                if list.I_am_a_list: #render the List object
                    list.lvl = self.lvl
                    self.s.append(str(list))
            except AttributeError:
                pass
        else:
            self.s.append(self.pad*self.lvl + self.start_element())
            for item in list:
                itemtype = type(item)
                if itemtype == InstanceType:
                    try: #could be another nested List child object
                        if item.I_am_a_list:
                            itemtype = ListType
                    except AttributeError:
                        pass
                if itemtype == ListType:
                    self.sub_list(item) #recurse for sub lists
                else: # or just render it
                    self.s.append(self.render_list_item(item))
            # close out this list level
            self.s.append(self.pad*self.lvl + self.end_element())
        self.lvl = self.lvl - 1 #decrement indentation level

    def render_list_item(self, item):
        """Renders the individual list items

        Overloaded by child classes to represent other list styles.
        """
        return '%s<LI>%s</LI>\n' % (self.pad*self.lvl, item)

    def start_element(self):
        """Generic creator for the HTML element opening tag.

        Reads tagname, attrs and flags to return appropriate tag.
        """
        s = ['<' + self.tagname]
        for attr in self.attrs:
            try:
                s.append(' %s="%s"' % (attr, getattr(self, attr)))
            except AttributeError:
                pass
        for flag in self.flags:
            try:
                x = getattr(self, flag)
                s.append(' %s' % flag)
            except AttributeError:
                pass
        s.append('>\n')
        return string.join(s, '')

    def end_element(self):
        """Closes the HTML element
        """
        return '</%s>\n' % self.tagname

    def append(self, *items):
        """Append entries to the end of the list
        """
        for item in items:
            self.data.append(item)
    
UL = BulletList = List  #Aliases

class OrderedList(List):
    """Will generate a numbered list given a list arg.

    Optional keyword *type* can be used to specify whether you want
    the list items marked with: capital letters (type='A'), small
    letters (type='a'), large Roman numerals (type='I'), small Roman
    numerals (type='i'). The default is arabic numbers. The other
    types are HTML3.2 only and may not be supported by browsers yet.
    Any list contents will do. Each of the items will be emitted
    in HTML if they are themselves objects.
    """
    tagname = 'OL'
    attrs = ('type','class','id','style')

OL = NumberedList = OrderedList

class DefinitionList(List):
    """Show a series of items and item definitions.

    Arg is a list of tuple pairs:
    [(string/object,string/object),(,)...]  1st item in each pair is
    the word to be defined. It will be rendered in bold. 2nd is the
    string which will be indented to it's next-line-right. If the
    *compact* flag is set to non-empty, the definition side will be
    placed on the same line.  Example

        DefinitionList([( 4 , 'Number after 3') , ( 1 , 'Unity')] ) will emit:
        4
            Number after 3
        1
            Unity
    """
    tagname = 'DL'
    attrs = ('class','id','style')
    flags = ('compact',)
    def render_list_item(self, item):
        """Overload method to perform DT/DD markup.
        """
        return '%s<DT><B>%s</B><DD>%s\n' % (self.pad*self.lvl, item[0], item[1])

DL = DefinitionList

class ImageBulletList(List):
    """Show a list of images with adjoining text(or object).

    Arg is a list of tuple pairs: [(Image_obj, string/object),(,)...]
    Generates an inlined image then the text followed by a <BR>
    for each element.
    """
    tagname = 'UL'
    attrs = ()
    flags = ()
    def render_list_item(self, item):
        """Overload method to take first item from an item tuple and
        setting it next to the second item, using BR to separate list items.
        """
        return '%s%s %s<BR>\n' % (self.pad*self.lvl, item[0], item[1])

class NonBulletList(List):
    """Generate a raw indented list without bullet symbols.

    Arg is a list of python objects:
    """
    tagname = 'UL'
    attrs = ()
    flags = ()
    def render_list_item(self, item):
        """Overload method to take first item from an item tuple and
        setting it next to the second item, using BR to separate list items.
        """
        return '%s%s<BR>\n' % (self.pad*self.lvl, item)


####### FORM TAGS ########
class Form:
    """Define a user filled form. Uses POST method.
   
    *cgi* is the URL to the CGI processing program.  Input objects
    (any others as well) are appended to this container widget.
    
    Keywords
    
        name -- name of the form
        submit -- The Input object to be used as the submit button.
                  If none specified a Submit button will automatically
                  be appended to the form. Do not manually append your
                  submit button. HTMLgen will append it for you.
        reset  -- Input object to be used as a reset button.
        target -- set a TARGET attribute
        enctype -- specify an Encoding type.
        onSubmit -- script, which is executed, when the form is submitted
    """
    def __init__(self, cgi = None, **kw):
        self.contents = []
        self.cgi = cgi
        self.submit = Input(type='submit', name='SubmitButton', value='Send')
        self.reset = None
        self.target = None
        self.enctype = None
        self.name = None
        self.onSubmit = ''
        overlay_values(self, kw)

    def append(self, *items):
        """Append any number of items to the form container.
        """
        for item in items:
            self.contents.append(str(item))

    def __str__(self):
        s = ['\n<FORM METHOD="POST"']
        if self.cgi: s.append(' ACTION="%s"' % self.cgi)
        if self.enctype: s.append(' ENCTYPE="%s"' % self.enctype)
        if self.target: s.append(' TARGET="%s"' % self.target)
        if self.name: s.append(' NAME="%s"' % self.name)
        if self.onSubmit: s.append(' onSubmit="%s"' % self.onSubmit)
        s.append('>\n')
        s = s + self.contents
        s.append(str(self.submit))
        if self.reset: s.append(str(self.reset))
        s.append('\n</FORM>\n')
        return string.join(s, '')
        
        
def overlay_values(obj, dict):
    """Adds each item from dict to the given object iff there already
    exists such a key. Raises KeyError if you try to update the value
    of non-existing keys.
    """
    for key in dict.keys():
        if hasattr(obj, key):
            obj.__dict__[key] = dict[key]
        else:
            raise KeyError(`key` + ' not a keyword for ' + obj.__class__.__name__)


class Input:
    """General Form Input tags.

    Keyword Arguments

        type -- 'TEXT' (default) Supported types include password, checkbox,
                      radio, file, submit, reset, hidden.
        name -- provides the datum name
        id -- global identifier
        value -- the initial value of the input item
        checked --  flag indicating if the item is checked initially
        size -- size of the widget (e.g. size=10 for a text widget is it's width)
        maxlength -- maximum number of characters accepted by the textfield.
        border -- border width in pixels for an image type.
        align -- top|middle|bottom align w.r.t. adjoining text for image types.
        llabel  --  an optional string set to the left of the widget
        rlabel  --  an optional string set to the right of the widget
        onBlur -- script, which is executed, when the field loses focus,
                  useful for the text-type 
        onChange -- script, which is executed, when the field value changed,
                    useful for the text-type
        onClick -- script, which is executed, when the field in clicked,
                   useful for the button, checkbox, radio, submit, reset type
        onFocus -- script, which is executed, when the field receives focus,
                   useful for the text-type
        onSelect -- script, which is executed, when part of the field 
                    is selected, useful for the text-type
    """
    re_type = re.compile('text|password|checkbox|radio|image|button|file|submit|reset|hidden',
                            re.IGNORECASE)
    def __init__(self, **kw):
        self.type = 'TEXT'
        self.name = 'Default_Name'
        self.id = None
        self.value = None
        self.checked = ''
        self.size = 0
        self.maxlength = 0
        self.llabel = ''
        self.rlabel = ''
        self.onBlur = ''
        self.onChange = ''
        self.onClick = ''
        self.onFocus = ''
        self.onSelect = ''
        self.border = None
        self.align = ''
        for item in kw.keys():
            if self.__dict__.has_key(item):
                self.__dict__[item] = kw[item]
            else:
                raise KeyError, `item`+' not a valid parameter of the Input class.'
        if Input.re_type.search(self.type) is None:
            raise KeyError, `self.type`+' not a valid type of Input class.'

    def __str__(self):
        s = []
        if self.llabel: s.append(str(self.llabel))
        s.append('\n<INPUT')
        if self.type: s.append(' TYPE="%s"' % self.type)
        if self.name: s.append(' NAME="%s"' % self.name)
        if self.id is not None: s.append(' ID="%s"' % self.id)
        if self.value is not None: s.append(' VALUE="%s"' % self.value)
        if self.checked: s.append(' CHECKED')
        if self.size: s.append(' SIZE=%s' % self.size)
        if self.maxlength: s.append(' MAXLENGTH=%s' % self.maxlength)
        if self.onBlur: s.append(' onBlur="%s"' % self.onBlur)
        if self.onChange: s.append(' onChange="%s"' % self.onChange)
        if self.onClick: s.append(' onClick="%s"' % self.onClick)
        if self.onFocus: s.append(' onFocus="%s"' % self.onFocus)
        if self.onSelect: s.append(' onSelect="%s"' % self.onSelect)
        if self.border is not None: s.append(' BORDER="%s"' % self.border)
        if self.align: s.append(' ALIGN="%s"' % self.align)
        s.append('>')
        if self.rlabel: s.append(str(self.rlabel))
        return string.join(s, '')


class Select(UserList.UserList):
    """Used to define a list widget or option widget.
    
    Pass a list of strings to show a list with those values. Alternatively
    can pass a list of tuple pairs. Each pair contains the displayed string
    and it's associatated value mapping. If no value mapping is needed just
    use something that evaluates to None.

    Keyword Arguments:
    
        name -- provides the datum name
        id -- global identifier
        size -- the visual size. 1 means use an option popup widget. 
                               >=2 means use a list widget with that many lines.
        multiple -- flag to indicate whether multiple selections are supported.
        selected -- list of values to be shown as pre-selected.
        onBlur -- script, which is executed, when the field loses focus
        onChange -- script, which is executed, when the field value changed
        onFocus -- script, which is executed, when the field receives focus
    """
    def __init__(self, data=None, **kw):
        UserList.UserList.__init__(self, data)
        self.name = ''
        self.id = None
        self.size = 1
        self.multiple = None
        self.selected = []
        self.onBlur = ''
        self.onChange = ''
        self.onFocus = ''
        for item in kw.keys():
            if self.__dict__.has_key(item):
                self.__dict__[item] = kw[item]
            else:
                raise KeyError, `item`+' not a valid parameter of the Select class.'

    def __str__(self):
        s = ['<SELECT NAME="%s"' % self.name]
        if self.size: s.append(' SIZE=%s' % self.size)
        if self.id is not None: s.append(' ID="%s"' % self.id)
        if self.multiple: s.append(' MULTIPLE')
        if self.onBlur: s.append(' onBlur="%s"' % self.onBlur)
        if self.onChange: s.append(' onChange="%s"' % self.onChange)
        if self.onFocus: s.append(' onFocus="%s"' % self.onFocus)
        s.append('>\n')
        if type(self.data[0]) is TupleType:
            for item, value in self.data:
                s.append('<OPTION')
                if value is not None:
                    s.append(' Value="%s"' % value)
                if value in self.selected:
                    s.append(' SELECTED')
                else:
                    if item in self.selected:
                        s.append(' SELECTED')
                s.append('>%s\n' % item)
        else:
            for item in self.data:
                if item not in self.selected:
                    s.append('<OPTION>%s\n' % item)
                else:
                    s.append('<OPTION SELECTED>%s\n' % item)
        s.append('</SELECT>\n')
        return string.join(s, '')

class Textarea:
    """Used for an entry widget to type multi-line text (for forms).

    Keyword Arguments:

        rows -- sets the number of text rows. (default=4)
        cols -- sets the number of text columns. (default=40)
        onBlur -- script, which is executed, when the field loses focus
        onChange -- script, which is executed, when the field value changed
        onFocus -- script, which is executed, when the field receives focus
        onSelect -- script, which is executed, when part of the field 
                    is selected
    """
    def __init__(self, text='', **kw):
        self.text = text
        self.name = 'text_area'
        self.rows = 4
        self.cols = 40
        self.onBlur = ''
        self.onChange = ''
        self.onFocus = ''
        self.onSelect = ''
        for item in kw.keys():
            if self.__dict__.has_key(item):
                self.__dict__[item] = kw[item]
            else:
                raise KeyError, `item`+' not a valid parameter of the Textarea class.'

    def __str__(self):
        s = ['<TEXTAREA NAME="%s" ROWS=%s COLS=%s' % (self.name, self.rows, self.cols)]
        if self.onBlur: s.append(' onBlur="%s"' % self.onBlur)
        if self.onChange: s.append(' onChange="%s"' % self.onChange)
        if self.onFocus: s.append(' onFocus="%s"' % self.onFocus)
        if self.onSelect: s.append(' onSelect="%s"' % self.onSelect)
        s.append('>')
        s.append(str(self.text))
        s.append('</TEXTAREA>')
        return string.join(s, '')

class Script:
    """Construct a Script

    Keyword Arguments

        Defaults in (parenthesis).  Keyword parameters may be set as attributes of 
        the instantiated script object as well.

        language -- specifies the language ('JavaScript')
        src -- specifies the location
        code -- script code, which is printed in comments, to hide it from non
                java-script browsers
    """
    def __init__(self, **kw):
        # Specify the default values
        self.language = 'JavaScript'
        self.src = ''
        self.code = ''
        # Now overlay the keyword arguments from caller
        for k in kw.keys():
            if self.__dict__.has_key(k):
                self.__dict__[k] = kw[k]
            else:
                print `k`, "isn't a valid parameter for this class."

    def __str__(self):
        s = ['<SCRIPT LANGUAGE="%s" ' % self.language]
        if self.src: s.append('SRC="%s" ' % self.src)
        s.append('>')
        if self.code: s.append('<!--\n%s\n//-->\n' % self.code)
        s.append('</SCRIPT>')
        return string.join(s, '')
    
    def append(self, s):
        self.code = self.code + s
  

####################


class Table:
    """Construct a Table with Python lists.

    Instantiate with a string argument for the table's name (caption).
    Set object.heading to a list of strings representing the column headings.
    Set object.body to a list of lists representing rows. **WARNING:** the body
    attribute will be edited to conform to html. If you don't want your
    data changed make a copy of this list and use that with the table object.

    Keyword Parameters

        Defaults in (parenthesis).  Keyword parameters may be set as attributes of the
        instantiated table object as well.

        caption_align -- 'top'|'bottom'  specifies the location of the table title ('top')
        border -- the width in pixels of the bevel effect around the table (2)
        cell_padding -- the distance between cell text and the cell boundary (4)
        cell_spacing -- the width of the cell borders themselves (1)
        width -- the width of the entire table wrt the current window width ('100%')
        colspan -- a list specifying the number of columns spanned by that heading
               index. e.g. t.colspan = [2,2] will place 2 headings spanning
               2 columns each (assuming the body has 4 columns).
        heading --  list of strings, the length of which determine the number of
                   columns.  ( ['&nbsp']*3 )
        heading_align -- 'center'|'left'|'right'
                        horizontally align text in the header row ('center')
        heading_valign --  'middle' |'top'|'bottom'
                        vertically align text in the header row ('middle')
        body_color -- a list of colors, for each column (None)
        heading_color -- a list of color for each column heading (None)
             For both these the index used is i%len(..._color) so
             the color cycles through the columns
        body -- a list of lists in row major order containing strings or objects
               to populate the body of the table. ( [['&nbsp']*3] )
        column1_align -- 'left'|'right'|'center'  text alignment of the first column
        cell_align --    'left'|'right'|'center'  text alignment for all other cells
        cell_line_breaks -- 1|0  flag to determine if newline char in body text will be
                  converted to <br> symbols; 1 they will, 0 they won't. (1)
        
    """
    def __init__(self, tabletitle='', **kw):
        """Arg1 is a string title for the table caption, optional keyword
        arguments follow.
        """
        # Specify the default values
        self.tabletitle = tabletitle
        self.caption_align = 'top'
        self.border = 2
        self.cell_padding = 4
        self.cell_spacing = 1
        self.width = '100%'
        self.heading = None
        self.heading_align = 'center'
        self.heading_valign = 'middle'
        self.body = [['&nbsp;']*3]
        self.column1_align = 'left'
        self.cell_align = 'left'
        self.cell_line_breaks = 1
        self.colspan = None
        self.body_color= None
        self.heading_color=None
        # Now overlay the keyword arguments from caller
        for k in kw.keys():
            if self.__dict__.has_key(k):
                self.__dict__[k] = kw[k]
            else:
                print `k`, "isn't a valid parameter for this class."

    def __str__(self):
        """Generates the html for the entire table.
        """
        if self.tabletitle:
           s = [str(Name(self.tabletitle)) + '\n<P>']
        else:
           s = []

        s.append('<TABLE border=%s cellpadding=%s cellspacing=%s width="%s">\n' % \
                (self.border, self.cell_padding, self.cell_spacing, self.width))
        if self.tabletitle:
            s.append('<CAPTION align=%s><STRONG>%s</STRONG></CAPTION>\n' % \
                    (self.caption_align, self.tabletitle))

        for i in range(len(self.body)):
            for j in range(len(self.body[i])):
                if type(self.body[i][j]) == StringType:
                    #process cell contents to insert breaks for \n char.
                    if self.cell_line_breaks:
                        self.body[i][j] = string.replace(self.body[i][j], '\n','<br>')
                    else:
                        self.body[i][j] = Text(self.body[i][j])

        # Initialize colspan property to 1 for each
        # heading column if user doesn't provide it.
        if self.heading:
            if not self.colspan:
                if type(self.heading[0]) == ListType:
                    self.colspan = [1]*len(self.heading[0])
                else:
                    self.colspan = [1]*len(self.heading)
        # Construct heading spec
        #  can handle multi-row headings. colspan is a list specifying how many
        #  columns the i-th element should span. Spanning only applies to the first
        #  or only heading line.
        if self.heading:
            prefix = '<TR Align=' + self.heading_align + '> '
            postfix = '</TR>\n'
            middle = ''
            if type(self.heading[0]) == ListType:
                for i in range(len(self.heading[0])):
                    middle = middle + '<TH ColSpan=%s%s>' % \
                             (self.colspan[i], \
                              self.get_body_color(self.heading_color,i)) \
                              + str(self.heading[0][i]) +'</TH>'
                s.append(prefix + middle + postfix)
                for i in range(len(self.heading[1])):
                    middle = middle + '<TH>' + str(self.heading[i]) +'</TH>'
                for heading_row in self.heading[1:]:
                    for i in range(len(self.heading[1])):
                        middle = middle + '<TH>' + heading_row[i] +'</TH>'
                    s.append(prefix + middle + postfix)
            else:
                for i in range(len(self.heading)):
                    middle = middle + '<TH ColSpan=%s%s>' % \
                             (self.colspan[i], \
                              self.get_body_color(self.heading_color,i)) \
                              + str(self.heading[i]) +'</TH>'
                s.append(prefix + middle + postfix)
        # construct the rows themselves
        stmp = '<TD Align=%s %s>'
        for row in self.body:
            s.append('<TR>')
            for i in range(len(row)):
                if i == 0 :
                    ss1 = self.column1_align
                else:
                    ss1 = self.cell_align
                s.append(stmp % (ss1, self.get_body_color(self.body_color,i)))
                s.append(str(row[i]))
                s.append('</TD>\n')
            s.append('</TR>\n')
        #close table
        s.append('</TABLE><P>\n')
        return string.join(s, '')
    
    def get_body_color(self, colors, i):
        """Return bgcolor argument for column number i
        """
        if colors is not None: 
            try: 
                index = i % len(colors) 
                return ' bgcolor="%s"' % colors[index] 
            except: 
                pass 
        return ''


#--------------------New stuff--------------------------#

def _make_attr_inits(opts):
    """Construct a format string and dictionary suitable for value
    substitution of tag attributes.
    """
    a = []
    d = {}
    for name in opts:
        a.append('%('+name+')s')
        d[name] = ''
    return string.join(a, ''), d  

class AbstractTagSingle:
    "Abstract base class for all tag markup classes not requiring a closure tag."
    tagname = '' # to be provided by derived classes
    attrs = ()   # to be provided by derived classes
    attr_template = '' # to be provided by derived classes
    attr_dict = {}     # to be provided by derived classes
    
    def __init__(self, *args, **kw):
        self.__dict__['attr_dict'] = copy.copy(self.__class__.attr_dict)
        self.args = args
        for name, value in kw.items():
            name = string.lower(name)
            setattr(self, name, value)

    def __str__(self):
        """Generate an HTML formatted string for this object.
        """
        return  '<%s' % self.tagname + self.attr_template % self.attr_dict + '>'
        
    def __setattr__(self, name, value):
        """Intercept attribute assignments.

        If the attribute is a legal HTML tag attribute add it to the
        dict used for substitution in __str__, otherwise just set it as
        an instance attribute.
        """
        name = string.lower(name)
        if name in self.attrs:
            self.attr_dict[name] = ' %s="%s"' % (name, value)
        self.__dict__[name] = value

class Image(AbstractTagSingle):
    """Inlined Image

    The *filename* argument is a filename, or URL of a graphic image,
    or a triple of ( filename, width, height ) where dimensions are in
    pixels. Where the filename is found to be a valid pathname to an
    existing graphic file that file will be read to determine its width and
    height properties. GIF, JPEG, and PNG files are understood.
    
    Keyword Arguments
    
        width  -- (int) Width in pixels
        height -- (int) Height in pixels
        border -- (int) Border width in pixels
        align  -- (string) 'top'|'middle'|'bottom'|'right'|'left'
        alt    -- (string) Text to substitute for the image in nonGUI browsers
        usemap -- Imagemap name or Map object
        ismap  -- Flag (1|0) indicating if a server side imagemap is available.
        absolute -- Absolute path to the directory containing the image
        prefix -- Relative path or URL to directory containing the image
        hspace -- Number of pixels to be added to the left and right of the image.
        vspace -- Number of pixels to be added to the top and bottom of the image.
        name -- A name for this image to be used by JavaScript
        Class -- A CSS class this tag belongs to.
        style -- A CSS inline style specification.
    """
    tagname = 'IMG'
    attrs = ('src', 'height', 'width', 'alt', 'border', 'align', 'class','id',
             'hspace','vspace', 'lowsrc', 'name', 'style', 'usemap', 'ismap' )
    attr_template , attr_dict = _make_attr_inits(attrs)

    def __init__(self, *args, **kw):
        apply(AbstractTagSingle.__init__, (self,) + args, kw)
        self.prefix = None
        self.absolute = None
        if self.args:
            self.process_arg(self.args[0])

    def process_arg(self, arg):
        # unpack the tuple if needed
        if type(arg) == TupleType:
            self.filename = arg[0]
            self.width = arg[1]
            self.height = arg[2]
        else:
            self.filename = arg
        self.src = self.filename
        # if the file is there test it to get size
        if not self.attr_dict['width']: # assume if the user has set the width property
            # she knows the image size already or wants to resize it.
            try:
                self.width, self.height = imgsize(self.filename)
            except IOError:
                pass

    def calc_rel_path(self, from_dir=None):
        """Calculate the relative path from 'from_dir' to the
        absolute location of the image file.

        Sets self.prefix.
        """
        if not from_dir:
            from_dir = os.getcwd()
        if self.absolute:
            self.prefix = relpath(from_dir, self.absolute)

    def __str__(self):
        if self.prefix:
            self.src = os.path.join(self.prefix, self.filename)
        if not self.attr_dict['alt']:
            self.alt = os.path.basename(self.filename)
        if self.attr_dict['usemap']:
            if type(self.attr_dict['usemap']) == InstanceType:
                # can use a Map instance for this
                try:
                    self.usemap = '#' + self.usemap.name
                except:
                    pass
        return AbstractTagSingle.__str__(self)

IMG = Image # alias

class BR(AbstractTagSingle):
    """Break tag. Argument is an integer integer multiplier. BR(2)=='<BR><BR>'
    """
    tagname = 'BR'
    attrs = ('clear',)
    attr_template , attr_dict = _make_attr_inits(attrs)
    
    def __str__(self):
        s = AbstractTagSingle.__str__(self)
        if self.args and type(self.args[0]) is IntType:
            return s*self.args[0]
        else:
            return s

class Base(AbstractTagSingle):
    """Specify the base URL for all relative URLs in this document.

    One string argument required. It must be a complete file name, and
    is usually the original URL of this document.  If this file is
    moved, having the BASE set to the original URL eliminates the need
    to also move all the documents which are identified by relative
    URL links in this document.
    """
    tagname = 'BASE'
    attrs = ('href', 'target')
    attr_template , attr_dict = _make_attr_inits(attrs)

class BaseFont(AbstractTagSingle):
    """Specify the font size for subsequent text.
    """
    tagname = 'BASEFONT'
    attrs = ('color', 'name', 'size')
    attr_template , attr_dict = _make_attr_inits(attrs)

class Embed(AbstractTagSingle):
    """Embed an application in this document.
    """
    tagname = 'EMBED'
    attrs = ('align', 'border', 'height', 'hidden', 'hspace',
             'name', 'palette', 'pluginspage', 'src', 'type',
             'units', 'vspace', 'width')
    attr_template , attr_dict = _make_attr_inits(attrs)

class HR(AbstractTagSingle):
    """Break the current text flow and insert a horizontal rule.
    """
    tagname = 'HR'
    attrs = ('align', 'class','id', 'color', 'noshade', 'size',
             'style', 'width')
    attr_template , attr_dict = _make_attr_inits(attrs)


class AbstractTag:
    "Abstract base class for all tag markup classes requiring a closure tag."
    tagname = '' # to be provided by derived classes
    attrs = ()   # to be provided by derived classes
    attr_template = '' # to be provided by derived classes
    attr_dict = {}      # to be provided by derived classes
    html_escape = 'ON'
    trailer = '\n'
    
    def __init__(self, *contents, **kw):
        self.__dict__['contents'] = []
        self.__dict__['attr_dict'] = copy.copy(self.__class__.attr_dict)
        for item in contents:
            self.contents.append(item)
        for name, value in kw.items():
            name = string.lower(name)
            setattr(self, name, value)

    def __str__(self):
        """Generate an HTML formatted string for this object.
        """
        s = ['<%s' % self.tagname]  # tag opening
        s.append(self.attr_template % self.attr_dict + '>') # options
        # crunch through the contents
        for item in self.contents:
            if type(item) is StringType and self.html_escape == 'ON':
                s.append(escape(item))
            else:
                s.append(str(item))
        # close out the marked region
        s.append( '</%s>' % self.tagname)
        return string.join(s, '') + self.trailer
        
    def __setattr__(self, name, value):
        """Intercept attribute assignments.

        If the attribute is a legal HTML tag attribute add it to the
        dict used for substitution in __str__, otherwise just set it as
        an instance attribute.
        """
        name = string.lower(name)
        if name in self.attrs:
            self.attr_dict[name] = ' %s="%s"' % (name, value)
        else:
            self.__dict__[name] = value

    def __call__(self, text):
        """Enable instances to be callable as text processing functions.

        For Example:

          >>> S = HTMLgen.Strong()
          >>> print S('Hi!')
          >>> <STRONG>Hi!</STRONG>
        """
        self.__dict__['contents'] = [text]
        return str(self)

    def __add__(self, other):
        """Support self + list
        """
        if type(other) is ListType:
            self.contents = self.contents + other
            return self
        else:
            raise TypeError, 'can only add lists to this object'
            
    def append(self, *items):
        """Append one or more items to the end of the container.
        """
        for item in items:
            self.contents.append(item)

    def prepend(self, *items):
        """Prepend one or more items to the top of the container.
        """
        for item in items:
            self.contents.insert(0, item)

    def empty(self):
        """Empty the contents of the container.
        """
        self.contents = []

    def __len__(self):
        """Return the integer length of the container list.
        """
        return len(self.contents)

    def last(self):
        """Return a reference to the last item in the container.
        """
        return self.contents[-1]

    def copy(self):
        """Return a full copy of the object.
        """
        return copy.deepcopy(self)

    def markup(self, rex=None, marker=None, **kw):
        """Markup the contained text matching a regular expression with
        a tag class instance or function. 

        Arguments

            rex -- a regular expression object or pattern which will be used
                to match all text patterns in the Paragraph body. Must have a single
                group defined. Group 1 is the matching text that will be marked.
                Default to all parenthetical text.
            marker -- an HTMLgen class instance to which the found text will
                be sent for wrapping (using its __call__ method). Default is Emphasis.

        Keywords
        
            collapse -- When set to 1 removes the non-grouped matching text
                from the output. Default 0.

        Returns the number of matching text groups.
        """
        collapse = 0
        if kw.has_key('collapse'): collapse = kw['collapse']
        text = string.join(map(str, self.contents))
        newtext, count = markup_re(text, rex, marker, collapse)
        if count:
            self.contents = [newtext]
            self.html_escape = 'OFF'
        return count


class Area(AbstractTagSingle):
    """Specify a click-sensitive area of an image.

    The area is linked to a HREF specified by the *href* attribute.
    The *coords* attribute is required and describes the position of
    an area (in pixels) of the image in comma-separated x,y
    coordinates where the upper-left corner is "0,0". For shape='rect'
    (the default), it is "left,top,right,bottom". For shape='circle',
    it is "center_x,center_y,radius". For shape='polygon', it is
    successive x,y vertices of the polygon. If the first and last
    coordinates are not the same, then a segment is inferred to close
    the polygon. If no *href* keyword is given a *NOHREF* will be
    generated indicating that this region should generate no links.
    
    Keyword Arguments
    
        href --  Typically a reference to an image
        coords --  string holding a list of coordinates defining
        shape  -- 'rect'|'circle'|'polygon'
    """
    tagname = 'AREA'
    attrs = ('alt','class','coords','href','id','name',
	     'onmouseout','onmouseover','shape','target')
    attr_template, attr_dict = _make_attr_inits(attrs)
    attr_dict['href'] = ' nohref'

###### FRAME SUPPORT ######

class Frameset(AbstractTag):
    """Define a Frameset to contain Frames or more Framesets"""
    tagname = 'FRAMESET'
    attrs = ('border','bordercolor','cols','frameborder','framespacing','onblur',
	     'onfocus','onload','onunload','rows')
    attr_template, attr_dict = _make_attr_inits(attrs)

class NoFrames(AbstractTag):
    """Issue a message on browsers that don't support frames"""
    tagname = 'NOFRAMES'
    attrs = ()
    attr_template, attr_dict = _make_attr_inits(attrs)

    def __init__(self, *contents, **kw):
	AbstractTag.__init__(self)
	for content in contents: self.append(content)
	for name, value in kw.items(): self.__setattr__(name,value)
	if len(contents) == 0:
	    self.append(Heading(2,'Frame ALERT!',align='center'),
			Para("""This document is designed to be viewed using Netscape's
			Frame features.  If you are seeing this message, you are using
			a frame challenged browser."""),
			Para('A ',Strong('Frame-capable'),' browser can be retrieved from',
			     Href('http://home.netscape.com/','Netscape Communications'),
			     ' or ',
			     Href('http://www.microsoft.com/','Microsoft')))

class Frame(AbstractTag):
    """Define the characteristics of an individual frame.

    Keywords Arguments

        src  -- is a HREF which points to the initial contents of the frame.
        name -- is the window name used by others to direct content into this frame.
        marginwidth -- is the number of pixels used to pad the left and right
               sides of the frame.
        marginheight -- is the number of pixels used to pad the top and bottom
               sides of the frame.
        scrolling -- is used to indicate scrolling policy set to 'yes'|'no'|'auto'
        noresize -- is a flag which instructs the browser to disallow frame resizing. 
               set to non zero lock size ( noresize=1 ).
    """
    
    tagname = 'FRAME'
    attrs = ('align','bordercolor','frameborder','marginheight','marginwidth','name',
	     'noresize','scrolling','src')
    attr_template, attr_dict = _make_attr_inits(attrs)



class Paragraph(AbstractTag):
    """Define a Paragraph.

    Takes a single string/object argument and the optional
    keyword argument 'align' which may be one of (left, right,
    center).  As always, Class and style keywords are supported.
    **Not to be confused with class P**. That is
    just for inserting a para break.

    Example:
    
        Paragraph('Some text to center', align='center')
    """
    tagname = 'P'
    attrs = ('class','id', 'style', 'align')
    attr_template , attr_dict = _make_attr_inits(attrs)

Para = Paragraph # Alias

# Headings

class Heading(AbstractTag):
    """Heading markups for H1 - H6

    Heading(level, text, **kw)

    The *level* arg is an integer for the level of the heading.
    Valid levels are 1-6.
    The *text* arg is a string (or any object) for the text of the heading.
    Keyword arguments are align, Class, and style.

    For example:
    h = Heading(2, 'Chapter 3', align='center')
    """
    tagname = ''
    attrs = ('class','id', 'style', 'align')
    attr_template , attr_dict = _make_attr_inits(attrs)
    def __str__(self):
        if not self.tagname:
            if self.contents[0] not in (1,2,3,4,5,6):
                raise AttributeError, "First arg of Heading must be int from 1 to 6."
            self.tagname = 'H%d' % self.contents[0]
            del self.contents[0]
        return AbstractTag.__str__(self)

H = Head = Header = Heading # Aliases

class Caption(AbstractTag):
    """Define a caption for a table.
    """
    tagname = 'CAPTION'
    attrs = ('class','id', 'style', 'align', 'valign')
    attr_template , attr_dict = _make_attr_inits(attrs)

class TH(AbstractTag):
    """Define a table header cell.
    """
    tagname = 'TH'
    attrs = ('class','id', 'style', 'nowrap', 'align','valign','rowspan',
             'colspan', 'height', 'width', 'bgcolor', 'background',
             'bordercolor', 'bordercolordark', 'bordercolorlight')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class TR(AbstractTag):
    """Define a row of cells within a table.
    """
    tagname = 'TR'
    attrs = ('class','id', 'style', 'align', 'bgcolor', 'bordercolor',
             'bordercolordark', 'bordercolorlight', 'nowrap', 'valign')
    attr_template , attr_dict = _make_attr_inits(attrs)

class TD(AbstractTag):
    """Define a table data cell.
    """
    tagname = 'TD'
    attrs = ('class','id', 'style', 'nowrap', 'align','valign', 'background',
             'bordercolor', 'bordercolordark', 'bordercolorlight',
             'rowspan','colspan','height', 'width','bgcolor')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class TableLite(AbstractTag):
    """Container class for TH TD TR and Caption objects.
    """
    tagname = 'TABLE'
    attrs = ('class','id', 'style', 'align', 'background', 'border',
             'bordercolor', 'bordercolordark', 'bordercolorlight',
             'cols', 'frame', 'cellpadding', 'cellspacing',
             'height', 'hspace', 'width', 'bgcolor', 'nowrap',
             'rules', 'valign', 'vspace')
    attr_template , attr_dict = _make_attr_inits(attrs)

class Pre(AbstractTag):
    """Render the text verbatim honoring line breaks and spacing.

    Does not escape special characters. To override this set html_escape
    to 'ON'.
    """
    tagname = 'PRE'
    attrs = ('width',)
    attr_template , attr_dict = _make_attr_inits(attrs)
    html_escape = 'OFF'

class Strike(AbstractTag):
    """The text is struck trough with a horizontal line.
    """
    tagname = 'STRIKE'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class Blockquote(AbstractTag):
    """Indent text as a block quotation.
    """
    tagname = 'BLOCKQUOTE'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)

Indent = Blockquote

class Big(AbstractTag):
    """Format text in a bigger font.
    """
    tagname = 'BIG'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class Font(AbstractTag):
    """Set the size or color of the text.
    """
    tagname = 'FONT'
    attrs = ('color', 'face', 'size')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class Address(AbstractTag):
    """A mailing address. Not a URL.
    """
    tagname = 'ADDRESS'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class Emphasis(AbstractTag):
    """Format with additional emphasis. (usually italics)
    """
    tagname = 'EM'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class Center(AbstractTag):
    """Center the text.
    """
    tagname = 'center'
    attrs = ()
    attr_template , attr_dict = _make_attr_inits(attrs)
    
class Cite(AbstractTag):
    """A citation.
    """
    tagname = 'CITE'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class KBD(AbstractTag):
    """Keyboard-like input.
    """
    tagname = 'KBD'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    html_escape = 'OFF'

class Sample(AbstractTag):
    """Sample text. Escaping of special characters is not performed.

    To enable escaping set html_escape='ON'.
    """
    tagname = 'SAMP'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    html_escape = 'OFF'
    
class Strong(AbstractTag):
    """Strongly emphasize the text.
    """
    tagname = 'STRONG'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class Code(AbstractTag):
    """Code sample. Escaping of special characters is not performed.

    To enable escaping set html_escape='ON'.
    """
    tagname = 'CODE'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    html_escape = 'OFF'

class Define(AbstractTag):
    """Format as definition text.
    """
    tagname = 'DFN'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)

class Var(AbstractTag):
    """Used for variable names.
    """
    tagname = 'VAR'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class Div(AbstractTag):
    """Specify a division within a document.
    """
    tagname = 'DIV'
    attrs = ('class','id', 'style', 'align', 'lang', 'nowrap')
    attr_template , attr_dict = _make_attr_inits(attrs)

class TT(AbstractTag):
    """Format teletype style.
    """
    tagname = 'TT'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class U(AbstractTag):
    """Underlined text.
    """
    tagname = 'U'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class Nobr(AbstractTag):
    """Specify non-breaking text.
    """
    tagname = 'NOBR'
    attrs = ()
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''
    
class Small(AbstractTag):
    """Render in a smaller font.
    """
    tagname = 'SMALL'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''
    
class Sub(AbstractTag):
    """Render as subscript.
    """
    tagname = 'SUB'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''

class Sup(AbstractTag):
    """Render as subscript.
    """
    tagname = 'SUP'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)
    trailer = ''
    
class Span(AbstractTag):
    """Generic tag to mark text for a style application.
    """
    tagname = 'SPAN'
    attrs = ('class','id', 'style')
    attr_template , attr_dict = _make_attr_inits(attrs)



# Text Formatting Classes
class InitialCaps:
    """Utility class to process text into Initial Upper Case style
    using Font specifications. All text is converted to upper case
    and the initial characters are altered by the size given by
    the optional second argument. The rest of the characters are
    altered by the size given in the optional third argument.

    For example:
    
       InitialCaps('We the people', '+3', '+1')
    """
    def __init__(self, text='', upsize = '+2', downsize = '+1'):
        self.hi = Font(size=upsize)
        self.lo = Font(size=downsize)
        self.text = text
        self.upsize = upsize
        self.downsize = downsize

    def __str__(self):
        list = string.split(self.text)
        wordlist = []
        for word in list:
            word = self.hi(string.upper(word[0])) + self.lo(string.upper(word[1:]))
            wordlist.append(word)
        return string.join(wordlist)

    def __call__(self, text):
        self.text = text
        return self.__str__()

class Text:
    """Class to encapsulate text. Escape special characters for HTML.
    """
    def __init__(self, text=''):
        if type(text) == StringType:
            text = escape(text)
        self.text = str(text)

    def append(self, text=''):
        """Concatenate text characters onto the end.
        
        Will escape special characters.
        """
        if type(text) == StringType:
            text = escape(text)
        self.text = self.text + ' ' + str(text)

    def __str__(self):
        return self.text

class RawText:
    """Class to encapsulate raw text. Does **NOT** escape special characters.
    """
    def __init__(self, text=''):
        self.text = text
        
    def append(self, text):
        self.text = self.text + str(text)

    def __str__(self):
        return self.text

# ALIASES
PRE = Pre
Bold = STRONG = Strong
Italic = EM = Emphasis
Typewriter = TT

class Comment:
    """Place a comment internal to the HTML document.

    Will not be visible from the browser.
    """
    def __init__(self, text=''):
        self.text = text

    def __str__(self):
        return '\n<!-- %s -->\n' % self.text

    def __call__(self, text):
        self.text = text
        return self.__str__()

###### UTILITIES USED INTERNALLY ########

def escape(text, replace=string.replace):
    """Converts the special characters '<', '>', and '&'.

    RFC 1866 specifies that these characters be represented
    in HTML as &lt; &gt; and &amp; respectively. In Python
    1.5 we use the new string.replace() function for speed.
    """
    text = replace(text, '&', '&amp;') # must be done 1st
    text = replace(text, '<', '&lt;')
    text = replace(text, '>', '&gt;')
    return text

def markup_re(text, rex=None, marker=None, collapse=0):
    """Markup the contained text with a given re pattern/object with
    a given tag class instance. Uses re module.

    Arguments

        text -- string to act on
        rex -- a regular expression object or pattern from the re module which will be used
            to match all text patterns in the Paragraph body. Must have a single
            group defined. Group 1 is the matching text that will be marked.
            Defaults to all parenthetical text.
        marker -- an HTMLgen class instance to which the found text will
            be sent for wrapping (using its __call__ method). Default is Emphasis.
            Can be your function as well.
        collapse -- Optional flag. When set to 1 removes the non-
            grouped matching text from the output. Default 0.

    Returns tuple pair of the marked text and the number of matching text groups.
    """
    if rex is None: rex = re.compile('\(([^)]*)\)')
    if marker is None: marker = Emphasis()
    if type(rex) == StringType: rex = re.compile(rex)
    endpoints = []
    output = []
    i = 0
    count = 0
    while 1:
        # build up a list of tuples: ( 'u'|'m', begin, end ) 
        # 'u' indicates unmarked text and 'm' marked text
        # begin and end is the range of characters
        match = rex.search(text, i)
        if match:
            if collapse: #skip chars outside group1
                endpoints.append( ('u', i, match.start(0)) )
                i = match.end(0)
            else: #incl chars outside group1
                endpoints.append( ('u', i, match.start(1)) )
                i = match.end(1)
            endpoints.append( ('m', match.start(1), match.end(1)) ) #text2Bmarked
            count = count + 1
        else:
            endpoints.append( ('u', i, len(text) ) ) # tack on an ending slice
            break

    if count == 0: return text, 0  # didn't find any matches
    for (style, begin, end) in endpoints:
        if style == 'm':
            output.append(marker(text[begin:end]))
        else:
            output.append(text[begin:end])
    return string.join(output, ''), count


class URL:
    """Represent a Universal Resource Locator.
    
    Assumed to be of the form: **http://www.node.edu/directory/file.html**
    with *http* being an example protocol, *www.node.edu* being an example
    network node, *directory* being the directory path on that node, and
    *file.html* being the target filename. The argument string is parsed
    into attributes .proto , .node , .dir , .file respectively and may
    be altered individually after instantiation. The __str__ method
    simply reassembles the components into a full URL string.
    """
    def __init__(self, url):
        self.url = url
        self.parse(url)
    def parse(self, url):
        import urlparse
        self.unparse = urlparse.urlunparse
        self.proto, self.node, self.path, self.params, self.query, self.fragment = \
                    urlparse.urlparse(url)
        self.dir, self.file = self.split(self.path)

    def split(self, p):
        """Same as posixpath.split()

        Copied here for availability on the Mac.
        """
        i = string.rfind(p, '/') + 1
        head, tail = p[:i], p[i:]
        if head and head != '/'*len(head):
                while head[-1] == '/':
                        head = head[:-1]
        return head, tail

    def __str__(self):
        return self.unparse( (self.proto,
                              self.node,
                              self.dir+self.file,
                              self.params,
                              self.query, self.fragment) )

    def copy(self):
        """No argument. Return a copy of this object.
        """
        return copy.deepcopy(self)

def mpath(path):
    """Converts a POSIX path to an equivalent Macintosh path.

    Works for ./x ../x /x and bare pathnames.
    Won't work for '../../style/paths'.

    Also will expand environment variables and Cshell tilde
    notation if running on a POSIX platform.
    """
    import os
    if os.name == 'mac' : #I'm on a Mac
        if path[:3] == '../': #parent
            mp = '::'
            path = path[3:]
        elif path[:2] == './': #relative
            mp = ':'
            path = path[2:]
        elif path[0] == '/': #absolute
            mp = ''
            path = path[1:]
        else: # bare relative
            mp = ''
        pl = string.split(path, '/')
        mp = mp + string.join(pl, ':')
        return mp
    elif os.name == 'posix': # Expand Unix variables
        if path[0] == '~' :
            path = os.path.expanduser( path )
        if '$' in path:
            path = os.path.expandvars( path )
        return path
    else: # needs to take care of dos & nt someday
        return path

#_realopen = open  #If I do a lot of mpath I can overload 'open'
#def open(filename, mode = 'r', bufsize = -1):
#    return _realopen( mpath(filename), mode, bufsize )

def relpath(path1, path2):
    """Return the relative path from directory 'path1' to directory 'path2'

    Both arguments are assumed to be directory names as there is no
    way to really distinguish a file from a directory by names
    alone. To loosen this restriction one can either assume that both
    arguments represent files or directories currently extant so that
    they can be tested, or add extra arguments to flag the path types
    (file or directory).

    I chose to impose this restriction because I will use this function
    in places where the pathnames are for files yet to be created.
    """
    common = os.path.commonprefix([path1, path2])
    sliceoff = len(common)
    path1 = path1[sliceoff:]
    path2 = path2[sliceoff:]

    dirs1 = string.split(path1, os.sep) # list of directory components below
                                        # the common path
    dirs1 = filter(lambda x: x, dirs1)  # filter out empty elements
    rel = (os.pardir+os.sep)*len(dirs1) # construct the relative path to the
                                        # common point
    return rel+path2


def compare_f2f(f1, f2):
    """Helper to compare two files, return 0 if they are equal."""

    BUFSIZE = 8192
    fp1 = open(f1)
    try:
        fp2 = open(f2)
        try:
            while 1:
                b1 = fp1.read(BUFSIZE)
                b2 = fp2.read(BUFSIZE)
                if not b1 and not b2: return 0
                c = cmp(b1, b2)
                if c:
                    return c
        finally:
            fp2.close()
    finally:
        fp1.close()

def compare_s2f(s, f2):
    """Helper to compare a string to a file, return 0 if they are equal."""

    BUFSIZE = 8192
    i = 0
    fp2 = open(f2)
    try:
        while 1:
            try:
                b1 = s[i: i + BUFSIZE]
                i = i + BUFSIZE
            except IndexError:
                b1 = ''
            b2 = fp2.read(BUFSIZE)
            if not b1 and not b2: return 0
            c = cmp(b1, b2)
            if c: return c
    finally:
        fp2.close()