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
|
# HTMLgen.py
# COPYRIGHT (C) 1996, 1997 ROBIN FRIEDRICH
# 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.0 and proposed tag elements. An
excellant reference for HTML tag elements can be found at
[Sandia National Labs]. I'll try to keep up with this list.
The Document class is a container object which acts 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 (persistant 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.
.. [Sandia National Labs] http://www.sandia.gov/sci_compute/elements.html
.. [Yale Web Style Manual] http://info.med.yale.edu/caim/StyleManual_Top.HTML
"""
import string, regex, regsub, time, os
import UserList, copy
__author__ = 'Robin Friedrich friedric@phoenix.net'
__version__ = '1.2.0'
StringType = type('s')
IntType = type(3)
ListType = type([1])
TupleType = type((1,2))
InstanceType = type(UserList.UserList())
DOCTYPE = '<!DOCTYPE HTML PUBLIC "-//Netscape_Microsoft//DTD HTML 3.0//EN">\n<HTML>\n'
PRINTECHO = 0
###### UTILITIES USED INTERNALLY ########
def escape(text):
"""Converts the special characters '<', '>', and '&'.
RFC 1866 specifies that these characters be represented
in HTML as < > and & respectively.
"""
# using this join/split technique is faster than regsub
# for strings with few occurances. Otherwise it's a wash.
text = text.split('&').join('&') # must be done 1st
text = text.split('<').join('<')
text = text.split('>').join('>')
return text
def getgifsize(path, data=None):
""" getgifsize reads the first ten bytes of the
file specified by path, checks whether it's a GIF file,
returns the width and height of the GIF file if it is one,
else TypeError is raised.
"""
# Author: just@knoware.nl (Just van Rossum)
# Modified by: johnm@magnet.com (John Mitchell)
# -- added predefined-data argument, fixed endian problem
# by using ord() instead of struct
if not data:
gifFile = open(path, "rb")
data = gifFile.read(10)
gifFile.close()
if data[:4] <> "GIF8":
raise TypeError("Not a GIF file.")
return (ord(data[7])*256 + ord(data[6]),
ord(data[9])*256 + ord(data[8]))
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 __repr__ method
simply reassembles the components into a full URL string.
"""
pattern = regex.compile('^\([a-z]+\)://\([^/ ]*\)\([^ ]*\)$',regex.casefold)
def __init__(self, url=''):
self.proto = ''
self.node = ''
self.dir = ''
self.file = ''
if not url: return
if self.pattern.search(url) > -1:
self.proto, self.node, path = self.pattern.group(1,2,3)
self.dir, self.file = self.split(path)
else:
print('Invalid URL: '+ url)
def split(self, p):
"""Same as posixpath.split()
Copied here for availability on the Mac.
"""
i = p.rfind('/') + 1
head, tail = p[:i], p[i:]
if head and head != '/'*len(head):
while head[-1] == '/':
head = head[:-1]
return head, tail
def __repr__(self):
return '%s://%s%s/%s' % (self.proto, self.node, self.dir, self.file)
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'. I'm not sure the Mac
has such a concept.
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 = path.split('/')
mp = mp + pl.join(':')
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 = path1.split(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
#################
# CLASS LIBRARY #
#################
class Document:
"""Primary container class for an HTML document.
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
scripts -- 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
"""
def __init__(self, resource='', **kw):
self.contents = []
self.base = None
self.meta = None
self.cgi = None
self.printecho = None
self.logo = ('/image/logo.gif', 36, 35)
self.banner = ('/image/banner.gif', 472, 30)
self.title = ''
self.subtitle = ''
self.author = 'Micky Mouse'
self.email = 'micky@disney.com'
self.zone = ' Central US'
self.datetime = time.ctime(time.time()) + self.zone
self.date = regsub.sub('[0-9]+:[0-9]+:[0-9]+ ', '',
time.ctime(time.time()))
self.bgcolor = ''
self.background = ''
self.textcolor = ''
self.linkcolor = ''
self.vlinkcolor = ''
self.alinkcolor = ''
self.place_nav_buttons = 'yes'
self.blank = ('../image/blank.gif', 71, 19)
self.prev = ('../image/BTN_PrevPage.gif', 71, 19)
self.next = ('../image/BTN_NextPage.gif', 71, 19)
self.top = ('../image/BTN_ManualTop.gif', 74, 19)
self.home = ('../image/BTN_HomePage.gif', 74, 19)
self.goprev = '' # URLs for above navigation buttons
self.gonext = ''
self.gotop = ''
self.gohome = ''
self.script = []
self.onLoad = ''
self.onUnload = ''
self.server = ''
self.server_root = ''
self.myurl = ''
# Read stuff from resource file into instance namespace
if resource: execfile(mpath(resource), self.__dict__)
# Overlay keyword parameter values
for item in kw.keys():
if item in self.__dict__:
self.__dict__[item] = kw[item]
else:
raise KeyError(`item`+' not a valid parameter of the Document class.')
def __repr__(self):
if self.cgi:
mime = 'Content-Type: text/html\n\n'
s = mime + DOCTYPE
else:
s = DOCTYPE
s = s + str(Comment('This file generated using Python HTMLgen module.'))
# build the HEAD and BODY tags
s = s + self.htmlhead()
# HEADER SECTION
s = s + self.header()
# DOCUMENT CONTENT SECTION and FOOTER added on
bodystring = '%s\n' * len(self.contents)
s = s + (bodystring % tuple(self.contents)) + self.footer()
return s
def htmlhead(self):
"""Generate the HEAD TITLE and BODY tags.
"""
if self.subtitle: titlebar = self.subtitle
else: titlebar = self.title
s = '<HEAD>\n <META NAME="GENERATOR" CONTENT="HTMLgen 1.1">\n\
<TITLE>%s</TITLE>\n' % titlebar
if self.meta: s = s + str(self.meta)
if self.base: s = s + str(self.base)
if self.script: # for javascripts
if type(self.script) in (TupleType, ListType):
for script in self.script:
s = s + str(script)
else:
s = s + str(self.script)
s = s + '</HEAD>\n'
s = s + '<BODY'
if self.bgcolor: s = s + ' BGCOLOR="%s"' % self.bgcolor
if self.background: s = s + ' BACKGROUND="%s"' % self.background
if self.textcolor: s = s + ' TEXT="%s"' % self.textcolor
if self.linkcolor: s = s + ' LINK="%s"' % self.linkcolor
if self.vlinkcolor: s = s + ' VLINK="%s"' % self.vlinkcolor
if self.alinkcolor: s = s + ' ALINK="%s"' % self.alinkcolor
if self.onLoad: s = s + ' onLoad="%s"' % self.onLoad
if self.onUnload: s = s + ' onUnload="%s"' % self.onUnload
s = s + '>\n'
return 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 = str(Image(self.banner, border=0)) + '<BR>\n'
elif bannertype == InstanceType:
s = str(self.banner) + '<BR>\n'
else:
raise TypeError('banner must be either a tuple, instance, or string.')
if self.place_nav_buttons:
s = s + self.nav_buttons()
s = s + str(Heading(3,self.title))
if self.subtitle:
s = s + '<H2>%s</H2>\n' % self.subtitle
s = s + '<HR>\n\n'
return s
def footer(self):
"""Generate the standard footer markups.
"""
# FOOTER SECTION - overload this if you don't like mine.
s = '\n<P><HR>\n'
if self.place_nav_buttons:
s = s + self.nav_buttons()
s = s + '<BR>' + str(Image(self.logo, align='bottom'))
s = s + '\n<FONT SIZE="-1"><P>Copyright © %s<BR>All Rights Reserved<BR>\n' \
% self.author
s = s + '\nComments to author: ' + `MailTo(self.email)`
s = s + '<br>\nGenerated: %s <BR>' % self.date # can use self.datetime here instead
s = s + '<hr>' + self.myurl
s = s + '\n</FONT> </BODY> </HTML>'
return 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 = s + str(link) + ' \n'
else: # place a blank gif as spacer
btn = Image(self.blank)
s = s + str(btn) + ' \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 = s + str(link) + ' \n'
else: # place a blank gif as spacer
btn = Image(self.blank)
s = s + 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 = s + str(link) + ' \n'
else: # place a blank gif as spacer
btn = Image(self.blank)
s = s + 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 = s + str(link) + ' \n'
else: # place a blank gif as spacer
btn = Image(self.blank)
s = s + str(btn) + ' \n'
return s
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 into file.
In Unix you can use environment variables in filenames.
Will print to stdout if no argument.
"""
if filename:
f = open(mpath(filename), 'w')
else:
import sys
f = sys.stdout
f.write(str(self))
f.close()
if PRINTECHO and filename: print('wrote: "'+filename+'"')
class MinimalDocument(Document):
"""A Document without all the header and footer stuff.
Can be given a resource file as in Document.
Uses the following attributes in the same way as Document:
title, subtitle, meta, base, bgcolor, background, textcolor,
linkcolor, vlinkcolor, cgi. *All others ignored.*
"""
def __repr__(self):
if self.cgi:
s = 'Content-Type: text/html\n\n' + DOCTYPE
else:
s = DOCTYPE
s = s + str(Comment('This file generated using the Python HTMLgen module.'))
s = s + self.htmlhead()
# DOCUMENT CONTENT SECTION
for object in self.contents:
s = s + str(object)
s = s + '\n </BODY> </HTML>'
return s
class FramesetDocument(Document):
"""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
scripts -- 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. Useful methods are *append*, *prepend*, *write*,
and *copy*. **Others inherited from Document class are unused.**
"""
def __init__(self, *contents, **kw):
self.contents = map(None, contents)
self.meta = None
self.base = None
self.title = ''
self.cgi = 0
self.script = []
for item in kw.keys():
if item in self.__dict__:
self.__dict__[item] = kw[item]
else:
raise KeyError(`item`+' not a valid parameter of the Document class.')
def __repr__(self):
if self.cgi:
mime = 'Content-Type: text/html\n\n'
s = mime + DOCTYPE
else:
s = DOCTYPE
s = s + `Comment('This file generated with Python HTMLgen module.')`
s = s + '<HEAD>\n<TITLE>%s</TITLE>\n' % self.title
if self.meta: s = s + str(self.meta)
if self.base: s = s + str(self.base)
if self.script: # for javascripts
if type(self.script) in (TupleType, ListType):
for script in self.script:
s = s + str(script)
else:
s = s + str(self.script)
s = s + '</HEAD>\n'
for item in self.contents:
s = s + str(item)
s = s + '</HTML>'
return s
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 __repr__(self):
s = '<META'
if self.equiv: s = s + ' HTTP-EQUIV="%s"' % self.equiv
if self.name: s = s + ' NAME="%s"' % self.name
if self.content: s = s + ' CONTENT="%s"' % self.content
if self.url: s = s + ' URL="%s"' % self.url
s = s + '>\n'
return s
class Base:
"""Set the the absolute URL base to be used for relative URL links.
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.
"""
def __init__(self, url=''):
self.url = url
def __repr__(self):
return '<BASE HREF="%s">\n' % self.url
##### 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 __repr__(self):
s = '\n<MAP NAME="%s">\n' % self.name
for area in self.areas:
s = s + str(area)
s = s + '</MAP>\n'
return s
class Area:
"""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'
"""
def __init__(self, **kw):
self.shape = 'RECT'
self.coords = ''
self.href = ''
for item in kw.keys():
if item in self.__dict__:
self.__dict__[item] = kw[item]
else:
raise KeyError(`item`+' not a valid parameter of the Area class.')
def __repr__(self):
if not self.coords:
raise AttributeError('Area object requires coords attribute.')
s = '<AREA SHAPE="%s" COORDS="%s"' % (self.shape, self.coords)
if self.href: s = s + ' HREF="%s"' % self.href
else: s = s + ' NOHREF'
s = s + '>\n'
return s
###### FRAME SUPPORT ######
class Frameset:
"""Define a FRAMESET.
Framesets can contain either Frames or other Framesets.
Keywords/Attributes
rows -- is a string like "20%,*,20%" stating the number and widths of
rows defined for the frames. Widths for each row can be expressed in
integer (absolute pixels), percentage (of overall browser window or
containing frame), or asterisk(*) to indicate an auto-resized remainder
width.
cols -- is a string of the same format as above except applied to column
widths. There should not be both a rows and cols attribute in the same
Frameset tag.
frame_warning -- optional flag is available to indicate if a <NOFRAMES>
statement should be included with the Frameset to alert browsers without
frame support. Default is 1 (yes).
onLoad -- is a string of script code, which is executed when the frameset
is loaded
onUnload -- is a string of script code, which is executed when the frameset
is unloaded
"""
def __init__(self, *contents, **kw):
"""contents is a series of Frame objects, key word arguments supported.
"""
self.rows = ''
self.cols = ''
self.contents = []
self.frame_warning = 1
self.onLoad = ''
self.onUnload = ''
if contents: apply(self.append, contents)
for item in kw.keys():
if item in self.__dict__:
self.__dict__[item] = kw[item]
else:
raise KeyError(`item`+' not a valid parameter of the Frameset class.')
if self.rows and self.cols:
raise AttributeError("Can't use both rows and cols in a Frameset.")
def append(self, *items):
"""Add Frames to the Frameset.
*items* can be one or more Frame object.
"""
for item in items:
self.contents.append(item)
def __repr__(self):
s = '\n<FRAMESET'
if self.rows: s = s + ' ROWS="%s"' % self.rows
if self.cols: s = s + ' COLS="%s"' % self.cols
if self.onLoad: s = s + ' onLoad="%s"' % self.onLoad
if self.onUnload: s = s + ' onUnload="%s"' % self.onUnload
s = s + '>\n'
if self.frame_warning:
s = s + """<NOFRAMES>
<H2 align=center>Frame ALERT!</h2>
<p>
This document is designed to be viewed using <b>Netscape</b>'s
Frame features. If you are seeing this message, you are using
a frame challenged browser.
</p>
<p>
A <b>Frame-capable</b> browser can be gotten from
<a href="http://home.netscape.com/">Netscape Communications</a>.
</p>
</NOFRAMES>
"""
# I'll add microsoft.com when IE supports Frames RSN.
for item in self.contents:
s = s + str(item)
s = s + '\n</FRAMESET>\n'
return s
class Frame:
"""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 ).
"""
def __init__(self, **kw):
"""keyword arguments are supported.
"""
self.src = ''
self.name = ''
self.marginwidth = 0
self.marginheight = 0
self.scrolling = None
self.noresize = None
for item in kw.keys():
if item in self.__dict__:
self.__dict__[item] = kw[item]
else:
raise KeyError(`item`+' not a valid parameter of the Frameset class.')
def __repr__(self):
s = ' <FRAME'
if self.src: s = s + ' SRC="%s"' % self.src
if self.name: s = s + ' NAME="%s"' % self.name
if self.marginwidth: s = s + ' MARGINWIDTH=%s' % self.marginwidth
if self.marginheight: s = s + ' MARGINHEIGHT=%s' % self.marginheight
if self.scrolling: s = s + ' SCROLLING="%s"' % self.scrolling
if self.noresize: s = s + ' NORESIZE'
s = s + '>\n'
return 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 -- is the script-code which is executed when the mouse
moves of the link.
"""
def __init__(self, url='', text='', **kw):
self.target = None
self.onClick = None
self.onMouseOver = None
self.url = url
self.text = text
for item in kw.keys():
if item in self.__dict__:
self.__dict__[item] = kw[item]
else:
raise KeyError(`item`+' not a valid parameter for this class.')
def __repr__(self):
s = '<A HREF="%s"' % self.url
if self.target: s = s + ' TARGET="%s"' % self.target
if self.onClick: s = s + ' onClick="%s"' % self.onClick
if self.onMouseOver: s = s + ' onMouseOver="%s"' % self.onMouseOver
s = s + '>%s</A>' % self.text
return 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 __repr__(self):
s = '<A NAME="%s">%s</A>' % (self.url, self.text)
return s
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.
"""
def __init__(self, address='', text=None):
self.address = address
self.text = text or address
def __repr__(self):
return '<A HREF="mailto:%s">%s</A>' % (self.address, self.text)
MAILTO = Mailto = MailTo
# Inline Images
class Image:
"""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 GIF file that file will be read to determine its width and
height properties.
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 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
"""
def __init__(self, filename='', **kw):
self.prefix = None
self.absolute = None
self.filename = filename
self.width = None
self.height = None
self.border = None
self.align = None
self.alt = None
self.ismap = None
self.usemap = None
for item in kw.keys():
self.__dict__[item] = kw[item]
# unpack the tuple if needed
if type(self.filename) == TupleType:
self.width = self.filename[1]
self.height = self.filename[2]
self.filename = self.filename[0]
# if the file is there test it to get size of GIF
if self.filename and not self.width:
try:
self.width, self.height = getgifsize(filename)
except (IOError, TypeError):
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 __repr__(self):
if self.prefix:
fullname = os.path.join(self.prefix, self.filename)
else:
fullname = self.filename
s = '<IMG SRC="%s"' % fullname
if self.width: s = s + ' WIDTH=%s' % self.width
if self.height: s = s + ' HEIGHT=%s' % self.height
if type(self.border)==IntType: s = s + ' BORDER=%s' % self.border
if self.align: s = s + ' ALIGN=%s' % self.align
if self.alt:
s = s + ' ALT="%s"' % self.alt
else:
s = s + ' ALT="%s"' % os.path.basename(self.filename)
if self.usemap:
if type(self.usemap) == StringType:
# Normally it's a string with a leading # sign
s = s + ' USEMAP="%s"' % self.usemap
elif type(self.usemap) == InstanceType:
# can use a Map instance for this
try:
s = s + ' USEMAP="#%s"' % self.usemap.name
except:
pass
if self.ismap: s = s + ' ISMAP'
s = s + '>'
return s
IMG = Image # alias
# Headings
class Heading:
"""Heading markups for H1 - H6
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.
"""
def __init__(self, level=1, text=''):
if level not in (1,2,3,4,5,6):
raise ValueError(`level`+' is not a valid heading level')
self.level = level
if type(text) == StringType: text = escape(text)
self.text = str(text)
def __repr__(self):
s = '<H%s>%s</H%s>\n' % (self.level, self.text, self.level)
return s
def append(self, text):
"""Add text onto the end of the Heading object.
"""
if type(text) == StringType: text = escape(text)
self.text = self.text + str(text)
H = Head = Header = Heading # Aliases
# Paragraph blocks
class Paragraph:
"""Define a Paragraph.
Takes a single string/object argument and the optional
keyword argument 'align' which may be one of (left, right,
center). **Not to be confused with class P**. That is
just for inserting a para break.
Example:
Paragraph('Some text to center', align='center')
"""
def __init__(self, text='', **kw):
self.align = ''
self.contents = []
if type(text) == StringType: text = escape(text)
self.contents.append(text)
if 'align' in kw: self.align = kw['align']
def append(self, text):
"""Adds text (or any object) onto the end of object."""
if type(text) == StringType: text = escape(text)
self.contents.append(text)
def __repr__(self):
s = '\n<P'
if self.align: s = s + ' ALIGN=%s' % self.align
s = s + '>'
for item in self.contents:
s = s + str(item)
s = s + '</P>\n'
return s
def markup(self, rex= None, classobj=None, kw=None):
"""Markup the text with a given regex with a given tag class.
Arguments
rex -- a regular expression object or string which will be used
to match all text patterns in the Paragraph body.
classobj -- a class object to which the found text will be sent for
wrapping.
kw -- Optional dictionary of keyword parameter mappings sent
along to the class to tailor the instance.
**[EXPERIMENTAL]**
"""
#the pattern used to find parenthetical text
self.text = ''
for item in self.contents:
self.text = self.text + str(item)
if not rex: rex = regex.compile('([^)]*)')
if not classobj: classobj = Emphasis
if not kw: kw = {}
if type(rex) == StringType:
rex = regex.compile(rex)
endpoints = [(0,0)] #start off the list of slices with 0
output = []
i = 0
while 1:
# build up a list of tuples each containing the
# endpoints of the found parenthetic text
if rex.search(self.text, i) > -1:
endpoints.append(rex.regs[0])
i = rex.regs[0][1]
else:
break
if len(endpoints) == 1: return 0# didn't find any matches
# tack on a ending slice
endpoints.append( ( len(self.text)-1 , len(self.text)-1 ) )
length = len(endpoints)
for i in range(1, length-1):
# first take the text before the paren starts
# and send it unchanged to the output list
a , b = endpoints[i-1][1] , endpoints[i][0]
output.append(self.text[a:b])
# next take the paren slice itself and wrap
# it in the markup class
a , b = endpoints[i][0] , endpoints[i][1]
output.append( str(apply(classobj,(self.text[a:b],),kw)) )
# the loop didn't get the last stretch of plain text
# so this finishes up
a , b = endpoints[i][1], endpoints[i+1][0] + 1
output.append(self.text[a:b])
# rejoin the list and set it to the internal text
self.text = output.join('')
self.contents = [self.text]
return length - 2 #return the number of items found
Para = Paragraph # Alias
class P:
"""Just echo a <P> tag"""
def __repr__(self):
return '\n<P>\n'
# List constructs
class List(UserList.UserList):
"""Will generate a bulleted list given a list arg.
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 however.
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')
flags = ('compact')
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__[item.lower()] = kw[item]
def __repr__(self):
self.firstitem = 1
self.s = ''
if self.indent:
self.s = 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 = self.s + self.render_list_item(item)
if self.indent: #close out this level of list
self.s = self.s + self.pad*self.lvl + self.end_element()
self.lvl = 0
return 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 = self.s + str(list)
except AttributeError:
pass
else:
self.s = self.s + 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 = self.s + self.render_list_item(item)
# close out this list level
self.s = self.s + 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\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 = s + ' %s="%s"' % (attr, getattr(self, attr))
except AttributeError:
pass
for flag in self.flags:
try:
x = getattr(self, flag)
s = s + ' %s' % flag
except AttributeError:
pass
return s + '>\n'
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',)
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 = ()
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 -- flag indicating if a Submit button should automatically
be appended to the form.
reset -- flag indicating if a Reset button should automatically
be appended to the form.
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 = None
self.reset = None
self.target = None
self.enctype = None
self.name = None
self.onSubmit = ''
for item in kw.keys():
if item in self.__dict__:
self.__dict__[item] = kw[item]
else:
raise KeyError(`item`+' not a valid parameter of the Form class.')
def append(self, *items):
"""Append any number of items to the form container.
"""
for item in items:
self.contents.append(item)
def __repr__(self):
if not self.submit:
self.contents.append(Input(type='submit', name='SubmitButton',value='Send'))
else:
self.contents.append(self.submit)
if self.reset:
self.contents.append(self.reset)
s = '\n<FORM METHOD="POST"'
if self.cgi: s = s + ' ACTION="%s"' % self.cgi
if self.enctype: s = s + ' ENCTYPE="%s"' % self.enctype
if self.target: s = s + ' TARGET="%s"' % self.target
if self.name: s = s + ' NAME="%s"' % self.name
if self.onSubmit: s = s + ' onSubmit="%s"' % self.onSubmit
s = s + '>\n'
for item in self.contents:
s = s + str(item)
s = s + '\n</FORM>\n'
return s
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
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.
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 = regex.compile('text\|password\|checkbox\|radio\|file\|submit\|reset\|hidden',
regex.casefold)
def __init__(self, **kw):
self.type = 'TEXT'
self.name = 'Default_Name'
self.value= ''
self.checked = ''
self.size = 0
self.maxlength = 0
self.llabel = ''
self.rlabel = ''
self.onBlur = ''
self.onChange = ''
self.onClick = ''
self.onFocus = ''
self.onSelect = ''
for item in kw.keys():
if item in self.__dict__:
self.__dict__[item] = kw[item]
else:
raise KeyError(`item`+' not a valid parameter of the Input class.')
if Input.re_type.search(self.type) < 0:
raise KeyError(`self.type`+' not a valid type of Input class.')
def __repr__(self):
s = ''
if self.llabel: s = str(self.llabel)
s = s + '<INPUT '
if self.type: s = s + 'TYPE="%s" ' % self.type
if self.name: s = s + 'NAME="%s" ' % self.name
if self.value: s = s + 'VALUE="%s" ' % self.value
if self.checked: s = s + 'CHECKED '
if self.size: s = s + 'SIZE=%s ' % self.size
if self.maxlength: s = s + 'MAXLENGTH=%s ' % self.maxlength
if self.onBlur: s = s + 'onBlur="%s" ' %self.onBlur
if self.onChange: s = s + 'onChange="%s" ' %self.onChange
if self.onClick: s = s + 'onClick="%s" ' %self.onClick
if self.onFocus: s = s + 'onFocus="%s" ' %self.onFocus
if self.onSelect: s = s + 'onSelect="%s" ' %self.onSelect
s = s + '>\n'
if self.rlabel: s = s + str(self.rlabel)
return 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
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.
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.size = 1
self.multiple = None
self.selected = []
self.onBlur = ''
self.onChange = ''
self.onFocus = ''
for item in kw.keys():
if item in self.__dict__:
self.__dict__[item] = kw[item]
else:
raise KeyError(`item`+' not a valid parameter of the Select class.')
def __repr__(self):
s = '<SELECT NAME="%s"' % self.name
if self.size: s = s + ' SIZE=%s' % self.size
if self.multiple: s = s + ' MULTIPLE'
if self.onBlur: s = s + ' onBlur="%s"' % self.onBlur
if self.onChange: s = s + ' onChange="%s"' % self.onChange
if self.onFocus: s = s + ' onFocus="%s"' % self.onFocus
s = s + '>\n'
if type(self.data[0]) == TupleType:
for item, value in self.data:
s = s + '<OPTION'
if value:
s = s + ' Value="%s"' % value
if value in self.selected:
s = s + ' SELECTED'
else:
if item in self.selected:
s = s + ' SELECTED'
s = s + '>%s\n' % item
else:
for item in self.data:
if item not in self.selected:
s = s + '<OPTION>%s\n' % item
else:
s = s + '<OPTION SELECTED>%s\n' % item
s = s + '</SELECT>\n'
return 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 item in self.__dict__:
self.__dict__[item] = kw[item]
else:
raise KeyError(`item`+' not a valid parameter of the Textarea class.')
def __repr__(self):
s = '<TEXTAREA NAME="%s" ROWS=%s COLS=%s' % (self.name, self.rows, self.cols)
if self.onBlur: s = s + ' onBlur="%s"' % self.onBlur
if self.onChange: s = s + ' onChange="%s"' % self.onChange
if self.onFocus: s = s + ' onFocus="%s"' % self.onFocus
if self.onSelect: s = s + ' onSelect="%s"' % self.onSelect
s = s + '>'
s = s + str(self.text)
s = s + '</TEXTAREA>'
return 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 k in self.__dict__:
self.__dict__[k] = kw[k]
else:
print(`k` + " isn't a valid parameter for this class.")
def __repr__(self):
s = '<SCRIPT LANGUAGE="%s" ' % self.language
if self.src: s = s + 'SRC="%s" ' % self.src
s = s + '>'
if self.code: s = s + '<!--\n%s\n//-->\n' % self.code
s = s + '</SCRIPT>'
return 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. ( [' ']*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 -- a list of lists in row major order containing strings or objects
to populate the body of the table. ( [[' ']*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 = [' ']*3
self.heading_align = 'center'
self.heading_valign = 'middle'
self.body = [[' ']*3]
self.column1_align = 'left'
self.cell_align = 'left'
self.cell_line_breaks = 1
self.colspan = None
# Now overlay the keyword arguments from caller
for k in kw.keys():
if k in self.__dict__:
self.__dict__[k] = kw[k]
else:
print(`k` + " isn't a valid parameter for this class.")
def __repr__(self):
"""Generates the html for the entire table.
"""
if self.tabletitle:
s = `Name(self.tabletitle)` + '\n<P>'
else:
s = ''
s = s + '<TABLE border=%s cellpadding=%s cellspacing=%s width="%s">\n' % \
(self.border, self.cell_padding, self.cell_spacing, self.width)
if self.tabletitle:
s = s + '<CAPTION align=%s><STRONG>%s</STRONG></CAPTION>\n' % \
(self.caption_align, self.tabletitle)
#convert all body data to string
for i in range(len(self.body)):
for j in range(len(self.body[i])):
if type(self.body[i][j]) == StringType:
self.body[i][j] = escape(self.body[i][j])
#process cell contents to insert breaks for \n char.
if self.cell_line_breaks:
self.body[i][j] = regsub.gsub('\n','<br>',self.body[i][j].strip())
else: #could be a markup object so just repr it
self.body[i][j] = str(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>' % self.colspan[i] + str(self.heading[0][i]) +'</TH>'
s = s + 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 = s + prefix + middle + postfix
else:
for i in range(len(self.heading)):
middle = middle + '<TH ColSpan=%s>' % self.colspan[i] + str(self.heading[i]) +'</TH>'
s = s + prefix + middle + postfix
# construct the rows themselves
for row in self.body:
prefix = '<TR> <TD Align='+ self.column1_align + '>'
postfix = '</TD> </TR>\n'
infix = '</TD> <TD Align='+self.cell_align+'>'
s = s + prefix + row.join(infix) + postfix
#close table
s = s + '</TABLE><P>\n'
return s
class Tbase:
"Abstract base class for the table tag markup classes."
tagname = '' # to be provided by derived classes
attrs = () # to be provided by derived classes
flags = () # to be provided by derived classes
def __init__(self, arg=None, **kw):
self.arg = arg
for item in kw.keys():
self.__dict__[item.lower()] = kw[item]
def start_tag(self):
"Return start tag for this tag type including all attributes"
s = '<' + self.tagname
for attr in self.attrs:
try:
s = s + ' %s="%s"' % (attr, getattr(self, attr))
except AttributeError:
pass
for flag in self.flags:
try:
x = getattr(self, flag)
s = s + ' %s' % flag
except AttributeError:
pass
return s + '>'
def end_tag(self):
"Return end tag for this tag type"
return '</%s>' % self.tagname
def __repr__(self):
"default string for this markup"
return self.start_tag() + str(self.arg) + self.end_tag()
class Container:
"""Generic base class providing methods associated with managing
an object with a content list. Used by the TableLite class and friends.
"""
def __init__(self, *args, **kw):
self.contents = []
for arg in args:
self.contents.append(arg)
for item in kw.keys():
self.__dict__[item.lower()] = kw[item]
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 length(self):
"""Return the integer length of the container list.
"""
return len(self.contents)
def last(self):
"""Return 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 __add__(self, other):
"""Support self + list
"""
if type(other)==type([]):
self.contents = self.contents + other
return self
else:
raise TypeError('can only add lists to this object')
def __repr__(self):
s = '\n' + self.start_tag()
for item in self.contents:
s = s + str(item)
s = s + self.end_tag()
return s
class Caption(Tbase):
"""Caption used to annotate the entire Table.
Keywords Available
align -- set to either top|bottom|left|right
"""
tagname = 'CAPTION'
attrs = ('align',)
class TH(Tbase):
"""Column Heading cell.
Keywords Available
align -- set to either top|bottom|left|right
valign -- set to either top|middle|bottom|baseline
rowspan -- integer number of rows this heading cell should span
colspan -- integer number of columns this heading cell should span
nowrap -- flag to indicate that no text wrapping be done on this cell
width -- integer or percent to force cell width
"""
tagname = 'TH'
attrs = ('align','valign','rowspan','colspan','bgcolor')
flags = ('nowrap',)
class TD(Container, Tbase):
"""Data cell.
Keywords Available
align -- set to either left|right|center|justify|char|decimal
valign -- set to either top|middle|bottom|baseline
rowspan -- integer number of rows this cell should span
colspan -- integer number of columns this cell should span
nowrap -- flag to indicate that no text wrapping be done on this cell
width -- integer or percent to force cell width
bgcolor -- color string used for the background of the table data
"""
tagname = 'TD'
attrs = ('align','valign','rowspan','colspan','width','bgcolor')
flags = ('nowrap',)
class TR(Container, Tbase):
"""Table Row. Contains a series of TH or TD cells.
Keywords Available
align -- set to either left|right|center|justify|char|decimal
valign -- set ot either top|middle|bottom|baseline for vertical alignment
bgcolor -- color string used for the background of the table row
"""
tagname = 'TR'
attrs = ('align','valign', 'bgcolor')
class TableLite(Container, Tbase):
"""Container for HTML tables.
This is an implementation for Peter Gerhard who wanted a
completely flexible Table class. Well, I also used it as
an exercise in multiple inheritance. Gosh it's easy!
Keywords Available
align -- set to either left|right|center|justify|bleedleft|bleedright
border -- outside frame width in pixels. set to 0 for no border
frame -- set to void|above|below|hsides|lhs|rhs|vsides|box|border
cellpadding -- integer spacing between cell content and cell border
cellspacing -- integer spacing between cells (defines cell border thickness)
width -- integer or percentage width of entire table. percentages are the
fraction of the window while a literal integer forces a pixel width.
"""
tagname = 'TABLE'
attrs = ('align','border','frame','cellpadding','cellspacing','width','bgcolor')
class Multicol(Container):
"""Declare a multi-column section of the page.
Instantiate the Multicol class and use the append method to add contents
to the section. Mutlicol sections can be nested but it's not recommended
for appearance reasons.
Keyword Arguments
cols -- number of column. If not provided it defaults to 2.
gutter -- width in pixels of the gap between the columns. Default is 10.
width -- width in pixels of the columns. All columns are the same width.
Overall width of the multicol section is
(cols * width) + ((cols - 1) * gutter)
The method *total_width* is provided for the lazy folks who don't want
to calculate the above equation.
"""
cols = 2
gutter = 0
width = 0
def start_tag(self):
"Return start tag for this tag type including all attributes"
s = '<MULTICOL'
s = s + ' COLS=%s' % self.cols
if self.gutter: s = s + ' GUTTER=%s' % self.gutter
if self.width: s = s + ' WIDTH=%s' % self.width
return s + '>'
def end_tag(self):
"Return end tag for this tag type"
return '</MULTICOL>\n'
def total_width(self):
"""Calculates the total width in pixels of the multi-column section
given the current object properties.
"""
if self.width > 0:
gutter = self.gutter or 10
cols = self.cols
width = self.width
return `(cols * width) + ((cols - 1) * gutter)` + ' pixels'
else:
return 'Need a specific column width first.'
def set_total_width(self, total):
"""Calculates and sets the column width needed to make this section
the given the total width in pixels desired.
"""
gutter = self.gutter or 10
cols = self.cols
self.width = (total - ((cols - 1) * gutter)) / cols
MULTICOL = Multicol
class Spacer:
"""Spacing object to force spcific spacing between other objects.
The *type* attribute has three possible values: 'horizontal',
'vertical', and 'block'. The default value is horizontal.
- The horizontal spacer inserts horizontal space between
words. The width of the space is controlled by the *size*
attribute.
- The vertical spacer inserts vertical space between
lines. Implicit in this spacer is a line break to end the
current line, then the vertical space is added before the
beginning of the next line. The height of the space is
controlled by the *size* attribute.
- The block spacer behaves almost exactly like an invisible
image. When using this type of spacer the *size* attribute is
ignored, and instead the *width*, *height*, and *align*
attributes are applied just as they would be for the <IMG>
tag.
The *size* attribute only applies when the spacer has a type of
horizontal or vertical. Then this attribute controls the absolute
width or height in pixels of the spacing added.
The *width* attribute only applies when the spacer is of type
block. Then this attribute controls the absolute width in pixels
of the spacing rectangle added.
The *height* attribute only applies when the spacer is of type
block. Then this attribute controls the absolute height in pixels
of the spacing rectangle added.
The *align* attribute only applies when the spacer is of type
block. Then this attribute controls the alignment of the spacing
rectangle in exactly the same way it would control the alignment
of an <IMG> tag. """
def __init__(self, **kw):
self.type = 'horizontal'
self.size = None
self.width = None
self.height = None
self.align = None
for item in kw.keys():
if item in self.__dict__:
self.__dict__[item] = kw[item]
else:
raise KeyError(`item`+' not a valid parameter of the Spacer class.')
def __repr__(self):
s = '<SPACER TYPE="%s"' % self.type
if self.size: s = s + ' SIZE=%s' % self.size
if self.width: s = s + ' WIDTH=%s' % self.width
if self.height: s = s + ' HEIGHT=%s' % self.height
if self.align: s = s + ' ALIGN="%s"' % self.align
return s + '>'
# 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'):
list = text.split()
self.wordlist = []
for word in list:
word = `Font(word[0].upper(), size=upsize)` + \
`Font(word[1:].upper(), size=downsize)`
self.wordlist.append(word)
def __repr__(self):
return self.wordlist.join()
class Text:
"""Base class for text.
Escape the special characters for html.
"""
def __init__(self, text='', **kw):
if type(text) == StringType: text = escape(text)
self.text = str(text)
self.size = None
self.color = None
self.face = None
for item in kw.keys():
self.__dict__[item] = kw[item]
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 __repr__(self):
return self.text
class RawText(Text):
"""Base class for raw text.
Does **NOT** escape the special characters."""
def __init__(self, text=''):
self.text = text
def append(self, text=''):
self.text = self.text + ' ' + str(text)
class Pre(RawText):
"""Preformatted text. Will not escape any special characters.
"""
def __repr__(self):
return '<PRE>\n%s\n</PRE>\n' % self.text
class Backquote(Text):
"""Use the <BLOCKQUOTE> tag
Typically used to include lengthy quotations in
a separate block on the screen.
"""
def __repr__(self):
return '<BACKQUOTE>\n%s\n</BACKQUOTE>\n' % self.text
class Font(Text):
"""Lets you change the attributes of text.
Argument is a text string or object with a string representation.
Keyword arguments control the rendered apperance of this text.
size -- Browsers typically represent font size as an index from 1 to 7.
Normal body text is declared a 3 on this scale (not in point size).
Relative font changes can be declared by using a '+/-N' notation
where N is the number of size steps relative to the surrounding
text.
color -- Set the color of the text using the hex triple notation.
face -- A string listing (comma separated) the preferred font family names.
e.g., face="Times,Times New Roman" or face="Helvetica,Arial,Geneva".
This allows more direct usage of font capabilities on browsing
machines.
Arguments must be strings. e.g. Font('Blazing',color='#cc0000',size='+2')
will present the word *Blazing* in the color red and a font two sizes
larger than the surrounding text.
"""
def __repr__(self):
# every time we add an attribute to test on be sure to
# add it to the __init__ constructor in the base class (Text).
s = '<FONT'
if self.size: s = s + ' SIZE="%s"' % self.size
if self.color: s = s + ' COLOR="%s"' % self.color
if self.face: s = s + ' FACE="%s"' % self.face
s = s + '>'
s = s + str(self.text) + '</FONT>'
return s
class Address(Text):
"""Used to specify the author of a document, etc.
Has nothing to do with Postal addressing. That should be PREformatted.
"""
def __repr__(self):
return '<ADDRESS>%s</ADDRESS>\n' % self.text
class Emphasis(Text):
"""Use for emphasis.
Typically displayed in italics.
"""
def __repr__(self):
return '<EM>%s</EM>' % self.text
class Cite(Text):
"""Use for titles of books, films, etc.
Typically displayed in italics.
"""
def __repr__(self):
return '<CITE>%s</CITE>' % self.text
class KBD(RawText):
"""Use for user keyboard entry.
Typically displayed in plain fixed-width font.
"""
def __repr__(self):
return '<KBD>%s</KBD>' % self.text
class Sample(RawText):
"""Use for a sequence of literal characters.
Displayed in a fixed-width font.
"""
def __repr__(self):
return '<SAMP>%s</SAMP>' % self.text
class Strong(Text):
"""Use for strong emphasis.
Typically displayed in bold.
"""
def __repr__(self):
return '<STRONG>%s</STRONG>' % self.text
class Code(RawText):
"""Use for computer code.
Displayed in a fixed-width font.
"""
def __repr__(self):
return '\n<CODE>%s\n</CODE>\n' % self.text
class Define(Text):
"""Use for a word being defined.
Typically displayed in italics.
"""
def __repr__(self):
return '<DFN>%s</DFN>' % self.text
class Var(Text):
"""Use for a variable.
Used where you will replace the variable with specific information.
Typically displayed in italics.
"""
def __repr__(self):
return '<VAR>%s</VAR>' % self.text
PRE = Pre
Bold = STRONG = Strong
Italic = EM = Emphasis
Typewriter = Sample
# Misc items
class HR:
"""Horizontal Rule.
thickness -- The pixel thickness of the line
Keyword Arguments
width -- either int number of pixels or string percentage of window
align -- string either 'left'|'right'|'center'
noshade -- set to non-null value to force no bevel shading effect
"""
def __init__(self, thickness=None, **kw):
self.size = thickness
self.width = None
self.align = None
self.noshade = None
for item in kw.keys():
self.__dict__[item] = kw[item]
def __repr__(self):
s = '\n<HR'
if self.size: s = s + ' SIZE=%s' % self.size
if self.width: s = s + ' WIDTH=%s' % self.width
if self.align: s = s + ' ALIGN=%s' % self.align
if self.noshade: s = s + ' NOSHADE'
s = s + '>\n'
return s
class BR:
"""Force Line break. Integer argument causes repeated breaks.
"""
def __init__(self, multi = 1):
self.multi = multi
def __repr__(self):
return '\n' + '<BR>'*self.multi + '\n'
class NOBR:
"""No Break.
All the text between the start and end of the NOBR elements cannot
have line breaks inserted between them by the browser's wrapping
mechanism. """
def __init__(self, text=''):
self.text = text
def __repr__(self):
return '<NOBR>%s</NOBR>' % self.text
class Center:
"""A general form to center arbitrary content.
Netscape folks say this is prefered over <P align=center>.
"""
def __init__(self, text=''):
self.text = text
def __repr__(self):
return '<CENTER>%s</CENTER>' % self.text
class Indent(Text):
"""Indent text using the UL tag.
"""
def __init__(self, text=''):
self.text = text
def __repr__(self):
return '<UL>\n%s</UL>' % self.text
class Comment:
"""Place a comment internal to the html doc.
Will not be visible from the browser.
"""
def __init__(self, text=''):
self.text = text
def __repr__(self):
return '\n<!-- %s -->\n' % self.text
|