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
|
#!/usr/bin/python
import webbrowser
from ms.html_utils import makelink, tidy
from ms.iter_utils import chop
import os, sys, re
INCLUDE = re.compile(r"\n.. include::\s+([\w\./]+)")
BGCOLOR = "lightblue"
CSS = """
<STYLE TYPE="text/css">
body { font-size: 160%; }
</STYLE>
"""
class TableList(list):
"""A table is a list of lists/tuple of the same length.
A table has methods to display itself as plain text and
as HTML. """
border = 1
color = "white"
def __str__(self):
return self.to_html()
def to_text(self):
return "\n".join(["\t".join(map(str,t)) for t in self])
def _gen_html(self):
"""Returns an HTML table as an iterator."""
yield "\n<table border=%r summary='a table'>\n" % self.border
header = self.header
for row in self:
yield "<tr>\n "
for el in row:
if header:
yield "<th>%s</th> " % el
else:
yield '<td bgcolor="%s">%s</td> ' % \
(getattr(row, "color", self.color), el)
yield "\n</tr>\n"
header = False
yield "</table>\n"
def to_html(self, header = False):
"""Generates an HTML table."""
self.header = header
return "".join(self._gen_html())
@classmethod
def make(cls, *args, **kw): # convenience factory
ncols = kw.get("ncols", 2)
missingcols = ncols - (len(args) % ncols)
args += ('',) * missingcols # add empty columns to fill the table
table = cls(chop(args, ncols))
vars(table).update(kw)
return table
@classmethod
def row(cls, *args, **kw): # convenience factory
table = cls([args])
vars(table).update(kw)
return table
@classmethod
def col(cls, *args, **kw): # convenience factory
table = cls([(arg,) for arg in args])
vars(table).update(kw)
return table
def testTable():
t=TableList([
['a', 1, 2],
['b',2, 3],
['c',2, 3],
])
print t.to_html()
print t
class HTMLDocument(list):
def __init__(self, ls = []):
super(HTMLDocument, self).__init__(ls)
self.reorder()
def reorder(self):
"""Generates circular 'prev' and 'next' tabs."""
self.npages = len(self)
self.pageindex = []
for i, page in enumerate(self):
page.prev = self[i-1].namext
if i == self.npages-1: i = -1
page.next = self[i+1].namext
page.first = self[0].namext
page.last = self[-1].namext
page.document = self
self.pageindex.append(page)
class HTMLPage(object):
def __init__(self, id_txt):
self.BGCOLOR = BGCOLOR
id, txt = id_txt
if isinstance(id, int):
self.name = "P%02d" % (id + 1)
else:
self.name = id
self.namext = self.name + ".html"
lines = txt.splitlines()
if lines: # empty page
self.title = lines[0]
self.txt = "\n".join(lines[2:])
else:
self.title = ""
self.txt = ""
self.txt = "<h1>%s</h1><br/>\n" % self.title + \
INCLUDE.sub(lambda m: "<pre>%s</pre>" %
file(m.group(1)).read(), self.txt)
self.head = """
<head>
<meta name="generator" content="Generated by Python">
<title>%s</title>
%s
</head>""" % (self.name, CSS)
def html(self):
self.body = """\n<body bgcolor="%(BGCOLOR)s">\n
%(txt)s
</body>
""" % vars(self)
return """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>%s\n</html>""" % (self.head + self.body)
def __str__(self):
box = TableList.make(
makelink(self.next, "Next"),
makelink(self.prev, "Prev"),
makelink(self.last, "Last"),
makelink(self.first, "First"),
"Current",
self.name,
border = 0,
color = BGCOLOR)
logo = TableList.col(
'<img src = "accu2005.png" alt = "logo">',
border = 0,
color = BGCOLOR)
links = [makelink(page.namext, page.name)
for page in self.document.pageindex]
opts = dict(border = 0, color = BGCOLOR, ncols=2)
index = TableList.make(*links, **opts)
self.txt = str(
TableList.row("<small>%s</small>" % TableList.col(box, index, logo,
border = 0, color = BGCOLOR),
self.txt, border = 0, color = BGCOLOR))
return self.html()
#return tidy(self.html(), "-i")[0]
def main(fname):
BLANK = re.compile(r"\n\n\n\s*")
chunks = BLANK.split(file(fname).read())
pages = HTMLDocument(map(HTMLPage, enumerate(chunks)))
for page in pages:
print >> file(page.namext, "w"), page
#os.system("xterm -e elinks P01.html&")
webbrowser.open("file:%s/P01.html" % os.getcwd())
if __name__ == "__main__":
main(sys.argv[1])
|