Created
March 28, 2018 13:22
-
-
Save zopieux/0dfb6b8d1182987721b43b0e96fb9495 to your computer and use it in GitHub Desktop.
Sphinx citation title
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import docutils.nodes | |
import sphinx.roles | |
class CiteRole(sphinx.roles.XRefRole): | |
def result_nodes(self, document, env, node, is_ref): | |
keys = node['reftarget'].split(',') | |
refnodes = [ | |
docutils.nodes.reference(classes=['citation'], refuri=key) | |
for key in keys] | |
return refnodes, [] | |
def process_citation_references(app, doctree, docname): | |
mapping = {} | |
for node in doctree.traverse(docutils.nodes.citation): | |
mapping[node[0].astext()] = node[1][:] | |
for node in doctree.traverse(docutils.nodes.reference): | |
if node['classes'] == ['citation']: | |
idx = node.parent.index(node) | |
node.parent[idx:idx] = mapping[node['refuri']] | |
def setup(app): | |
app.add_role("citename", CiteRole()) | |
app.connect("doctree-resolved", process_citation_references) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment