예제 #1
0
def test_escaping():
    """Check if escaping works properly."""
    tree = parse("\\__bar\\__")
    assert tree == nodes.Document([nodes.Text("__bar__")])

    tree = parse("'bar'")
    assert tree == nodes.Document([nodes.Text("'bar'")])
예제 #2
0
def test_autoclosing():
    """Check if the automatic closing works."""
    tree = parse("''foo __bar")
    assert tree == nodes.Document([
        nodes.Emphasized(
            [nodes.Text('foo '),
             nodes.Underline([nodes.Text('bar')])])
    ])
def test_default_itransformer():
    """Test that the default ITransformer implementation returns
    the tree unchanged"""
    transformer = ITransformer()
    tree = nodes.Document(
        [nodes.Text('\nYea'),
         nodes.Strong([nodes.Text('foo')])])
    transformed = transformer.transform(tree)
    eq_(render(transformed, None, 'html'), '\nYea<strong>foo</strong>')
예제 #4
0
def test_simple_lists():
    """Check if simple lists work."""
    tree = parse(' * foo\n * ^^(bar)^^\n * ,,(baz),,')
    assert tree == nodes.Document([
        nodes.List('unordered', [
            nodes.ListItem([nodes.Text('foo')]),
            nodes.ListItem([nodes.Sup([nodes.Text('bar')])]),
            nodes.ListItem([nodes.Sub([nodes.Text('baz')])])
        ])
    ])
예제 #5
0
def test_table_cell_classes():
    """Test the table cell classes."""
    tree = parse('||<cellclass=foo>1||<bar>2||')
    assert tree == nodes.Document([
        nodes.Table([
            nodes.TableRow([
                nodes.TableCell([nodes.Text('1')], class_='foo'),
                nodes.TableCell([nodes.Text('2')], class_='bar')
            ])
        ])
    ])
예제 #6
0
def test_table_row_classes():
    """Test the table row class assignments."""
    tree = parse('||<foo>1||2||3')
    assert tree == nodes.Document([
        nodes.Table([
            nodes.TableRow([
                nodes.TableCell([nodes.Text('1')]),
                nodes.TableCell([nodes.Text('2')]),
                nodes.TableCell([nodes.Text('3')])
            ],
                           class_='foo')
        ])
    ])
예제 #7
0
def parse(markup, catch_stack_errors=True, transformers=None):
    """Parse markup into a node."""
    try:
        return Parser(markup, transformers).parse()
    except StackExhaused:
        if not catch_stack_errors:
            raise
        return nodes.Paragraph([
            nodes.Strong([nodes.Text(_(u'Internal parser error: '))]),
            nodes.Text(
                _(u'The parser could not process the text because '
                  u'it exceeded the maximum allowed depth for nested '
                  u'elements'))
        ])
예제 #8
0
def test_span_table():
    """Test tables with col/rowspans."""
    tree = parse('||<-2>1||<|2>2||\n||3||4||')
    assert tree == nodes.Document([
        nodes.Table([
            nodes.TableRow([
                nodes.TableCell([nodes.Text('1')], colspan=2),
                nodes.TableCell([nodes.Text('2')], rowspan=2)
            ]),
            nodes.TableRow([
                nodes.TableCell([nodes.Text('3')]),
                nodes.TableCell([nodes.Text('4')])
            ])
        ])
    ])
예제 #9
0
def test_external_links():
    """Test all kind of external links."""
    tree = parse('[http://example.org :blub:][?action=edit]')
    assert tree == nodes.Document([
        nodes.Link('http://example.org', [nodes.Text(':blub:')]),
        nodes.Link('?action=edit')
    ])
예제 #10
0
def test_table_alignment():
    """Check if table alignment parameters work."""
    tree = parse('||<:~>1||<(v>2||<)^>3||')
    assert tree == nodes.Document([
        nodes.Table([
            nodes.TableRow([
                nodes.TableCell([nodes.Text('1')],
                                align='center',
                                valign='middle'),
                nodes.TableCell([nodes.Text('2')],
                                align='left',
                                valign='bottom'),
                nodes.TableCell([nodes.Text('3')], align='right', valign='top')
            ])
        ])
    ])
예제 #11
0
def test_nested_lists():
    """Check if nested lists work."""
    tree = parse(' * foo\n  1. bar\n   a. baz\n * blub')
    assert tree == nodes.Document([
        nodes.List('unordered', [
            nodes.ListItem([
                nodes.Text('foo'),
                nodes.List('arabic', [
                    nodes.ListItem([
                        nodes.Text('bar'),
                        nodes.List('alphalower',
                                   [nodes.ListItem([nodes.Text('baz')])])
                    ])
                ])
            ]),
            nodes.ListItem([nodes.Text('blub')])
        ])
    ])
def test_automatic_paragraphs_transformer():
    transformer = AutomaticParagraphs()
    tree = nodes.Document([
        nodes.Text('\nYea'),
        nodes.Strong([nodes.Text('foo')]),
        nodes.Text('\n\nfaz')
    ])
    transformed = transformer.transform(tree)
    eq_(render(transformed, None, 'html'),
        '<p>\nYea<strong>foo</strong></p><p>faz</p>')

    # test paragraph with block tags
    tree = nodes.Document(
        [nodes.Text('\nYea'),
         nodes.Ruler(),
         nodes.Text('\n\nfaz')])
    transformed = transformer.transform(tree)
    eq_(render(transformed, None, 'html'), '<p>\nYea</p><hr><p>faz</p>')
def test_headline_processor_transformer():
    transformer = HeadlineProcessor()
    tree = nodes.Document([
        nodes.Headline(1, [nodes.Text('foo')], 'foo'),
        nodes.Paragraph([nodes.Text('some text')]),
        nodes.Headline(2, [nodes.Text('faz')], ''),
        nodes.Paragraph([nodes.Text('other text')]),
        nodes.Headline(1, [nodes.Text('foo')], 'foo')
    ])

    expected = nodes.Document([
        nodes.Headline(1, [nodes.Text('foo')], 'foo'),
        nodes.Paragraph([nodes.Text('some text')]),
        nodes.Headline(2, [nodes.Text('faz')], 'empty-headline'),
        nodes.Paragraph([nodes.Text('other text')]),
        nodes.Headline(1, [nodes.Text('foo')], 'foo-2')
    ])

    transformed = transformer.transform(tree)
    eq_(transformed, expected)
예제 #14
0
    def parse_code(self, stream):
        """
        Parse inline code, don't confuse that with `parse_pre_block`.

        Returns a `Code` node.
        """
        stream.expect('code_begin')
        buffer = []
        while stream.current.type == 'text':
            buffer.append(stream.current.value)
            stream.next()
        stream.expect('code_end')
        return nodes.Code([nodes.Text(u''.join(buffer))])
예제 #15
0
    def parse_escaped_code(self, stream):
        """
        This parses escaped code formattings.  Escaped code formattings work
        like normal code formattings but their delimiter backticks are doubled
        so that one can use single backticks inside.

        Returns a `Code` node.
        """
        stream.expect('escaped_code_begin')
        buffer = []
        while stream.current.type != 'escaped_code_end':
            buffer.append(stream.current.value)
            stream.next()
        stream.expect('escaped_code_end')
        return nodes.Code([nodes.Text(u''.join(buffer))])
예제 #16
0
def test_inline_formattings():
    """Simple test for some inline formattings."""
    tree = parse("''baz'' but '''foo''' and __bar__.")
    assert tree == nodes.Document([
        nodes.Emphasized([nodes.Text('baz')]),
        nodes.Text(' but '),
        nodes.Strong([nodes.Text('foo')]),
        nodes.Text(' and '),
        nodes.Underline([nodes.Text('bar')]),
        nodes.Text('.')
    ])
예제 #17
0
    def transform(self, parent):
        """
        Insert real paragraphs into the node and return it.
        """
        for node in parent.children:
            if node.is_container and not node.is_raw:
                self.transform(node)

        if not parent.allows_paragraphs:
            return parent

        paragraphs = [[]]

        for child in self.joined_text_iter(parent):
            if child.is_text_node:
                blockiter = iter(_paragraph_re.split(child.text))
                for block in blockiter:
                    try:
                        is_paragraph = blockiter.next()
                    except StopIteration:
                        is_paragraph = False
                    if block:
                        paragraphs[-1].append(nodes.Text(block))
                    if is_paragraph:
                        paragraphs.append([])
            elif child.is_block_tag:
                paragraphs.extend((child, []))
            else:
                paragraphs[-1].append(child)

        del parent.children[:]
        for paragraph in paragraphs:
            if not isinstance(paragraph, list):
                parent.children.append(paragraph)
            else:
                for node in paragraph:
                    if not node.is_text_node or node.text:
                        parent.children.append(nodes.Paragraph(paragraph))
                        break

        return parent
예제 #18
0
def test_simple_table():
    """Test the simple table markup."""
    tree = parse('||1||2||3||\n||4||5||6||')
    assert tree == nodes.Document([
        nodes.Table([
            nodes.TableRow([
                nodes.TableCell([nodes.Text('1')]),
                nodes.TableCell([nodes.Text('2')]),
                nodes.TableCell([nodes.Text('3')])
            ]),
            nodes.TableRow([
                nodes.TableCell([nodes.Text('4')]),
                nodes.TableCell([nodes.Text('5')]),
                nodes.TableCell([nodes.Text('6')])
            ])
        ])
    ])
예제 #19
0
    def parse_pre_block(self, stream):
        """
        Parse a pre block or parser block.  If a shebang is present the parser
        with that name is instanciated and expanded, if it's a dynamic parser
        a `Parser` node will be returned.

        If no shebang is present or a parser with that name does not exist the
        data is handled as preformatted block data and a `Preformatted` node
        is returned.
        """
        stream.expect('pre_begin')
        if stream.current.type == 'parser_begin':
            name = stream.current.value
            stream.next()
            args, kwargs = self.parse_arguments(stream, 'parser_end')
            if stream.current.type != 'pre_end':
                stream.next()
        else:
            name = None

        children = []
        text_node = None
        while stream.current.type != 'pre_end':
            node = self.parse_node(stream)
            if node.is_text_node:
                if text_node is None and node.text[:1] == '\n':
                    node.text = node.text[1:]
                text_node = node
            children.append(node)
        if text_node is not None and text_node.text[-1:] == '\n':
            text_node.text = text_node.text[:-1]
        stream.expect('pre_end')

        if name is None:
            return nodes.Preformatted(children)

        data = u''.join(x.text for x in children)
        return nodes.Preformatted([nodes.Text(data)])
예제 #20
0
 def flush_text_buf():
     if text_buf:
         text = u''.join(text_buf)
         if text:
             yield nodes.Text(text)
         del text_buf[:]
예제 #21
0
def test_pre():
    """Test normal pre blocks."""
    tree = parse('{{{\nfoo\n}}}')
    assert tree == nodes.Document([nodes.Preformatted([nodes.Text('foo')])])
예제 #22
0
def test_breakers():
    """Test the ruler."""
    tree = parse('foo\n-----------------')
    assert tree == nodes.Document([nodes.Text('foo\n'), nodes.Ruler()])
예제 #23
0
 def parse_text(self, stream):
     """Expects a ``'text'`` token and returns a `nodes.Text`."""
     return nodes.Text(stream.expect('text').value)
예제 #24
0
 def parse_raw(self, stream):
     """Parse a raw marked section."""
     return nodes.Raw([nodes.Text(stream.expect('raw').value)])