Пример #1
0
        def _factory(cls, type_input, type_output, **kw):
            pygments_name = None
            # first we check the input type against all mimetypes pygments knows:
            for name, short_names, patterns, mime_types in pygments.lexers.get_all_lexers(
            ):
                for mt in mime_types:
                    if Type(mt).issupertype(type_input):
                        pygments_name = name
                        break
                if pygments_name:
                    break

            # if we still don't know the lexer name for pygments, check some formats
            # that were supported by special parsers in moin 1.x:
            if pygments_name is None:
                moin_pygments = [
                    ('python', 'Python'),
                    ('diff', 'Diff'),
                    ('irssi', 'IRC logs'),
                    ('irc', 'IRC logs'),
                    ('java', 'Java'),
                    ('cplusplus', 'C++'),
                    ('pascal', 'Delphi'),
                ]
                for moin_format, pygments_name in moin_pygments:
                    if Type('x-moin/format;name={0}'.format(
                            moin_format)).issupertype(type_input):
                        break
                else:
                    pygments_name = None

            logging.debug("pygments_name: %r" % pygments_name)
            if pygments_name:
                lexer = pygments.lexers.find_lexer_class(pygments_name)
                return cls(lexer())
Пример #2
0
    def parser(self, name, args, content):
        if '/' in name:
            type = Type(name)
        else:
            type = Type(type='x-moin',
                        subtype='format',
                        parameters={'name': name})
        logging.debug("parser type: %r" % (type, ))

        elem = moin_page.part(attrib={moin_page.content_type: type})

        if args:
            elem_arguments = moin_page.arguments()
            elem.append(elem_arguments)

            for key, value in args.items():
                attrib = {}
                if key:
                    attrib[moin_page.name] = key
                elem_arg = moin_page.argument(attrib=attrib,
                                              children=(value, ))
                elem_arguments.append(elem_arg)

        if content:
            elem.append(moin_page.body(children=content))

        return elem
Пример #3
0
 def eval_object_type(self, mimetype, href):
     """
     Returns the type of an object as a str, one of the following: img, video, audio, object
     """
     if Type('image/').issupertype(mimetype):
         return "img"
     elif Type('video/').issupertype(mimetype):
         return "video"
     elif Type('audio/').issupertype(mimetype):
         return "audio"
     else:
         # Nothing else worked...try using <object>
         return "object"
Пример #4
0
    def visit_moinpage_object(self, element):
        """
        Convert::

        <object type='image/' xlink:href='uri'/>

        to::

            <inlinemediaobject>
                  <imageobject>
                        <imagedata fileref="uri" />
                  </imageobject>
            </inlinemediaobject>

        Similar for video and audio object.
        """
        href = element.get(xlink.href, None)
        attrib = {}
        mimetype = Type(
            _type=element.get(moin_page.type_, CONTENTTYPE_NONEXISTENT))
        if href:
            attrib[docbook.fileref] = href
            if Type('image/').issupertype(mimetype):
                object_data = self.new(docbook.imagedata,
                                       attrib=attrib,
                                       children=[])
                object_element = self.new(docbook.imageobject,
                                          attrib={},
                                          children=[object_data])
            elif Type('video/').issupertype(mimetype):
                object_data = self.new(docbook.videodata,
                                       attrib=attrib,
                                       children=[])
                object_element = self.new(docbook.videoobject,
                                          attrib={},
                                          children=[object_data])
            elif Type('audio/').issupertype(mimetype):
                object_data = self.new(docbook.audiodata,
                                       attrib=attrib,
                                       children=[])
                object_element = self.new(docbook.audioobject,
                                          attrib={},
                                          children=[object_data])
            else:
                return
        else:
            return
        return self.new(docbook.inlinemediaobject,
                        attrib={},
                        children=[object_element])
Пример #5
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
Пример #6
0
def decode_data(data, contenttype=None):
    """
    read and decode data, return unicode text

    supported types for data:
    - rev object
    - str
    - unicode

    file-like objects and str need to be either utf-8 (or ascii, which is a subset of utf-8)
    encoded or contenttype (including a charset parameter) needs to be given.
    """
    if not isinstance(data, (str, unicode)):
        data = data.data.read()
    if isinstance(data, str):
        coding = 'utf-8'
        if contenttype is not None:
            ct = Type(contenttype)
            coding = ct.parameters.get('charset', coding)
        data = data.decode(coding)
    if not isinstance(data, unicode):
        raise TypeError(
            "data must be rev or str (requires contenttype with charset) or unicode, "
            "but we got {0!r}".format(data))
    return data
Пример #7
0
def register(cls):
    content_registry.register(
        RegistryContent.Entry(cls._factory, Type(cls.contenttype),
                              cls.default_contenttype_params, cls.display_name,
                              cls.ingroup_order,
                              RegistryContent.PRIORITY_MIDDLE), cls.group)
    return cls
Пример #8
0
 def eval_object_type(self, mimetype, href):
     """
     Returns the type of an object.
     Return value is an str, one of the following:
         image, video, audio, object
     """
     if Type('image/').issupertype(mimetype) and not Type('image/svg+xml').issupertype(mimetype):
         # Firefox fails completely to show svg in img tags (displays: nothing).
         # Firefox displays them with on object tag (but sometimes displays scrollbars without need).
         return "img"
     elif Type('video/').issupertype(mimetype):
         return "video"
     elif Type('audio/').issupertype(mimetype):
         return "audio"
     else:
         # Nothing else worked...try using <object>
         return "object"
Пример #9
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)
Пример #10
0
    def visit_moinpage_object(self, elem):
        """
        elem of type img are converted to img tags here, others are left as object tags
        """
        href = elem.get(xlink.href, None)
        attrib = {}
        whitelist = ['width', 'height', 'alt', 'class', 'data-href']
        for key in elem.attrib:
            if key.name in whitelist:
                attrib[key] = elem.attrib[key]
        mimetype = Type(_type=elem.get(moin_page.type_, CONTENTTYPE_NONEXISTENT))
        if elem.get(moin_page.type_):
            del elem.attrib[moin_page.type_]
        # Get the object type
        obj_type = self.eval_object_type(mimetype, href)

        # The attribute source attribute for img,video, and audio is the same (src)
        # <object>'s attribute is 'data'
        attr = html.src if obj_type != "object" else html.data

        # The return element
        new_elem = None

        if href is not None:
            # Set the attribute of the returned element appropriately
            attrib[attr] = href
        alt = convert_getlink_to_showlink(unicode(href))
        alt = re.sub('^\/', '', alt)

        if obj_type == "img":
            # Images must have alt attribute in html5, but if user did not specify then default to url
            if not attrib.get(html.alt):
                attrib[html.alt] = alt
            new_elem = html.img(attrib=attrib)

        else:
            if obj_type != "object":
                # Non-objects like video and audio have the "controls" attribute
                attrib[html.controls] = 'controls'
                new_elem = self.new_copy(getattr(html, obj_type), elem, attrib)
            else:
                # is an object
                new_elem = html.object(attrib=attrib)
            # alt attr is invalid within object, audio, and video tags , append alt text as a child
            if new_elem.attrib.get(html.alt):
                new_elem.append(new_elem.attrib.get(html.alt))
                del new_elem.attrib[html.alt]
            else:
                new_elem.append(alt)

        if obj_type == "object" and getattr(href, 'scheme', None):
            # items similar to {{http://moinmo.in}} are marked here, other objects are marked in include.py
            return mark_item_as_transclusion(new_elem, href)
        return new_elem
Пример #11
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: ''})
Пример #12
0
    def __call__(self, value, start_pos=0, positions=False, **kwargs):
        """
        Tokenizer behaviour:

        Input: u"text/x.moin.wiki;charset=utf-8"
        Output: u"text/x.moin.wiki;charset=utf-8", u"text", u"x.moin.wiki", u"charset=utf-8"

        Input: u"application/pdf"
        Output: u"application/pdf", u"application", u"pdf"

        :param value: String for tokenization
        :param start_pos: The position number of the first token. For example,
            if you set start_pos=2, the tokens will be numbered 2,3,4,...
            instead of 0,1,2,...
        :param positions: Whether to record token positions in the token.
        """
        assert isinstance(value, unicode), "{0!r} is not unicode".format(value)
        if u'/' not in value:  # Add '/' if user forgot do this
            value += u'/'
        pos = start_pos
        tk = Token()
        tp = Type(value)
        # we need to yield the complete contenttype in one piece,
        # so we can find it with Term(CONTENTTYPE, contenttype):
        if tp.type is not None and tp.subtype is not None:
            # note: we do not use "value" directly, so Type.__unicode__ can normalize it:
            tk.text = unicode(tp)
            if positions:
                tk.pos = pos
                pos += 1
            yield tk
        # now yield the pieces:
        tk.text = tp.type
        if positions:
            tk.pos = pos
            pos += 1
        yield tk
        if tp.subtype is not None:
            tk.text = tp.subtype
            if positions:
                tk.pos = pos
                pos += 1
            yield tk
        for key, value in tp.parameters.items():
            tk.text = u"{0}={1}".format(key, value)
            if positions:
                tk.pos = pos
                pos += 1
            yield tk
Пример #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)
Пример #14
0
        def __init__(self, lexer=None, contenttype=None):
            """
            Create a Pygments Converter.

            :param lexer: pygments lexer instance
            :param contenttype: contenttype to get a lexer for
            """
            if lexer is None and contenttype is not None:
                ct = Type(contenttype)
                # pygments can't process parameters (like e.g. ...;charset=utf-8):
                mimetype = '{0}/{1}'.format(ct.type, ct.subtype)
                try:
                    lexer = pygments.lexers.get_lexer_for_mimetype(mimetype)
                except pygments.util.ClassNotFound:
                    lexer = pygments.lexers.get_lexer_for_mimetype(
                        'text/plain')
            self.lexer = lexer
Пример #15
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
Пример #16
0
    def handle_macro(self, elem, page):
        logging.debug("handle_macro elem: %r" % elem)
        type = elem.get(moin_page.content_type)
        alt = elem.get(moin_page.alt)

        if not type:
            return

        type = Type(type)
        if not (type.type == 'x-moin' and type.subtype == 'macro'):
            logging.debug("not a macro, skipping: %r" % (type, ))
            return

        name = type.parameters['name']
        context_block = elem.tag == moin_page.part
        args = elem[0] if len(elem) else None
        elem_body = context_block and moin_page.body() or moin_page.inline_body()
        elem_error = moin_page.error()

        try:
            cls = plugins.importPlugin(app.cfg, 'macro', name, function='Macro')
            macro = cls()
            ret = macro((), args, page, alt, context_block)
            elem_body.append(ret)

        except PluginMissingError:
            elem_error.append('<<%s>> %s' % (name, _('Error: invalid macro name.')))

        except Exception as e:
            # we do not want that a faulty macro aborts rendering of the page
            # and makes the wiki UI unusable (by emitting a Server Error),
            # thus, in case of exceptions, we just log the problem and return
            # some standard text.
            logging.exception("Macro {0} raised an exception:".format(name))
            elem_error.append(_('<<%(macro_name)s: execution failed [%(error_msg)s] (see also the log)>>',
                              macro_name=name, error_msg=unicode(e), ))

        if len(elem_body):
            elem.append(elem_body)
        if len(elem_error):
            elem.append(elem_error)
Пример #17
0
    def visit_moinpage_object(self, elem):
        href = elem.get(xlink.href, None)
        if self.base_url:
            href = ''.join([self.base_url, href])
        if href:
            if isinstance(href, unicode): # XXX sometimes we get Iri, sometimes unicode - bug?
                h = href
            else: # Iri
                h = href.path[-1] # XXX BUG Iri doesn't have a path if we access the root page (eg. http://google.de doesn't have a path)
        attrib = {}
        mimetype = Type(_type=elem.get(moin_page.type_, 'application/x-nonexistent'))
        # Get the object type
        obj_type = self.eval_object_type(mimetype, href)

        # The attribute source attribute for img,video, and audio is the same (src)
        # <object>'s attribute is 'data'
        attr = html.src if obj_type != "object" else html.data

        # The return element
        new_elem = None

        if href is not None:
            # Set the attribute of the returned element appropriately
            attrib[attr] = href

        if obj_type == "img":
            # Images have alt text
            alt = ''.join(str(e) for e in elem) # XXX handle non-text e
            if alt:
                attrib[html.alt] = alt
            new_elem = self.new(html.img, attrib)

        else:
            if obj_type != "object":
                # Non-objects have the "controls" attribute
                attrib[html.controls] = 'controls'
            new_elem = self.new_copy(getattr(html, obj_type), elem, attrib)

        return wrap_object_with_overlay(new_elem, href=href)
Пример #18
0
        def __init__(self, lexer=None, contenttype=None):
            """
            Create a Pygments Converter.

            :param lexer: pygments lexer instance
            :param contenttype: contenttype to get a lexer for
            """
            if lexer is None and contenttype is not None:
                ct = Type(contenttype)
                # pygments can't process parameters (like e.g. ...;charset=utf-8):
                mimetype = '{0}/{1}'.format(ct.type, ct.subtype)

                # TODO: fix pygments and remove this workaround for missing mimetypes; see issue #16
                alias_mimetypes = {'text/x.moin.wiki': 'text/x-trac-wiki',
                                   'text/x.moin.creole': 'text/x-trac-wiki',
                                   'application/docbook+xml': 'application/xml'}
                mimetype = alias_mimetypes[mimetype] if mimetype in alias_mimetypes else mimetype

                try:
                    lexer = pygments.lexers.get_lexer_for_mimetype(mimetype)
                except pygments.util.ClassNotFound:
                    lexer = pygments.lexers.get_lexer_for_mimetype('text/plain')
            self.lexer = lexer
Пример #19
0
""" % (html, id, id, id))

        elem_note = ET.XML("""
<html:p xmlns:html="%s" html:id="note-%d"><html:sup><html:a html:href="#note-%d-ref">%d</html:a></html:sup></html:p>
""" % (html, id, id, id))

        elem_note.extend(body)
        self._special_stack[-1].add_footnote(elem_note)

        return elem_ref

    def visit_moinpage_table_of_content(self, elem):
        level = int(elem.get(moin_page.outline_level, 6))

        attrib = {html.class_: 'moin-table-of-contents'}
        elem = self.new(html.div, attrib)

        self._special_stack[-1].add_toc(elem, level)
        return elem


class ConverterDocument(ConverterPage):
    """
    Converter application/x.moin.document -> application/xhtml+xml
    """


from . import default_registry
from MoinMoin.util.mime import Type, type_moin_document
default_registry.register(ConverterPage._factory, type_moin_document, Type('application/x-xhtml-moin-page'))
Пример #20
0

from emeraldtree import ElementTree as ET

from MoinMoin.i18n import _, L_, N_
from MoinMoin.util.iri import Iri
from MoinMoin.util.tree import moin_page, xlink
from MoinMoin.constants.contenttypes import CONTENTTYPE_NONEXISTENT


class Converter(object):
    """
    Convert a non-existing item to DOM Tree.
    """
    @classmethod
    def _factory(cls, input, output, **kw):
        return cls()

    def __call__(self, rev, contenttype=None, arguments=None):
        item_name = rev.item.fqname.value
        attrib = {
            xlink.href: Iri(scheme='wiki', authority='', path='/' + item_name, query='do=modify'),
        }
        a = moin_page.a(attrib=attrib, children=[_("%(item_name)s does not exist. Create it?", item_name=item_name)])
        body = moin_page.body(children=(a, ))
        return moin_page.page(children=(body, ))

from . import default_registry
from MoinMoin.util.mime import Type, type_moin_document
default_registry.register(Converter._factory, Type(CONTENTTYPE_NONEXISTENT), type_moin_document)
Пример #21
0
        # Parse the high-level elements, md_root is an ElementTree object
        md_root = self.markdown.parser.parseDocument(lines).getroot()

        # Run the tree-processors
        for treeprocessor in self.markdown.treeprocessors.values():
            new_md_root = treeprocessor.run(md_root)
            if new_md_root:
                md_root = new_md_root

        # }}} end stolen from Markdown.convert

        add_lineno = bool(flaskg and flaskg.add_lineno_attr)

        # run markdown post processors and convert from ElementTree to an EmeraldTree object
        converted = self.do_children(md_root, add_lineno=add_lineno)

        # convert html embedded in text strings to EmeraldTree nodes
        self.convert_embedded_markup(converted)
        # convert P-tags containing block elements to DIV-tags
        self.convert_invalid_p_nodes(converted)

        body = moin_page.body(children=converted)
        root = moin_page.page(children=[body])

        return root

from . import default_registry
from MoinMoin.util.mime import Type, type_moin_document
default_registry.register(Converter._factory, Type("text/x-markdown"), type_moin_document)
default_registry.register(Converter._factory, Type('x-moin/format;name=markdown'), type_moin_document)
Пример #22
0
        """Recognize inline elements within the given text"""

        pos = 0
        for match in inline_re.finditer(text):
            # Handle leading text
            stack.top_append_ifnotempty(text[pos:match.start()])
            pos = match.end()

            self._apply(match, 'inline', stack)

        # Handle trailing text
        stack.top_append_ifnotempty(text[pos:])

    def macro_text(self, text):
        """
        Return an ET tree branch representing the markup present in the input text. Used for FootNotes, etc.
        """
        p = moin_page.p()
        iter_content = _Iter(text)
        stack = _Stack(p, iter_content=iter_content)
        self.parse_inline(text, stack, self.inline_re)
        return p


from . import default_registry
from MoinMoin.util.mime import Type, type_moin_document, type_moin_creole
default_registry.register(Converter.factory, type_moin_creole,
                          type_moin_document)
default_registry.register(Converter.factory, Type('x-moin/format;name=creole'),
                          type_moin_document)
Пример #23
0
                except pygments.util.ClassNotFound:
                    lexer = pygments.lexers.get_lexer_for_mimetype('text/plain')
            self.lexer = lexer

        def __call__(self, data, contenttype=None, arguments=None):
            text = decode_data(data, contenttype)
            content = normalize_split_text(text)
            content = u'\n'.join(content)
            blockcode = moin_page.blockcode(attrib={moin_page.class_: 'highlight'})
            pygments.highlight(content, self.lexer, TreeFormatter(), blockcode)
            body = moin_page.body(children=(blockcode, ))
            return moin_page.page(children=(body, ))

    from . import default_registry
    from MoinMoin.util.mime import Type, type_moin_document
    default_registry.register(Converter._factory, Type(type='text'), type_moin_document)
    default_registry.register(Converter._factory, Type('x-moin/format'), type_moin_document)

else:
    # we have no Pygments, minimal Converter replacement, so highlight view does not crash
    class Converter(object):
        def __init__(self, lexer=None, contenttype=None):
            pass

        def __call__(self, content, arguments=None):
            """Parse the text and return DOM tree."""
            blockcode = moin_page.blockcode()
            for line in content:
                if len(blockcode):
                    blockcode.append('\n')
                blockcode.append(line.expandtabs())
Пример #24
0
        return body

    def parse_inline(self, text, stack, inline_re):
        """Recognize inline elements within the given text"""

        pos = 0
        for match in inline_re.finditer(text):
            # Handle leading text
            stack.top_append_ifnotempty(text[pos:match.start()])
            pos = match.end()

            self._apply(match, 'inline', stack)

        # Handle trailing text
        stack.top_append_ifnotempty(text[pos:])

    def macro_text(self, text):
        """
        Return an ET tree branch representing the markup present in the input text. Used for FootNotes, etc.
        """
        p = moin_page.p()
        iter_content = _Iter(text)
        stack = _Stack(p, iter_content=iter_content)
        self.parse_inline(text, stack, self.inline_re)
        return p

from . import default_registry
from MoinMoin.util.mime import Type, type_moin_document, type_moin_wiki
default_registry.register(Converter.factory, type_moin_wiki, type_moin_document)
default_registry.register(Converter.factory, Type('x-moin/format;name=wiki'), type_moin_document)
Пример #25
0
                        'text/plain')
            self.lexer = lexer

        def __call__(self, data, contenttype=None, arguments=None):
            text = decode_data(data, contenttype)
            content = normalize_split_text(text)
            content = u'\n'.join(content)
            blockcode = moin_page.blockcode(
                attrib={moin_page.class_: 'highlight'})
            pygments.highlight(content, self.lexer, TreeFormatter(), blockcode)
            body = moin_page.body(children=(blockcode, ))
            return moin_page.page(children=(body, ))

    from . import default_registry
    from MoinMoin.util.mime import Type, type_moin_document
    default_registry.register(Converter._factory, Type(type='text'),
                              type_moin_document)
    default_registry.register(Converter._factory, Type('x-moin/format'),
                              type_moin_document)

else:
    # we have no Pygments, minimal Converter replacement, so highlight view does not crash
    class Converter(object):
        def __init__(self, lexer=None, contenttype=None):
            pass

        def __call__(self, content, arguments=None):
            """Parse the text and return DOM tree."""
            blockcode = moin_page.blockcode()
            for line in content:
                if len(blockcode):
Пример #26
0
application/vnd.oasis.opendocument.database
application/vnd.oasis.opendocument.formula
application/vnd.oasis.opendocument.graphics
application/vnd.oasis.opendocument.graphics-template
application/vnd.oasis.opendocument.image
application/vnd.oasis.opendocument.presentation
application/vnd.oasis.opendocument.presentation-template
application/vnd.oasis.opendocument.spreadsheet
application/vnd.oasis.opendocument.spreadsheet-template
application/vnd.oasis.opendocument.text
application/vnd.oasis.opendocument.text-master
application/vnd.oasis.opendocument.text-template
application/vnd.oasis.opendocument.text-web""".split()

for t in opendocument_types:
    default_registry.register(OpenDocumentIndexingConverter._factory, Type(t),
                              type_text_plain)

# use same converter for the old *.sx? (pre-opendocument) openoffice documents:
OpenOfficeIndexingConverter = OpenDocumentIndexingConverter

openoffice_types = """\
application/vnd.sun.xml.calc
application/vnd.sun.xml.draw
application/vnd.sun.xml.impress
application/vnd.sun.xml.math
application/vnd.sun.xml.writer
application/vnd.sun.xml.writer.global""".split()

for t in openoffice_types:
    default_registry.register(OpenOfficeIndexingConverter._factory, Type(t),
Пример #27
0
                    \s
                    |
                    [,.:;!?()]
                    (\s | $)
                )
            )
        )
    """

    def inline_url_repl(self, stack, url, url_target):
        url = Iri(url_target)
        attrib = {xlink.href: url}
        element = moin_page.a(attrib=attrib, children=[url_target])
        stack.top_append(element)

    inline = Converter.inline + (
        inline_freelink,
        inline_url,
    )
    inline_re = re.compile('|'.join(inline), re.X | re.U)


from . import default_registry
from MoinMoin.util.mime import Type, type_moin_document, type_moin_wiki
default_registry.register(ConverterFormat19.factory,
                          Type('text/x.moin.wiki;format=1.9'),
                          type_moin_document)
default_registry.register(ConverterFormat19.factory,
                          Type('x-moin/format;name=wiki;format=1.9'),
                          type_moin_document)
Пример #28
0
                               laparams=laparams,
                               showpageno=showpageno)
        self.__text = []

    def write_text(self, text):
        self.__text.append(text)

    def read_result(self):
        return u''.join(self.__text)


class PDFIndexingConverter(object):
    @classmethod
    def _factory(cls, input, output, **kw):
        return cls()

    def __call__(self, rev, contenttype=None, arguments=None):
        rsrcmgr = PDFResourceManager()
        device = UnicodeConverter(rsrcmgr, laparams=LAPARAMS)
        try:
            process_pdf(rsrcmgr, device, rev)
            return device.read_result()
        finally:
            device.close()


from . import default_registry
from MoinMoin.util.mime import Type, type_text_plain
default_registry.register(PDFIndexingConverter._factory,
                          Type('application/pdf'), type_text_plain)
Пример #29
0
    def visit_simple_tag(self, element, depth):
        """
        Some docbook tags can be converted directly to an equivalent
        DOM Tree element. We retrieve the equivalent tag from the
        simple_tags dictionnary defined at the begining of this file.
        """
        tag_to_return = self.simple_tags[element.tag.name]
        return self.new_copy(tag_to_return, element, depth, attrib={})

    def start_dom_tree(self, element, depth):
        """
        Return the root element of the DOM tree, with all the children.

        We also add a <table-of-content> element if needed.
        """
        attrib = {}
        if self.standard_attribute:
            attrib.update(self.standard_attribute)
            self.standard_attribute = {}
        children = []
        children.append(self.visit(element, depth))
        # We show the table of content only if it is not empty
        if self.is_section:
            children.insert(0, self.new(moin_page('table-of-content'), attrib={}, children={}))
        body = self.new(moin_page.body, attrib={}, children=children)
        return self.new(moin_page.page, attrib=attrib, children=[body])

from . import default_registry
from MoinMoin.util.mime import Type, type_moin_document
default_registry.register(Converter._factory, Type('application/docbook+xml'), type_moin_document)
Пример #30
0
 def __call__(self, content_type, *args, **kw):
     if self.content_type.issupertype(Type(content_type)):
         return self.factory(content_type, *args, **kw)