本文整理汇总了Python中twisted.web.domhelpers.findElementsWithAttribute函数的典型用法代码示例。如果您正苦于以下问题:Python findElementsWithAttribute函数的具体用法?Python findElementsWithAttribute怎么用?Python findElementsWithAttribute使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了findElementsWithAttribute函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: formulaeToImages
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)
示例2: formulaeToImages
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)
示例3: test_findElementsWithAttribute
def test_findElementsWithAttribute(self):
doc1 = self.dom.parseString('<a foo="1"><b foo="2"/><c foo="1"/><d/></a>')
node_list = domhelpers.findElementsWithAttribute(doc1, 'foo')
actual = ''.join([node.tagName for node in node_list])
self.assertEqual(actual, 'abc')
node_list = domhelpers.findElementsWithAttribute(doc1, 'foo', '1')
actual = ''.join([node.tagName for node in node_list])
self.assertEqual(actual, 'ac')
示例4: test_findElementsWithAttribute
def test_findElementsWithAttribute(self):
doc1=microdom.parseString('<a foo="1"><b foo="2"/><c foo="1"/><d/></a>')
node_list=domhelpers.findElementsWithAttribute(doc1, 'foo')
actual=''.join([node.tagName for node in node_list])
expected='abc'
assert actual==expected, 'expected %s, got %s' % (expected, actual)
node_list=domhelpers.findElementsWithAttribute(doc1, 'foo', '1')
actual=''.join([node.tagName for node in node_list])
expected='ac'
assert actual==expected, 'expected %s, got %s' % (expected, actual)
示例5: munge
def munge(document, template, linkrel, d, fullpath, ext, url, config):
fixRelativeLinks(template, linkrel)
addMtime(template, fullpath)
removeH1(document)
fixAPI(document, url)
fontifyPython(document)
addPyListings(document, d)
addHTMLListings(document, d)
fixLinks(document, ext)
putInToC(template, generateToC(document))
footnotes(document)
notes(document)
# Insert the document into the template
title = domhelpers.findNodesNamed(document, 'title')[0].childNodes
setTitle(template, title)
authors = domhelpers.findNodesNamed(document, 'link')
authors = [(n.getAttribute('title',''), n.getAttribute('href', ''))
for n in authors if n.getAttribute('rel', '') == 'author']
setAuthors(template, authors)
body = domhelpers.findNodesNamed(document, "body")[0]
tmplbody = domhelpers.findElementsWithAttribute(template, "class",
"body")[0]
tmplbody.childNodes = body.childNodes
tmplbody.setAttribute("class", "content")
示例6: makeBook
def makeBook(dom, d):
body = microdom.Element('body')
body.appendChild(domhelpers.findNodesNamed(dom, 'h1')[0])
toc = domhelpers.findElementsWithAttribute(dom, 'class', 'toc')[0]
toc = domhelpers.findNodesNamed(toc, 'li')
for node in toc:
if (node.hasAttribute('class') and
node.getAttribute('class')=='tocignore'):
continue
parents = domhelpers.getParents(node)
nodeLevel = len([1 for parent in parents if hasattr(parent, 'tagName')
and parent.tagName in ('ol', 'ul')])
data = node.childNodes[0].data != ''
if not data:
node = node.childNodes[1]
newNode = lowerDocument(node.getAttribute('href'), d, nodeLevel)
for child in newNode.childNodes:
body.appendChild(child)
else:
text = microdom.Text(node.childNodes[0].data)
newNode = microdom.Element('h'+str(nodeLevel))
newNode.appendChild(text)
body.appendChild(newNode)
origBody = domhelpers.findNodesNamed(dom, 'body')[0]
origBody.parentNode.replaceChild(body, origBody)
示例7: fixAPI
def fixAPI(document, url):
"""
Replace API references with links to API documentation.
@type document: A DOM Node or Document
@param document: The input document which contains all of the content to be
presented.
@type url: C{str}
@param url: A string which will be interpolated with the fully qualified
Python name of any API reference encountered in the input document, the
result of which will be used as a link to API documentation for that name
in the output document.
@return: C{None}
"""
# API references
for node in domhelpers.findElementsWithAttribute(document, "class", "API"):
fullname = _getAPI(node)
anchor = dom.Element('a')
anchor.setAttribute('href', url % (fullname,))
anchor.setAttribute('title', fullname)
while node.childNodes:
child = node.childNodes[0]
node.removeChild(child)
anchor.appendChild(child)
node.appendChild(anchor)
if node.hasAttribute('base'):
node.removeAttribute('base')
示例8: setIndexLink
def setIndexLink(template, indexFilename):
if not indexFilename:
return
indexLinks = domhelpers.findElementsWithAttribute(template, "class", "index-link")
for link in indexLinks:
link.nodeName = link.tagName = link.endTagName = 'a'
link.attributes = InsensitiveDict({'href': indexFilename})
示例9: setAuthors
def setAuthors(template, authors):
# First, similarly to setTitle, insert text into an <div class="authors">
text = ''
for name, href in authors:
# FIXME: Do proper quoting/escaping (is it ok to use
# xml.sax.saxutils.{escape,quoteattr}?)
anchor = '<a href="%s">%s</a>' % (href, name)
if (name, href) == authors[-1]:
if len(authors) == 1:
text = anchor
else:
text += 'and ' + anchor
else:
text += anchor + ','
childNodes = microdom.parseString('<span>' + text +'</span>').childNodes
for node in domhelpers.findElementsWithAttribute(template,
"class", 'authors'):
node.childNodes.extend(childNodes)
# Second, add appropriate <link rel="author" ...> tags to the <head>.
head = domhelpers.findNodesNamed(template, 'head')[0]
authors = [microdom.parseString('<link rel="author" href="%s" title="%s"/>'
% (href, name)).childNodes[0]
for name, href in authors]
head.childNodes.extend(authors)
示例10: setTitle
def setTitle(template, title, chapterNumber):
"""
Add title and chapter number information to the template document.
The title is added to the end of the first C{title} tag and the end of the
first tag with a C{class} attribute set to C{title}. If specified, the
chapter is inserted before the title.
@type template: A DOM Node or Document
@param template: The output template which defines the presentation of the
version information.
@type title: C{list} of DOM Nodes
@param title: Nodes from the input document defining its title.
@type chapterNumber: C{int}
@param chapterNumber: The chapter number of this content in an overall
document. If not applicable, any C{False} value will result in this
information being omitted.
@return: C{None}
"""
for nodeList in (
domhelpers.findNodesNamed(template, "title"),
domhelpers.findElementsWithAttribute(template, "class", "title"),
):
if nodeList:
if numberer.getNumberSections() and chapterNumber:
nodeList[0].childNodes.append(microdom.Text("%s. " % chapterNumber))
nodeList[0].childNodes.extend(title)
示例11: addHTMLListings
def addHTMLListings(document, dir):
for node in domhelpers.findElementsWithAttribute(document, "class",
"html-listing"):
filename = node.getAttribute("href")
val = ('<pre class="htmlsource">\n%s</pre>' %
cgi.escape(open(os.path.join(dir, filename)).read()))
_replaceWithListing(node, val, filename, "html-listing")
示例12: visitNode_body
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)
示例13: footnotes
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)
示例14: addPyListings
def addPyListings(document, dir):
"""
Insert Python source listings into the given document from files in the
given directory based on C{py-listing} nodes.
Any node in C{document} with a C{class} attribute set to C{py-listing} will
have source lines taken from the file named in that node's C{href}
attribute (searched for in C{dir}) inserted in place of that node.
If a node has a C{skipLines} attribute, its value will be parsed as an
integer and that many lines will be skipped at the beginning of the source
file.
@type document: A DOM Node or Document
@param document: The document within which to make listing replacements.
@type dir: C{str}
@param dir: The directory in which to find source files containing the
referenced Python listings.
@return: C{None}
"""
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, writer=htmlizer.SmallerHTMLWriter)
val = outfile.getvalue()
_replaceWithListing(node, val, filename, "py-listing")
示例15: munge
def munge(document, template, linkrel, d, fullpath, ext, url, config):
# FIXME: This has *way* to much duplicated crap in common with tree.munge
#fixRelativeLinks(template, linkrel)
removeH1(document)
fixAPI(document, url)
fontifyPython(document)
addPyListings(document, d)
addHTMLListings(document, d)
#fixLinks(document, ext)
#putInToC(template, generateToC(document))
template = template.cloneNode(1)
# Insert the slides into the template
slides = []
pos = 0
for title, slide in splitIntoSlides(document):
t = template.cloneNode(1)
text = dom.Text()
text.data = title
setTitle(t, [text])
tmplbody = domhelpers.findElementsWithAttribute(t, "class", "body")[0]
tmplbody.childNodes = slide
tmplbody.setAttribute("class", "content")
# FIXME: Next/Prev links
# FIXME: Perhaps there should be a "Template" class? (setTitle/setBody
# could be methods...)
slides.append(HTMLSlide(t, title, pos))
pos += 1
insertPrevNextLinks(slides, os.path.splitext(os.path.basename(fullpath)), ext)
return slides