def formulaeToImages(document, dir, _system=os.system): # gather all macros macros = '' for node in domhelpers.findElementsWithAttribute(document, 'class', 'latexmacros'): macros += domhelpers.getNodeText(node) node.parentNode.removeChild(node) i = 0 for node in domhelpers.findElementsWithAttribute(document, 'class', 'latexformula'): latexText='''\\documentclass[12pt]{amsart}%s \\begin{document}\[%s\] \\end{document}''' % (macros, domhelpers.getNodeText(node)) # This file really should be cleaned up by this function, or placed # somewhere such that the calling code can find it and clean it up. file = tempfile.mktemp() f = open(file+'.tex', 'w') f.write(latexText) f.close() _system('latex %s.tex' % file) _system('dvips %s.dvi -o %s.ps' % (os.path.basename(file), file)) baseimgname = 'latexformula%d.png' % i imgname = os.path.join(dir, baseimgname) i += 1 _system('pstoimg -type png -crop a -trans -interlace -out ' '%s %s.ps' % (imgname, file)) newNode = dom.parseString( '<span><br /><img src="%s" /><br /></span>' % ( baseimgname,)).documentElement node.parentNode.replaceChild(newNode, node)
def formulaeToImages(document, dir): # gather all macros macros = '' for node in domhelpers.findElementsWithAttribute(document, 'class', 'latexmacros'): macros += domhelpers.getNodeText(node) node.parentNode.removeChild(node) i = 0 for node in domhelpers.findElementsWithAttribute(document, 'class', 'latexformula'): latexText='''\\documentclass[12pt]{amsart}%s \\begin{document}\[%s\] \\end{document}''' % (macros, domhelpers.getNodeText(node)) file = tempfile.mktemp() open(file+'.tex', 'w').write(latexText) os.system('latex %s.tex' % file) os.system('dvips %s.dvi -o %s.ps' % (os.path.basename(file), file)) baseimgname = 'latexformula%d.png' % i imgname = os.path.join(dir, baseimgname) i += 1 os.system('pstoimg -type png -crop a -trans -interlace -out ' '%s %s.ps' % (imgname, file)) newNode = microdom.parseString('<span><br /><img src="%s" /><br /></span>' % baseimgname) node.parentNode.replaceChild(newNode, node)
def formulaeToImages(document, dir): # gather all macros macros = '' for node in domhelpers.findElementsWithAttribute(document, 'class', 'latexmacros'): macros += domhelpers.getNodeText(node) node.parentNode.removeChild(node) i = 0 for node in domhelpers.findElementsWithAttribute(document, 'class', 'latexformula'): latexText = '''\\documentclass[12pt]{amsart}%s \\begin{document}\[%s\] \\end{document}''' % (macros, domhelpers.getNodeText(node)) file = tempfile.mktemp() f = open(file + '.tex', 'w') f.write(latexText) f.close() os.system('latex %s.tex' % file) os.system('dvips %s.dvi -o %s.ps' % (os.path.basename(file), file)) baseimgname = 'latexformula%d.png' % i imgname = os.path.join(dir, baseimgname) i += 1 os.system('pstoimg -type png -crop a -trans -interlace -out ' '%s %s.ps' % (imgname, file)) newNode = microdom.parseString( '<span><br /><img src="%s" /><br /></span>' % baseimgname) node.parentNode.replaceChild(newNode, node)
def formulaeToImages(document, dir, _system=os.system): # gather all macros macros = '' for node in domhelpers.findElementsWithAttribute(document, 'class', 'latexmacros'): macros += domhelpers.getNodeText(node) node.parentNode.removeChild(node) i = 0 for node in domhelpers.findElementsWithAttribute(document, 'class', 'latexformula'): latexText = '''\\documentclass[12pt]{amsart}%s \\begin{document}\[%s\] \\end{document}''' % (macros, domhelpers.getNodeText(node)) # This file really should be cleaned up by this function, or placed # somewhere such that the calling code can find it and clean it up. file = tempfile.mktemp() f = open(file + '.tex', 'w') f.write(latexText) f.close() _system('latex %s.tex' % file) _system('dvips %s.dvi -o %s.ps' % (os.path.basename(file), file)) baseimgname = 'latexformula%d.png' % i imgname = os.path.join(dir, baseimgname) i += 1 _system('pstoimg -type png -crop a -trans -interlace -out ' '%s %s.ps' % (imgname, file)) newNode = dom.parseString('<span><br /><img src="%s" /><br /></span>' % (baseimgname, )).documentElement node.parentNode.replaceChild(newNode, node)
def getTitleLink(url): d = client.getPage("http://moshez.org/discuss/rss") d.addCallback(microdom.parseString) d.addCallback(lambda d: domhelpers.findNodesNamed(d, 'item')[0]) d.addCallback(lambda d: ( domhelpers.getNodeText(domhelpers.findNodesNamed(d, 'link')[0]), domhelpers.getNodeText(domhelpers.findNodesNamed(d, 'title')[0]), )) return d
def check_title(self, dom, filename): doc = dom.documentElement title = domhelpers.findNodesNamed(dom, 'title') if len(title) != 1: return self._reportError(filename, doc, 'not exactly one title') h1 = domhelpers.findNodesNamed(dom, 'h1') if len(h1) != 1: return self._reportError(filename, doc, 'not exactly one h1') if domhelpers.getNodeText(h1[0]) != domhelpers.getNodeText(title[0]): self._reportError(filename, h1[0], 'title and h1 text differ')
def check_title(self, dom, filename): doc = dom.documentElement title = domhelpers.findNodesNamed(dom, 'title') if len(title)!=1: return self._reportError(filename, doc, 'not exactly one title') h1 = domhelpers.findNodesNamed(dom, 'h1') if len(h1)!=1: return self._reportError(filename, doc, 'not exactly one h1') if domhelpers.getNodeText(h1[0]) != domhelpers.getNodeText(title[0]): self._reportError(filename, h1[0], 'title and h1 text differ')
def test_getNodeUnicodeText(self): """ L{domhelpers.getNodeText} returns a C{unicode} string when text nodes are represented in the DOM with unicode, whether or not there are non-ASCII characters present. """ node = self.dom.parseString("<foo>bar</foo>") text = domhelpers.getNodeText(node) self.assertEqual(text, u"bar") self.assertIsInstance(text, unicode) node = self.dom.parseString(u"<foo>\N{SNOWMAN}</foo>".encode('utf-8')) text = domhelpers.getNodeText(node) self.assertEqual(text, u"\N{SNOWMAN}") self.assertIsInstance(text, unicode)
def visitNode_a_href(self, node): supported_schemes = ['http', 'https', 'ftp', 'mailto'] href = node.getAttribute('href') if urlparse.urlparse(href)[0] in supported_schemes: text = domhelpers.getNodeText(node) self.visitNodeDefault(node) if text != href: self.writer('\\footnote{%s}' % latexEscape(href)) else: path, fragid = (href.split('#', 1) + [None])[:2] if path == '': path = self.filename else: path = os.path.join(os.path.dirname(self.filename), path) #if path == '': #path = os.path.basename(self.filename) #else: # # Hack for linking to man pages from howtos, i.e. # # ../doc/foo-man.html -> foo-man.html # path = os.path.basename(path) path = realpath(path) if fragid: ref = path + 'HASH' + fragid else: ref = path self.writer('\\textit{') self.visitNodeDefault(node) self.writer('}') self.writer('\\loreref{%s}' % ref)
def test_getNodeText(self): """ L{getNodeText} returns the concatenation of all the text data at or beneath the node passed to it. """ node = self.dom.parseString('<foo><bar>baz</bar><bar>quux</bar></foo>') self.assertEqual(domhelpers.getNodeText(node), "bazquux")
def visitNode_body(self, node): # Adapted from tree.generateToC self.fontStack = [('standard', None)] # Title slide self.writer(self.start_h2) self.writer(self.title) self.writer(self.end_h2) self.writer('%center\n\n\n\n\n') for authorNode in domhelpers.findElementsWithAttribute(node, 'class', 'author'): getLatexText(authorNode, self.writer, entities=entities) self.writer('\n') # Table of contents self.writer(self.start_h2) self.writer(self.title) self.writer(self.end_h2) for element in getHeaders(node): level = int(element.tagName[1])-1 self.writer(level * '\t') self.writer(domhelpers.getNodeText(element)) self.writer('\n') self.visitNodeDefault(node)
def visitNode_a_href(self, node): supported_schemes=['http', 'https', 'ftp', 'mailto'] href = node.getAttribute('href') if urlparse.urlparse(href)[0] in supported_schemes: text = domhelpers.getNodeText(node) self.visitNodeDefault(node) if text != href: self.writer('\\footnote{%s}' % latexEscape(href)) else: path, fragid = (href.split('#', 1) + [None])[:2] if path == '': path = self.filename else: path = os.path.join(os.path.dirname(self.filename), path) #if path == '': #path = os.path.basename(self.filename) #else: # # Hack for linking to man pages from howtos, i.e. # # ../doc/foo-man.html -> foo-man.html # path = os.path.basename(path) path = realpath(path) if fragid: ref = path + 'HASH' + fragid else: ref = path self.writer('\\textit{') self.visitNodeDefault(node) self.writer('}') self.writer('\\loreref{%s}' % ref)
def check_pre_py_listing(self, dom, filename): for node in domhelpers.findNodesNamed(dom, 'pre'): if node.getAttribute('class') == 'python': try: text = domhelpers.getNodeText(node) # Fix < and > text = text.replace('>', '>').replace('<', '<') # Strip blank lines lines = filter(None, [l.rstrip() for l in text.split('\n')]) # Strip leading space while not [ 1 for line in lines if line[:1] not in ('', ' ') ]: lines = [line[1:] for line in lines] text = '\n'.join(lines) + '\n' try: parser.suite(text) except SyntaxError: # Pretend the "..." idiom is syntactically valid text = text.replace("...", "'...'") parser.suite(text) except SyntaxError as e: self._reportError(filename, node, 'invalid python code:' + str(e))
def getDescription(self): # http://purl.org/dc/elements/1.1/ description l = domhelpers.findNodesNamed(self.dom, 'description') if l: return domhelpers.getNodeText(l[0]) else: return None
def generateToC(document): """ Create a table of contents for the given document. @type document: A DOM Node or Document @rtype: A DOM Node @return: a Node containing a table of contents based on the headers of the given document. """ toc, level, id = '\n<ol>\n', 0, 0 for element in getHeaders(document): elementLevel = int(element.tagName[1]) - 2 toc += (level - elementLevel) * '</ul>\n' toc += (elementLevel - level) * '<ul>' toc += '<li><a href="#auto%d">' % id toc += domhelpers.getNodeText(element) toc += '</a></li>\n' level = elementLevel anchor = microdom.parseString('<a name="auto%d" />' % id).documentElement element.childNodes.append(anchor) id += 1 toc += '</ul>\n' * level toc += '</ol>\n' return microdom.parseString(toc).documentElement
def visitNode_body(self, node): # Adapted from tree.generateToC self.fontStack = [('standard', None)] # Title slide self.writer(self.start_h2) self.writer(self.title) self.writer(self.end_h2) self.writer('%center\n\n\n\n\n') for authorNode in domhelpers.findElementsWithAttribute( node, 'class', 'author'): getLatexText(authorNode, self.writer, entities=entities) self.writer('\n') # Table of contents self.writer(self.start_h2) self.writer(self.title) self.writer(self.end_h2) for element in getHeaders(node): level = int(element.tagName[1]) - 1 self.writer(level * '\t') self.writer(domhelpers.getNodeText(element)) self.writer('\n') self.visitNodeDefault(node)
def generateToC(document): """ Create a table of contents for the given document. @type document: A DOM Node or Document @rtype: A DOM Node @return: a Node containing a table of contents based on the headers of the given document. """ toc, level, id = "\n<ol>\n", 0, 0 for element in getHeaders(document): elementLevel = int(element.tagName[1]) - 2 toc += (level - elementLevel) * "</ul>\n" toc += (elementLevel - level) * "<ul>" toc += '<li><a href="#auto%d">' % id toc += domhelpers.getNodeText(element) toc += "</a></li>\n" level = elementLevel anchor = microdom.parseString('<a name="auto%d" />' % id).documentElement element.childNodes.append(anchor) id += 1 toc += "</ul>\n" * level toc += "</ol>\n" return microdom.parseString(toc).documentElement
def footnotes(document): """ Find footnotes in the given document, move them to the end of the body, and generate links to them. A footnote is any node with a C{class} attribute set to C{footnote}. Footnote links are generated as superscript. Footnotes are collected in a C{ol} node at the end of the document. @type document: A DOM Node or Document @param document: The input document which contains all of the content to be presented. @return: C{None} """ footnotes = domhelpers.findElementsWithAttribute(document, "class", "footnote") if not footnotes: return footnoteElement = microdom.Element("ol") id = 1 for footnote in footnotes: href = microdom.parseString('<a href="#footnote-%(id)d">' "<super>%(id)d</super></a>" % vars()).documentElement text = " ".join(domhelpers.getNodeText(footnote).split()) href.setAttribute("title", text) target = microdom.Element("a", attributes={"name": "footnote-%d" % id}) target.childNodes = [footnote] footnoteContent = microdom.Element("li") footnoteContent.childNodes = [target] footnoteElement.childNodes.append(footnoteContent) footnote.parentNode.replaceChild(href, footnote) id += 1 body = domhelpers.findNodesNamed(document, "body")[0] header = microdom.parseString("<h2>Footnotes</h2>").documentElement body.childNodes.append(header) body.childNodes.append(footnoteElement)
def render_GET(self, request): url = urlTemplate % request.args['user'][0] client.getPage(url).addCallback(microdom.parseString).addCallback( lambda t: domhelpers.findNodesNamed(t, 'item') ).addCallback(lambda itms: zip( [domhelpers.findNodesNamed(x, 'title')[0] for x in itms ], [domhelpers.findNodesNamed(x, 'link')[0] for x in itms] )).addCallback( lambda itms: '<html><head></head><body><ul>%s</ul></body></html>' % '\n'.join([ '<li><a href="%s">%s</a></li>' % (domhelpers.getNodeText(link), domhelpers.getNodeText(title)) for (title, link) in itms ])).addCallback(lambda s: (request.write(s), request.finish( ))).addErrback(lambda e: (request.write('Error: %s' % e), request.finish())) return server.NOT_DONE_YET
def _replaceWithListing(node, val, filename, class_): captionTitle = domhelpers.getNodeText(node) if captionTitle == os.path.basename(filename): captionTitle = 'Source listing' text = ('<div class="%s">%s<div class="caption">%s - ' '<a href="%s"><span class="filename">%s</span></a></div></div>' % (class_, val, captionTitle, filename, filename)) newnode = microdom.parseString(text).documentElement node.parentNode.replaceChild(newnode, node)
def _replaceWithListing(node, val, filename, class_): captionTitle = domhelpers.getNodeText(node) if captionTitle == os.path.basename(filename): captionTitle = 'Source listing' text = ('<div class="%s">%s<div class="caption">%s - ' '<a href="%s"><span class="filename">%s</span></a></div></div>' % (class_, val, captionTitle, filename, filename)) newnode = dom.parseString(text).documentElement node.parentNode.replaceChild(newnode, node)
def check_texturl_matches_href(self, dom, filename): for node in domhelpers.findNodesNamed(dom, "a"): if not node.hasAttribute("href"): continue text = domhelpers.getNodeText(node) proto = urlparse.urlparse(text)[0] if proto and " " not in text: if text != node.getAttribute("href"): self._reportError(filename, node, "link text does not match href")
def render_GET(self, request): url = urlTemplate % request.args['user'][0] client.getPage(url).addCallback( microdom.parseString).addCallback( lambda t: domhelpers.findNodesNamed(t, 'item')).addCallback( lambda itms: zip([domhelpers.findNodesNamed(x, 'title')[0] for x in itms], [domhelpers.findNodesNamed(x, 'link')[0] for x in itms] )).addCallback( lambda itms: '<html><head></head><body><ul>%s</ul></body></html>' % '\n'.join( ['<li><a href="%s">%s</a></li>' % ( domhelpers.getNodeText(link), domhelpers.getNodeText(title)) for (title, link) in itms]) ).addCallback(lambda s: (request.write(s),request.finish())).addErrback( lambda e: (request.write('Error: %s' % e),request.finish())) return server.NOT_DONE_YET
def check_texturl_matches_href(self, dom, filename): for node in domhelpers.findNodesNamed(dom, 'a'): if not node.hasAttribute('href'): continue text = domhelpers.getNodeText(node) proto = urlparse.urlparse(text)[0] if proto and ' ' not in text: if text != node.getAttribute('href'): self._reportError(filename, node, 'link text does not match href')
def findRSSFolder(dom, folderName=None): if folderName is None: folderName = "RSS" if folderName == "": return dom for folder in domhelpers.findNodesNamed(dom, "folder"): for title in domhelpers.findNodesNamed(folder, "title"): text = domhelpers.getNodeText(title) if text == folderName: return folder return None
def extractRSSFeeds(dom): for bookmark in domhelpers.findNodesNamed(dom, "bookmark"): titleNodes = domhelpers.findNodesNamed(bookmark, "title") if titleNodes: title = domhelpers.getNodeText(titleNodes[0]) else: title = None url = bookmark.getAttribute("href") url = urllib.unquote(url) yield title, url
def addItem(headerElement, parent): anchor = dom.Element('a') name = 'auto%d' % (auto(), ) anchor.setAttribute('href', '#' + name) text = dom.Text() text.data = domhelpers.getNodeText(headerElement) anchor.appendChild(text) headerNameItem = dom.Element('li') headerNameItem.appendChild(anchor) parent.appendChild(headerNameItem) anchor = dom.Element('a') anchor.setAttribute('name', name) headerElement.appendChild(anchor)
def render_GET(self, request): """ Get an xml feed from LiveJournal and construct a new HTML page using the 'title' and 'link' parsed from the xml document. """ url = urlTemplate % request.args['user'][0] client.getPage(url, timeout=30).addCallback( microdom.parseString).addCallback( lambda t: domhelpers.findNodesNamed(t, 'item')).addCallback( lambda itms: zip([domhelpers.findNodesNamed(x, 'title')[0] for x in itms], [domhelpers.findNodesNamed(x, 'link')[0] for x in itms] )).addCallback( lambda itms: '<html><head></head><body><ul>%s</ul></body></html>' % '\n'.join( ['<li><a href="%s">%s</a></li>' % ( domhelpers.getNodeText(link), domhelpers.getNodeText(title)) for (title, link) in itms]) ).addCallback(lambda s: (request.write(s),request.finish())).addErrback( lambda e: (request.write('Error: %s' % e),request.finish())) return server.NOT_DONE_YET
def addItem(headerElement, parent): anchor = dom.Element('a') name = 'auto%d' % (auto(),) anchor.setAttribute('href', '#' + name) text = dom.Text() text.data = domhelpers.getNodeText(headerElement) anchor.appendChild(text) headerNameItem = dom.Element('li') headerNameItem.appendChild(anchor) parent.appendChild(headerNameItem) anchor = dom.Element('a') anchor.setAttribute('name', name) headerElement.appendChild(anchor)
def visitNode_title(self, node): self.writer('@node ') self.visitNodeDefault(node) self.writer('\n') self.writer('@section ') self.visitNodeDefault(node) self.writer('\n') headers = tree.getHeaders(domhelpers.getParents(node)[-1]) if not headers: return self.writer('@menu\n') for header in headers: self.writer('* %s::\n' % domhelpers.getNodeText(header)) self.writer('@end menu\n')
def splitIntoSlides(document): body = domhelpers.findNodesNamed(document, 'body')[0] slides = [] slide = [] title = '(unset)' for child in body.childNodes: if isinstance(child, dom.Element) and child.tagName == 'h2': if slide: slides.append((title, slide)) slide = [] title = domhelpers.getNodeText(child) else: slide.append(child) slides.append((title, slide)) return slides
def splitIntoSlides(document): body = domhelpers.findNodesNamed(document, 'body')[0] slides = [] slide = [] title = '(unset)' for child in body.childNodes: if isinstance(child, microdom.Element) and child.tagName == 'h2': if slide: slides.append((title, slide)) slide = [] title = domhelpers.getNodeText(child) else: slide.append(child) slides.append((title, slide)) return slides
def visitNode_a_listing(self, node): fileName = os.path.join(self.currDir, node.getAttribute('href')) self.writer('\\begin{verbatim}\n') lines = map(string.rstrip, open(fileName).readlines()) lines = lines[int(node.getAttribute('skipLines', 0)):] self.writer(text.removeLeadingTrailingBlanks('\n'.join(lines))) self.writer('\\end{verbatim}') # Write a caption for this source listing fileName = os.path.basename(fileName) caption = domhelpers.getNodeText(node) if caption == fileName: caption = 'Source listing' self.writer('\parbox[b]{\linewidth}{\\begin{center}%s --- ' '\\begin{em}%s\\end{em}\\end{center}}' % (latexEscape(caption), latexEscape(fileName)))
def generateToC(document): toc, level, id = '\n<ol>\n', 0, 0 for element in getHeaders(document): elementLevel = int(element.tagName[1])-2 toc += (level-elementLevel)*'</ul>\n' toc += (elementLevel-level)*'<ul>' toc += '<li><a href="#auto%d">' % id toc += domhelpers.getNodeText(element) toc += '</a></li>\n' level = elementLevel anchor = microdom.parseString('<a name="auto%d" />' % id).documentElement element.childNodes.append(anchor) id += 1 toc += '</ul>\n' * level toc += '</ol>\n' return microdom.parseString(toc).documentElement
def check_pre_py_listing(self, dom, filename): for node in domhelpers.findNodesNamed(dom, "pre"): if node.getAttribute("class") == "python": try: text = domhelpers.getNodeText(node) # Fix < and > text = text.replace(">", ">").replace("<", "<") # Strip blank lines lines = filter(None, [l.rstrip() for l in text.split("\n")]) # Strip leading space while not [1 for line in lines if line[:1] not in ("", " ")]: lines = [line[1:] for line in lines] text = "\n".join(lines) + "\n" try: parser.suite(text) except parserErrors, e: # Pretend the "..." idiom is syntactically valid text = text.replace("...", "'...'") parser.suite(text) except parserErrors, e: self._reportError(filename, node, "invalid python code:" + str(e))
def check_pre_py_listing(self, dom, filename): for node in domhelpers.findNodesNamed(dom, 'pre'): if node.getAttribute('class') == 'python': try: text = domhelpers.getNodeText(node) # Fix < and > text = text.replace('>', '>').replace('<', '<') # Strip blank lines lines = filter(None,[l.rstrip() for l in text.split('\n')]) # Strip leading space while not [1 for line in lines if line[:1] not in ('',' ')]: lines = [line[1:] for line in lines] text = '\n'.join(lines) + '\n' try: parser.suite(text) except parserErrors, e: # Pretend the "..." idiom is syntactically valid text = text.replace("...","'...'") parser.suite(text) except parserErrors, e: self._reportError(filename, node, 'invalid python code:' + str(e))
def check_80_columns(self, dom, filename): for node in domhelpers.findNodesNamed(dom, 'pre'): for line in domhelpers.getNodeText(node).split('\n'): if len(line.rstrip()) > 80: self._reportError(filename, node, 'text wider than 80 columns in pre') for node in domhelpers.findNodesNamed(dom, 'a'): if node.getAttribute('class', '').endswith('listing'): try: fn = os.path.dirname(filename) fn = os.path.join(fn, node.getAttribute('href')) lines = open(fn,'r').readlines() except: self._reportError(filename, node, 'bad listing href: %r' % node.getAttribute('href')) continue for line in lines: if len(line.rstrip()) > 80: self._reportError(filename, node, 'listing wider than 80 columns')
def footnotes(document): """ Find footnotes in the given document, move them to the end of the body, and generate links to them. A footnote is any node with a C{class} attribute set to C{footnote}. Footnote links are generated as superscript. Footnotes are collected in a C{ol} node at the end of the document. @type document: A DOM Node or Document @param document: The input document which contains all of the content to be presented. @return: C{None} """ footnotes = domhelpers.findElementsWithAttribute(document, "class", "footnote") if not footnotes: return footnoteElement = dom.Element('ol') id = 1 for footnote in footnotes: href = dom.parseString('<a href="#footnote-%(id)d">' '<super>%(id)d</super></a>' % vars()).documentElement text = ' '.join(domhelpers.getNodeText(footnote).split()) href.setAttribute('title', text) target = dom.Element('a') target.setAttribute('name', 'footnote-%d' % (id, )) target.childNodes = [footnote] footnoteContent = dom.Element('li') footnoteContent.childNodes = [target] footnoteElement.childNodes.append(footnoteContent) footnote.parentNode.replaceChild(href, footnote) id += 1 body = domhelpers.findNodesNamed(document, "body")[0] header = dom.parseString('<h2>Footnotes</h2>').documentElement body.childNodes.append(header) body.childNodes.append(footnoteElement)
def visitNode_a_listing(self, node): """ Writes a I{verbatim} block when it encounters a code listing (represented by an I{a} element with a I{listing} class). @param node: The element to process. @type node: C{xml.dom.minidom.Element} """ fileName = os.path.join(self.currDir, node.getAttribute('href')) self.writer('\\begin{verbatim}\n') lines = map(str.rstrip, open(fileName).readlines()) skipLines = int(node.getAttribute('skipLines') or 0) lines = lines[skipLines:] self.writer(tree._removeLeadingTrailingBlankLines('\n'.join(lines))) self.writer('\\end{verbatim}') # Write a caption for this source listing fileName = os.path.basename(fileName) caption = domhelpers.getNodeText(node) if caption == fileName: caption = 'Source listing' self.writer('\parbox[b]{\linewidth}{\\begin{center}%s --- ' '\\begin{em}%s\\end{em}\\end{center}}' % (latexEscape(caption), latexEscape(fileName)))
def footnotes(document): footnotes = domhelpers.findElementsWithAttribute(document, "class", "footnote") if not footnotes: return footnoteElement = microdom.Element('ol') id = 1 for footnote in footnotes: href = microdom.parseString('<a href="#footnote-%(id)d">' '<super>%(id)d</super></a>' % vars()).documentElement text = ' '.join(domhelpers.getNodeText(footnote).split()) href.setAttribute('title', text) target = microdom.Element('a', attributes={'name': 'footnote-%d' % id}) target.childNodes = [footnote] footnoteContent = microdom.Element('li') footnoteContent.childNodes = [target] footnoteElement.childNodes.append(footnoteContent) footnote.parentNode.replaceChild(href, footnote) id += 1 body = domhelpers.findNodesNamed(document, "body")[0] header = microdom.parseString('<h2>Footnotes</h2>').documentElement body.childNodes.append(header) body.childNodes.append(footnoteElement)
def visitNode_title(self, node): self.title = domhelpers.getNodeText(node)
def check_style(self, dom, filename): for node in domhelpers.findNodesNamed(dom, 'style'): if domhelpers.getNodeText(node) != '': self._reportError(filename, node, 'hand hacked style')
def visitNode_span_latexformula(self, node): self.writer('\[') self.writer(domhelpers.getNodeText(node)) self.writer('\]')
def visitNode_div_latexmacros(self, node): self.writer(domhelpers.getNodeText(node))