1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
from docutils.nodes import literal, Text
from docutils.parsers.rst import roles
from sphinx.roles import XRefRole
from sphinx import version_info as sphinx_version_info
class SymbolRole(XRefRole):
def __call__(self, *args, **kwargs):
nodes, messages = XRefRole.__call__(self, *args, **kwargs)
for node in nodes:
attrs = node.attributes
target = attrs['reftarget']
parens = ''
if target.endswith('()'):
# Function call, :symbol:`mongoc_init()`
target = target[:-2]
parens = '()'
if ':' in target:
# E.g., 'bson:bson_t' has domain 'bson', target 'bson_t'
attrs['domain'], name = target.split(':', 1)
attrs['reftarget'] = name
old = node.children[0].children[0]
assert isinstance(old, Text)
new = Text(name + parens, name + parens)
# Ensure setup_child is called.
node.children[0].replace(old, new)
else:
attrs['reftarget'] = target
attrs['reftype'] = 'doc'
attrs['classes'].append('symbol')
if sphinx_version_info >= (1, 6):
# https://github.com/sphinx-doc/sphinx/issues/3698
attrs['refdomain'] = 'std'
return nodes, messages
def setup(app):
roles.register_local_role(
'symbol', SymbolRole(warn_dangling=True, innernodeclass=literal))
return {
'version': '1.0',
'parallel_read_safe': True,
'parallel_write_safe': True,
}
|