Exemplo n.º 1
0
    def inline_object_repl(self, stack, object, object_url=None, object_item=None,
                           object_text=None, object_args=None):
        """Handles objects transcluded within the page."""
        if object_args:
            args = parse_arguments(object_args).keyword  # XXX needs different parsing
        else:
            args = {}

        query_keys = {}
        attrib = {}
        whitelist = ['width', 'height', 'class']
        for attr, value in args.iteritems():
            if attr.startswith('&'):
                query_keys[attr[1:]] = value
            elif attr in whitelist:
                attrib[html(attr)] = value
        if object_text:
            attrib[html.alt] = object_text
        if object_item is not None:
            # img tag
            query = url_encode(query_keys, charset=CHARSET, encode_keys=True)
            # TODO: moin 1.9 needed this for an attached file; move functionality to script/migration/moin/import19.py
            att = 'attachment:'
            if object_item.startswith(att):
                object_item = '/' + object_item[len(att):]  # now we have a subitem
            target = Iri(scheme='wiki.local', path=object_item, query=query, fragment=None)
            attrib[xinclude.href] = target
            element = xinclude.include(attrib=attrib)
            stack.top_append(element)
        else:
            # object tag
            target = Iri(object_url)
            attrib[xlink.href] = target
            element = moin_page.object(attrib)
            stack.top_append(element)
Exemplo n.º 2
0
    def visit_reference(self, node):
        refuri = node.get('refuri', u'')
        if refuri.startswith(u'<<') and refuri.endswith(u'>>'):  # moin macro
            macro_name = refuri[2:-2].split(u'(')[0]
            if macro_name == u"TableOfContents":
                arguments = refuri[2:-2].split(u'(')[1][:-1].split(u',')
                node = moin_page.table_of_content()
                self.open_moin_page_node(node)
                if arguments and arguments[0]:
                    node.set(moin_page.outline_level, arguments[0])
                return
            if macro_name == u"Include":
                # include macros are expanded by include.py similar to transclusions
                # rst include handles only wiki pages and does not support additional arguments like moinwiki
                arguments = refuri[2:-2].split(u'(')[1][:-1].split(u',')
                link = Iri(scheme=u'wiki.local', path=arguments)
                node = xinclude.include(
                    attrib={
                        xinclude.href: link,
                        moin_page.alt: refuri,
                        moin_page.content_type: 'x-moin/macro;name=' +
                        macro_name,
                    })
                self.open_moin_page_node(node)
                return
            try:
                arguments = refuri[2:-2].split(u'(')[1][:-1]
            except IndexError:
                arguments = u''  # <<DateTime>>

            self.open_moin_page_node(
                moin_page.inline_part(
                    attrib={
                        moin_page.content_type:
                        "x-moin/macro;name={0}".format(macro_name)
                    }))
            if arguments:
                self.open_moin_page_node(moin_page.arguments())
                self.open_moin_page_node(arguments)
                self.close_moin_page_node()
                self.close_moin_page_node()
            return

        if not allowed_uri_scheme(refuri):
            self.visit_error(node)
            return
        if refuri == u'':
            # build a link to a heading or an explicitly defined anchor
            refuri = Iri(scheme='wiki.local',
                         fragment=node.attributes['name'].replace(' ', '_'))
        self.open_moin_page_node(moin_page.a(attrib={xlink.href: refuri}))
Exemplo n.º 3
0
    def visit_image(self, node):
        """
        Processes images and other transcluded objects.
        """
        whitelist = [
            'width',
            'height',
            'alt',
        ]
        attrib = {}
        for key in whitelist:
            if node.get(key):
                attrib[html(key)] = node.get(key)

        # there is no 'scale' attribute, hence absent from whitelist, handled separately
        if node.get('scale'):
            scaling_factor = int(node.get('scale')) / 100.0
            for key in ('width', 'height'):
                if html(key) in attrib:
                    attrib[html(key)] = int(
                        int(attrib[html(key)]) * scaling_factor)

        # "align" parameter is invalid in HTML5. Convert it to a class defined in userstyles.css.
        userstyles = {
            'left': 'left',
            'center': 'center',
            'right': 'right',
            'top':
            'top',  # rst parser creates error messages for top, bottom, and middle
            'bottom': 'bottom',
            'middle': 'middle',
        }
        alignment = userstyles.get(node.get('align'))
        if alignment:
            attrib[html.class_] = alignment

        url = Iri(node['uri'])
        if url.scheme is None:
            # img
            target = Iri(scheme='wiki.local', path=node['uri'], fragment=None)
            attrib[xinclude.href] = target
            new_node = xinclude.include(attrib=attrib)
        else:
            # obj
            new_node = moin_page.object(attrib)
            new_node.set(xlink.href, url)

        self.open_moin_page_node(new_node)
Exemplo n.º 4
0
 def inline_object_repl(self, stack, object, object_page=None, object_url=None, object_text=None):
     """Handles objects included in the page."""
     attrib = {}
     if object_text:
         attrib[html.alt] = object_text
     if object_page is not None:
         att = 'attachment:'  # moin 1.9 needed this for an attached file
         if object_page.startswith(att):
             object_page = '/' + object_page[len(att):]  # now we have a subitem
         target = Iri(scheme='wiki.local', path=object_page)
         attrib[xinclude.href] = target
         element = xinclude.include(attrib=attrib)
     else:
         attrib[xlink.href] = object_url
         element = moin_page.object(attrib)
     stack.top_append(element)
Exemplo n.º 5
0
 def visit_img(self, element):
     """
     <img src="URI" /> --> <object xlink:href="URI />
     """
     attrib = {}
     url = Iri(element.attrib.get('src'))
     if element.attrib.get('alt'):
         attrib[html.alt] = element.attrib.get('alt')
     if element.attrib.get('title'):
         attrib[html.title_] = element.attrib.get('title')
     if url.scheme is None:
         # img tag
         target = Iri(scheme='wiki.local', path=element.attrib.get("src"), fragment=None)
         attrib[xinclude.href] = target
         new_node = xinclude.include(attrib=attrib)
     else:
         # object tag
         attrib[xlink.href] = url
         new_node = moin_page.object(attrib)
     return new_node
Exemplo n.º 6
0
    def _Include_repl(self, args, text, context_block):
        """
        Return a moin_page node representing an include macro that will be processed
        further in /converter/include.py.

        The transclusion {{jpeg.jpg}} and the macro <<Include(jpeg.jpg)>> will have
        identical output.

        If context_block is true, the macro expansion will be enclosed in a DIV-tag, else the
        macro output will be enclosed in a SPAN-tag. converter/include.py will resolve
        HTML 5 validation issues should the macro output block tags within an inline context.
        """
        def error_message(msg):
            txt = moin_page.p(children=(text, ))
            msg = moin_page.p(children=(msg, ))
            msg.set(moin_page.class_, 'moin-error')
            div = moin_page.div(children=(txt, msg))
            return div

        if args:
            args = parse_arguments(args, parse_re=include_re)
        else:
            return error_message(
                _("Include Macro above has invalid format, missing item name"))
        pagename = args[0]
        heading = None
        level = None
        try:
            heading = args[1]
            level = int(args[2])
        except (IndexError, ValueError):
            pass
        sort = 'sort' in args and args['sort']
        if sort and sort not in ('ascending', 'descending'):
            return error_message(
                _("Include Macro above has invalid format, expected sort=ascending or descending"
                  ))
        # TODO: We need corresponding code in include.py to process items, skipitems, titlesonly, and editlink
        items = 'items' in args and int(args['items'])
        skipitems = 'skipitems' in args and int(args['skipitems'])
        titlesonly = 'titlesonly' in args
        editlink = 'editlink' in args

        attrib = {}
        xpointer = []
        xpointer_moin = []

        def add_moin_xpointer(function, args):
            args = unicode(args).replace('^', '^^').replace('(', '^(').replace(
                ')', '^)')
            xpointer_moin.append(function + u'(' + args + u')')

        moin_args = []

        if pagename.startswith(u'^'):
            add_moin_xpointer(u'pages', pagename)
            if sort:
                add_moin_xpointer(u'sort', sort)
            if items:
                add_moin_xpointer(u'items', items)
            if skipitems:
                add_moin_xpointer(u'skipitems', skipitems)
        else:
            link = iri.Iri(scheme=u'wiki.local', path=pagename)
            attrib[xinclude.href] = link

        if heading is not None:
            add_moin_xpointer(u'heading', heading)
        if level:
            add_moin_xpointer(u'level', str(level))
        if titlesonly:
            add_moin_xpointer(u'titlesonly', u'')
        if editlink:
            add_moin_xpointer(u'editlink', u'')

        if xpointer_moin:
            xpointer.append(u'page:include({0})'.format(
                u' '.join(xpointer_moin)))

        if xpointer:
            # TODO: Namespace?
            ns = 'xmlns(page={0}) '.format(moin_page)

            attrib[xinclude.xpointer] = ns + ' '.join(xpointer)

        span_wrap = xinclude.include(attrib=attrib)
        if not context_block:
            return span_wrap
        attrib = {moin_page.class_: 'moin-p'}
        return moin_page.div(attrib=attrib, children=[span_wrap])