diff options
Diffstat (limited to 'docutils')
| -rw-r--r-- | docutils/HISTORY.txt | 10 | ||||
| -rw-r--r-- | docutils/docutils/transforms/references.py | 71 | ||||
| -rw-r--r-- | docutils/test/test_transforms/itest_hyperlinks_de.py | 76 | ||||
| -rwxr-xr-x | docutils/test/test_transforms/test_hyperlinks.py | 9 |
4 files changed, 124 insertions, 42 deletions
diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt index 85ec5a2a2..4867cdb34 100644 --- a/docutils/HISTORY.txt +++ b/docutils/HISTORY.txt @@ -57,6 +57,10 @@ Changes Since 0.17.1 .. _LaTeX syntax for mathematics: docs/ref/rst/mathematics.html +* docutils/transforms/references.py + + - Skip system_messages when propagating targets. Fixes bug #425. + * docutils/utils/__init__.py - Removed ``unique_combinations`` (obsoleted by ``itertools.combinations``). @@ -109,13 +113,13 @@ Changes Since 0.17.1 - Apply patch #181 "Fix tocdepth when chapter/part in use" by John Thorvald Wodder II. - + - Fix newlines after/before ids_to_labels() (cf. patch #183). - Refactor/revise ToC writing. - + - Don't add ``\phantomsection`` to labels in math-blocks. - + - Improve spacing and allow customization of Docutils-generated table of contents. diff --git a/docutils/docutils/transforms/references.py b/docutils/docutils/transforms/references.py index 472880b0c..0a5defc2f 100644 --- a/docutils/docutils/transforms/references.py +++ b/docutils/docutils/transforms/references.py @@ -42,48 +42,51 @@ class PropagateTargets(Transform): def apply(self): for target in self.document.traverse(nodes.target): - # Only block-level targets without reference (like ".. target:"): + # Only block-level targets without reference (like ".. _target:"): if (isinstance(target.parent, nodes.TextElement) or (target.hasattr('refid') or target.hasattr('refuri') or target.hasattr('refname'))): continue assert len(target) == 0, 'error: block-level target has children' next_node = target.next_node(ascend=True) + # skip system messages (may be removed by universal.FilterMessages) + while isinstance(next_node, nodes.system_message): + next_node = next_node.next_node(ascend=True, descend=False) # Do not move names and ids into Invisibles (we'd lose the # attributes) or different Targetables (e.g. footnotes). - if (next_node is not None and - ((not isinstance(next_node, nodes.Invisible) and - not isinstance(next_node, nodes.Targetable)) or - isinstance(next_node, nodes.target))): - next_node['ids'].extend(target['ids']) - next_node['names'].extend(target['names']) - # Set defaults for next_node.expect_referenced_by_name/id. - if not hasattr(next_node, 'expect_referenced_by_name'): - next_node.expect_referenced_by_name = {} - if not hasattr(next_node, 'expect_referenced_by_id'): - next_node.expect_referenced_by_id = {} - for id in target['ids']: - # Update IDs to node mapping. - self.document.ids[id] = next_node - # If next_node is referenced by id ``id``, this - # target shall be marked as referenced. - next_node.expect_referenced_by_id[id] = target - for name in target['names']: - next_node.expect_referenced_by_name[name] = target - # If there are any expect_referenced_by_... attributes - # in target set, copy them to next_node. - next_node.expect_referenced_by_name.update( - getattr(target, 'expect_referenced_by_name', {})) - next_node.expect_referenced_by_id.update( - getattr(target, 'expect_referenced_by_id', {})) - # Set refid to point to the first former ID of target - # which is now an ID of next_node. - target['refid'] = target['ids'][0] - # Clear ids and names; they have been moved to - # next_node. - target['ids'] = [] - target['names'] = [] - self.document.note_refid(target) + if (next_node is None + or isinstance(next_node, (nodes.Invisible, nodes.Targetable)) + and not isinstance(next_node, nodes.target)): + continue + next_node['ids'].extend(target['ids']) + next_node['names'].extend(target['names']) + # Set defaults for next_node.expect_referenced_by_name/id. + if not hasattr(next_node, 'expect_referenced_by_name'): + next_node.expect_referenced_by_name = {} + if not hasattr(next_node, 'expect_referenced_by_id'): + next_node.expect_referenced_by_id = {} + for id in target['ids']: + # Update IDs to node mapping. + self.document.ids[id] = next_node + # If next_node is referenced by id ``id``, this + # target shall be marked as referenced. + next_node.expect_referenced_by_id[id] = target + for name in target['names']: + next_node.expect_referenced_by_name[name] = target + # If there are any expect_referenced_by_... attributes + # in target set, copy them to next_node. + next_node.expect_referenced_by_name.update( + getattr(target, 'expect_referenced_by_name', {})) + next_node.expect_referenced_by_id.update( + getattr(target, 'expect_referenced_by_id', {})) + # Set refid to point to the first former ID of target + # which is now an ID of next_node. + target['refid'] = target['ids'][0] + # Clear ids and names; they have been moved to + # next_node. + target['ids'] = [] + target['names'] = [] + self.document.note_refid(target) class AnonymousHyperlinks(Transform): diff --git a/docutils/test/test_transforms/itest_hyperlinks_de.py b/docutils/test/test_transforms/itest_hyperlinks_de.py new file mode 100644 index 000000000..77c32439c --- /dev/null +++ b/docutils/test/test_transforms/itest_hyperlinks_de.py @@ -0,0 +1,76 @@ +#! /usr/bin/env python + +# $Id$ +# Author: David Goodger <goodger@python.org> +# Copyright: This module has been placed in the public domain. + +""" +Tests for docutils.transforms.references.Hyperlinks with non-English language. + +TODO: This test fails currently when run as part of "alltests" because + + - the "info" system-messages for directive fallbacks are only generated + once (the name -> directive mapping is cached in + ``docutils.parsers.rst.directives._directives``). + + - the cache is not reset between after processing a document + (i.e. it contains name -> directive mappings from other tests). + + See also https://sourceforge.net/p/docutils/feature-requests/71/ +""" + +from __future__ import absolute_import + +if __name__ == '__main__': + import __init__ +from test_transforms import DocutilsTestSupport +from docutils.transforms.references import PropagateTargets, \ + AnonymousHyperlinks, IndirectHyperlinks, ExternalTargets, \ + InternalTargets, DanglingReferences +from docutils.parsers.rst import Parser, directives + +def suite(): + parser = Parser() + settings = {} + settings['language_code'] = 'de' + s = DocutilsTestSupport.TransformTestSuite( + parser, suite_settings=settings) + s.generateTests(totest) + return s + +totest = {} + +totest['hyperlinks'] = ((PropagateTargets, AnonymousHyperlinks, + IndirectHyperlinks, ExternalTargets, + InternalTargets, DanglingReferences), [ + +["""\ +Target_ should propagate past the system_message to set "id" on note. + +.. _target: +.. note:: Kurznotiz + :name: mynote +""", +"""\ +<document source="test data"> + <paragraph> + <reference name="Target" refid="target"> + Target + should propagate past the system_message to set "id" on note. + <target refid="target"> + <system_message level="1" line="4" source="test data" type="INFO"> + <paragraph> + No directive entry for "note" in module "docutils.parsers.rst.languages.de". + Using English fallback for directive "note". + <note ids="mynote target" names="mynote target"> + <paragraph> + Kurznotiz + <system_message level="1" source="test data" type="INFO"> + <paragraph> + Using <module 'docutils.languages.de' from '/usr/local/src/docutils-git-svn/docutils/docutils/languages/de.pyc'> for language "de". +"""], +]) + +if __name__ == '__main__': + import unittest + unittest.main(defaultTest='suite') diff --git a/docutils/test/test_transforms/test_hyperlinks.py b/docutils/test/test_transforms/test_hyperlinks.py index 8ec258d16..b20b05284 100755 --- a/docutils/test/test_transforms/test_hyperlinks.py +++ b/docutils/test/test_transforms/test_hyperlinks.py @@ -30,9 +30,8 @@ totest = {} # target/reference, direct/indirect, internal/external, and named/anonymous, # plus embedded URIs. totest['exhaustive_hyperlinks'] = ((PropagateTargets, AnonymousHyperlinks, - IndirectHyperlinks, - ExternalTargets, InternalTargets, - DanglingReferences), [ + IndirectHyperlinks, ExternalTargets, + InternalTargets, DanglingReferences), [ ["""\ direct_ external @@ -185,7 +184,7 @@ Direct internal reference: Implicit_ internal <target ids="indirect" names="indirect" refname="implicit"> <paragraph> - Direct internal reference: + Direct internal reference: \n\ <problematic ids="problematic-2" refid="system-message-2"> Implicit_ <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="11" source="test data" type="ERROR"> @@ -840,7 +839,7 @@ Testing an `indirect reference to the table of contents`_. <title refid="toc-entry-1"> Section <paragraph> - Testing an + Testing an \n\ <reference name="indirect reference to the table of contents" refid="table-of-contents"> indirect reference to the table of contents . |
