Beispiel #1
0
 def _expand_document(self, doc):
     from moin.converters 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
Beispiel #2
0
    def internal_representation(self, attributes=None, preview=None):
        """
        Return the internal representation of a document using a DOM Tree
        """
        doc = cid = None
        if preview is None:
            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,
                                attrs=repr(attributes))
                doc = app.cache.get(cid)
        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 moin.converters 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
            name = self.rev.fqname.fullname if self.rev else self.name
            links = Iri(scheme='wiki', authority='', path='/' + name)
            doc = input_conv(preview or 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, str(links))
            if self.contenttype.startswith((
                    'text/x.moin.wiki',
                    'text/x-mediawiki',
                    'text/x.moin.creole',
            )):
                doc = smiley_conv(doc)
            if cid:
                app.cache.set(cid, doc)
        return doc
Beispiel #3
0
 def _render_data(self, preview=None):
     try:
         from moin.converters import default_registry as reg
         # TODO: Real output format
         doc = self.internal_representation(preview=preview)
         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).
         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
Beispiel #4
0
 def _render_data_highlight(self):
     from moin.converters 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 moin.converters.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: ''})
Beispiel #5
0
    def _convert(self, doc):
        from emeraldtree import ElementTree as ET
        from moin.converters 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 BytesIO object
        # With the appropriate namespace
        # TODO: Some other operation should probably be done here too
        # like adding a doctype
        file_to_send = BytesIO()
        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 BytesIO, 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)
Beispiel #6
0
def test_converter_args(kwargs, expected_class):
    conv = default_registry.get(type_moin_document, type_moin_document,
                                **kwargs)
    assert isinstance(conv, expected_class)
Beispiel #7
0
def test_converter_finder(type_input, type_output, expected_class):
    conv = default_registry.get(type_input, type_output)
    assert isinstance(conv, expected_class)