Fix #1381 :rfc: and :pep: roles support custom link text

This commit is contained in:
tk0miya 2014-08-20 12:27:08 +09:00
parent f14f77688a
commit dd8a02d5e1
3 changed files with 26 additions and 14 deletions

View File

@ -158,7 +158,7 @@ class XRefRole(object):
return [node], [] return [node], []
def indexmarkup_role(typ, rawtext, etext, lineno, inliner, def indexmarkup_role(typ, rawtext, text, lineno, inliner,
options={}, content=[]): options={}, content=[]):
"""Role for PEP/RFC references that generate an index entry.""" """Role for PEP/RFC references that generate an index entry."""
env = inliner.document.settings.env env = inliner.document.settings.env
@ -166,47 +166,53 @@ def indexmarkup_role(typ, rawtext, etext, lineno, inliner,
typ = env.config.default_role typ = env.config.default_role
else: else:
typ = typ.lower() 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') targetid = 'index-%s' % env.new_serialno('index')
indexnode = addnodes.index() indexnode = addnodes.index()
targetnode = nodes.target('', '', ids=[targetid]) targetnode = nodes.target('', '', ids=[targetid])
inliner.document.note_explicit_target(targetnode) inliner.document.note_explicit_target(targetnode)
if typ == 'pep': if typ == 'pep':
indexnode['entries'] = [ indexnode['entries'] = [
('single', _('Python Enhancement Proposals; PEP %s') % text, ('single', _('Python Enhancement Proposals; PEP %s') % target,
targetid, '')] targetid, '')]
anchor = '' anchor = ''
anchorindex = text.find('#') anchorindex = target.find('#')
if anchorindex > 0: 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: try:
pepnum = int(text) pepnum = int(target)
except ValueError: except ValueError:
msg = inliner.reporter.error('invalid PEP number %s' % text, msg = inliner.reporter.error('invalid PEP number %s' % target,
line=lineno) line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg) prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg] return [prb], [msg]
ref = inliner.document.settings.pep_base_url + 'pep-%04d' % pepnum 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, rn = nodes.reference('', '', internal=False, refuri=ref+anchor,
classes=[typ]) classes=[typ])
rn += sn rn += sn
return [indexnode, targetnode, rn], [] return [indexnode, targetnode, rn], []
elif typ == 'rfc': elif typ == 'rfc':
indexnode['entries'] = [('single', 'RFC; RFC %s' % text, targetid, '')] indexnode['entries'] = [('single', 'RFC; RFC %s' % target, targetid, '')]
anchor = '' anchor = ''
anchorindex = text.find('#') anchorindex = target.find('#')
if anchorindex > 0: 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: try:
rfcnum = int(text) rfcnum = int(target)
except ValueError: except ValueError:
msg = inliner.reporter.error('invalid RFC number %s' % text, msg = inliner.reporter.error('invalid RFC number %s' % target,
line=lineno) line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg) prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg] return [prb], [msg]
ref = inliner.document.settings.rfc_base_url + inliner.rfc_url % rfcnum 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, rn = nodes.reference('', '', internal=False, refuri=ref+anchor,
classes=[typ]) classes=[typ])
rn += sn rn += sn

View File

@ -132,7 +132,9 @@ Adding \n to test unescaping.
*Linking inline markup* *Linking inline markup*
* :pep:`8` * :pep:`8`
* :pep:`Python Enhancement Proposal #8 <8>`
* :rfc:`1` * :rfc:`1`
* :rfc:`Request for Comments #1 <1>`
* :envvar:`HOME` * :envvar:`HOME`
* :keyword:`with` * :keyword:`with`
* :token:`try statement <try_stmt>` * :token:`try statement <try_stmt>`

View File

@ -129,8 +129,12 @@ HTML_XPATH = {
(".//li/code/em/span[@class='pre']", '^i$'), (".//li/code/em/span[@class='pre']", '^i$'),
(".//a[@href='http://www.python.org/dev/peps/pep-0008']" (".//a[@href='http://www.python.org/dev/peps/pep-0008']"
"[@class='pep reference external']/strong", 'PEP 8'), "[@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']" (".//a[@href='http://tools.ietf.org/html/rfc1.html']"
"[@class='rfc reference external']/strong", 'RFC 1'), "[@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']" (".//a[@href='objects.html#envvar-HOME']"
"[@class='reference internal']/code/span[@class='pre']", 'HOME'), "[@class='reference internal']/code/span[@class='pre']", 'HOME'),
(".//a[@href='#with']" (".//a[@href='#with']"