def test_munge(self): indexer.setIndexFilename("lore_index_file.html") doc = microdom.parse(open(self.file)) templ = microdom.parse(open(d['template'])) node = templ.cloneNode(1) tree.munge(doc, node, self.linkrel, os.path.dirname(self.file), self.file, d['ext'], d['baseurl'], d) self.assertEqualsFile('good_internal.xhtml', node.toprettyxml())
def lookupTemplate(self, request): """ Use acquisition to look up the template named by self.templateFile, located anywhere above this object in the heirarchy, and use it as the template. The first time the template is used it is cached for speed. """ if self.template: return microdom.parseString(self.template, caseInsensitive=0, preserveCase=0) if not self.templateDirectory: mod = sys.modules[self.__module__] if hasattr(mod, '__file__'): self.templateDirectory = os.path.split(mod.__file__)[0] # First see if templateDirectory + templateFile is a file templatePath = os.path.join(self.templateDirectory, self.templateFile) if not os.path.exists(templatePath): raise RuntimeError, "The template %r was not found." % templatePath # Check to see if there is an already parsed copy of it mtime = os.path.getmtime(templatePath) cachedTemplate = templateCache.get(templatePath, None) compiledTemplate = None if cachedTemplate is not None: if cachedTemplate[0] == mtime: compiledTemplate = templateCache[templatePath][1].cloneNode(deep=1) if compiledTemplate is None: compiledTemplate = microdom.parse(templatePath, caseInsensitive=0, preserveCase=0) templateCache[templatePath] = (mtime, compiledTemplate.cloneNode(deep=1)) return compiledTemplate
def lookupTemplate(self, request): """ Use acquisition to look up the template named by self.templateFile, located anywhere above this object in the heirarchy, and use it as the template. The first time the template is used it is cached for speed. """ if self.template: return microdom.parseString(self.template) if not self.templateDirectory: mod = sys.modules[self.__module__] if hasattr(mod, '__file__'): self.templateDirectory = os.path.split(mod.__file__)[0] # First see if templateDirectory + templateFile is a file templatePath = os.path.join(self.templateDirectory, self.templateFile) # Check to see if there is an already compiled copy of it templateName = os.path.splitext(self.templateFile)[0] compiledTemplateName = '.' + templateName + '.pxp' compiledTemplatePath = os.path.join(self.templateDirectory, compiledTemplateName) # No? Compile and save it if (not os.path.exists(compiledTemplatePath) or os.stat(compiledTemplatePath)[stat.ST_MTIME] < os.stat(templatePath)[stat.ST_MTIME]): compiledTemplate = microdom.parse(templatePath) pickle.dump(compiledTemplate, open(compiledTemplatePath, 'wb'), 1) else: compiledTemplate = pickle.load(open(compiledTemplatePath, "rb")) return compiledTemplate
def get(self, url): tree = microdom.parse(self.filename, beExtremelyLenient=True) for dl in tree.getElementsByTagName('dl'): for a in dl.getElementsByTagName('a'): if a.getAttribute('href') == url: if a.parentNode.nodeName == 'dt': return self._nodeToBookmark(a.parentNode)
def __init__(self, path, registry, *a, **kw): self.path = path self.registry = registry doc = microdom.parse(self.path) self.content = doc.getElementsByTagName('body')[0].childNodes # we will get title out of the original's <head> so we can replace it # with a default when necessary. _title = doc.getElementsByTagName('title') if len(_title) >= 1: title = _title[0] self.titleNodes = title.childNodes else: self.titleNodes = None title = None # then, everything else in original's head _head = doc.getElementsByTagName('head') if len(_head) >= 1: head = _head[0] self.originalHead = [n for n in head.childNodes if n is not title] else: self.originalHead = [] rend.Page.__init__(self, *a, **kw)
def doFile(infile, outfile): dom = microdom.parse(open(infile)) dir = os.path.dirname(infile) makeBook(dom, dir) outfile = open(outfile, 'w') dom.writexml(outfile) outfile.close()
def unjellyFromXML(stringOrFile): """I convert a string or the contents of an XML file into a Python object. """ if hasattr(stringOrFile, "read"): document = parse(stringOrFile) else: document = parseString(stringOrFile) return unjellyFromDOM(document)
def test_setIndexLink(self): """ Tests to make sure that index links are processed when an index page exists and removed when there is not. """ templ = microdom.parse(open(d['template'])) indexFilename = 'theIndexFile' numLinks = len(domhelpers.findElementsWithAttribute(templ, "class", "index-link")) # if our testing template has no index-link nodes, complain about it self.assertNotEquals( [], domhelpers.findElementsWithAttribute(templ, "class", "index-link")) tree.setIndexLink(templ, indexFilename) self.assertEquals( [], domhelpers.findElementsWithAttribute(templ, "class", "index-link")) indexLinks = domhelpers.findElementsWithAttribute(templ, "href", indexFilename) self.assertTrue(len(indexLinks) >= numLinks) templ = microdom.parse(open(d['template'])) self.assertNotEquals( [], domhelpers.findElementsWithAttribute(templ, "class", "index-link")) indexFilename = None tree.setIndexLink(templ, indexFilename) self.assertEquals( [], domhelpers.findElementsWithAttribute(templ, "class", "index-link"))
def generate_html(self, options, filenameGenerator=tree.getOutputFileName): n = default.htmlDefault.copy() n.update(options) options = n try: fp = open(options['template']) templ = microdom.parse(fp) except IOError, e: raise process.NoProcessorError(e.filename+": "+e.strerror)
def parseFileAndReport(filename): try: return microdom.parse(open(filename)) except microdom.MismatchedTags, e: raise process.ProcessingFailure( "%s:%s: begin mismatched tags <%s>/</%s>" % (e.begLine, e.begCol, e.got, e.expect), "%s:%s: end mismatched tags <%s>/</%s>" % (e.endLine, e.endCol, e.got, e.expect))
def generate_html(self, d): n = htmlDefault.copy() n.update(d) d = n try: fp = open(d['template']) templ = microdom.parse(fp) except IOError, e: raise process.NoProcessorError(e.filename+": "+e.strerror)
def lowerDocument(href, d, nodeLevel): newNode = microdom.parse(open(os.path.join(d, href))) newNode = domhelpers.findNodesNamed(newNode, 'body')[0] headers = domhelpers.findElements(newNode, lambda x: len(x.tagName)==2 and x.tagName[0]=='h' and x.tagName[1] in '123456') for header in headers: header.tagName = 'h'+str(int(header.tagName[1])+nodeLevel) return newNode
def generate_html(self, options, filenameGenerator=tree.getOutputFileName): n = default.htmlDefault.copy() n.update(options) options = n try: fp = open(options['template']) templ = microdom.parse(fp) except IOError, e: raise process.NoProcessorError(e.filename + ": " + e.strerror)
def test_indexAnchorsAdded(self): indexer.setIndexFilename('theIndexFile.html') # generate the output file templ = microdom.parse(open(d['template'])) tmp = self.makeTemp('lore_index_test.xhtml') tree.doFile(os.path.join(tmp, 'lore_index_test.xhtml'), self.linkrel, '.html', d['baseurl'], templ, d) self.assertEqualFiles1("lore_index_test_out.html", os.path.join(tmp, "lore_index_test.html"))
def load(self, ctx=None): mtime = os.path.getmtime(self._filename) if mtime != self._mtime or self._cache is None: from twisted.web import microdom doc = microdom.parse(self._filename, beExtremelyLenient=self.beExtremelyLenient) doc = flat.precompile(doc, ctx) if self.pattern is not None: doc = inevow.IQ(doc).onePattern(self.pattern) self._mtime = mtime self._cache = doc return self._cache
def load(self, xmlfile, element): """Load from XML @param xmlfile file name @param element """ self.xmlfile = xmlfile self.element = element tree = microdom.parse(xmlfile) nodes = tree.getElementsByTagName(element) for node in nodes: self.parse_element(node) del tree
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, writer=htmlizer.SmallerHTMLWriter) newio.seek(0) newel = microdom.parse(newio).documentElement newel.setAttribute("class", "python") node.parentNode.replaceChild(newel, node)
def _getAllBookmarks(self): # workaround for http://twistedmatrix.com/bugs/issue1358 laterClosers = {} laterClosers.update(microdom.MicroDOMParser.laterClosers) laterClosers['p'] = microdom.MicroDOMParser.laterClosers.get('p', []) + ['DT'] laterClosers['dt'] = microdom.MicroDOMParser.laterClosers.get('dt', []) + ['DD'] laterClosers['dd'] = microdom.MicroDOMParser.laterClosers.get('dd', []) + ['DT'] tree = microdom.parse(self.filename, beExtremelyLenient=True, laterClosers=laterClosers) for dl in tree.getElementsByTagName('dl'): for dt in dl.getElementsByTagName('dt'): yield self._nodeToBookmark(dt)
def __init__(self, templateDirectory, viewFactory=None, metaTemplate=None): """ Create a tapestry with a specified template directory. """ Resource.__init__(self) self.templateDirectory = templateDirectory if viewFactory is not None: self.viewFactory = viewFactory if metaTemplate: self.metaTemplate = microdom.parse(open( os.path.join(templateDirectory, metaTemplate))) else: self.metaTemplate = None
def __init__(self, templateDirectory, viewFactory=None, metaTemplate=None): """ Create a tapestry with a specified template directory. """ Resource.__init__(self) self.templateDirectory = templateDirectory if viewFactory is not None: self.viewFactory = viewFactory if metaTemplate: self.metaTemplate = microdom.parse( open(os.path.join(templateDirectory, metaTemplate))) else: self.metaTemplate = None
def load(self, config_file=None): """ Loads an xml configuration file, which says what avatars, props etc are avaiable to this stage""" config_file = config_file or self.config_file tree = microdom.parse(config_file) self.created = tree.documentElement.getAttribute('created') or str(datetime.now()) self.clear() #Heath Behrens 10/08/2011 - Moved so this is done correctly, they where in the if statements but that was horrible # and buggy splashnodes = tree.getElementsByTagName('splash') propBgColornodes = tree.getElementsByTagName('bgpropbgcolour') toolsBgColorNodes = tree.getElementsByTagName('toolsbgcolour') chatBgColorNodes = tree.getElementsByTagName('chatbgcolour') pageBgColorNodes = tree.getElementsByTagName('pagebgcolour') accessOneNodes = tree.getElementsByTagName('access_one') accessTwoNodes = tree.getElementsByTagName('access_two') accessThreeNodes = tree.getElementsByTagName('access_three') debugScreenNodes = tree.getElementsByTagName('showDebugScreen') try: #Heath Behrens 10/08/2011 - changed the if statements so they now check for none and process each # node correctly. if splashnodes and splashnodes[0].firstChild() is not None: self.splash_message = splashnodes[0].firstChild().toxml() if propBgColornodes and propBgColornodes[0].firstChild() is not None: self.backgroundPropBgColour = propBgColornodes[0].firstChild().toxml() if chatBgColorNodes and chatBgColorNodes[0].firstChild() is not None: self.chatBgColour = chatBgColorNodes[0].firstChild().toxml() if toolsBgColorNodes and toolsBgColorNodes[0].firstChild() is not None: self.toolsBgColour = toolsBgColorNodes[0].firstChild().toxml() if pageBgColorNodes and pageBgColorNodes[0].firstChild() is not None: self.pageBgColour = pageBgColorNodes[0].firstChild().toxml() if accessOneNodes and accessOneNodes[0].firstChild() is not None: #Heath Behrens 10/08/2011 - loop over the items in the node and split by comma for x in accessOneNodes[0].firstChild().toxml().split(','): self.access_level_one.append(x) # Heath Behrens 10/08/2011 - append the item to the list if accessTwoNodes and accessTwoNodes[0].firstChild() is not None: #Heath Behrens 10/08/2011 - loop over the items in the node and split by comma for x in accessTwoNodes[0].firstChild().toxml().split(','): self.access_level_two.append(x) # Heath Behrens 10/08/2011 - append the item to the list if accessThreeNodes and accessThreeNodes[0].firstChild() is not None: #Heath Behrens 10/08/2011 - loop over the items in the node and split by comma for x in accessThreeNodes[0].firstChild().toxml().split(','): self.access_level_three.append(x) # Heath Behrens 10/08/2011 - append the item to the list if debugScreenNodes and debugScreenNodes[0].firstChild() is not None: self.debugMessages = debugScreenNodes[0].firstChild().toxml() except Exception, e: print "Couldn't set splash message for '%s', because '%s'" % (self, e)
def test_setIndexLink(self): """ Tests to make sure that index links are processed when an index page exists and removed when there is not. """ templ = microdom.parse(open(d['template'])) indexFilename = 'theIndexFile' numLinks = len( domhelpers.findElementsWithAttribute(templ, "class", "index-link")) # if our testing template has no index-link nodes, complain about it self.assertNotEquals([], domhelpers.findElementsWithAttribute( templ, "class", "index-link")) tree.setIndexLink(templ, indexFilename) self.assertEquals([], domhelpers.findElementsWithAttribute( templ, "class", "index-link")) indexLinks = domhelpers.findElementsWithAttribute( templ, "href", indexFilename) self.assertTrue(len(indexLinks) >= numLinks) templ = microdom.parse(open(d['template'])) self.assertNotEquals([], domhelpers.findElementsWithAttribute( templ, "class", "index-link")) indexFilename = None tree.setIndexLink(templ, indexFilename) self.assertEquals([], domhelpers.findElementsWithAttribute( templ, "class", "index-link"))
def fontifyPythonNode(node): """ Syntax color the given node containing Python source code. @return: C{None} """ 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, writer=htmlizer.SmallerHTMLWriter) newio.seek(0) newel = microdom.parse(newio).documentElement newel.setAttribute("class", "python") node.parentNode.replaceChild(newel, node)
def fontifyPythonNode(node): """ Syntax color the given node containing Python source code. @return: C{None} """ 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, writer=htmlizer.SmallerHTMLWriter) newio.seek(0) newel = microdom.parse(newio).documentElement newel.setAttribute("class", "python") node.parentNode.replaceChild(newel, node)
def parseFileAndReport(filename): """ Parse and return the contents of the given lore XHTML document. @type filename: C{str} @param filename: The name of a file containing a lore XHTML document to load. @raise process.ProcessingFailure: When the contents of the specified file cannot be parsed. @rtype: A DOM Document @return: The document contained in C{filename}. """ try: return microdom.parse(open(filename)) except microdom.MismatchedTags, e: raise process.ProcessingFailure( "%s:%s: begin mismatched tags <%s>/</%s>" % (e.begLine, e.begCol, e.got, e.expect), "%s:%s: end mismatched tags <%s>/</%s>" % (e.endLine, e.endCol, e.got, e.expect), )
def parseFileAndReport(filename): """ Parse and return the contents of the given lore XHTML document. @type filename: C{str} @param filename: The name of a file containing a lore XHTML document to load. @raise process.ProcessingFailure: When the contents of the specified file cannot be parsed. @rtype: A DOM Document @return: The document contained in C{filename}. """ try: return microdom.parse(open(filename)) except microdom.MismatchedTags, e: raise process.ProcessingFailure( "%s:%s: begin mismatched tags <%s>/</%s>" % (e.begLine, e.begCol, e.got, e.expect), "%s:%s: end mismatched tags <%s>/</%s>" % (e.endLine, e.endCol, e.got, e.expect))
def parseXBEL(filename=None): if filename is None: filename = os.path.expanduser("~/.galeon/bookmarks.xbel") bookmarks = file(filename) dom = microdom.parse(bookmarks) return dom
def lookupTemplate(self, request): fullFile = os.path.join(self.templateDirectory, self.templateFile) document = microdom.parse(open(fullFile)) if self.tapestry: return self.tapestry.templateMutate(document, self.parentCount) return document
def processFile(spitter, fin): dom = microdom.parse(fin).documentElement spitter.visitNode(dom)
def _reallyLoad(self, path, ctx): doc = microdom.parse(path, beExtremelyLenient=self.beExtremelyLenient) doc = flat.precompile(doc, ctx) if self.pattern is not None: doc = inevow.IQ(doc).onePattern(self.pattern) return doc
def test_doFile_withFilenameGenerator(self): templ = microdom.parse(open(d['template'])) tree.doFile(self.file, self.linkrel, d['ext'], d['baseurl'], templ, d, filenameGenerator) self.assertEqualFiles('good_simple.xhtml', 'simple1.xhtml')