Debian Codegraph @

doc/usage/extensions/linkcode.rst

raw
1 :mod:`sphinx.ext.linkcode` -- Add external links to source code
2 ===============================================================
3
4 .. module:: sphinx.ext.linkcode
5 :synopsis: Add external links to source code.
6 .. moduleauthor:: Pauli Virtanen
7
8 .. versionadded:: 1.2
9
10 .. role:: code-py(code)
11 :language: Python
12
13 This extension looks at your object descriptions (``.. class::``,
14 ``.. function::`` etc.) and adds external links to code hosted
15 somewhere on the web. The intent is similar to the
16 ``sphinx.ext.viewcode`` extension, but assumes the source code can be
17 found somewhere on the Internet.
18
19 In your configuration, you need to specify a :confval:`linkcode_resolve`
20 function that returns an URL based on the object.
21
22
23 Configuration
24 -------------
25
26 .. confval:: linkcode_resolve
27 :type: :code-py:`Callable[[str, dict[str, str]], str | None] | None`
28 :default: :code-py:`None`
29
30 This is a function ``linkcode_resolve(domain, info)``,
31 which should return the URL to source code corresponding to
32 the object in given domain with given information.
33
34 The function should return ``None`` if no link is to be added.
35
36 The argument ``domain`` specifies the language domain the object is
37 in. ``info`` is a dictionary with the following keys guaranteed to
38 be present (dependent on the domain):
39
40 - ``py``: ``module`` (name of the module), ``fullname`` (name of the object)
41 - ``c``: ``names`` (list of names for the object)
42 - ``cpp``: ``names`` (list of names for the object)
43 - ``javascript``: ``object`` (name of the object), ``fullname``
44 (name of the item)
45
46 Example:
47
48 .. code-block:: python
49
50 def linkcode_resolve(domain, info):
51 if domain != 'py':
52 return None
53 if not info['module']:
54 return None
55 filename = info['module'].replace('.', '/')
56 return "https://somesite/sourcerepo/%s.py" % filename
57
58
59 Third-party domains
60 -------------------
61
62 Support for other domains can be added by extensions with
63 :py:func:`.add_linkcode_domain()`.
64 For example, a Sphinx extension that provides a ``php`` domain
65 could use the following code to support :mod:`~sphinx.ext.linkcode`:
66
67 .. code-block:: python
68
69 from sphinx.ext.linkcode import add_linkcode_domain
70
71 def setup(app):
72 add_linkcode_domain('php', ['namespace', 'class', 'fullname'])
73
74 .. autofunction:: add_linkcode_domain