From dd8a02d5e1a4fc7103850ed99e395f7db39974bc Mon Sep 17 00:00:00 2001 From: tk0miya Date: Wed, 20 Aug 2014 12:27:08 +0900 Subject: [PATCH] Fix #1381 :rfc: and :pep: roles support custom link text --- sphinx/roles.py | 34 ++++++++++++++++++++-------------- tests/root/markup.txt | 2 ++ tests/test_build_html.py | 4 ++++ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/sphinx/roles.py b/sphinx/roles.py index 122c52853..aaf6272bd 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 7ce721bab..f6f955e24 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 ` diff --git a/tests/test_build_html.py b/tests/test_build_html.py index d13c7ac6f..17a09eae9 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']"