summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortk0miya <i.tkomiya@gmail.com>2014-08-20 12:27:08 +0900
committertk0miya <i.tkomiya@gmail.com>2014-08-20 12:27:08 +0900
commit83dafb1d7d3879e3cab7873c5bd1f576acff1ce5 (patch)
tree6c8087d3874d740e2a3a5fe5db7465f0c8faf3a3
parent378625a66edd6fd10ffd2fe00582292897440778 (diff)
downloadsphinx-83dafb1d7d3879e3cab7873c5bd1f576acff1ce5.tar.gz
Fix #1381 :rfc: and :pep: roles support custom link text
-rw-r--r--sphinx/roles.py34
-rw-r--r--tests/root/markup.txt2
-rw-r--r--tests/test_build_html.py4
3 files changed, 26 insertions, 14 deletions
diff --git a/sphinx/roles.py b/sphinx/roles.py
index 122c5285..aaf6272b 100644
--- a/sphinx/roles.py
+++ b/sphinx/roles.py
@@ -158,7 +158,7 @@ class XRefRole(object):
return [node], []
-def indexmarkup_role(typ, rawtext, etext, lineno, inliner,
+def indexmarkup_role(typ, rawtext, text, lineno, inliner,
options={}, content=[]):
"""Role for PEP/RFC references that generate an index entry."""
env = inliner.document.settings.env
@@ -166,47 +166,53 @@ def indexmarkup_role(typ, rawtext, etext, lineno, inliner,
typ = env.config.default_role
else:
typ = typ.lower()
- text = utils.unescape(etext)
+ has_explicit_title, title, target = split_explicit_title(text)
+ title = utils.unescape(title)
+ target = utils.unescape(target)
targetid = 'index-%s' % env.new_serialno('index')
indexnode = addnodes.index()
targetnode = nodes.target('', '', ids=[targetid])
inliner.document.note_explicit_target(targetnode)
if typ == 'pep':
indexnode['entries'] = [
- ('single', _('Python Enhancement Proposals; PEP %s') % text,
+ ('single', _('Python Enhancement Proposals; PEP %s') % target,
targetid, '')]
anchor = ''
- anchorindex = text.find('#')
+ anchorindex = target.find('#')
if anchorindex > 0:
- text, anchor = text[:anchorindex], text[anchorindex:]
+ target, anchor = target[:anchorindex], target[anchorindex:]
+ if not has_explicit_title:
+ title = "PEP " + utils.unescape(title)
try:
- pepnum = int(text)
+ pepnum = int(target)
except ValueError:
- msg = inliner.reporter.error('invalid PEP number %s' % text,
+ msg = inliner.reporter.error('invalid PEP number %s' % target,
line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
ref = inliner.document.settings.pep_base_url + 'pep-%04d' % pepnum
- sn = nodes.strong('PEP '+text, 'PEP '+text)
+ sn = nodes.strong(title, title)
rn = nodes.reference('', '', internal=False, refuri=ref+anchor,
classes=[typ])
rn += sn
return [indexnode, targetnode, rn], []
elif typ == 'rfc':
- indexnode['entries'] = [('single', 'RFC; RFC %s' % text, targetid, '')]
+ indexnode['entries'] = [('single', 'RFC; RFC %s' % target, targetid, '')]
anchor = ''
- anchorindex = text.find('#')
+ anchorindex = target.find('#')
if anchorindex > 0:
- text, anchor = text[:anchorindex], text[anchorindex:]
+ target, anchor = target[:anchorindex], target[anchorindex:]
+ if not has_explicit_title:
+ title = "RFC " + utils.unescape(title)
try:
- rfcnum = int(text)
+ rfcnum = int(target)
except ValueError:
- msg = inliner.reporter.error('invalid RFC number %s' % text,
+ msg = inliner.reporter.error('invalid RFC number %s' % target,
line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
ref = inliner.document.settings.rfc_base_url + inliner.rfc_url % rfcnum
- sn = nodes.strong('RFC '+text, 'RFC '+text)
+ sn = nodes.strong(title, title)
rn = nodes.reference('', '', internal=False, refuri=ref+anchor,
classes=[typ])
rn += sn
diff --git a/tests/root/markup.txt b/tests/root/markup.txt
index 7ce721ba..f6f955e2 100644
--- a/tests/root/markup.txt
+++ b/tests/root/markup.txt
@@ -132,7 +132,9 @@ Adding \n to test unescaping.
*Linking inline markup*
* :pep:`8`
+* :pep:`Python Enhancement Proposal #8 <8>`
* :rfc:`1`
+* :rfc:`Request for Comments #1 <1>`
* :envvar:`HOME`
* :keyword:`with`
* :token:`try statement <try_stmt>`
diff --git a/tests/test_build_html.py b/tests/test_build_html.py
index d13c7ac6..17a09eae 100644
--- a/tests/test_build_html.py
+++ b/tests/test_build_html.py
@@ -129,8 +129,12 @@ HTML_XPATH = {
(".//li/code/em/span[@class='pre']", '^i$'),
(".//a[@href='http://www.python.org/dev/peps/pep-0008']"
"[@class='pep reference external']/strong", 'PEP 8'),
+ (".//a[@href='http://www.python.org/dev/peps/pep-0008']"
+ "[@class='pep reference external']/strong", 'Python Enhancement Proposal #8'),
(".//a[@href='http://tools.ietf.org/html/rfc1.html']"
"[@class='rfc reference external']/strong", 'RFC 1'),
+ (".//a[@href='http://tools.ietf.org/html/rfc1.html']"
+ "[@class='rfc reference external']/strong", 'Request for Comments #1'),
(".//a[@href='objects.html#envvar-HOME']"
"[@class='reference internal']/code/span[@class='pre']", 'HOME'),
(".//a[@href='#with']"