Exemple #1
0
    def testConverterFinder(self):
        for type_input, type_output, ExpectedClass in [
                # *_in converters
                (type_moin_wiki, type_moin_document, MoinwikiInConverter),
                (Type('x-moin/format;name=wiki'), type_moin_document, MoinwikiInConverter),
                # pygments_in can handle this too but html_in should have more priority
                (Type('text/html'), type_moin_document, HtmlInConverter),
                # fall back to pygments_in
                (Type('text/html+jinja'), type_moin_document, PygmentsInConverter),
                # fallback for any random text/* input types
                (Type('text/blahblah'), type_moin_document, TextInConverter),
                # fallback for anything
                (Type('mua/haha'), type_moin_document, EverythingConverter),

                # *_out converters
                (type_moin_document, Type('application/x-xhtml-moin-page'), HtmlOutConverterPage),
                (type_moin_document, type_moin_wiki, MoinwikiOutConverter),
                (type_moin_document, Type('x-moin/format;name=wiki'), MoinwikiOutConverter),
        ]:
            conv = default_registry.get(type_input, type_output)
            assert isinstance(conv, ExpectedClass)

        for kwargs, ExpectedClass in [
                # DOM converters, which depend on keyword argument to default_registry.get
                (dict(macros='expandall'), MacroConverter),
                (dict(includes='expandall'), IncludeConverter),
                (dict(links='extern'), LinkConverterExternOutput),
                (dict(items='refs'), LinkConverterItemRefs),
        ]:
            conv = default_registry.get(type_moin_document, type_moin_document, **kwargs)
            assert isinstance(conv, ExpectedClass)
Exemple #2
0
 def _expand_document(self, doc):
     from MoinMoin.converter import default_registry as reg
     flaskg.add_lineno_attr = False  # do not add data-lineno attr for transclusions, footnotes, etc.
     include_conv = reg.get(type_moin_document,
                            type_moin_document,
                            includes='expandall')
     macro_conv = reg.get(type_moin_document,
                          type_moin_document,
                          macros='expandall')
     nowiki_conv = reg.get(type_moin_document,
                           type_moin_document,
                           nowiki='expandall')
     link_conv = reg.get(type_moin_document,
                         type_moin_document,
                         links='extern')
     flaskg.clock.start('nowiki')
     doc = nowiki_conv(doc)
     flaskg.clock.stop('nowiki')
     flaskg.clock.start('conv_include')
     doc = include_conv(doc)
     flaskg.clock.stop('conv_include')
     flaskg.clock.start('conv_macro')
     doc = macro_conv(doc)
     flaskg.clock.stop('conv_macro')
     flaskg.clock.start('conv_link')
     doc = link_conv(doc)
     flaskg.clock.stop('conv_link')
     if 'regex' in request.args:
         highlight_conv = reg.get(type_moin_document,
                                  type_moin_document,
                                  highlight='highlight')
         flaskg.clock.start('highlight')
         doc = highlight_conv(doc)
         flaskg.clock.stop('highlight')
     return doc
    def testConverterFinder(self):
        for type_input, type_output, ExpectedClass in [
                # *_in converters
            (type_moin_wiki, type_moin_document, MoinwikiInConverter),
            (Type('x-moin/format;name=wiki'), type_moin_document,
             MoinwikiInConverter),
                # pygments_in can handle this too but html_in should have more priority
            (Type('text/html'), type_moin_document, HtmlInConverter),
                # fall back to pygments_in
            (Type('text/html+jinja'), type_moin_document, PygmentsInConverter),
                # fallback for any random text/* input types
            (Type('text/blahblah'), type_moin_document, TextInConverter),
                # fallback for anything
            (Type('mua/haha'), type_moin_document, EverythingConverter),

                # *_out converters
            (type_moin_document, Type('application/x-xhtml-moin-page'),
             HtmlOutConverterPage),
            (type_moin_document, type_moin_wiki, MoinwikiOutConverter),
            (type_moin_document, Type('x-moin/format;name=wiki'),
             MoinwikiOutConverter),
        ]:
            conv = default_registry.get(type_input, type_output)
            assert isinstance(conv, ExpectedClass)

        for kwargs, ExpectedClass in [
                # DOM converters, which depend on keyword argument to default_registry.get
            (dict(macros='expandall'), MacroConverter),
            (dict(includes='expandall'), IncludeConverter),
            (dict(links='extern'), LinkConverterExternOutput),
            (dict(items='refs'), LinkConverterItemRefs),
        ]:
            conv = default_registry.get(type_moin_document, type_moin_document,
                                        **kwargs)
            assert isinstance(conv, ExpectedClass)
Exemple #4
0
 def _render_data(self):
     try:
         from MoinMoin.converter import default_registry as reg
         # TODO: Real output format
         doc = self.internal_representation()
         doc = self._expand_document(doc)
         flaskg.clock.start('conv_dom_html')
         html_conv = reg.get(type_moin_document,
                             Type('application/x-xhtml-moin-page'))
         doc = html_conv(doc)
         flaskg.clock.stop('conv_dom_html')
         rendered_data = conv_serialize(doc, {html.namespace: ''})
     except Exception:
         # we really want to make sure that invalid data or a malfunctioning
         # converter does not crash the item view (otherwise a user might
         # not be able to fix it from the UI).
         import time
         import uuid
         error_id = uuid.uuid4()
         logging.exception(
             "An exception happened in _render_data (error_id = %s ):" %
             error_id)
         rendered_data = render_template(
             'crash.html',
             server_time=time.strftime("%Y-%m-%d %H:%M:%S %Z"),
             url=request.url,
             error_id=error_id)
     return rendered_data
Exemple #5
0
 def _expand_document(self, doc):
     from MoinMoin.converter import default_registry as reg
     flaskg.add_lineno_attr = False  # do not add data-lineno attr for transclusions, footnotes, etc.
     include_conv = reg.get(type_moin_document, type_moin_document, includes='expandall')
     macro_conv = reg.get(type_moin_document, type_moin_document, macros='expandall')
     link_conv = reg.get(type_moin_document, type_moin_document, links='extern')
     flaskg.clock.start('conv_include')
     doc = include_conv(doc)
     flaskg.clock.stop('conv_include')
     flaskg.clock.start('conv_macro')
     doc = macro_conv(doc)
     flaskg.clock.stop('conv_macro')
     flaskg.clock.start('conv_link')
     doc = link_conv(doc)
     flaskg.clock.stop('conv_link')
     return doc
Exemple #6
0
    def _render_data(self):
        try:
            from MoinMoin.converter import default_registry as reg

            # TODO: Real output format
            doc = self.internal_representation()
            doc = self._expand_document(doc)
            flaskg.clock.start("conv_dom_html")
            html_conv = reg.get(type_moin_document, Type("application/x-xhtml-moin-page"))
            doc = html_conv(doc)
            flaskg.clock.stop("conv_dom_html")
            rendered_data = conv_serialize(doc, {html.namespace: ""})
        except Exception:
            # we really want to make sure that invalid data or a malfunctioning
            # converter does not crash the item view (otherwise a user might
            # not be able to fix it from the UI).
            import time
            import uuid

            error_id = uuid.uuid4()
            logging.exception("An exception happened in _render_data (error_id = %s ):" % error_id)
            rendered_data = render_template(
                "crash.html", server_time=time.strftime("%Y-%m-%d %H:%M:%S %Z"), url=request.url, error_id=error_id
            )
        return rendered_data
Exemple #7
0
    def _expand_document(self, doc):
        from MoinMoin.converter import default_registry as reg

        include_conv = reg.get(type_moin_document, type_moin_document, includes="expandall")
        macro_conv = reg.get(type_moin_document, type_moin_document, macros="expandall")
        link_conv = reg.get(type_moin_document, type_moin_document, links="extern")
        flaskg.clock.start("conv_include")
        doc = include_conv(doc)
        flaskg.clock.stop("conv_include")
        flaskg.clock.start("conv_macro")
        doc = macro_conv(doc)
        flaskg.clock.stop("conv_macro")
        flaskg.clock.start("conv_link")
        doc = link_conv(doc)
        flaskg.clock.stop("conv_link")
        return doc
Exemple #8
0
    def internal_representation(self, attributes=None):
        """
        Return the internal representation of a document using a DOM Tree
        """
        hash_name = HASH_ALGORITHM
        hash_hexdigest = self.rev.meta.get(hash_name)
        if hash_hexdigest:
            cid = cache_key(usage="internal_representation",
                            hash_name=hash_name,
                            hash_hexdigest=hash_hexdigest)
            doc = app.cache.get(cid)
        else:
            # likely a non-existing item
            doc = cid = None
        if doc is None:
            # We will see if we can perform the conversion:
            # FROM_mimetype --> DOM
            # if so we perform the transformation, otherwise we don't
            from MoinMoin.converter import default_registry as reg
            input_conv = reg.get(Type(self.contenttype), type_moin_document)
            if not input_conv:
                raise TypeError(
                    "We cannot handle the conversion from {0} to the DOM tree".
                    format(self.contenttype))
            smiley_conv = reg.get(type_moin_document,
                                  type_moin_document,
                                  icon='smiley')

            # We can process the conversion
            links = Iri(scheme='wiki', authority='', path='/' + self.name)
            doc = input_conv(self.rev, self.contenttype, arguments=attributes)
            # XXX is the following assuming that the top element of the doc tree
            # is a moin_page.page element? if yes, this is the wrong place to do that
            # as not every doc will have that element (e.g. for images, we just get
            # moin_page.object, for a tar item, we get a moin_page.table):
            doc.set(moin_page.page_href, unicode(links))
            if self.contenttype.startswith((
                    u'text/x.moin.wiki',
                    u'text/x-mediawiki',
                    u'text/x.moin.creole',
            )):
                doc = smiley_conv(doc)
            if cid:
                app.cache.set(cid, doc)
        return doc
Exemple #9
0
 def _render_data_highlight(self):
     from MoinMoin.converter import default_registry as reg
     data_text = self.data_storage_to_internal(self.data)
     # TODO: use registry as soon as it is in there
     from MoinMoin.converter.pygments_in import Converter as PygmentsConverter
     pygments_conv = PygmentsConverter(contenttype=self.contenttype)
     doc = pygments_conv(data_text)
     # TODO: Real output format
     html_conv = reg.get(type_moin_document, Type('application/x-xhtml-moin-page'))
     doc = html_conv(doc)
     return conv_serialize(doc, {html.namespace: ''})
Exemple #10
0
 def _render_data_highlight(self):
     from MoinMoin.converter import default_registry as reg
     data_text = self.data_storage_to_internal(self.data)
     # TODO: use registry as soon as it is in there
     from MoinMoin.converter.pygments_in import Converter as PygmentsConverter
     pygments_conv = PygmentsConverter(contenttype=self.contenttype)
     doc = pygments_conv(data_text)
     # TODO: Real output format
     html_conv = reg.get(type_moin_document,
                         Type('application/x-xhtml-moin-page'))
     doc = html_conv(doc)
     return conv_serialize(doc, {html.namespace: ''})
Exemple #11
0
    def internal_representation(self, converters=['smiley'], attributes=None):
        """
        Return the internal representation of a document using a DOM Tree
        """
        hash_name = HASH_ALGORITHM
        hash_hexdigest = self.rev.meta.get(hash_name)
        if hash_hexdigest:
            cid = cache_key(usage="internal_representation",
                            hash_name=hash_name,
                            hash_hexdigest=hash_hexdigest)
            doc = app.cache.get(cid)
        else:
            # likely a non-existing item
            doc = cid = None
        if doc is None:
            # We will see if we can perform the conversion:
            # FROM_mimetype --> DOM
            # if so we perform the transformation, otherwise we don't
            from MoinMoin.converter import default_registry as reg
            input_conv = reg.get(Type(self.contenttype), type_moin_document)
            if not input_conv:
                raise TypeError("We cannot handle the conversion from {0} to the DOM tree".format(self.contenttype))
            smiley_conv = reg.get(type_moin_document, type_moin_document, icon='smiley')

            # We can process the conversion
            links = Iri(scheme='wiki', authority='', path='/' + self.name)
            doc = input_conv(self.rev, self.contenttype, arguments=attributes)
            # XXX is the following assuming that the top element of the doc tree
            # is a moin_page.page element? if yes, this is the wrong place to do that
            # as not every doc will have that element (e.g. for images, we just get
            # moin_page.object, for a tar item, we get a moin_page.table):
            doc.set(moin_page.page_href, unicode(links))
            for conv in converters:
                if conv == 'smiley':
                    doc = smiley_conv(doc)
            if cid:
                app.cache.set(cid, doc)
        return doc
Exemple #12
0
    def _convert(self, doc):
        from emeraldtree import ElementTree as ET
        from MoinMoin.converter import default_registry as reg

        doc = self._expand_document(doc)

        # We convert the internal representation of the document
        # into a DocBook document
        conv = reg.get(type_moin_document, Type('application/docbook+xml'))

        doc = conv(doc)

        # We determine the different namespaces of the output form
        output_namespaces = {
            docbook.namespace: '',
            xlink.namespace: 'xlink',
        }

        # We convert the result into a StringIO object
        # With the appropriate namespace
        # TODO: Some other operation should probably be done here too
        # like adding a doctype
        file_to_send = StringIO()
        tree = ET.ElementTree(doc)
        tree.write(file_to_send, namespaces=output_namespaces)

        # We determine the different parameters for the reply
        mt = MimeType(mimestr='application/docbook+xml;charset=utf-8')
        content_type = mt.content_type()
        as_attachment = mt.as_attachment(app.cfg)
        # After creation of the StringIO, we are at the end of the file
        # so position is the size the file.
        # and then we should move it back at the beginning of the file
        content_length = file_to_send.tell()
        file_to_send.seek(0)
        # Important: empty filename keeps flask from trying to autodetect filename,
        # as this would not work for us, because our file's are not necessarily fs files.
        return send_file(
            file=file_to_send,
            mimetype=content_type,
            as_attachment=as_attachment,
            attachment_filename=None,
            cache_timeout=10,  # wiki data can change rapidly
            add_etags=False,
            etag=None,
            conditional=True)
Exemple #13
0
    def _convert(self, doc):
        from emeraldtree import ElementTree as ET
        from MoinMoin.converter import default_registry as reg

        doc = self._expand_document(doc)

        # We convert the internal representation of the document
        # into a DocBook document
        conv = reg.get(type_moin_document, Type("application/docbook+xml"))

        doc = conv(doc)

        # We determine the different namespaces of the output form
        output_namespaces = {docbook.namespace: "", xlink.namespace: "xlink"}

        # We convert the result into a StringIO object
        # With the appropriate namespace
        # TODO: Some other operation should probably be done here too
        # like adding a doctype
        file_to_send = StringIO()
        tree = ET.ElementTree(doc)
        tree.write(file_to_send, namespaces=output_namespaces)

        # We determine the different parameters for the reply
        mt = MimeType(mimestr="application/docbook+xml;charset=utf-8")
        content_type = mt.content_type()
        as_attachment = mt.as_attachment(app.cfg)
        # After creation of the StringIO, we are at the end of the file
        # so position is the size the file.
        # and then we should move it back at the beginning of the file
        content_length = file_to_send.tell()
        file_to_send.seek(0)
        # Important: empty filename keeps flask from trying to autodetect filename,
        # as this would not work for us, because our file's are not necessarily fs files.
        return send_file(
            file=file_to_send,
            mimetype=content_type,
            as_attachment=as_attachment,
            attachment_filename=None,
            cache_timeout=10,  # wiki data can change rapidly
            add_etags=False,
            etag=None,
            conditional=True,
        )