Ejemplo n.º 1
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
Ejemplo n.º 2
0
    def __call__(self, rev, contenttype=None, arguments=None):
        item_name = rev.item.name
        query_keys = {'do': 'get', 'rev': rev.revid}
        attrib = {}
        if arguments:
            query = arguments.keyword.get(xinclude.href)
            if query and query.query:
                # query.query value is similar to  "w=75" given a transclusion "{{jpeg||&w=75 class="top"}}"
                query_keys.update(url_decode(query.query))
            attrib = arguments.keyword

        query = url_encode(query_keys, charset=CHARSET, encode_keys=True)

        attrib.update({
            moin_page.type_:
            unicode(self.input_type),
            xlink.href:
            Iri(scheme='wiki', authority='', path='/' + item_name,
                query=query),
        })

        obj = moin_page.object_(attrib=attrib, children=[
            item_name,
        ])
        body = moin_page.body(children=(obj, ))
        return moin_page.page(children=(body, ))
Ejemplo n.º 3
0
 def __call__(self, data, contenttype=None, arguments=None):
     text = decode_data(data, contenttype)
     content = normalize_split_text(text)
     # as of py 2.7.x (and in the year 2013), the csv module seems to still
     # have troubles with unicode, thus we encode to utf-8 ...
     content = [line.encode('utf-8') for line in content]
     dialect = csv.Sniffer().sniff(content[0])
     reader = csv.reader(content, dialect)
     # ... and decode back to unicode
     rows = []
     for encoded_row in reader:
         row = []
         for encoded_cell in encoded_row:
             row.append(encoded_cell.decode('utf-8'))
         if row:
             rows.append(row)
     head = None
     cls = None
     try:
         # fragile function throws errors when csv file is incorrectly formatted
         if csv.Sniffer().has_header('\n'.join(content)):
             head = rows[0]
             rows = rows[1:]
             cls = 'moin-sortable'
     except csv.Error as e:
         head = [_('Error parsing CSV file:'), str(e)]
     table = self.build_dom_table(rows, head=head, cls=cls)
     body = moin_page.body(children=(table, ))
     return moin_page.page(children=(body, ))
Ejemplo n.º 4
0
 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, ))
Ejemplo n.º 5
0
 def __call__(self, rev, contenttype=None, arguments=None):
     item_name = rev.item.name or rev.meta['name'][0]
     attrib = {
         xlink.href: Iri(scheme='wiki', authority='', path='/' + item_name,
                         query='do=get&rev={0}'.format(rev.revid)),
     }
     a = moin_page.a(attrib=attrib, children=[u"Download {0}.".format(item_name)])
     body = moin_page.body(children=(a, ))
     return moin_page.page(children=(body, ))
Ejemplo n.º 6
0
 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())
     body = moin_page.body(children=(blockcode, ))
     return moin_page.page(children=(body, ))
Ejemplo n.º 7
0
 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, ))
Ejemplo n.º 8
0
 def __init__(self):
     self.current_node = moin_page.body()
     self.root = moin_page.page(children=(self.current_node, ))
     self.path = [self.root, self.current_node]
     self.header_size = 1
     self.status = ['document']
     self.footnotes = dict()
     self.last_lineno = 0
     self.current_lineno = 0
Ejemplo n.º 9
0
    def __call__(self, data, contenttype=None, arguments=None):
        """
        Function called by the converter to process the
        conversion.

        TODO: Add support for different arguments
        """
        self.no_dups_flash = NoDupsFlash()

        text = decode_data(data, contenttype)
        # data cleanup is not needed by html_out, but is needed by moinwiki_out; CKEditor adds unwanted \n\t
        while '\t\t' in text:
            text = text.replace('\t\t', '\t')
        text = text.replace('\r\n\t', '').replace('\n\t', '')

        content = normalize_split_text(text)
        # Be sure we have empty string in the base url
        self.base_url = ''

        # We create an element tree from the HTML content
        # The content is a list of string, line per line
        # We can concatenate all in one string
        html_str = u'\n'.join(content)
        try:
            html_tree = HTML(html_str)
        except AssertionError as reason:
            # we suspect user has created or uploaded malformed HTML, try to show input as preformatted code
            msg = _('Error: malformed HTML: {reason}.').format(reason=reason)
            msg = '<div class="error"><p><strong>%s</strong></p></div>' % msg
            html_str = ''.join(
                ['<html>', msg, '<pre>', html_str, '</pre></html>'])
            try:
                html_tree = HTML(html_str)
            except ValueError:
                msg = _(
                    'Error: malformed HTML. Try viewing source with Highlight or Modify links.'
                )
                msg = '<div class="error"><p><strong>%s</strong></p></div>' % msg
                html_str = ''.join(['<html>', msg, '</html>'])
                html_tree = HTML(html_str)

        # We should have a root element, which will be converted as <page>
        # for the DOM Tree.
        # NB : If <html> used, it will be converted back to <div> after
        # one roundtrip
        if html_tree.tag.name != 'html':
            html_str = ''.join(['<div>', html_str, '</div>'])
            html_tree = HTML(html_str)

        # Start the conversion of the first element
        # Every child of each element will be recursively convert too
        element = self.do_children(html_tree)

        # Add Global element to our DOM Tree
        body = moin_page.body(children=element)
        root = moin_page.page(children=[body])
        return root
Ejemplo n.º 10
0
 def __call__(self, data, contenttype=None, arguments=None):
     text = decode_data(data, contenttype)
     content = normalize_split_text(text)
     blockcode = moin_page.blockcode()
     for line in content:
         if len(blockcode):
             blockcode.append('\n')
         blockcode.append(line.expandtabs())
     body = moin_page.body(children=(blockcode, ))
     return moin_page.page(children=(body, ))
Ejemplo n.º 11
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)
Ejemplo n.º 12
0
    def parse_block(self, iter_content, arguments):
        attrib = {}
        if arguments:
            for key, value in arguments.keyword.iteritems():
                if key in ('style', 'class', ):
                    attrib[moin_page(key)] = value

        body = moin_page.body(attrib=attrib)
        stack = _Stack(body, iter_content=iter_content)

        for line in iter_content:
            data = dict(((str(k), v) for k, v in self.indent_re.match(line).groupdict().iteritems() if v is not None))
            self.indent_repl(iter_content, stack, line, **data)

        return body
Ejemplo n.º 13
0
    def parse_block(self, iter_content, arguments):
        attrib = {}
        if arguments:
            for key, value in arguments.keyword.iteritems():
                if key in ('style', ):
                    attrib[moin_page(key)] = value

        body = moin_page.body(attrib=attrib)

        stack = _Stack(body, iter_content=iter_content)

        # Please note that the iterator can be modified by other functions
        for line in iter_content:
            match = self.block_re.match(line)
            self._apply(match, 'block', iter_content, stack)

        return body
Ejemplo n.º 14
0
 def __call__(self, rev, contenttype=None, arguments=None):
     item_name = rev.item.name
     attrib = {
         moin_page.type_:
         unicode(self.input_type),
         xlink.href:
         Iri(scheme='wiki',
             authority='',
             path='/' + item_name,
             query='do=get&rev={0}'.format(rev.revid)),
     }
     obj = moin_page.object_(
         attrib=attrib,
         children=[
             u'Your Browser does not support HTML5 audio/video element.',
         ])
     body = moin_page.body(children=(obj, ))
     return moin_page.page(children=(body, ))
Ejemplo n.º 15
0
 def __call__(self, rev, contenttype=None, arguments=None):
     self.item_name = rev.item.name
     try:
         contents = self.list_contents(rev.data)
         contents = [(
             self.process_size(size),
             self.process_datetime(dt),
             self.process_name(name),
         ) for size, dt, name in contents]
         table = self.build_dom_table(
             contents,
             head=[_("Size"), _("Timestamp"),
                   _("Name")],
             cls='zebra')
         body = moin_page.body(children=(table, ))
         return moin_page.page(children=(body, ))
     except ArchiveException as err:
         logging.exception(
             "An exception within archive file handling occurred:")
         # XXX we also use a table for error reporting, could be
         # something more adequate, though:
         return self.build_dom_table([[str(err)]])
Ejemplo n.º 16
0
    def parse_block(self, iter_content, arguments):
        attrib = {}
        if arguments:
            for key, value in arguments.keyword.iteritems():
                if key in ('style', ):
                    attrib[moin_page(key)] = value
                elif key == '_old':
                    attrib[moin_page.class_] = value.replace('/', ' ')

        body = moin_page.body(attrib=attrib)

        stack = _Stack(body, iter_content=iter_content)

        for line in iter_content:
            match = self.indent_re.match(line)
            if match:
                data = dict(((str(k), v)
                             for k, v in match.groupdict().iteritems()
                             if v is not None))
                self.indent_repl(iter_content, stack, line, **data)
            else:
                self.indent_repl(iter_content, stack, line, '', line)

        return body
Ejemplo n.º 17
0
    def __call__(self, data, contenttype=None, arguments=None):
        """
        Convert markdown to moin DOM.

        data is a pointer to an open file (ProtectedRevision object)
        contenttype is likely == u'text/x-markdown;charset=utf-8'
        arguments is not used

        Markdown processing takes place in five steps:

        1. A bunch of "preprocessors" munge the input text.
        2. BlockParser() parses the high-level structural elements of the
           pre-processed text into an ElementTree.
        3. A bunch of "treeprocessors" are run against the ElementTree. One
           such treeprocessor runs InlinePatterns against the ElementTree,
           detecting inline markup.
        4. Some post-processors are run against the ElementTree nodes containing text
            and the ElementTree is converted to an EmeraldTree.
        5. The root of the EmeraldTree is returned.

        """
        # read the data from wiki storage and convert to unicode
        text = decode_data(data, contenttype)

        # Normalize whitespace for consistent parsing. - copied from NormalizeWhitespace in markdown/preprocessors.py
        text = text.replace(md_util.STX, "").replace(md_util.ETX, "")
        text = text.replace("\r\n", "\n").replace("\r", "\n") + "\n\n"
        text = text.expandtabs(self.markdown.tab_length)
        text = re.sub(r'(?<=\n) +\n', '\n', text)

        # save line counts for start of each block, used later for edit autoscroll
        self.count_lines(text)

        # {{{ stolen from Markdown.convert

        # Split into lines and run the line preprocessors.
        lines = text.split("\n")
        for prep in self.markdown.preprocessors.values():
            lines = prep.run(lines)

        # 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 is not None:
                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