|
From: <mi...@us...> - 2022-01-11 17:40:17
|
Revision: 8943
http://sourceforge.net/p/docutils/code/8943
Author: milde
Date: 2022-01-11 17:40:14 +0000 (Tue, 11 Jan 2022)
Log Message:
-----------
Update recommonmark wrapper and tests.
Adapt to and test with "recommonmark" versions 0.6.0 and 0.7.1.
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/docutils/parsers/recommonmark_wrapper.py
trunk/docutils/test/DocutilsTestSupport.py
trunk/docutils/test/test_parsers/test_recommonmark/test_html_blocks.py
trunk/docutils/test/test_parsers/test_recommonmark/test_inline_markup.py
trunk/docutils/test/test_parsers/test_recommonmark/test_misc.py
trunk/docutils/test/test_parsers/test_recommonmark/test_section_headers.py
trunk/docutils/test/test_parsers/test_recommonmark/test_targets.py
trunk/docutils/test/test_parsers/test_rst/test_directives/test__init__.py
trunk/docutils/test/test_parsers/test_rst/test_directives/test_include.py
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2022-01-11 17:40:00 UTC (rev 8942)
+++ trunk/docutils/HISTORY.txt 2022-01-11 17:40:14 UTC (rev 8943)
@@ -32,8 +32,9 @@
- Raise ImportError, if import of the upstream parser module fails.
If called from an `"include" directive`_,
the system-message now has source/line info.
+ - Adapt to and test with "recommonmark" versions 0.6.0 and 0.7.1.
- .. _"include" directive: docs/ref/rst/directives.html#include
+ .. _"include" directive: docs/ref/rst/directives.html#include
* docutils/parsers/rst/directives/__init__.py
Modified: trunk/docutils/docutils/parsers/recommonmark_wrapper.py
===================================================================
--- trunk/docutils/docutils/parsers/recommonmark_wrapper.py 2022-01-11 17:40:00 UTC (rev 8942)
+++ trunk/docutils/docutils/parsers/recommonmark_wrapper.py 2022-01-11 17:40:14 UTC (rev 8943)
@@ -70,6 +70,8 @@
try:
CommonMarkParser.parse(self, inputstring, document)
except Exception as err:
+ if document.settings.traceback:
+ raise err
error = document.reporter.error('Parsing with "recommonmark" '
'returned the error:\n%s'%err)
document.append(error)
@@ -92,7 +94,8 @@
# add "code" class argument to literal elements (inline and block)
for node in document.findall(lambda n: isinstance(n,
(nodes.literal, nodes.literal_block))):
- node['classes'].append('code')
+ if 'code' not in node['classes']:
+ node['classes'].append('code')
# move "language" argument to classes
for node in document.findall(nodes.literal_block):
if 'language' in node.attributes:
@@ -99,14 +102,6 @@
node['classes'].append(node['language'])
del node['language']
- # remove empty target nodes
- for node in list(document.findall(nodes.target)):
- # remove empty name
- node['names'] = [v for v in node['names'] if v]
- if node.children or [v for v in node.attributes.values() if v]:
- continue
- node.parent.remove(node)
-
# replace raw nodes if raw is not allowed
if not document.settings.raw_enabled:
for node in document.findall(nodes.raw):
@@ -113,24 +108,6 @@
warning = document.reporter.warning('Raw content disabled.')
node.parent.replace(node, warning)
- # fix section nodes
- for node in document.findall(nodes.section):
- # remove spurious IDs (first may be from duplicate name)
- if len(node['ids']) > 1:
- node['ids'].pop()
- # fix section levels (recommonmark 0.4.0
- # later versions silently ignore incompatible levels)
- if 'level' in node:
- section_level = self.get_section_level(node)
- if node['level'] != section_level:
- warning = document.reporter.warning(
- 'Title level inconsistent. Changing from %d to %d.'
- %(node['level'], section_level),
- nodes.literal_block('', node[0].astext()))
- node.insert(1, warning)
- # remove non-standard attribute "level"
- del node['level']
-
# drop pending_xref (Sphinx cross reference extension)
for node in document.findall(addnodes.pending_xref):
reference = node.children[0]
@@ -139,16 +116,6 @@
reference.astext())
node.parent.replace(node, reference)
- def get_section_level(self, node):
- """Auxiliary function for post-processing in self.parse()"""
- level = 1
- while True:
- node = node.parent
- if isinstance(node, nodes.document):
- return level
- if isinstance(node, nodes.section):
- level += 1
-
def visit_document(self, node):
"""Dummy function to prevent spurious warnings.
Modified: trunk/docutils/test/DocutilsTestSupport.py
===================================================================
--- trunk/docutils/test/DocutilsTestSupport.py 2022-01-11 17:40:00 UTC (rev 8942)
+++ trunk/docutils/test/DocutilsTestSupport.py 2022-01-11 17:40:14 UTC (rev 8943)
@@ -164,8 +164,7 @@
self.run_in_debugger = run_in_debugger
self.suite_settings = suite_settings.copy() or {}
- # Ring your mother.
- unittest.TestCase.__init__(self, method_name)
+ super().__init__(method_name)
def __str__(self):
"""
@@ -508,37 +507,41 @@
class RecommonmarkParserTestCase(ParserTestCase):
- """Recommonmark-specific parser test case."""
+ """Test case for 3rd-party CommonMark parsers."""
+ # TODO: test with alternative CommonMark parsers?
+ parser_name = 'recommonmark'
+ # parser_name = 'pycmark'
+ # parser_name = 'myst'
try:
- parser_class = docutils.parsers.get_parser_class('recommonmark')
- parser = parser_class()
+ parser_class = docutils.parsers.get_parser_class(parser_name)
except ImportError:
parser_class = None
- # recommonmark_wrapper.Parser
- """Parser shared by all RecommonmarkParserTestCases."""
+ if parser_class and parser_name == 'recommonmark':
+ import recommonmark
+ if recommonmark.__version__ < '0.6.0':
+ # print(f'Skip Markdown tests, "{parser_name}" parser too old')
+ parser_class = None
+ if parser_class:
+ parser = parser_class()
+ option_parser = frontend.OptionParser(components=(parser_class,))
+ settings = option_parser.get_default_values()
+ settings.report_level = 5
+ settings.halt_level = 5
+ settings.debug = package_unittest.debug
- option_parser = frontend.OptionParser(components=(parser_class,))
- settings = option_parser.get_default_values()
- settings.report_level = 5
- settings.halt_level = 5
- settings.debug = package_unittest.debug
-
class RecommonmarkParserTestSuite(ParserTestSuite):
"""A collection of RecommonmarkParserTestCases."""
test_case_class = RecommonmarkParserTestCase
-
- def generateTests(self, dict, dictname='totest'):
- if not RecommonmarkParserTestCase.parser_class:
+
+ if not test_case_class.parser_class:
+ # print('No compatible CommonMark parser found.'
+ # ' Skipping all CommonMark/recommonmark tests.')
+ def generateTests(self, dict, dictname='totest'):
return
- # TODO: currently the tests are too version-specific
- import recommonmark
- if recommonmark.__version__ != '0.4.0':
- return
- ParserTestSuite.generateTests(self, dict, dictname='totest')
class GridTableParserTestCase(CustomTestCase):
Modified: trunk/docutils/test/test_parsers/test_recommonmark/test_html_blocks.py
===================================================================
--- trunk/docutils/test/test_parsers/test_recommonmark/test_html_blocks.py 2022-01-11 17:40:00 UTC (rev 8942)
+++ trunk/docutils/test/test_parsers/test_recommonmark/test_html_blocks.py 2022-01-11 17:40:14 UTC (rev 8943)
@@ -63,26 +63,28 @@
""",
"""\
<document source="test data">
- <paragraph>
- <raw format="html" xml:space="preserve">
- <a href="foo">
- \n\
- <emphasis>
- bar
- \n\
- <raw format="html" xml:space="preserve">
- </a>
-"""],
-["""\
-<!-- foo -->*bar*
-*baz*
-""",
-"""\
-<document source="test data">
<raw format="html" xml:space="preserve">
- <!-- foo -->*bar*
- *baz*
+ <a href="foo">
+ *bar*
+ </a>
"""],
+# In recommonmark 0.7.0, some raw blocks at paragraph start make the
+# paragraph a raw block :(
+# ["""\
+# <!-- foo -->*bar* (raw because of the comment tag at start of paragraph)
+# *baz*
+# """,
+# """\
+# <document source="test data">
+# <paragraph>
+# <raw format="html" xml:space="preserve">
+# <!-- foo -->
+# <emphasis>
+# bar
+# <paragraph>
+# <emphasis>
+# baz
+# """],
]
Modified: trunk/docutils/test/test_parsers/test_recommonmark/test_inline_markup.py
===================================================================
--- trunk/docutils/test/test_parsers/test_recommonmark/test_inline_markup.py 2022-01-11 17:40:00 UTC (rev 8942)
+++ trunk/docutils/test/test_parsers/test_recommonmark/test_inline_markup.py 2022-01-11 17:40:14 UTC (rev 8943)
@@ -263,7 +263,7 @@
<document source="test data">
<paragraph>
<literal classes="code">
- literal
+ literal \n\
no literal
<paragraph>
No warning for `standalone TeX quotes\' or other \n\
@@ -323,21 +323,22 @@
<reference name="ref" refuri="/uri">
ref
"""],
+# Fails with recommonmark 0.6.0:
+# ["""\
+# Inline image ![foo *bar*]
+# in a paragraph.
+#
+# [foo *bar*]: train.jpg "train & tracks"
+# """,
+# """\
+# <document source="test data">
+# <paragraph>
+# Inline image \n\
+# <image alt="foo " title="train & tracks" uri="train.jpg">
+# \n\
+# in a paragraph.
+# """],
["""\
-Inline image ![foo *bar*][foobar]
-in a paragraph.
-
-[FOOBAR]: train.jpg "train & tracks"
-""",
-"""\
-<document source="test data">
- <paragraph>
- Inline image \n\
- <image alt="foo " title="train & tracks" uri="train.jpg">
- \n\
- in a paragraph.
-"""],
-["""\
[phrase reference]
[phrase reference]: /uri
@@ -370,7 +371,7 @@
"""\
<document source="test data">
<paragraph>
- <reference name="phrase referenceacross lines" refuri="/uri">
+ <reference name="phrase reference across lines" refuri="/uri">
phrase reference
across lines
"""],
@@ -383,7 +384,7 @@
"""\
<document source="test data">
<paragraph>
- <reference name="anonymous reference" refuri="http://example.com">
+ <reference refuri="http://example.com">
anonymous reference
"""],
["""\
@@ -396,17 +397,18 @@
<image alt="a train" uri="train.jpg">
more text.
"""],
+# recommonmark 0.6.0 drops the "title"
+# ["""\
+# Inline image  more text.
+# """,
+# """\
+# <document source="test data">
+# <paragraph>
+# Inline image \n\
+# <image alt="foo" title="title" uri="/url">
+# more text.
+# """],
["""\
-Inline image  more text.
-""",
-"""\
-<document source="test data">
- <paragraph>
- Inline image \n\
- <image alt="foo" title="title" uri="/url">
- more text.
-"""],
-["""\
[URI must follow immediately]
(http://example.com)
""",
@@ -441,7 +443,7 @@
<paragraph>
CommonMark calls standalone hyperlinks
like \n\
- <reference name="http://example.com" refuri="http://example.com">
+ <reference refuri="http://example.com">
http://example.com
"autolinks".
"""],
@@ -478,17 +480,21 @@
"""],
["""\
Hard line breaks are not supported by Docutils.
-Not the soft line break preceded by two or more spaces, \n\
-nor the more visible alternative,\\
-a backslash before the line ending.
+"recommonmark 0.6.0" converts both, invisible \n\
+(two or more trailing spaces) nor visible\\
+(trailing backslash) to raw HTML.
""",
"""\
<document source="test data">
<paragraph>
Hard line breaks are not supported by Docutils.
- Not the soft line break preceded by two or more spaces,\
-nor the more visible alternative,\
-a backslash before the line ending.
+ "recommonmark 0.6.0" converts both, invisible
+ <raw format="html" xml:space="preserve">
+ <br />
+ (two or more trailing spaces) nor visible
+ <raw format="html" xml:space="preserve">
+ <br />
+ (trailing backslash) to raw HTML.
"""],
]
Modified: trunk/docutils/test/test_parsers/test_recommonmark/test_misc.py
===================================================================
--- trunk/docutils/test/test_parsers/test_recommonmark/test_misc.py 2022-01-11 17:40:00 UTC (rev 8942)
+++ trunk/docutils/test/test_parsers/test_recommonmark/test_misc.py 2022-01-11 17:40:14 UTC (rev 8943)
@@ -36,12 +36,12 @@
Final paragraph.
"""
-parser_class = DocutilsTestSupport.RecommonmarkParserTestCase.parser_class
+parser = DocutilsTestSupport.RecommonmarkParserTestCase.parser
skip_msg = 'optional module "recommonmark" not found'
-class reCommonMarkParserTests(unittest.TestCase):
+class RecommonmarkParserTests(unittest.TestCase):
- @unittest.skipUnless(parser_class, skip_msg)
+ @unittest.skipUnless(parser, skip_msg)
def test_raw_disabled(self):
output = publish_string(sample_with_html, parser_name='recommonmark',
settings_overrides={'warning_stream': '',
@@ -50,7 +50,7 @@
self.assertIn(b'<system_message', output)
self.assertIn(b'Raw content disabled.', output)
- @unittest.skipUnless(parser_class, skip_msg)
+ @unittest.skipUnless(parser, skip_msg)
def test_raw_disabled_inline(self):
output = publish_string('foo <a href="uri">', parser_name='recommonmark',
settings_overrides={'warning_stream': '',
@@ -61,7 +61,7 @@
self.assertIn(b'Raw content disabled.', output)
- @unittest.skipUnless(parser_class, skip_msg)
+ @unittest.skipUnless(parser, skip_msg)
def test_raw_disabled(self):
# silence Warnings in recommonmark (unmaintained 3rd party module):
warnings.filterwarnings('ignore', category=DeprecationWarning,
@@ -74,7 +74,7 @@
self.assertNotIn(b'<raw>', output)
self.assertNotIn(b'<system_message', output)
- @unittest.skipIf(parser_class,
+ @unittest.skipIf(parser,
'recommonmark_wrapper: parser found, fallback not used')
def test_missing_parser(self):
try:
Modified: trunk/docutils/test/test_parsers/test_recommonmark/test_section_headers.py
===================================================================
--- trunk/docutils/test/test_parsers/test_recommonmark/test_section_headers.py 2022-01-11 17:40:00 UTC (rev 8942)
+++ trunk/docutils/test/test_parsers/test_recommonmark/test_section_headers.py 2022-01-11 17:40:14 UTC (rev 8943)
@@ -122,22 +122,22 @@
<document source="test data">
<paragraph>
Test return to existing, highest-level section (Title 3).
- <section ids="title-1" names="title\\ 1">
+ <section ids="title-1" names="title\ 1">
<title>
Title 1
<paragraph>
Paragraph 1.
- <section ids="title-2" names="title\\ 2">
+ <section ids="title-2" names="title\ 2">
<title>
Title 2
<paragraph>
Paragraph 2.
- <section ids="title-3" names="title\\ 3">
+ <section ids="title-3" names="title\ 3">
<title>
Title 3
<paragraph>
Paragraph 3.
- <section ids="title-4" names="title\\ 4">
+ <section ids="title-4" names="title\ 4">
<title>
Title 4
<paragraph>
@@ -156,45 +156,25 @@
### Title 5
""",
-r"""
+"""\
<document source="test data">
<paragraph>
Test bad subsection order.
- <section ids="title-1" names="title\\ 1">
+ <section ids="title-1" names="title\ 1">
<title>
Title 1
- <system_message level="2" source="test data" type="WARNING">
- <paragraph>
- Title level inconsistent. Changing from 2 to 1.
- <literal_block xml:space="preserve">
- Title 1
- <section ids="title-2" names="title\\ 2">
+ <section ids="title-2" names="title\ 2">
<title>
Title 2
- <system_message level="2" source="test data" type="WARNING">
- <paragraph>
- Title level inconsistent. Changing from 2 to 1.
- <literal_block xml:space="preserve">
- Title 2
- <section ids="title-3" names="title\\ 3">
+ <section ids="title-3" names="title\ 3">
<title>
Title 3
<section ids="title-4" names="title\ 4">
<title>
Title 4
- <system_message level="2" source="test data" type="WARNING">
- <paragraph>
- Title level inconsistent. Changing from 4 to 2.
- <literal_block xml:space="preserve">
- Title 4
<section ids="title-5" names="title\ 5">
<title>
Title 5
- <system_message level="2" source="test data" type="WARNING">
- <paragraph>
- Title level inconsistent. Changing from 3 to 2.
- <literal_block xml:space="preserve">
- Title 5
"""],
["""\
Title containing *inline* ``markup``
@@ -204,7 +184,7 @@
""",
"""\
<document source="test data">
- <section ids="title-containing-inline-markup" names="title\\ containing\\ inline\\ markup">
+ <section ids="title-containing-inline-markup" names="title\ containing\ inline\ markup">
<title>
Title containing \n\
<emphasis>
@@ -251,7 +231,7 @@
""",
"""\
<document source="test data">
- <section ids="empty-section" names="empty\\ section">
+ <section ids="empty-section" names="empty\ section">
<title>
Empty Section
"""],
Modified: trunk/docutils/test/test_parsers/test_recommonmark/test_targets.py
===================================================================
--- trunk/docutils/test/test_parsers/test_recommonmark/test_targets.py 2022-01-11 17:40:00 UTC (rev 8942)
+++ trunk/docutils/test/test_parsers/test_recommonmark/test_targets.py 2022-01-11 17:40:14 UTC (rev 8943)
@@ -35,7 +35,7 @@
<document source="test data">
<paragraph>
External hyperlink \n\
- <reference name="target" refuri="http://www.python.org/">
+ <reference refuri="http://www.python.org/">
target
s:
"""],
@@ -64,7 +64,7 @@
<document source="test data">
<paragraph>
Duplicate external \n\
- <reference name="targets" refuri="first wins">
+ <reference name="targets" refuri="first%20wins">
targets
(different URIs):
"""],
Modified: trunk/docutils/test/test_parsers/test_rst/test_directives/test__init__.py
===================================================================
--- trunk/docutils/test/test_parsers/test_rst/test_directives/test__init__.py 2022-01-11 17:40:00 UTC (rev 8942)
+++ trunk/docutils/test/test_parsers/test_rst/test_directives/test__init__.py 2022-01-11 17:40:14 UTC (rev 8943)
@@ -51,16 +51,10 @@
directives.parser_name('null'))
self.assertEqual(docutils.parsers.rst.Parser,
directives.parser_name('rst'))
+ self.assertEqual(directives.parser_name('recommonmark'),
+ docutils.parsers.recommonmark_wrapper.Parser)
self.assertRaises(ValueError, directives.parser_name, 'fantasy')
- parser_class = DocutilsTestSupport.RecommonmarkParserTestCase.parser_class
- skip_msg = 'optional module "recommonmark" not found'
- @unittest.skipUnless(parser_class, skip_msg)
- def test_external_parser_name(self):
- self.assertEqual(docutils.parsers.recommonmark_wrapper.Parser,
- directives.parser_name('recommonmark'))
-
-
if __name__ == '__main__':
import unittest
unittest.main()
Modified: trunk/docutils/test/test_parsers/test_rst/test_directives/test_include.py
===================================================================
--- trunk/docutils/test/test_parsers/test_rst/test_directives/test_include.py 2022-01-11 17:40:00 UTC (rev 8942)
+++ trunk/docutils/test/test_parsers/test_rst/test_directives/test_include.py 2022-01-11 17:40:14 UTC (rev 8943)
@@ -18,8 +18,11 @@
def suite():
s = DocutilsTestSupport.ParserTestSuite()
+ # eventually skip optional parts:
if not with_pygments:
del(totest['include-code'])
+ if not DocutilsTestSupport.RecommonmarkParserTestCase.parser:
+ del(totest['include-recommonmark'])
s.generateTests(totest)
return s
@@ -64,36 +67,6 @@
InputError: [Errno 2] No such file or directory: '\u043c\u0438\u0440.txt'.\
"""
-# Parsing with Markdown (recommonmark) is an optional feature depending
-# on 3rd-party modules:
-if DocutilsTestSupport.RecommonmarkParserTestCase.parser_class:
- markdown_parsing_result = """\
- <section ids="title-1" names="title\\ 1">
- <title>
- Title 1
- <paragraph>
- <emphasis>
- emphasis
- and \n\
- <emphasis>
- also emphasis
- <paragraph>
- No whitespace required around a
- <reference name="phrase reference" refuri="/uri">
- phrase reference
- ."""
-else:
- markdown_parsing_result = """\
- <system_message level="3" line="3" source="test data" type="ERROR">
- <paragraph>
- Error in "include" directive:
- invalid option value: (option: "parser"; value: \'markdown\')
- Parser "markdown" missing. No module named 'recommonmark'.
- Parsing "recommonmark" Markdown flavour requires the package https://pypi.org/project/recommonmark.
- <literal_block xml:space="preserve">
- .. include:: test_parsers/test_rst/test_directives/include.md
- :parser: markdown"""
-
totest = {}
totest['include'] = [
@@ -214,21 +187,28 @@
This file is used by ``test_include.py``.
""" % reldir(include1)],
["""\
-Include markdown (recommonmark).
+Include with unknown parser.
.. include:: %s
- :parser: markdown
+ :parser: sillyformat
A paragraph.
-""" % include_md,
+""" % include1,
"""\
<document source="test data">
<paragraph>
- Include markdown (recommonmark).
-%s
+ Include with unknown parser.
+ <system_message level="3" line="3" source="test data" type="ERROR">
+ <paragraph>
+ Error in "include" directive:
+ invalid option value: (option: "parser"; value: \'sillyformat\')
+ Parser "sillyformat" missing. No module named 'sillyformat'.
+ <literal_block xml:space="preserve">
+ .. include:: test_parsers/test_rst/test_directives/include1.txt
+ :parser: sillyformat
<paragraph>
A paragraph.
-""" % markdown_parsing_result],
+"""],
["""\
Let's test the parse context.
@@ -1310,6 +1290,41 @@
Some include text."""],
]
+# Parsing with Markdown (recommonmark) is an optional feature depending
+# on 3rd-party modules:
+totest['include-recommonmark'] = [
+["""\
+Include markdown (recommonmark).
+
+.. include:: %s
+ :parser: markdown
+
+A paragraph.
+""" % include_md,
+"""\
+<document source="test data">
+ <paragraph>
+ Include markdown (recommonmark).
+ <section ids="title-1" names="title\\ 1">
+ <title>
+ Title 1
+ <paragraph>
+ <emphasis>
+ emphasis
+ and \n\
+ <emphasis>
+ also emphasis
+ <paragraph>
+ No whitespace required around a
+ <reference name="phrase reference" refuri="/uri">
+ phrase reference
+ .
+ <paragraph>
+ A paragraph.
+"""],
+]
+
+
if __name__ == '__main__':
import unittest
unittest.main(defaultTest='suite')
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|