Debian Codegraph @

doc/usage/extensions/viewcode.rst

raw
1 :mod:`sphinx.ext.viewcode` -- Add links to highlighted source code
2 ==================================================================
3
4 .. module:: sphinx.ext.viewcode
5 :synopsis: Add links to a highlighted version of the source code.
6 .. moduleauthor:: Georg Brandl
7
8 .. versionadded:: 1.0
9
10 .. role:: code-py(code)
11 :language: Python
12
13 This extension looks at your Python object descriptions (``.. class::``, ``..
14 function::`` etc.) and tries to find the source files where the objects are
15 contained. When found, a separate HTML page will be output for each module with
16 a highlighted version of the source code, and a link will be added to all object
17 descriptions that leads to the source code of the described object. A link back
18 from the source to the description will also be inserted.
19
20 .. warning::
21
22 Basically, ``viewcode`` extension will import the modules being linked to.
23 If any modules have side effects on import, these will be executed when
24 ``sphinx-build`` is run.
25
26 If you document scripts (as opposed to library modules), make sure their
27 main routine is protected by a ``if __name__ == '__main__'`` condition.
28
29 In addition, if you don't want to import the modules by ``viewcode``,
30 you can tell the location of the location of source code to ``viewcode``
31 using the :event:`viewcode-find-source` event.
32
33 If :confval:`viewcode_follow_imported_members` is enabled,
34 you will also need to resolve imported attributes
35 using the :event:`viewcode-follow-imported` event.
36
37 This extension works only on HTML related builders like ``html``,
38 ``applehelp``, ``devhelp``, ``htmlhelp``, ``qthelp`` and so on except
39 ``singlehtml``. By default ``epub`` builder doesn't
40 support this extension (see :confval:`viewcode_enable_epub`).
41
42 Configuration
43 -------------
44
45 .. confval:: viewcode_follow_imported_members
46 :type: :code-py:`bool`
47 :default: :code-py:`True`
48
49 If this is ``True``, viewcode extension will emit
50 :event:`viewcode-follow-imported` event to resolve the name of the module
51 by other extensions.
52
53 .. versionadded:: 1.3
54
55 .. versionchanged:: 1.8
56 Renamed from ``viewcode_import`` to ``viewcode_follow_imported_members``.
57
58 .. confval:: viewcode_enable_epub
59 :type: :code-py:`bool`
60 :default: :code-py:`False`
61
62 If this is ``True``, viewcode extension is also enabled even if you use
63 epub builders. This extension generates pages outside toctree, but this
64 is not preferred as epub format.
65
66 Until 1.4.x, this extension is always enabled. If you want to generate
67 epub as same as 1.4.x, you should set ``True``, but epub format checker's
68 score becomes worse.
69
70 .. versionadded:: 1.5
71
72 .. warning::
73
74 Not all epub readers support pages generated by viewcode extension.
75 These readers ignore links to pages are not under toctree.
76
77 Some reader's rendering result are corrupted and
78 `epubcheck <https://github.com/IDPF/epubcheck>`_'s score
79 becomes worse even if the reader supports.
80
81 .. confval:: viewcode_line_numbers
82 :type: :code-py:`bool`
83 :default: :code-py:`False`
84
85 If set to ``True``, inline line numbers will be added to the highlighted code.
86
87 .. versionadded:: 7.2
88
89 .. event:: viewcode-find-source (app, modname)
90
91 .. versionadded:: 1.8
92
93 Find the source code for a module.
94 An event handler for this event should return
95 a tuple of the source code itself and a dictionary of tags.
96 The dictionary maps the name of a class, function, attribute, etc
97 to a tuple of its type, the start line number, and the end line number.
98 The type should be one of "class", "def", or "other".
99
100 :param app: The Sphinx application object.
101 :param modname: The name of the module to find source code for.
102
103 .. event:: viewcode-follow-imported (app, modname, attribute)
104
105 .. versionadded:: 1.8
106
107 Find the name of the original module for an attribute.
108
109 :param app: The Sphinx application object.
110 :param modname: The name of the module that the attribute belongs to.
111 :param attribute: The name of the member to follow.