示例#1
0
文件: parser.py 项目: EnTeQuAk/dmlt
    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()
示例#2
0
    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()
示例#3
0
文件: parser.py 项目: EnTeQuAk/dmlt
 def parse(self, stream):
     stream.expect("code_begin")
     data = escape(u"".join(filter_stream(stream, "code_end")))
     stream.expect("code_end")
     return nodes.Preformatted([nodes.HTML(data)])
示例#4
0
 def parse(self, stream):
     stream.expect('code_begin')
     data = escape(u''.join(filter_stream(stream, 'code_end')))
     stream.expect('code_end')
     return nodes.Preformatted([nodes.HTML(data)])