Пример #1
0
def testis_URL():
    sample_schemes = ['http', 'https', 'ftp', 'ssh']
    for scheme in sample_schemes:
        result = wikiutil.is_URL(scheme + ':MoinMoin')
        assert result

    # arg without ':' which is a mandatory requirement
    result = wikiutil.is_URL('MoinMoin')
    assert not result
    # invalid scheme
    result = wikiutil.is_URL('invalid_scheme:MoinMoin')
    assert not result
Пример #2
0
    def split_navilink(self, text):
        """
        Split navibar links into pagename, link to page

        Admin or user might want to use shorter navibar items by using
        the [[page|title]] or [[url|title]] syntax.

        Supported syntax:
            * PageName
            * WikiName:PageName
            * wiki:WikiName:PageName
            * url
            * all targets as seen above with title: [[target|title]]

        :param text: the text used in config or user preferences
        :rtype: tuple
        :returns: pagename or url, link to page or url
        """
        title = None
        wiki_local = ''  # means local wiki

        # Handle [[pagename|title]] or [[url|title]] formats
        if text.startswith('[[') and text.endswith(']]'):
            text = text[2:-2]
            try:
                target, title = text.split('|', 1)
                target = target.strip()
                title = title.strip()
            except (ValueError, TypeError):
                # Just use the text as is.
                target = text.strip()
        else:
            target = text

        if wikiutil.is_URL(target):
            if not title:
                title = target
            return target, title, wiki_local

        # remove wiki: url prefix
        if target.startswith("wiki:"):
            target = target[5:]

        wiki_name, namespace, field, item_name = split_interwiki(target)
        if wiki_name == 'Self':
            wiki_name = ''
        href = url_for_item(item_name,
                            namespace=namespace,
                            wiki_name=wiki_name,
                            field=field)
        if not title:
            title = shorten_fqname(CompositeName(namespace, field, item_name))
        return href, title, wiki_name
Пример #3
0
    def split_navilink(self, text):
        """
        Split navibar links into pagename, link to page

        Admin or user might want to use shorter navibar items by using
        the [[page|title]] or [[url|title]] syntax.

        Supported syntax:
            * PageName
            * WikiName:PageName
            * wiki:WikiName:PageName
            * url
            * all targets as seen above with title: [[target|title]]

        :param text: the text used in config or user preferences
        :rtype: tuple
        :returns: pagename or url, link to page or url
        """
        title = None
        wiki_local = ''  # means local wiki

        # Handle [[pagename|title]] or [[url|title]] formats
        if text.startswith('[[') and text.endswith(']]'):
            text = text[2:-2]
            try:
                target, title = text.split('|', 1)
                target = target.strip()
                title = title.strip()
            except (ValueError, TypeError):
                # Just use the text as is.
                target = text.strip()
        else:
            target = text

        if wikiutil.is_URL(target):
            if not title:
                title = target
            return target, title, wiki_local

        # remove wiki: url prefix
        if target.startswith("wiki:"):
            target = target[5:]

        wiki_name, namespace, field, item_name = split_interwiki(target)
        if wiki_name == 'Self':
            wiki_name = ''
        href = url_for_item(item_name, namespace=namespace, wiki_name=wiki_name, field=field)
        if not title:
            title = shorten_fqname(CompositeName(namespace, field, item_name))
        return href, title, wiki_name
Пример #4
0
def macro_EmbedObject(
        macro,
        target=wikiutil.required_arg(unicode),
        pagename=None,
        width=wikiutil.UnitArgument(None,
                                    float, ['px', 'em', 'pt', 'in', 'mm', '%'],
                                    defaultunit='px'),
        height=wikiutil.UnitArgument(None,
                                     float,
                                     ['px', 'em', 'pt', 'in', 'mm', '%'],
                                     defaultunit='px'),
        alt=u'',
        play=False,
        stop=True,
        loop=False,
        quality=(u'high', u'low', u'medium'),
        op=True,
        repeat=False,
        autostart=False,
        align=(u'middle', u'top', u'bottom'),
        hidden=False,
        menu=True,
        wmode=u'transparent',
        url_mimetype=None):
    """ This macro is used to embed an object into a wiki page """
    # Join unit arguments with their units
    if width:
        if width[1] == 'px':
            width = '%dpx' % int(width[0])
        else:
            width = '%g%s' % width

    if height:
        if height[1] == 'px':
            height = '%dpx' % int(height[0])
        else:
            height = '%g%s' % height

    request = macro.request
    _ = macro.request.getText
    fmt = macro.formatter

    # AttachFile calls always with pagename. Users can call the macro from a different page as the attachment is saved.
    if not pagename:
        pagename = fmt.page.page_name

    if not wikiutil.is_URL(target):
        pagename, fname = AttachFile.absoluteName(target, pagename)

        if not AttachFile.exists(request, pagename, fname):
            linktext = _('Upload new attachment "%(filename)s"') % {
                'filename': fname
            }
            target = AttachFile.getAttachUrl(pagename,
                                             fname,
                                             request,
                                             do='upload_form')
            return (fmt.url(1, target) + fmt.text(linktext) + fmt.url(0))

        url = AttachFile.getAttachUrl(pagename, fname, request)
        mt = wikiutil.MimeType(filename=fname)
    else:
        if not url_mimetype:
            return fmt.text(
                _('%(extension_name)s %(extension_type)s: Required argument %(argument_name)s missing.'
                  ) % {
                      "extension_name": extension_name,
                      "extension_type": extension_type,
                      "argument_name": "url_mimetype",
                  })
        else:
            url = target
            mt = wikiutil.MimeType()  # initialize dict
            try:
                mt.major, mt.minor = url_mimetype.split('/')
            except ValueError:
                return fmt.text(
                    _('%(extension_name)s %(extension_type)s: Invalid %(argument_name)s=%(argument_value)s!'
                      ) % {
                          "extension_name": extension_name,
                          "extension_type": extension_type,
                          "argument_name": "url_mimetype",
                          "argument_value": str(url_mimetype),
                      })

    mime_type = "%s/%s" % (
        mt.major,
        mt.minor,
    )
    dangerous = mime_type in request.cfg.mimetypes_xss_protect

    if not mime_type in request.cfg.mimetypes_embed or dangerous:
        return "%s: %s%s%s" % (fmt.text(
            _("Current configuration does not allow embedding of the file %(file)s because of its mimetype %(mimetype)s."
              ) % {
                  "mimetype": mime_type,
                  "file": target
              }), fmt.url(1, url), fmt.text(target), fmt.url(0))

    if not alt:
        alt = "%(text)s %(mime_type)s" % {
            'text': _("Embedded"),
            'mime_type': mime_type
        }

    embed_src = ''
    if mt.major == 'video':
        if not width and not height:
            width = '400px'
            height = '400px'

        embed_src = '''
<object %(ob_data)s %(ob_type)s %(ob_width)s %(ob_height)s %(ob_align)s %(ob_standby)s %(ob_stop)s>
%(wmode)s%(movie)s%(play)s%(stop)s%(repeat)s%(autostart)s%(op)s%(menu)s
<p>%(alt)s</p>
</object>''' % {
            "ob_data": _check_object_value("data", url),
            "ob_type": _check_object_value("type", mime_type),
            "ob_width": _check_object_value("width", width),
            "ob_height": _check_object_value("height", height),
            "ob_align": _check_object_value("align", align),
            "ob_standby": _check_object_value("standby", alt),
            "ob_stop": _check_object_value("stop", stop),
            "wmode": _check_param_value("wmode", wmode, "data"),
            "movie": _check_param_value("movie", url, "data"),
            "play": _check_param_value("play", play, "data"),
            "stop": _check_param_value("stop", stop, "data"),
            "repeat": _check_param_value("repeat", repeat, "data"),
            "autostart": _check_param_value("autostart", autostart, "data"),
            "op": _check_param_value("op", op, "data"),
            "menu": _check_param_value("menu", menu, "data"),
            "alt": wikiutil.escape(alt),
        }

    elif mt.major in ['image', 'chemical', 'x-world']:
        embed_src = '''
<object %(ob_data)s %(ob_type)s  %(ob_width)s %(ob_height)s %(ob_align)s>
%(name)s
<p>%(alt)s</p>
</object>''' % {
            "mime_type": mime_type,
            "ob_data": _check_object_value("data", url),
            "ob_width": _check_object_value("width", width),
            "ob_height": _check_object_value("height", height),
            "ob_type": _check_object_value("type", mime_type),
            "ob_align": _check_object_value("align", align),
            "name": _check_param_value("name", url, "data"),
            "alt": wikiutil.escape(alt),
        }

    elif mt.major == 'audio':
        if not width and not height:
            width = '400px'
            height = '100px'
        embed_src = '''
<object %(ob_data)s %(ob_type)s  %(ob_width)s %(ob_height)s %(ob_align)s>
%(audio)s%(repeat)s%(autostart)s%(op)s%(play)s%(stop)s%(hidden)s<p>%(alt)s</p>
</object>''' % {
            "ob_data": _check_object_value("data", url),
            "ob_width": _check_object_value("width", width or "60"),
            "ob_height": _check_object_value("height", height or "20"),
            "ob_type": _check_object_value("type", mime_type),
            "ob_align": _check_object_value("align", align),
            "audio": _check_param_value("audio", url, "data"),
            "repeat": _check_param_value("repeat", repeat, "data"),
            "autostart": _check_param_value("autostart", autostart, "data"),
            "op": _check_param_value("op", op, "data"),
            "play": _check_param_value("play", play, "data"),
            "stop": _check_param_value("stop", stop, "data"),
            "hidden": _check_param_value("hidden", hidden, "data"),
            "alt": wikiutil.escape(alt),
        }

    elif mt.major == 'application':
        # workaround for the acroread browser plugin not knowing the size to embed
        # we use a width of 100% for the case that there is no width given.
        # A height of 100% gives a fullscreen pdf file view without embedding it into the wikicontent.
        if mt.minor == 'pdf':
            width = width or '100%'
            height = height or '800px'
            embed_src = '''
<object %(ob_data)s %(ob_type)s %(ob_width)s %(ob_height)s %(ob_align)s>
<p>%(alt)s</p>
</object>''' % {
                "ob_data": _check_object_value("data", url),
                "ob_width": _check_object_value("width", width),
                "ob_height": _check_object_value("height", height),
                "ob_type": _check_object_value("type", mime_type),
                "ob_align": _check_object_value("align", align),
                "alt": wikiutil.escape(alt),
            }
        else:
            embed_src = '''
<object %(ob_data)s %(ob_type)s %(ob_width)s %(ob_height)s %(ob_align)s>
%(movie)s%(quality)s%(wmode)s%(autostart)s%(play)s%(loop)s%(menu)s<p>%(alt)s</p>
</object>''' % {
                "ob_data": _check_object_value("data", url),
                "ob_width": _check_object_value("width", width),
                "ob_height": _check_object_value("height", height),
                "ob_type": _check_object_value("type", mime_type),
                "ob_align": _check_object_value("align", align),
                "movie": _check_param_value("movie", url, "data"),
                "quality": _check_param_value("quality", quality, "data"),
                "wmode": _check_param_value("wmode", wmode, "data"),
                "autostart": _check_param_value("autostart", autostart,
                                                "data"),
                "play": _check_param_value("play", play, "data"),
                "loop": _check_param_value("loop", loop, "data"),
                "menu": _check_param_value("menu", menu, "data"),
                "alt": wikiutil.escape(alt),
            }

    return fmt.rawHTML(embed_src)
Пример #5
0
def macro_EmbedObject(macro, target=wikiutil.required_arg(unicode), pagename=None,
                      width=wikiutil.UnitArgument(None, float, ['px', 'em', 'pt', 'in', 'mm', '%'], defaultunit='px'),
                      height=wikiutil.UnitArgument(None, float, ['px', 'em', 'pt', 'in', 'mm', '%'], defaultunit='px'),
                      alt=u'',
                      play=False, stop=True, loop=False, quality=(u'high', u'low', u'medium'),
                      op=True, repeat=False, autostart=False, align=(u'middle', u'top', u'bottom'), hidden=False,
                      menu=True, wmode=u'transparent', url_mimetype=None):
    """ This macro is used to embed an object into a wiki page """
    # Join unit arguments with their units
    if width:
        if width[1] == 'px':
            width = '%dpx' % int(width[0])
        else:
            width = '%g%s' % width

    if height:
        if height[1] == 'px':
            height = '%dpx' % int(height[0])
        else:
            height = '%g%s' % height

    request = macro.request
    _ = macro.request.getText
    fmt = macro.formatter

    # AttachFile calls always with pagename. Users can call the macro from a different page as the attachment is saved.
    if not pagename:
        pagename = fmt.page.page_name

    if not wikiutil.is_URL(target):
        pagename, fname = AttachFile.absoluteName(target, pagename)

        if not AttachFile.exists(request, pagename, fname):
            linktext = _('Upload new attachment "%(filename)s"') % {'filename': fname}
            target = AttachFile.getAttachUrl(pagename, fname, request, do='upload_form')
            return (fmt.url(1, target) +
                    fmt.text(linktext) +
                    fmt.url(0))

        url = AttachFile.getAttachUrl(pagename, fname, request)
        mt = wikiutil.MimeType(filename=fname)
    else:
        if not url_mimetype:
            return fmt.text(_('%(extension_name)s %(extension_type)s: Required argument %(argument_name)s missing.') % {
                "extension_name": extension_name,
                "extension_type": extension_type,
                "argument_name": "url_mimetype",
            })
        else:
            url = target
            mt = wikiutil.MimeType() # initialize dict
            try:
                mt.major, mt.minor = url_mimetype.split('/')
            except ValueError:
                return fmt.text(_('%(extension_name)s %(extension_type)s: Invalid %(argument_name)s=%(argument_value)s!') % {
                   "extension_name": extension_name,
                   "extension_type": extension_type,
                   "argument_name": "url_mimetype",
                   "argument_value": str(url_mimetype),
                })

    mime_type = "%s/%s" % (mt.major, mt.minor, )
    dangerous = mime_type in request.cfg.mimetypes_xss_protect

    if not mime_type in request.cfg.mimetypes_embed or dangerous:
        return "%s: %s%s%s" % (fmt.text(
                _("Current configuration does not allow embedding of the file %(file)s because of its mimetype %(mimetype)s.") % {
                    "mimetype": mime_type,
                    "file": target}),
                fmt.url(1, url),
                fmt.text(target),
                fmt.url(0))

    if not alt:
        alt = "%(text)s %(mime_type)s" % {'text': _("Embedded"), 'mime_type': mime_type}

    embed_src = ''
    if mt.major == 'video':
        if not width and not height:
            width = '400px'
            height = '400px'

        embed_src = '''
<object %(ob_data)s %(ob_type)s %(ob_width)s %(ob_height)s %(ob_align)s %(ob_standby)s %(ob_stop)s>
%(wmode)s%(movie)s%(play)s%(stop)s%(repeat)s%(autostart)s%(op)s%(menu)s
<p>%(alt)s</p>
</object>''' % {
    "ob_data": _check_object_value("data", url),
    "ob_type": _check_object_value("type", mime_type),
    "ob_width": _check_object_value("width", width),
    "ob_height": _check_object_value("height", height),
    "ob_align": _check_object_value("align", align),
    "ob_standby": _check_object_value("standby", alt),
    "ob_stop": _check_object_value("stop", stop),
    "wmode": _check_param_value("wmode", wmode, "data"),
    "movie": _check_param_value("movie", url, "data"),
    "play": _check_param_value("play", play, "data"),
    "stop": _check_param_value("stop", stop, "data"),
    "repeat": _check_param_value("repeat", repeat, "data"),
    "autostart": _check_param_value("autostart", autostart, "data"),
    "op": _check_param_value("op", op, "data"),
    "menu": _check_param_value("menu", menu, "data"),
    "alt": wikiutil.escape(alt),
}

    elif mt.major in ['image', 'chemical', 'x-world']:
        embed_src = '''
<object %(ob_data)s %(ob_type)s  %(ob_width)s %(ob_height)s %(ob_align)s>
%(name)s
<p>%(alt)s</p>
</object>''' % {
    "mime_type": mime_type,
    "ob_data": _check_object_value("data", url),
    "ob_width": _check_object_value("width", width),
    "ob_height": _check_object_value("height", height),
    "ob_type": _check_object_value("type", mime_type),
    "ob_align": _check_object_value("align", align),
    "name": _check_param_value("name", url, "data"),
    "alt": wikiutil.escape(alt),
}

    elif mt.major == 'audio':
        if not width and not height:
            width = '400px'
            height = '100px'
        embed_src = '''
<object %(ob_data)s %(ob_type)s  %(ob_width)s %(ob_height)s %(ob_align)s>
%(audio)s%(repeat)s%(autostart)s%(op)s%(play)s%(stop)s%(hidden)s<p>%(alt)s</p>
</object>''' % {
    "ob_data": _check_object_value("data", url),
    "ob_width": _check_object_value("width", width or "60"),
    "ob_height": _check_object_value("height", height or "20"),
    "ob_type": _check_object_value("type", mime_type),
    "ob_align": _check_object_value("align", align),
    "audio": _check_param_value("audio", url, "data"),
    "repeat": _check_param_value("repeat", repeat, "data"),
    "autostart": _check_param_value("autostart", autostart, "data"),
    "op": _check_param_value("op", op, "data"),
    "play": _check_param_value("play", play, "data"),
    "stop": _check_param_value("stop", stop, "data"),
    "hidden": _check_param_value("hidden", hidden, "data"),
    "alt": wikiutil.escape(alt),
}

    elif mt.major == 'application':
        # workaround for the acroread browser plugin not knowing the size to embed
        # we use a width of 100% for the case that there is no width given.
        # A height of 100% gives a fullscreen pdf file view without embedding it into the wikicontent.
        if mt.minor == 'pdf':
            width = width or '100%'
            height = height or '800px'
            embed_src = '''
<object %(ob_data)s %(ob_type)s %(ob_width)s %(ob_height)s %(ob_align)s>
<p>%(alt)s</p>
</object>''' % {
    "ob_data": _check_object_value("data", url),
    "ob_width": _check_object_value("width", width),
    "ob_height": _check_object_value("height", height),
    "ob_type": _check_object_value("type", mime_type),
    "ob_align": _check_object_value("align", align),
    "alt": wikiutil.escape(alt),
}
        else:
            embed_src = '''
<object %(ob_data)s %(ob_type)s %(ob_width)s %(ob_height)s %(ob_align)s>
%(movie)s%(quality)s%(wmode)s%(autostart)s%(play)s%(loop)s%(menu)s<p>%(alt)s</p>
</object>''' % {
    "ob_data": _check_object_value("data", url),
    "ob_width": _check_object_value("width", width),
    "ob_height": _check_object_value("height", height),
    "ob_type": _check_object_value("type", mime_type),
    "ob_align": _check_object_value("align", align),
    "movie": _check_param_value("movie", url, "data"),
    "quality": _check_param_value("quality", quality, "data"),
    "wmode": _check_param_value("wmode", wmode, "data"),
    "autostart": _check_param_value("autostart", autostart, "data"),
    "play": _check_param_value("play", play, "data"),
    "loop": _check_param_value("loop", loop, "data"),
    "menu": _check_param_value("menu", menu, "data"),
    "alt": wikiutil.escape(alt),
}

    return fmt.rawHTML(embed_src)
Пример #6
0
    def split_navilink(self, text, localize=1):
        """
        Split navibar links into pagename, link to page

        Admin or user might want to use shorter navibar items by using
        the [[page|title]] or [[url|title]] syntax. In this case, we don't
        use localization, and the links goes to page or to the url, not
        the localized version of page.

        Supported syntax:
            * PageName
            * WikiName:PageName
            * wiki:WikiName:PageName
            * url
            * all targets as seen above with title: [[target|title]]

        @param text: the text used in config or user preferences
        @rtype: tuple
        @return: pagename or url, link to page or url
        """
        title = None
        wiki_local = ''  # means local wiki

        # Handle [[pagename|title]] or [[url|title]] formats
        if text.startswith('[[') and text.endswith(']]'):
            text = text[2:-2]
            try:
                target, title = text.split('|', 1)
                target = target.strip()
                title = title.strip()
                localize = 0
            except (ValueError, TypeError):
                # Just use the text as is.
                target = text.strip()
        else:
            target = text

        if wikiutil.is_URL(target):
            if not title:
                title = target
            return target, title, wiki_local

        # remove wiki: url prefix
        if target.startswith("wiki:"):
            target = target[5:]

        # try handling interwiki links
        wiki_name, item_name = split_interwiki(target)
        wiki_name, wiki_base_url, item_name, err = resolve_interwiki(wiki_name, item_name)
        href = join_wiki(wiki_base_url, item_name)
        if wiki_name not in [self.cfg.interwikiname, 'Self', ]:
            if not title:
                title = item_name
            return href, title, wiki_name

        # Handle regular pagename like "FrontPage"
        item_name = wikiutil.normalize_pagename(item_name, self.cfg)

        # Use localized pages for the current user
        if localize:
            item_name = self.translated_item_name(item_name)

        if not title:
            title = item_name
        href = url_for('frontend.show_item', item_name=item_name)
        return href, title, wiki_local