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