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, ))
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_: str(self.input_type), xlink.href: Iri(scheme='wiki', authority='', path='/' + rev.item.fqname.fullname, query=query), }) obj = moin_page.object_(attrib=attrib, children=[ item_name, ]) body = moin_page.body(children=(obj, )) return moin_page.page(children=(body, ))
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, ))
def __call__(self, rev, contenttype=None, arguments=None): item_name = rev.item.fqname.fullname 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, ))
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, ))
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
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
def __call__(self, data, contenttype=None, arguments=None): text = decode_data(data, contenttype) content = normalize_split_text(text) iter_content = _Iter(content) self.preprocessor = self.Mediawiki_preprocessor() body = self.parse_block(iter_content, arguments) root = moin_page.page(children=(body, )) return root
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, ))
def __call__(self, data, contenttype=None, arguments=None): text = decode_data(data, contenttype) lines = normalize_split_text(text) iter_content = _Iter(lines) body = self.parse_block(iter_content, arguments) root = moin_page.page(children=[body]) return root
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, ))
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, ))
def __call__(self, rev, contenttype=None, arguments=None): item_name = rev.item.name attrib = { moin_page.type_: str(self.input_type), xlink.href: Iri(scheme='wiki', authority='', path='/' + item_name, query='do=get&rev={0}'.format(rev.revid)), } if arguments and html.alt in arguments: attrib[html.alt] = arguments[html.alt] elif rev.meta.get(SUMMARY): attrib[html.alt] = rev.meta[SUMMARY] obj = moin_page.object_(attrib=attrib, children=['Your Browser does not support HTML5 audio/video element.', ]) body = moin_page.body(children=(obj, )) return moin_page.page(children=(body, ))
def __call__(self, rev, contenttype=None, arguments=None): try: item_name = rev.item.name or rev.meta['name'][0] except IndexError: # item is deleted message = _( 'This deleted item must be restored before it can be viewed or downloaded, ItemID = {itemid}' ).format(itemid=rev.item.itemid) admonition = moin_page.div( attrib={moin_page.class_: 'error'}, children=[moin_page.p(children=[message])]) body = moin_page.body(children=(admonition, )) return moin_page.page(children=(body, )) 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=["Download {0}.".format(item_name)]) body = moin_page.body(children=(a, )) return moin_page.page(children=(body, ))
def __call__(self, rev, contenttype=None, arguments=None): fqname = CompositeName(rev.meta[NAMESPACE], NAME_EXACT, rev.meta[NAME][0]) self.fullname = fqname.fullname 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)]])
def block_nowiki_repl(self, iter_content, stack, nowiki): """Handles a complete nowiki block""" stack.clear() try: firstline = iter_content.next() except StopIteration: stack.push(moin_page.blockcode()) return # Stop directly if we got an end marker in the first line match = self.nowiki_end_re.match(firstline) if match and not match.group('escape'): stack.push(moin_page.blockcode()) return lines = _Iter(self.block_nowiki_lines(iter_content), startno=iter_content.lineno) match = self.nowiki_interpret_re.match(firstline) if match: name = match.group('nowiki_name') args = match.group('nowiki_args') if args: args = parse_arguments(args) # Parse it directly if the type is ourself if not name or name == 'creole': body = self.parse_block(lines, args) elem = moin_page.page(children=(body, )) stack.top_append(elem) else: stack.top_append(self.parser(name, args, lines)) else: elem = moin_page.blockcode(children=(firstline, )) stack.top_append(elem) for line in lines: elem.append('\n') elem.append(line)
def __call__(self, data, contenttype=None, arguments=None): text = decode_data(data, contenttype) content = normalize_split_text(text) dialect = csv.Sniffer().sniff(content[0]) reader = csv.reader(content, dialect) rows = list(reader) 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, ))
def handle_nowiki(self, elem, page): """{{{* where * may be #!wiki, #!csv, #!highlight python, "", etc., or an invalid argument.""" logging.debug("handle_nowiki elem: %r" % elem) marker_len, all_nowiki_args, content = elem._children nowiki_args = all_nowiki_args[0].strip() # remove all the old children of the element, new children will be added elem.remove_all() if not nowiki_args: # input similar to: {{{\ntext\n}}}\n blockcode = moin_page.blockcode(children=(content, )) elem.append(blockcode) return if nowiki_args.startswith('#!') and len(nowiki_args) > 2: arguments = nowiki_args[2:].split(' ', 1) # skip leading #! nowiki_name = arguments[0] optional_args = arguments[1] if len(arguments) > 1 else None else: nowiki_name = optional_args = None lexer = None if nowiki_name in set(('diff', 'cplusplus', 'python', 'java', 'pascal', 'irc')): # make old style markup similar to {{{#!python like new style {{{#!highlight python optional_args = nowiki_name if not optional_args else nowiki_name + ' ' + optional_args nowiki_name = 'highlight' if nowiki_name == u'highlight': # TODO: support moin 1.9 options like numbers=on start=222 step=10 optional_args = optional_args.split()[0] # ignore all parameters except lexer name try: lexer = pygments.lexers.get_lexer_by_name(optional_args) except ClassNotFound: try: lexer = pygments.lexers.get_lexer_for_mimetype(optional_args) except ClassNotFound: self.invalid_args(elem, all_nowiki_args) lexer = pygments.lexers.get_lexer_by_name('text') if lexer: blockcode = moin_page.blockcode(attrib={moin_page.class_: 'highlight'}) pygments.highlight(content, lexer, TreeFormatter(), blockcode) elem.append(blockcode) return if nowiki_name in ('csv', 'text/csv'): # TODO: support moin 1.9 options: quotechar, show, hide, autofilter, name, link, static_cols, etc delim = None if optional_args: m = re.search('delimiter=(.?)', optional_args) if m and m.group(1): delim = m.group(1) if not delim: delim = optional_args.split()[0] # ignore all parameters except a delimiter in first position if len(delim) > 1: delim = None sep = delim or u';' content = content.split('\n') head = content[0].split(sep) rows = [x.split(sep) for x in content[1:]] csv_builder = TableMixin() table = csv_builder.build_dom_table(rows, head=head, cls='moin-csv-table moin-sortable') elem.append(table) return if nowiki_name in ('wiki', 'text/x.moin.wiki',): from .moinwiki_in import Converter as moinwiki_converter moinwiki = moinwiki_converter() lines = normalize_split_text(content) lines = _Iter(lines) # reparse arguments from original: {{{#!wiki solid/orange (style="color: red;") wiki_args = parse_arguments(all_nowiki_args[0][2:]) if len(wiki_args.positional) > 1: wiki_args.keyword['class'] = u' '.join(wiki_args.positional[1:]) del wiki_args.positional[:] body = moinwiki.parse_block(lines, wiki_args) page = moin_page.page(children=(body, )) elem.append(page) return if nowiki_name in ('creole', 'text/x.moin.creole'): from .creole_in import Converter as creole_converter creole = creole_converter() lines = normalize_split_text(content) lines = _Iter(lines) body = creole.parse_block(lines, optional_args) page = moin_page.page(children=(body, )) elem.append(page) return if nowiki_name in ('rst', 'text/x-rst'): from .rst_in import Converter as rst_converter rst = rst_converter() page = rst(content, contenttype=u'text/x-rst;charset=utf-8') elem.append(page) return if nowiki_name in ('docbook', 'application/docbook+xml'): from .docbook_in import Converter as docbook_converter docbook = docbook_converter() page = docbook(content, contenttype=u'application/docbook+xml;charset=utf-8') elem.append(page) return if nowiki_name in ('markdown', 'text/x-markdown'): from .markdown_in import Converter as markdown_converter markdown = markdown_converter() page = markdown(content, contenttype=u'text/x-markdown;charset=utf-8') elem.append(page) return if nowiki_name in ('mediawiki', 'text/x-mediawiki'): from .mediawiki_in import Converter as mediawiki_converter mediawiki = mediawiki_converter() page = mediawiki(content, optional_args) elem.append(page) return if nowiki_name in ('html', 'HTML', 'text/html'): from .html_in import Converter as html_converter html = html_converter() page = html(content, optional_args) elem.append(page) return self.invalid_args(elem, all_nowiki_args) lexer = pygments.lexers.get_lexer_by_name('text') blockcode = moin_page.blockcode(attrib={moin_page.class_: 'highlight'}) pygments.highlight(content, lexer, TreeFormatter(), blockcode) elem.append(blockcode) return
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) # {{{ similar to parts of Markdown 3.0.0 core.py convert method # Split into lines and run the line preprocessors. lines = text.split("\n") for prep in self.markdown.preprocessors: lines = prep.run(lines) # Parse the high-level elements. root = self.markdown.parser.parseDocument(lines).getroot() # Run the tree-processors for treeprocessor in self.markdown.treeprocessors: newRoot = treeprocessor.run(root) if newRoot is not None: root = newRoot # }}} end Markdown 3.0.0 core.py convert method 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(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