class ListDirective(Directive): rules = [ rule(make_bbcode_tag('list', True), bygroups('list_type'), enter='list'), rule(r'\[\*\]\s*(.*)(?m)', bygroups('value'), enter='list_item', leave='list_item', one=True), rule(make_bbcode_end('list', False), leave='list') ] def parse(self, stream): if stream.test('list_item'): # parse list items stream.next() val = self.machine.dispatch_node(stream) return nodes.ListItem([nodes.Text(val)]) def finish(): return nodes.List(list_type, children) def is_empty_node(node): return node.is_linebreak_node or \ (node.is_text_node and not node.text.strip()) def finish_if_list_end(): if stream.test('list_end'): stream.next() return finish() stream.expect('list_begin') t = stream.expect('list_type') if not t.value: list_type = 'unordered' else: list_type = { '1': 'arabic', 'a': 'alphalower', 'A': 'alphalower', '*': 'unordered' }.get(t.value, None) if list_type is None: ret = u'[list]' + (u''.join( filter_stream(stream, ('list_end', 'eof'), False))) ret += stream.expect('list_end').value return nodes.Text(ret) children = filter(lambda n: not is_empty_node(n), parse_child_nodes(stream, self, ('list_end', 'eof'))) # broken markup, no end tags... if stream.eof: return finish() finish_if_list_end() return finish()
class LinkDirective(Directive): rule = rule(r'\[([^\s]+?\S)(\s(?:.+?))?\]', bygroups('link_href', 'link_title'), enter='link', one=True) def parse(self, stream): stream.expect('link') return nodes.Link(stream.expect('link_href').value, title=stream.expect('link_title').value)
class ColorDirective(Directive): rules = [ rule(make_bbcode_tag('color', True), bygroups('color'), enter='color'), rule(make_bbcode_end('color'), leave='color') ] def parse(self, stream): stream.expect('color_begin') color = stream.expect('color').value children = parse_child_nodes(stream, self, 'color_end') stream.expect('color_end') return nodes.Color(color, children)
class HeadlineDirective(Directive): rule = rule(r'(={1,6})(.*?)(\1)', bygroups('headline_level', 'headline_text'), enter='headline', one=True) def parse(self, stream): stream.expect('headline') token = stream.expect('headline_level') return nodes.Headline( len(token.value.strip()), self.machine.parse( self.machine.tokenize(stream.expect('headline_text').value), True))
class UrlDirective(Directive): rules = [ rule(make_bbcode_tag('url', True), bygroups('url_source'), enter='url'), rule(make_bbcode_end('url'), leave='url'), ] def parse(self, stream): stream.expect('url_begin') href = stream.expect('url_source').value children = parse_child_nodes(stream, self, 'url_end') title = children and u''.join(n.text for n in children) if href is None: href = title stream.expect('url_end') return nodes.Link(href, children, title)
class QuoteDirective(Directive): rules = [ rule(make_bbcode_tag('quote', True), bygroups('quote_user'), enter='quote'), rule(make_bbcode_end('quote'), leave='quote') ] def parse(self, stream): stream.expect('quote_begin') user = stream.expect('quote_user') ret = [] if user.value is not None: u = user.value user = u[-1] == ':' and u or u'%s said:' % u ret = [nodes.Strong([nodes.Text(user)]), nodes.Newline()] children = parse_child_nodes(stream, self, 'quote_end') stream.expect('quote_end') return nodes.Container(ret + [nodes.Quote(children)])