def _transformSLVToHTML(self, string, editor='kupu', encoding=None):
        """transform Silva XML to HTML"""
        transformer = EditorTransformer(editor=editor)

        # we need to know what browser we are dealing with in order to know
        # what html to produce, unfortunately Mozilla uses different tags in
        # some cases (b instead of strong, i instead of em)
        browser = 'Mozilla'
        if self.REQUEST['HTTP_USER_AGENT'].find('MSIE') > -1:
            browser = 'IE'

        ctx = Context(f=StringIO(),
                      last_version=1,
                      url=self.aq_parent.absolute_url(),
                      browser=browser,
                      model=self.aq_parent)
        ctx.f.write(string)

        htmlnode = transformer.to_target(sourceobj=ctx.f.getvalue(),
                                         context=ctx)
        if encoding is not None:
            ret = htmlnode.asBytes(encoding=encoding)
            ret = ret.replace('\xa0', ' ')
        else:
            ret = unicode(htmlnode.asBytes('utf8'), 'utf8')
            ret = ret.replace(u'\xa0', u' ')

        return ret
Exemplo n.º 2
0
    def get_document_xml_as(self, format='kupu', request=None):
        """Render the Document XML on a different format.
        """
        transformer = EditorTransformer(editor=format)
        context = Context(self, request)

        rendered_document = transformer.to_target(
            sourceobj=self.get_document_xml(), context=context)

        result = unicode(rendered_document.asBytes('utf8'), 'utf8')
        result = result.replace(u'\xa0', u' ')
        return result
    def setUp(self):
        self.root = self.layer.get_application()
        self.layer.login('editor')
        factory = self.root.manage_addProduct['SilvaDocument']
        factory.manage_addDocument('document', 'Document')
        factory = self.root.manage_addProduct['Silva']
        factory.manage_addFolder('folder', 'Folder')
        with self.layer.open_fixture('chocobo.jpg') as image:
            factory.manage_addImage('chocobo', 'Chocobo', image)

        self.transformer = Transformer.EditorTransformer(editor='kupu')
        self.context = Context(
            self.root.document.get_editable(), TestRequest())
    def editor_xml(self):
        browser = 'Mozilla'
        if self.REQUEST['HTTP_USER_AGENT'].find('MSIE') > -1:
            browser = 'IE'

        context = Context(f=StringIO(),
                            last_version=1,
                            url=self.absolute_url(),
                            browser=browser,
                            model=self)
        self.to_xml(context)
        xml = context.f.getvalue()
        return xml
Exemplo n.º 5
0
    def set_document_xml_from(self, data, format='kupu', request=None):
        """Set the document xml of the version from the given data
        in the given format.
        """
        transformer = EditorTransformer(editor=format)
        context = Context(self, request)

        document = transformer.to_source(targetobj=data, context=context)[0]
        title = document.find('title')[0].extract_text()
        content = document.find('doc')[0].asBytes(encoding="UTF8")
        self.content.manage_edit(content)
        self.set_title(title)

        notify(lifecycleevent.ObjectModifiedEvent(self))
 def setUp(self):
     self.root = self.layer.get_application()
     self.layer.login('editor')
     factory = self.root.manage_addProduct['SilvaDocument']
     factory.manage_addDocument('document', 'Document')
     factory = self.root.manage_addProduct['Silva']
     factory.manage_addFolder('folder', 'Folder')
     factory.manage_addFolder('other', 'Other')
     factory.manage_addPublication('publication', 'Publication')
     with self.layer.open_fixture('chocobo.jpg') as image:
         factory.manage_addImage('chocobo', 'Chocobo', image)
     self.transformer = Transformer.EditorTransformer(editor='kupu')
     # Context need the a document version in order to determine
     # references, and REQUEST to compute link URLs
     request = TestRequest()
     self.context = Context(self.root.document.get_editable(), request)
Exemplo n.º 7
0
 def upgrade(self, doc):
     # The 3.0 upgrader only upgrade the published, working and
     # last closed version. Only apply this upgrader on thoses.
     for version_id in [
             doc.get_public_version(),
             doc.get_unapproved_version(),
             doc.get_last_closed_version()
     ]:
         if version_id is None:
             continue
         version = doc._getOb(version_id, None)
         if version is None or not hasattr(version, 'content'):
             continue
         dom = version.content.documentElement
         context = Context(version, None)
         self._upgrade_links(version, context, dom)
         self._upgrade_images(version, context, dom)
     return doc
    def _transformHTMLToSLV(self, string, editor='kupu', encoding=None):
        """transform (messy structure-wise) HTML to Silva XML"""
        transformer = EditorTransformer(editor=editor)

        # we need to know what browser we are dealing with in order to know
        # what html to produce, unfortunately Mozilla uses different tags in
        # some cases (b instead of strong, i instead of em)
        browser = 'Mozilla'
        if self.REQUEST['HTTP_USER_AGENT'].find('MSIE') > -1:
            browser = 'IE'

        ctx = Context(url=self.aq_parent.absolute_url(),
                      browser=browser,
                      model=self.aq_parent,
                      request=self.REQUEST)
        silvanode = transformer.to_source(targetobj=string, context=ctx)[0]

        title = silvanode.find('title')[0].extract_text()  # unicode
        docnode = silvanode.find('doc')[0]
        content = docnode.asBytes(encoding="UTF8")  # UTF-8

        return title, content
    def test_existing_link_round_trip_on_copy(self):
        """We edit a existing link that have been created on a
        previous version of the document.
        """
        # Step one, create a link
        service = component.getUtility(IReferenceService)
        version = self.root.document.get_editable()
        reference = service.new_reference(version, name="document link")
        reference.set_target(self.root.folder)
        reference_name = "existing-link-id"
        reference.add_tag(reference_name)

        # Step two, make a new copy
        IPublicationWorkflow(self.root.document).publish()
        self.root.document.create_copy()
        new_version = self.root.document.get_editable()
        self.failIfEqual(version, new_version)
        new_reference = service.get_reference(new_version, name=reference_name)
        self.failIfEqual(reference, new_reference)

        # Now, the target is point by two relations, one for each version
        self.assertEqual(
            list(service.get_references_to(self.root.publication)), [])
        self.assertItemsEqual(
            list(service.get_references_to(self.root.folder)),
            [reference, new_reference])
        self.assertEqual(list(service.get_references_from(version)),
                         [reference])
        self.assertEqual(list(service.get_references_from(new_version)),
                         [new_reference])

        # To spice up the situation, we are going to modify the
        # reference on the new version to the publication
        new_target_id = get_content_id(self.root.publication)

        html = TEST_LINK_HTML % (reference_name, new_target_id)

        context = Context(new_version, TestRequest())
        result = self.transformer.to_source(targetobj=html,
                                            context=context).asBytes('utf-8')
        self.assertEqual(
            result,
            '<link target="_blank" reference="%s" title="">My link</link>' %
            (reference_name))

        # Now, things are changed correctly
        self.assertEqual(
            list(service.get_references_to(self.root.publication)),
            [new_reference])
        self.assertEqual(list(service.get_references_to(self.root.folder)),
                         [reference])
        self.assertEqual(list(service.get_references_from(version)),
                         [reference])
        self.assertEqual(list(service.get_references_from(new_version)),
                         [new_reference])

        # We can get back the HTML we started from for the new version
        roundtrip = self.transformer.to_target(
            sourceobj=result, context=context).asBytes('utf-8')
        self.assertEqual(roundtrip, html)

        # And things stays the same
        self.assertEqual(
            list(service.get_references_to(self.root.publication)),
            [new_reference])
        self.assertEqual(list(service.get_references_to(self.root.folder)),
                         [reference])
        self.assertEqual(list(service.get_references_from(version)),
                         [reference])
        self.assertEqual(list(service.get_references_from(new_version)),
                         [new_reference])