Python sphinx.roles.XRefRole() Examples

The following are 2 code examples of sphinx.roles.XRefRole(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sphinx.roles , or try the search function .
Example #1
Source File: numfig.py    From factor with GNU General Public License v2.0 6 votes vote down vote up
def setup(app):
    app.add_config_value('number_figures', True, True)
    app.add_config_value('figure_caption_prefix', "Figure", True)

    app.add_node(page_ref,
                 text=(skip_page_ref, None),
                 html=(skip_page_ref, None),
                 latex=(latex_visit_page_ref, None))

    app.add_role('page', XRefRole(nodeclass=page_ref))

    app.add_node(num_ref,
                 latex=(latex_visit_num_ref, None))

    app.add_role('num', XRefRole(nodeclass=num_ref))

    app.connect("builder-inited", clean_env)
    app.connect('doctree-read', doctree_read)
    app.connect('doctree-resolved', doctree_resolved) 
Example #2
Source File: briandoc.py    From brian2genn with GNU General Public License v2.0 4 votes vote down vote up
def brianobj_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
    '''
    A Sphinx role, used as a wrapper for the default `py:obj` role, allowing
    us to use the simple backtick syntax for brian classes/functions without
    having to qualify the package for classes/functions that are available after
    a `from brian2 import *`, e.g `NeuronGroup`.
    Also allows to directly link to preference names using the same syntax.
    '''
    if text in prefs:
        linktext = text.replace('_', '-').replace('.', '-')
        text = '%s <brian-pref-%s>' % (text, linktext)
        # Use sphinx's cross-reference role
        xref = XRefRole(warn_dangling=True)
        return xref('std:ref', rawtext, text, lineno, inliner, options, content)
    else:
        if text and (not '~' in text):
            try:
                # A simple class or function name
                if not '.' in text:
                    module = __import__('brian2genn', fromlist=[str(text)])
                    imported = getattr(module, str(text), None)
                    if hasattr(imported, '__module__'):
                        text = '~' + imported.__module__ + '.' + text
                        if inspect.isfunction(imported):
                            text += '()'
                # Possibly a method/classmethod/attribute name
                elif len(text.split('.')) == 2:
                    classname, attrname = text.split('.')
                    # Remove trailing parentheses (will be readded for display)
                    if attrname.endswith('()'):
                        attrname = attrname[:-2]
                    module = __import__('brian2genn', fromlist=[str(classname)])
                    imported = getattr(module, str(classname), None)
                    if hasattr(imported, '__module__'):
                        # Add trailing parentheses only for methods not for
                        # attributes
                        if inspect.ismethod(getattr(imported,
                                                    str(attrname),
                                                    None)):
                            parentheses = '()'
                        else:
                            parentheses = ''

                        text = ('{classname}.{attrname}{parentheses} '
                                '<{modname}.{classname}.{attrname}>').format(classname=classname,
                                                                             attrname=attrname,
                                                                             modname=imported.__module__,
                                                                             parentheses=parentheses)

            except ImportError:
                pass
        role = 'py:obj'
        py_role = PyXRefRole()
        return py_role(role, rawtext, text, lineno, inliner, options, content)