# Twisted, the Framework of Your Internet # Copyright (C) 2001 Matthew W. Lefkowitz # # This library is free software; you can redistribute it and/or # modify it under the terms of version 2.1 of the GNU Lesser General Public # License as published by the Free Software Foundation. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import re, os, cStringIO, time, cgi, string, urlparse from twisted import copyright from twisted.python import htmlizer, text from twisted.web import microdom, domhelpers import process, latex, indexer, numberer, htmlbook from twisted.python.util import InsensitiveDict # relative links to html files def fixLinks(document, ext): supported_schemes=['http', 'https', 'ftp', 'mailto'] for node in domhelpers.findElementsWithAttribute(document, 'href'): href = node.getAttribute("href") if urlparse.urlparse(href)[0] in supported_schemes: continue if node.getAttribute("class", "") == "absolute": continue if node.getAttribute("class", "").find('listing') != -1: continue # This is a relative link, so it should be munged. if href.endswith('html') or href[:href.rfind('#')].endswith('html'): fname, fext = os.path.splitext(href) if '#' in fext: fext = ext+'#'+fext.split('#', 1)[1] else: fext = ext node.setAttribute("href", fname + fext) def addMtime(document, fullpath): for node in domhelpers.findElementsWithAttribute(document, "class","mtime"): node.appendChild(microdom.Text(time.ctime(os.path.getmtime(fullpath)))) def _getAPI(node): base = "" if node.hasAttribute("base"): base = node.getAttribute("base") + "." return base+node.childNodes[0].nodeValue def fixAPI(document, url): # API references for node in domhelpers.findElementsWithAttribute(document, "class", "API"): fullname = _getAPI(node) node2 = microdom.Element('a', {'href': url%fullname, 'title': fullname}) node2.childNodes = node.childNodes node.childNodes = [node2] node.removeAttribute('base') def fontifyPython(document): def matcher(node): return (node.nodeName == 'pre' and node.hasAttribute('class') and node.getAttribute('class') == 'python') for node in domhelpers.findElements(document, matcher): fontifyPythonNode(node) def fontifyPythonNode(node): oldio = cStringIO.StringIO() latex.getLatexText(node, oldio.write, entities={'lt': '<', 'gt': '>', 'amp': '&'}) oldio = cStringIO.StringIO(oldio.getvalue().strip()+'\n') newio = cStringIO.StringIO() htmlizer.filter(oldio, newio) newio.seek(0) newel = microdom.parse(newio).documentElement newel.setAttribute("class", "python") node.parentNode.replaceChild(newel, node) def addPyListings(document, dir): for node in domhelpers.findElementsWithAttribute(document, "class", "py-listing"): filename = node.getAttribute("href") outfile = cStringIO.StringIO() lines = map(string.rstrip, open(os.path.join(dir, filename)).readlines()) data = '\n'.join(lines[int(node.getAttribute('skipLines', 0)):]) data = cStringIO.StringIO(text.removeLeadingTrailingBlanks(data)) htmlizer.filter(data, outfile) val = outfile.getvalue() _replaceWithListing(node, val, filename, "py-listing") def _replaceWithListing(node, val, filename, class_): captionTitle = domhelpers.getNodeText(node) if captionTitle == os.path.basename(filename): captionTitle = 'Source listing' text = ('
\n%s' % cgi.escape(open(os.path.join(dir, filename)).read())) _replaceWithListing(node, val, filename, "html-listing") def addPlainListings(document, dir): for node in domhelpers.findElementsWithAttribute(document, "class", "listing"): filename = node.getAttribute("href") val = ('
\n%s' % cgi.escape(open(os.path.join(dir, filename)).read())) _replaceWithListing(node, val, filename, "listing") def getHeaders(document): return domhelpers.findElements(document, lambda n,m=re.compile('h[23]$').match:m(n.nodeName)) def generateToC(document): toc, level, id = '\n