Skip to content

Instantly share code, notes, and snippets.

@zopieux
Created March 28, 2018 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zopieux/0dfb6b8d1182987721b43b0e96fb9495 to your computer and use it in GitHub Desktop.
Save zopieux/0dfb6b8d1182987721b43b0e96fb9495 to your computer and use it in GitHub Desktop.
Sphinx citation title
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