diff options
| author | Georg Brandl <georg@python.org> | 2012-10-28 18:21:57 +0100 |
|---|---|---|
| committer | Georg Brandl <georg@python.org> | 2012-10-28 18:21:57 +0100 |
| commit | bda6110fe0420798fe20adb2e3110afcff8be187 (patch) | |
| tree | 962a3f649bcebc3d8f5c6a97b6ffa9ffab21d41f /sphinx | |
| parent | dca15200c3185ad18b42f771c6f2431e5bfa162e (diff) | |
| parent | bb8d2307d28803acab6f1d3aab7e83dcd0d114b9 (diff) | |
| download | sphinx-bda6110fe0420798fe20adb2e3110afcff8be187.tar.gz | |
Merged in pv/sphinx-work/ext-linkcode (pull request #47)
Diffstat (limited to 'sphinx')
| -rw-r--r-- | sphinx/ext/linkcode.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/sphinx/ext/linkcode.py b/sphinx/ext/linkcode.py new file mode 100644 index 00000000..ffe5b9c0 --- /dev/null +++ b/sphinx/ext/linkcode.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- +""" + sphinx.ext.linkcode + ~~~~~~~~~~~~~~~~~~~ + + Add external links to module code in Python object descriptions. + + :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +from docutils import nodes + +from sphinx import addnodes +from sphinx.locale import _ +from sphinx.errors import SphinxError + +class LinkcodeError(SphinxError): + category = "linkcode error" + +def doctree_read(app, doctree): + env = app.builder.env + + resolve_target = getattr(env.config, 'linkcode_resolve', None) + if not callable(env.config.linkcode_resolve): + raise LinkcodeError( + "Function `linkcode_resolve` is not given in conf.py") + + domain_keys = dict( + py=['module', 'fullname'], + c=['names'], + cpp=['names'], + js=['object', 'fullname'], + ) + + for objnode in doctree.traverse(addnodes.desc): + domain = objnode.get('domain') + uris = set() + for signode in objnode: + if not isinstance(signode, addnodes.desc_signature): + continue + + # Convert signode to a specified format + info = {} + for key in domain_keys.get(domain, []): + value = signode.get(key) + if not value: + value = '' + info[key] = value + if not info: + continue + + # Call user code to resolve the link + uri = resolve_target(domain, info) + if not uri: + # no source + continue + + if uri in uris or not uri: + # only one link per name, please + continue + uris.add(uri) + + onlynode = addnodes.only(expr='html') + onlynode += nodes.reference('', '', internal=False, refuri=uri) + onlynode[0] += nodes.inline('', _('[source]'), + classes=['viewcode-link']) + signode += onlynode + +def setup(app): + app.connect('doctree-read', doctree_read) + app.add_config_value('linkcode_resolve', None, 'env') |
