Mercurial > dandelion
changeset 476:534123ed9474
unit tests for wikiparser
| author | sheep@ghostwheel |
|---|---|
| date | Sun, 05 Oct 2008 19:15:54 +0200 |
| parents | b31cef9e55f0 |
| children | 7cc1359eb4fb |
| files | dandelion/wikiparser.py test.py |
| diffstat | 2 files changed, 44 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/dandelion/wikiparser.py Fri Sep 12 19:08:44 2008 +0200 +++ b/dandelion/wikiparser.py Sun Oct 05 19:15:54 2008 +0200 @@ -7,13 +7,37 @@ from template import html_start, html_text, html_end, Stream class WikiParser(object): + """ + + >>> [tag for tag in WikiParser(["ziew"])()] + [('START', (QName(u'p'), Attrs()), (None, -1, -1)), ('TEXT', u'ziew', (None, -1, -1)), ('END', QName(u'p'), (None, -1, -1))] + + >>> WikiParser(["ziew"])().render() + '<p>ziew</p>' + + + >>> WikiParser([ + ... "normal **bold** //italic//", + ... "**//bold italic//** //**italic bold**//", + ... ])().render() + '<p>normal <b>bold</b> <i>italic</i><b><i>bold italic</i></b> <i><b>italic bold</b></i></p>' + + >>> WikiParser([ + ... "* one", + ... "* two", + ... "** sub", + ... "* three", + ... ])().render() + '<ul><li>one</li><li>two<ul><li>sub</li></ul></li><li>three</li></ul>' + """ + bullets_pat = ur"^\s*[*]+\s+" bullets_re = re.compile(bullets_pat, re.U) heading_pat = ur"^\s*=+" heading_re = re.compile(heading_pat, re.U) block = { "bullets": bullets_pat, - "code": ur"^[{][{][{]+\s*$", + "code": ur"^\{\{\{+\s*$", "macro": ur"^<<\w+\s*$", "empty": ur"^\s*$", "heading": heading_pat, @@ -24,7 +48,7 @@ block_re = re.compile(ur"|".join("(?P<%s>%s)" % kv for kv in sorted(block.iteritems()))) code_close_re = re.compile(ur"^\}\}\}\s*$", re.U) - macro_close_re = re.compile(ur"^>>\s*$", re.U) + macro_close_re = re.compile(ur"^\>\>\s*$", re.U) image_pat = ur"\{\{(?P<image_target>[^|}]+)(\|(?P<image_text>[^}]+))?}}" image_re = re.compile(image_pat, re.U) smilies = { @@ -57,14 +81,14 @@ r",,": u"„", } markup = { - "bold": ur"[*][*]", - "code": ur"[{][{][{](?P<code_text>([^}]|[^}][}]|[^}][}][}])*[}]*)[}][}][}]", + "bold": ur"\*\*", + "code": ur"\{\{\{(?P<code_text>([^}]|[^}][}]|[^}][}][}])*[}]*)\}\}\}", "free_link": ur"""(http|https|ftp)://\S+[^\s.,:;!?()'"=+<>-]""", "italic": ur"//", "link": ur"\[\[(?P<link_target>[^|\]]+)(\|(?P<link_text>[^\]]+))?\]\]", "image": image_pat, "linebreak": ur"\\\\", - "macro": ur"[<][<](?P<macro_name>\w+)\s+(?P<macro_text>([^>]|[^>][>])+)[>][>]", + "macro": ur"\<\<(?P<macro_name>\w+)\s+(?P<macro_text>([^>]|[^>][>])+)\>\>", "math": ur"\$\$(?P<math_text>[^$]+)\$\$", "newline": ur"\n", "punct": ur"|".join(re.escape(k) for k in punct), @@ -164,7 +188,6 @@ for part in self.line_image(match.groupdict()): yield part else: -# inside = escape(text) yield html_text(text) yield html_end('a') @@ -178,8 +201,6 @@ def line_macro(self, groups): name = groups['macro_name'] text = groups['macro_text'].strip() -# return u'<span class="%s">%s</span>' % (escape(name, quote=True), -# escape(text)) yield html_start('span', class_=name) yield html_text(text) yield html_end('span') @@ -197,8 +218,6 @@ yield html_start('pre', class_='code') yield html_text(inside) yield html_end('pre') -# yield tag.pre(inside, class_="code") -# yield u'<pre class="code">%s</pre>' % escape(inside) def block_macro(self, block): # XXX A hack to handle <<...>> macro blocks, this method reads lines @@ -214,14 +233,8 @@ yield html_start('div', class_=name) yield html_text(inside) yield html_end('div') -# yield tag.div(inside, class_=name) -# yield u'<div class="%s">%s</div>' % (escape(name, quote=True), -# escape(inside)) def block_paragraph(self, block): -# text = u"".join(block) -# yield u'<p>%s%s</p>' % (u"".join(self.parse_line(text)), -# self.pop_to("")) yield html_start('p') for line in block: for part in self.parse_line(line): @@ -234,9 +247,6 @@ yield html_start('pre') yield html_text(u"\n".join(line.rstrip() for line in block)) yield html_end('pre') -# yield tag.pre(u"\n".join(line.rstrip() for line in block)) -# yield u'<pre>%s</pre>' % escape(u"\n".join(line.rstrip() -# for line in block)) def block_table(self, block): yield html_start('table') @@ -255,15 +265,12 @@ yield None def block_rule(self, block): -# yield u'<hr>' yield html_start('hr') yield html_end('hr') def block_heading(self, block): for line in block: level = min(len(self.heading_re.match(line).group(0).strip()), 5) -# yield u'<h%d>%s</h%d>' % (level, -# escape(line.strip("= \t\n\r\v")), level) yield html_start("h%d" % level) yield html_text(line.strip("= \t\n\r\v")) yield html_end("h%d" % level) @@ -276,7 +283,6 @@ yield html_start('ul') level += 1 elif nest < level: -# yield '</li></ul></li>' yield html_end('li') yield html_end('ul') yield html_end('li') @@ -294,10 +300,19 @@ yield html_end('ul') def parse_line(self, line): + text_buffer = [] for m in self.markup_re.finditer(line): func = getattr(self, "line_%s" % m.lastgroup) for part in func(m.groupdict()): - yield part + if part[0] == 'TEXT': + text_buffer.append(part[1]) + else: + if text_buffer: + yield html_text(u"".join(text_buffer)) + text_buffer = [] + yield part + if text_buffer: + yield html_text(u"".join(text_buffer)) def parse_block(self): def key(line): @@ -313,3 +328,6 @@ def parse(self): return Stream(self.parse_block()) + + def __call__(self): + return self.parse()
