def getBody(self): u"Returns the <body> of the document." #$NON-NLS-1$ analyser = ZXhtmlBodyFindingAnalyser() self.analyse(analyser) return analyser.getBody()
def applyTemplateToDocument(template, document, mode = APPLY_TEMPLATE_MODE_FULL): u"""applyTemplateToDocument(IZTemplate, IZDocument) -> ZXHtmlDocument""" #$NON-NLS-1$ logger = getLoggerService() if template is None or document is None: logger.warning(u"applyTemplateToDocument:: Either template or document were None - skipping.") #$NON-NLS-1$ return None title = getSafeString(document.getTitle()) xhtmlDoc = None content = document.getContent() if content is not None: xhtmlDoc = content.getXhtmlDocument() if xhtmlDoc is None: logger.warning(u"applyTemplateToDocument:: XHtml document was None, skipping.") #$NON-NLS-1$ return None analyzer = ZXhtmlBodyFindingAnalyser() xhtmlDoc.analyse(analyzer) xhtmlBody = analyzer.getBody() # At this point, we have the document's title and body. logger.debug(u"applyTemplateToDocument:: title: %s" % title) #$NON-NLS-1$ # Get the path to the template file we want to use. templateDir = template.getTemplateDirectory() templateFileName = template.getAllFileName() if mode == APPLY_TEMPLATE_MODE_TITLE_AND_BODY: templateFileName = template.getTitleAndBodyFileName() elif mode == APPLY_TEMPLATE_MODE_BODY_ONLY: templateFileName = template.getBodyOnlyFileName() templateFilePath = os.path.join(templateDir, templateFileName) logger.debug(u"applyTemplateToDocument:: templateFileName: %s" % templateFileName) #$NON-NLS-1$ logger.debug(u"applyTemplateToDocument:: templateFilePath: %s" % templateFilePath) #$NON-NLS-1$ if os.path.isfile(templateFilePath) and xhtmlBody is not None: templateContent = getFileContents(templateFilePath) templateContent = _preprocessTemplateContent(templateContent, templateDir) templateDom = ZDom() templateDom.loadXML(templateContent) templateXHtml = ZXhtmlDocument(templateDom) # Get the DOCTYPE value from the root file (if any) rootFilePath = template.getResolvedRootFile() rootFileContent = None if os.path.isfile(rootFilePath): rootFile = open(rootFilePath, u"r") #$NON-NLS-1$ try: rootFileContent = rootFile.read() finally: rootFile.close() if rootFileContent: results = DOCTYPE_RE.findall(rootFileContent) if results: docTypeString = results[0] templateXHtml.setDocTypeString(docTypeString) # Find the template body elements. visitor = ZTemplateBodyFindingVisitor() visitor.visit(templateDom) templateBodyElements = visitor.getBodyElements() if not templateBodyElements: logger.warning(u"applyTemplateToDocument:: failed to find any template body elements") #$NON-NLS-1$ # Find the template title elements. visitor = ZTemplateTitleFindingVisitor() visitor.visit(templateDom) templateTitleElements = visitor.getTitleElements() if not templateTitleElements: logger.warning(u"applyTemplateToDocument:: failed to find any template title elements") #$NON-NLS-1$ # Do title stuff - replace the ravenTitle elem with a span containing the title for templateTitleElem in templateTitleElements: titleParentElem = templateTitleElem.parentNode newTitleElem = templateDom.createElement(u"span") #$NON-NLS-1$ newTitleElem.setText(title) titleParentElem.replaceChild(newTitleElem, templateTitleElem) # Do body stuff - insert all children of xhtmlBody, then remove the ravenBody elem for templateBodyElem in templateBodyElements: bodyParentElem = templateBodyElem.parentNode for node in xhtmlBody.getChildren(): bodyParentElem.insertBefore(templateDom.importNode(node, True), templateBodyElem) bodyParentElem.removeChild(templateBodyElem) # IE hates empty DIVs and %20's in attributes. Dunno why. _fixupIECrappiness(templateDom) return templateXHtml else: logger.warning(u"applyTemplateToDocument:: template file not found") #$NON-NLS-1$ return xhtmlDoc