diff options
Diffstat (limited to 'pypers/twisted/maketalk.py')
-rw-r--r-- | pypers/twisted/maketalk.py | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/pypers/twisted/maketalk.py b/pypers/twisted/maketalk.py new file mode 100644 index 0000000..eaeb3de --- /dev/null +++ b/pypers/twisted/maketalk.py @@ -0,0 +1,97 @@ +#!/usr/bin/python +import webbrowser +from ms.html_utils import makelink, TableList +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 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.first, "First"), + makelink(self.last, "Last"), + makelink(self.namext, self.name), + '', + border = 0, + color = BGCOLOR) + logo = TableList.col( + '<img src = "cjlogo.jpg" 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(logo, box, index, + border = 0, color = BGCOLOR), + self.txt, border = 0, color = BGCOLOR)) + return self.html() + +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]) |