From 5f5381734f9bc412a93b8279d9d16d2197f50b09 Mon Sep 17 00:00:00 2001 From: milde Date: Mon, 11 Mar 2013 21:01:03 +0000 Subject: Treat embedded standalone hyperlinks as URI, even if ending in underscore. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@7629 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docs/ref/rst/restructuredtext.txt | 26 +++++++++++++-------- docutils/parsers/rst/states.py | 3 ++- test/test_transforms/test_hyperlinks.py | 41 ++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/docs/ref/rst/restructuredtext.txt b/docs/ref/rst/restructuredtext.txt index fb07780b2..4f0165d3d 100644 --- a/docs/ref/rst/restructuredtext.txt +++ b/docs/ref/rst/restructuredtext.txt @@ -2656,8 +2656,9 @@ hyperlinks. Embedded URIs and Aliases ````````````````````````` -A hyperlink reference may directly embed a target URI or a hyperlink -reference within angle brackets ("<...>") as follows:: +A hyperlink reference may directly embed a target URI or (since +Docutils 0.11) a hyperlink reference within angle brackets ("<...>") +as follows:: See the `Python home page `_ for info. @@ -2692,20 +2693,26 @@ Equivalent to:: __ http://www.rfc-editor.org/rfc/rfc2396.txt __ http://www.rfc-editor.org/rfc/rfc2732.txt -If a target URI happens to end with an underscore, this needs to be -backslash-escaped to avoid being parsed as hyperlink reference. For -example :: +`Standalone hyperlinks`_ are treated as URIs, even if they end with an +underscore like in the example of a Python function documentation:: + + `__init__ `__ + +If a target URI that is not recognized as `standalone hyperlink`_ happens +to end with an underscore, this needs to be backslash-escaped to avoid +being parsed as hyperlink reference. For example :: Use the `source `__. creates an anonymous reference to the file ``parrots.txt_``. If the reference text happens to end with angle-bracketed text that is -*not* a URI or hyperlink reference, the open-angle-bracket needs to be -backslash-escaped. For example, here is a reference to a title -describing a tag:: +*not* a URI or hyperlink reference, at least one angle-bracket needs to +be backslash-escaped or an escaped space should follow. For example, here +are three references to titles describing a tag:: - See `HTML Element: \`_ below. + See `HTML Element: \`_, `HTML Element: `_, and + `HTML Element: \ `_. The reference text may also be omitted, in which case the URI will be duplicated for use as the reference text. This is useful for relative @@ -2820,6 +2827,7 @@ Examples:: addition to being replaced, the replacement text or element will refer to the "substitution and hyperlink reference" target. +.. _standalone hyperlink: Standalone Hyperlinks --------------------- diff --git a/docutils/parsers/rst/states.py b/docutils/parsers/rst/states.py index 8441d09ec..7b5f708e6 100644 --- a/docutils/parsers/rst/states.py +++ b/docutils/parsers/rst/states.py @@ -792,7 +792,8 @@ class Inliner: if match: # embedded or text = unescape(escaped[:match.start(0)]) aliastext = unescape(match.group(2), restore_backslashes=True) - if aliastext.endswith('_') and not aliastext.endswith(r'\_'): + if aliastext.endswith('_') and not (aliastext.endswith(r'\_') + or self.patterns.uri.match(aliastext)): aliastype = 'name' alias = normalize_name(aliastext[:-1]) target = nodes.target(match.group(1), refname=alias) diff --git a/test/test_transforms/test_hyperlinks.py b/test/test_transforms/test_hyperlinks.py index 6f4081ad9..2245e7245 100755 --- a/test/test_transforms/test_hyperlinks.py +++ b/test/test_transforms/test_hyperlinks.py @@ -374,7 +374,6 @@ An `anonymous embedded alias `__. . """], -# TODO: suppress the INFO message? ["""\ An `embedded alias `_ with unknown reference. """, @@ -397,6 +396,46 @@ An `embedded alias `_ with unknown reference. Hyperlink target "embedded alias" is not referenced.\ """], ["""\ +An embedded URI with trailing underline: +`__init__ `__. +""", +"""\ + + + An embedded URI with trailing underline: + + __init__ + . +"""], +["""\ +Hyperlinks with angle-bracketed text need escaping. + +See `Element \`_, `Element `_, and `Element \ `_. + +.. _`Element `: +.. _`Element `: +.. _`Element `: elements.txt +""", +"""\ + + + Hyperlinks with angle-bracketed text need escaping. + + See \n\ + + Element + , \n\ + + Element + , and \n\ + + Element + . + + + +"""], +["""\ .. _target: .. [1] Footnote; target_ -- cgit v1.2.1