Beispiel #1
0
    def _save(self):
        if not self.registryFile:
            return

        mediaStorageDir = os.path.basename(self.registryFile)

        dom = ZDom()
        dom.loadXML(u"<registry/>")  #$NON-NLS-1$
        registryElem = dom.documentElement
        for fileName in self.fileMap:
            (size, timestamp, uploadResponse) = self.fileMap[fileName]
            relativeFileName = makeRelativePath(mediaStorageDir, fileName)
            url = uploadResponse.getUrl()
            embedFragment = uploadResponse.getEmbedFragment()
            metaData = uploadResponse.getMetaData()

            entryElem = dom.createElement(u"entry")  #$NON-NLS-1$
            entryElem.setAttribute(u"size", unicode(size))  #$NON-NLS-1$
            entryElem.setAttribute(u"timestamp",
                                   unicode(timestamp))  #$NON-NLS-1$

            fileElem = dom.createElement(u"file")  #$NON-NLS-1$
            urlElem = dom.createElement(u"url")  #$NON-NLS-1$
            embedElem = dom.createElement(u"embed")  #$NON-NLS-1$
            metaDataElem = dom.createElement(u"metaData")  #$NON-NLS-1$

            # When in portable mode, save the file paths as relative (which will
            # only happen when the image is on the same drive as the app).
            if isPortableEnabled():
                fileElem.setText(relativeFileName)
            else:
                fileElem.setText(fileName)

            entryElem.appendChild(fileElem)
            if url:
                urlElem.setText(unicode(url))
            entryElem.appendChild(urlElem)
            if embedFragment is not None:
                embedElem.appendChild(dom.importNode(embedFragment, True))
            entryElem.appendChild(embedElem)
            if metaData is not None:
                metaDataElem.appendChild(dom.importNode(metaData, True))
            entryElem.appendChild(metaDataElem)

            registryElem.appendChild(entryElem)

        dom.save(self.registryFile, True)
    def _save(self):
        if not self.registryFile:
            return

        mediaStorageDir = os.path.basename(self.registryFile)

        dom = ZDom()
        dom.loadXML(u"<registry/>") #$NON-NLS-1$
        registryElem = dom.documentElement
        for fileName in self.fileMap:
            (size, timestamp, uploadResponse) = self.fileMap[fileName]
            relativeFileName = makeRelativePath(mediaStorageDir, fileName)
            url = uploadResponse.getUrl()
            embedFragment = uploadResponse.getEmbedFragment()
            metaData = uploadResponse.getMetaData()

            entryElem = dom.createElement(u"entry") #$NON-NLS-1$
            entryElem.setAttribute(u"size", unicode(size)) #$NON-NLS-1$
            entryElem.setAttribute(u"timestamp", unicode(timestamp)) #$NON-NLS-1$

            fileElem = dom.createElement(u"file") #$NON-NLS-1$
            urlElem = dom.createElement(u"url") #$NON-NLS-1$
            embedElem = dom.createElement(u"embed") #$NON-NLS-1$
            metaDataElem = dom.createElement(u"metaData") #$NON-NLS-1$

            # When in portable mode, save the file paths as relative (which will
            # only happen when the image is on the same drive as the app).
            if isPortableEnabled():
                fileElem.setText(relativeFileName)
            else:
                fileElem.setText(fileName)

            entryElem.appendChild(fileElem)
            if url:
                urlElem.setText(unicode(url))
            entryElem.appendChild(urlElem)
            if embedFragment is not None:
                embedElem.appendChild(dom.importNode(embedFragment, True))
            entryElem.appendChild(embedElem)
            if metaData is not None:
                metaDataElem.appendChild(dom.importNode(metaData, True))
            entryElem.appendChild(metaDataElem)

            registryElem.appendChild(entryElem)

        dom.save(self.registryFile, True)
Beispiel #3
0
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