from docutils.nodes import literal
from docutils.parsers.rst import roles

from sphinx.roles import XRefRole


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']
            if target.endswith('()'):
                # Function call, :symbol:`mongoc_init()`
                target = target[:-2]

            if ':' in target:
                # E.g., 'bson:bson_t' has domain 'bson', target 'bson_t'
                attrs['domain'], attrs['reftarget'] = target.split(':', 1)
            else:
                attrs['reftarget'] = target

            attrs['reftype'] = 'doc'
            attrs['classes'].append('symbol')
        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,
    }

@@ -27,17 +27,23 @@
  literal
+, Text
 %0A
-
 from doc
@@ -349,16 +349,40 @@
 arget'%5D%0A
+            parens = ''%0A
         
@@ -504,16 +504,46 @@
 get%5B:-2%5D
+%0A                parens = '()'
 %0A%0A      
@@ -676,49 +676,283 @@
 '%5D, 
-attrs%5B'reftarget'%5D = target.split(':', 1)
+name = target.split(':', 1)%0A                attrs%5B'reftarget'%5D = name%0A%0A                assert isinstance(node.children%5B0%5D.children%5B0%5D, Text)%0A                node.children%5B0%5D.children%5B0%5D = Text(name + parens,%0A                                                    name + parens)%0A
 %0A   

update sphinx extension