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 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
from docutils import nodes
from sphinx.util.nodes import make_refnode
MATCH_RE_RAW = r'\b(([A-Z][A-Za-z0-9]+)+)\b'
def setup(app):
app.connect('missing-reference', _missing_ref)
def _missing_ref(app, env, node, contnode):
# skip non-elements
if not isinstance(contnode, nodes.Element):
return
if node.get('refdomain') != 'py':
return
options = env.domains['py'].find_obj(
env, None, None, node.get('reftarget'), node.get('reftype'), 1)
if not options:
return
is_raw = node.get('py:module').startswith('gssapi.raw')
if len(options) > 1:
raw_opts = []
non_raw_opts = []
for opt in options:
type_info = opt[1]
mod_name = type_info.docname
if mod_name.startswith('gssapi.raw'):
raw_opts.append(opt)
else:
non_raw_opts.append(opt)
if is_raw:
if raw_opts:
choice = raw_opts[0]
elif non_raw_opts:
choice = non_raw_opts[0]
else:
return
else:
if non_raw_opts:
choice = non_raw_opts[0]
elif raw_opts:
choice = raw_opts[0]
else:
return
else:
choice = options[0]
choice_name = choice[0]
choice_info = choice[1]
choice_mod = choice_info.node_id
choice_type = choice_info.objtype
if choice_type == 'module':
return env.domains['py']._make_module_refnode(
app.builder, node.get('refdoc'), choice_name, contnode)
else:
return make_refnode(app.builder, node.get('refdoc'), choice_mod,
choice_name, contnode, choice_name)
|