Exemplo n.º 1
0
    def __init__(self, request, page):
        self.parser = Parser("", request)
        # Format the empty string, making it set its internal data (ugh!).
        out = cStringIO.StringIO()
        backup = sys.stdout, request.write
        sys.stdout, request.write = out, out.write
        self.parser.format(page.formatter)
        sys.stdout, request.write = backup
        self.include_re = re.compile("\[\[Include(?:\(.*?\))?\]\]")
        self.macro = macro.Macro(self.parser)

        # This is really deep and cool black magic. Basically, it creates
        # a local copy of the function macro.Include.execute that behaves
        # exactly like the original one, but with a different "Page"
        # global. This allows us to follow changes in the Include macro
        # without much trouble.
        from MoinMoin.macro.Include import execute
        func_globals = {}
        func_globals.update(execute.func_globals)

        class IncludePage(Page):
            incparser = self

            def send_page(self, request, msg=None, **keywords):
                request.write(self.incparser._parse(self.get_raw_body()))

        func_globals["Page"] = IncludePage
        self.execute = new.function(execute.func_code, func_globals,
                                    execute.func_name, execute.func_defaults)
Exemplo n.º 2
0
def make_macro(request, page):
    """ creates the macro """
    from MoinMoin import macro
    p = Parser("##\n", request)
    p.formatter = Formatter(request)
    p.formatter.page = page
    request.page = page
    request.formatter = p.formatter
    p.form = request.form
    m = macro.Macro(p)
    return m
Exemplo n.º 3
0
    def _macro_repl(self, word):
        """Handle macros ([[macroname]])."""
        macro_name = word[2:-2]
        self.inhibit_p = 0  # 1 fixes UserPreferences, 0 fixes paragraph formatting for macros

        # check for arguments
        args = None
        if macro_name.count("("):
            macro_name, args = macro_name.split('(', 1)
            args = args[:-1]

        # create macro instance
        if self.macro is None:
            self.macro = macro.Macro(self)
        return self.formatter.macro(self.macro, macro_name, args)
Exemplo n.º 4
0
def send_viewfile(pagename, request):
    _ = request.getText
    fmt = request.html_formatter

    pagename, filename, fpath = _access_file(pagename, request)
    if not filename:
        return

    request.write('<h2>' +
                  _("Attachment '%(filename)s'") % {'filename': filename} +
                  '</h2>')
    # show a download link above the content
    label = _('Download')
    link = (fmt.url(1,
                    getAttachUrl(pagename, filename, request, do='get'),
                    css_class="download") + fmt.text(label) + fmt.url(0))
    request.write('%s<br><br>' % link)

    if filename.endswith('.tdraw') or filename.endswith('.adraw'):
        request.write(fmt.attachment_drawing(filename, ''))
        return

    mt = wikiutil.MimeType(filename=filename)

    # destinguishs if browser need a plugin in place
    if mt.major == 'image' and mt.minor in config.browser_supported_images:
        url = getAttachUrl(pagename, filename, request)
        request.write('<img src="%s" alt="%s">' %
                      (wikiutil.escape(url, 1), wikiutil.escape(filename, 1)))
        return
    elif mt.major == 'text':
        ext = os.path.splitext(filename)[1]
        Parser = wikiutil.getParserForExtension(request.cfg, ext)
        if Parser is not None:
            try:
                content = file(fpath, 'r').read()
                content = wikiutil.decodeUnknownInput(content)
                colorizer = Parser(content, request, filename=filename)
                colorizer.format(request.formatter)
                return
            except IOError:
                pass

        request.write(request.formatter.preformatted(1))
        # If we have text but no colorizing parser we try to decode file contents.
        content = open(fpath, 'r').read()
        content = wikiutil.decodeUnknownInput(content)
        content = wikiutil.escape(content)
        request.write(request.formatter.text(content))
        request.write(request.formatter.preformatted(0))
        return

    try:
        package = packages.ZipPackage(request, fpath)
        if package.isPackage():
            request.write(
                "<pre><b>%s</b>\n%s</pre>" %
                (_("Package script:"), wikiutil.escape(package.getScript())))
            return

        if zipfile.is_zipfile(fpath) and mt.minor == 'zip':
            zf = zipfile.ZipFile(fpath, mode='r')
            request.write("<pre>%-46s %19s %12s\n" %
                          (_("File Name"), _("Modified") + " " * 5, _("Size")))
            for zinfo in zf.filelist:
                date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time
                request.write(
                    wikiutil.escape("%-46s %s %12d\n" %
                                    (zinfo.filename, date, zinfo.file_size)))
            request.write("</pre>")
            return
    except RuntimeError:
        # We don't want to crash with a traceback here (an exception
        # here could be caused by an uploaded defective zip file - and
        # if we crash here, the user does not get a UI to remove the
        # defective zip file again).
        # RuntimeError is raised by zipfile stdlib module in case of
        # problems (like inconsistent slash and backslash usage in the
        # archive).
        logging.exception(
            "An exception within zip file attachment handling occurred:")
        return

    from MoinMoin import macro
    from MoinMoin.parser.text import Parser

    macro.request = request
    macro.formatter = request.html_formatter
    p = Parser("##\n", request)
    m = macro.Macro(p)

    # use EmbedObject to view valid mime types
    if mt is None:
        request.write(
            '<p>' +
            _("Unknown file type, cannot display this attachment inline.") +
            '</p>')
        link = (fmt.url(1, getAttachUrl(pagename, filename, request)) +
                fmt.text(filename) + fmt.url(0))
        request.write('For using an external program follow this link %s' %
                      link)
        return
    request.write(
        m.execute('EmbedObject',
                  u'target="%s", pagename="%s"' % (filename, pagename)))
    return