mirror of
https://github.com/vale981/recommonmark
synced 2025-03-05 18:11:39 -05:00
482 lines
13 KiB
Python
482 lines
13 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import unittest
|
|
from textwrap import dedent
|
|
|
|
from docutils import nodes
|
|
from docutils.utils import new_document
|
|
from docutils.readers import Reader
|
|
from docutils.core import publish_parts
|
|
|
|
from commonmark import Parser
|
|
from recommonmark.parser import CommonMarkParser
|
|
|
|
|
|
class TestParsing(unittest.TestCase):
|
|
|
|
def assertParses(self, source, expected, alt=False): # noqa
|
|
parser = CommonMarkParser()
|
|
parser.parse(dedent(source), new_document('<string>'))
|
|
self.assertMultiLineEqual(
|
|
dedent(expected).lstrip(),
|
|
dedent(parser.document.asdom().toprettyxml(indent=' ')),
|
|
)
|
|
|
|
def test_heading(self):
|
|
self.assertParses(
|
|
"""
|
|
# Heading 1
|
|
|
|
## Heading 2
|
|
|
|
Body
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<section ids="heading-1" names="heading\ 1">
|
|
<title>Heading 1</title>
|
|
<section ids="heading-2" names="heading\ 2">
|
|
<title>Heading 2</title>
|
|
<paragraph>Body</paragraph>
|
|
</section>
|
|
</section>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_heading_inline(self):
|
|
self.assertParses(
|
|
"""# Heading *foo*""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<section ids="heading-foo" names="heading\ foo">
|
|
<title>
|
|
Heading \n\
|
|
<emphasis>foo</emphasis>
|
|
</title>
|
|
</section>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_paragraph(self):
|
|
self.assertParses(
|
|
"""This is a paragraph""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>This is a paragraph</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
self.assertParses(
|
|
"""This is a paragraph *foo***bar**""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>
|
|
This is a paragraph \n\
|
|
<emphasis>foo</emphasis>
|
|
<strong>bar</strong>
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
self.assertParses(
|
|
"""
|
|
This is a paragraph
|
|
This is a new line
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>
|
|
This is a paragraph
|
|
|
|
|
|
This is a new line
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_entities(self):
|
|
self.assertParses(
|
|
u"""
|
|
©
|
|
""",
|
|
u"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>©</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_links(self):
|
|
self.assertParses(
|
|
"""
|
|
This is a [link](http://example.com)
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>
|
|
This is a \n\
|
|
<reference refuri="http://example.com">link</reference>
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
self.assertParses(
|
|
"""
|
|
This is a [link][example]
|
|
|
|
[example]: http://example.com "Example"
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>
|
|
This is a \n\
|
|
<reference refuri="http://example.com" title="Example">link</reference>
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
self.assertParses(
|
|
"""
|
|
This is a [link][example]
|
|
|
|
[example]: http://example.com
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>
|
|
This is a \n\
|
|
<reference refuri="http://example.com">link</reference>
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
self.assertParses(
|
|
"""
|
|
<http://example.com>
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>
|
|
<reference refuri="http://example.com">http://example.com</reference>
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
self.assertParses(
|
|
"""
|
|
[link](/foo)
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>
|
|
<pending_xref refdomain="None" refexplicit="True" reftarget="/foo" reftype="any" refwarn="True">
|
|
<reference refuri="/foo">link</reference>
|
|
</pending_xref>
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
self.assertParses(
|
|
"""
|
|
[link](foo)
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>
|
|
<pending_xref refdomain="None" refexplicit="True" reftarget="foo" reftype="any" refwarn="True">
|
|
<reference refuri="foo">link</reference>
|
|
</pending_xref>
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
self.assertParses(
|
|
"""
|
|
**[link](foo)**
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>
|
|
<strong>
|
|
<pending_xref refdomain="None" refexplicit="True" reftarget="foo" reftype="any" refwarn="True">
|
|
<reference refuri="foo">link</reference>
|
|
</pending_xref>
|
|
</strong>
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_image(self):
|
|
self.assertParses(
|
|
"""
|
|

|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>
|
|
<image alt="foo "handle quotes"" uri="/url"></image>
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_inline_code(self):
|
|
self.assertParses(
|
|
"""
|
|
This is `code` right?
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>
|
|
This is \n\
|
|
<literal>code</literal>
|
|
right?
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_bullet_list(self):
|
|
self.assertParses(
|
|
"""
|
|
* List item 1
|
|
* List item 2
|
|
* List item 3
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<bullet_list>
|
|
<list_item>
|
|
<paragraph>List item 1</paragraph>
|
|
</list_item>
|
|
<list_item>
|
|
<paragraph>List item 2</paragraph>
|
|
</list_item>
|
|
<list_item>
|
|
<paragraph>List item 3</paragraph>
|
|
</list_item>
|
|
</bullet_list>
|
|
</document>
|
|
"""
|
|
)
|
|
self.assertParses(
|
|
"""
|
|
* [List item 1](/1)
|
|
* [List item 2](/2)
|
|
* [List item 3](/3)
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<bullet_list>
|
|
<list_item>
|
|
<paragraph>
|
|
<pending_xref refdomain="None" refexplicit="True" reftarget="/1" reftype="any" refwarn="True">
|
|
<reference refuri="/1">List item 1</reference>
|
|
</pending_xref>
|
|
</paragraph>
|
|
</list_item>
|
|
<list_item>
|
|
<paragraph>
|
|
<pending_xref refdomain="None" refexplicit="True" reftarget="/2" reftype="any" refwarn="True">
|
|
<reference refuri="/2">List item 2</reference>
|
|
</pending_xref>
|
|
</paragraph>
|
|
</list_item>
|
|
<list_item>
|
|
<paragraph>
|
|
<pending_xref refdomain="None" refexplicit="True" reftarget="/3" reftype="any" refwarn="True">
|
|
<reference refuri="/3">List item 3</reference>
|
|
</pending_xref>
|
|
</paragraph>
|
|
</list_item>
|
|
</bullet_list>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_enumerated_list(self):
|
|
self.assertParses(
|
|
"""
|
|
1. List item 1
|
|
2. List item 2
|
|
3. List item 3
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<enumerated_list>
|
|
<list_item>
|
|
<paragraph>List item 1</paragraph>
|
|
</list_item>
|
|
<list_item>
|
|
<paragraph>List item 2</paragraph>
|
|
</list_item>
|
|
<list_item>
|
|
<paragraph>List item 3</paragraph>
|
|
</list_item>
|
|
</enumerated_list>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_code(self):
|
|
self.assertParses(
|
|
"""
|
|
Code:
|
|
|
|
#!/bin/sh
|
|
python
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>Code:</paragraph>
|
|
<literal_block xml:space="preserve">#!/bin/sh
|
|
python</literal_block>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_block_quote(self):
|
|
self.assertParses(
|
|
"""
|
|
> Here is a quoted list:
|
|
> \n\
|
|
> * Item 1
|
|
> * Item 2
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<block_quote>
|
|
<paragraph>Here is a quoted list:</paragraph>
|
|
<bullet_list>
|
|
<list_item>
|
|
<paragraph>Item 1</paragraph>
|
|
</list_item>
|
|
<list_item>
|
|
<paragraph>Item 2</paragraph>
|
|
</list_item>
|
|
</bullet_list>
|
|
</block_quote>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_horizontal_rule(self):
|
|
self.assertParses(
|
|
"""
|
|
Foo
|
|
|
|
----
|
|
|
|
Bar
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>Foo</paragraph>
|
|
<transition/>
|
|
<paragraph>Bar</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_html_inline(self):
|
|
self.assertParses(
|
|
"""
|
|
Foo
|
|
|
|
<blink>Blink</blink>
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>Foo</paragraph>
|
|
<paragraph>
|
|
<raw format="html" xml:space="preserve"><blink></raw>
|
|
Blink
|
|
<raw format="html" xml:space="preserve"></blink></raw>
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_html_block(self):
|
|
self.assertParses(
|
|
"""
|
|
Foo
|
|
|
|
<div>
|
|
<blink>Blink</blink>
|
|
</div>
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>Foo</paragraph>
|
|
<raw format="html" xml:space="preserve"><div>
|
|
<blink>Blink</blink>
|
|
</div></raw>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_eval(self):
|
|
self.assertParses(
|
|
"""
|
|
```eval_rst
|
|
.. image:: figure.png
|
|
```
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<literal_block language="eval_rst" xml:space="preserve">\
|
|
.. image:: figure.png</literal_block>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
def test_linebreak(self):
|
|
self.assertParses(
|
|
"""
|
|
line1
|
|
line2
|
|
""",
|
|
"""
|
|
<?xml version="1.0" ?>
|
|
<document source="<string>">
|
|
<paragraph>
|
|
line1
|
|
<raw format="html" xml:space="preserve"><br /></raw>
|
|
line2
|
|
</paragraph>
|
|
</document>
|
|
"""
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|