From 59be638dfec6c143a38fec04495ae26e4142992c Mon Sep 17 00:00:00 2001 From: milde Date: Mon, 6 May 2024 07:55:16 +0000 Subject: [PATCH] Fix recommonmark parser post-processing. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Do not insert a `` where it is invalid (paragraphs, …). Use a `` node instead and place the `` at the end of the document. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@9687 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/docutils/parsers/recommonmark_wrapper.py | 13 +++++++++-- .../test_parsers/test_recommonmark/test_misc.py | 27 +++++++++++----------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/docutils/docutils/parsers/recommonmark_wrapper.py b/docutils/docutils/parsers/recommonmark_wrapper.py index ac16221f5..4e4698c3c 100644 --- a/docutils/docutils/parsers/recommonmark_wrapper.py +++ b/docutils/docutils/parsers/recommonmark_wrapper.py @@ -123,8 +123,17 @@ class Parser(CommonMarkParser): # replace raw nodes if raw is not allowed if not document.settings.raw_enabled: for node in document.findall(nodes.raw): - warning = document.reporter.warning('Raw content disabled.') - node.parent.replace(node, warning) + message = document.reporter.warning('Raw content disabled.') + if isinstance(node.parent, nodes.TextElement): + msgid = document.set_id(message) + problematic = nodes.problematic('', node.astext(), + refid=msgid) + node.parent.replace(node, problematic) + prbid = document.set_id(problematic) + message.add_backref(prbid) + document.append(message) + else: + node.parent.replace(node, message) # drop pending_xref (Sphinx cross reference extension) for node in document.findall(addnodes.pending_xref): diff --git a/docutils/test/test_parsers/test_recommonmark/test_misc.py b/docutils/test/test_parsers/test_recommonmark/test_misc.py index 5ab7b9fd1..cc9902f81 100755 --- a/docutils/test/test_parsers/test_recommonmark/test_misc.py +++ b/docutils/test/test_parsers/test_recommonmark/test_misc.py @@ -44,6 +44,11 @@ Final paragraph. @unittest.skipIf(Parser is None, 'Optional "recommonmark" module not found.') class RecommonmarkParserTests(unittest.TestCase): + mysettings = {'output_encoding': 'unicode', + 'warning_stream': '', + 'raw_enabled': False, + } + def test_parser_name(self): # cf. ../test_rst/test_directives/test__init__.py # this is used in the "include" directive's :parser: option. @@ -51,22 +56,18 @@ class RecommonmarkParserTests(unittest.TestCase): def test_raw_disabled(self): output = publish_string(sample_with_html, parser=Parser(), - settings_overrides={ - 'warning_stream': '', - 'raw_enabled': False, - }) - self.assertNotIn(b'', output) - self.assertIn(b'', output) + self.assertIn('', parser=Parser(), - settings_overrides={'warning_stream': '', - 'raw_enabled': False, - }) - self.assertNotIn(b'', output) - self.assertIn(b'', output) + self.assertIn('