Debian Codegraph @

docs/conf.py

raw
1 # -*- coding: utf-8 -*-
2 """Sphinx build configuration file."""
3
4 import os
5 import sys
6
7 from sphinx.ext import apidoc
8
9 from docutils import nodes
10 from docutils import transforms
11
12 # Change PYTHONPATH to include dfwinreg module and dependencies.
13 sys.path.insert(0, os.path.abspath('..'))
14
15 import dfwinreg # pylint: disable=wrong-import-position
16
17 import utils.dependencies # pylint: disable=wrong-import-position
18
19
20 # -- General configuration ------------------------------------------------
21
22 # If your documentation needs a minimal Sphinx version, state it here.
23 needs_sphinx = '2.0.1'
24
25 # Add any Sphinx extension module names here, as strings. They can be
26 # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
27 # ones.
28 extensions = [
29 'recommonmark',
30 'sphinx.ext.autodoc',
31 'sphinx.ext.coverage',
32 'sphinx.ext.doctest',
33 'sphinx.ext.napoleon',
34 'sphinx.ext.viewcode',
35 'sphinx_markdown_tables',
36 'sphinx_rtd_theme',
37 ]
38
39 # We cannot install architecture dependent Python modules on readthedocs,
40 # therefore we mock most imports.
41 pip_installed_modules = set()
42
43 dependency_helper = utils.dependencies.DependencyHelper(
44 dependencies_file=os.path.join('..', 'dependencies.ini'),
45 test_dependencies_file=os.path.join('..', 'test_dependencies.ini'))
46 modules_to_mock = set(dependency_helper.dependencies.keys())
47 modules_to_mock = modules_to_mock.difference(pip_installed_modules)
48
49 autodoc_mock_imports = sorted(modules_to_mock)
50
51 # Options for the Sphinx Napoleon extension, which reads Google-style
52 # docstrings.
53 napoleon_google_docstring = True
54 napoleon_numpy_docstring = False
55 napoleon_include_init_with_doc = True
56 napoleon_include_private_with_doc = False
57 napoleon_include_special_with_doc = True
58
59 # General information about the project.
60 # pylint: disable=redefined-builtin
61 project = 'dfWinReg'
62 copyright = 'The dfWinReg authors'
63 version = dfwinreg.__version__
64 release = dfwinreg.__version__
65
66 # Add any paths that contain templates here, relative to this directory.
67 templates_path = ['_templates']
68
69 # List of patterns, relative to source directory, that match files and
70 # directories to ignore when looking for source files.
71 exclude_patterns = ['_build']
72
73 # The master toctree document.
74 master_doc = 'index'
75
76 # The name of the Pygments (syntax highlighting) style to use.
77 pygments_style = 'sphinx'
78
79
80 # -- Options for HTML output ----------------------------------------------
81
82 # The theme to use for HTML and HTML Help pages. See the documentation for
83 # a list of builtin themes.
84 html_theme = 'sphinx_rtd_theme'
85
86 # Output file base name for HTML help builder.
87 htmlhelp_basename = 'dfwinregdoc'
88
89
90 # -- Options linkcheck ----------------------------------------------------
91
92 linkcheck_ignore = [
93 ]
94
95
96 # -- Code to rewrite links for readthedocs --------------------------------
97
98 # This function is a Sphinx core event callback, the format of which is detailed
99 # here: https://www.sphinx-doc.org/en/master/extdev/appapi.html#events
100
101 # pylint: disable=unused-argument
102 def RunSphinxAPIDoc(app):
103 """Runs sphinx-apidoc to auto-generate documentation.
104
105 Args:
106 app (sphinx.application.Sphinx): Sphinx application. Required by the
107 the Sphinx event callback API.
108 """
109 current_directory = os.path.abspath(os.path.dirname(__file__))
110 module_path = os.path.join(current_directory, '..', 'dfwinreg')
111 api_directory = os.path.join(current_directory, 'sources', 'api')
112 apidoc.main(['-o', api_directory, module_path, '--force'])
113
114
115 class MarkdownLinkFixer(transforms.Transform):
116 """Transform definition to parse .md references to internal pages."""
117
118 default_priority = 1000
119
120 _URI_PREFIXES = []
121
122 def _FixLinks(self, node):
123 """Corrects links to .md files not part of the documentation.
124
125 Args:
126 node (docutils.nodes.Node): docutils node.
127
128 Returns:
129 docutils.nodes.Node: docutils node, with correct URIs outside
130 of Markdown pages outside the documentation.
131 """
132 if isinstance(node, nodes.reference) and 'refuri' in node:
133 reference_uri = node['refuri']
134 for uri_prefix in self._URI_PREFIXES:
135 if (reference_uri.startswith(uri_prefix) and not (
136 reference_uri.endswith('.asciidoc') or
137 reference_uri.endswith('.md'))):
138 node['refuri'] = reference_uri + '.md'
139 break
140
141 return node
142
143 def _Traverse(self, node):
144 """Traverses the document tree rooted at node.
145
146 Args:
147 node (docutils.nodes.Node): docutils node.
148 """
149 self._FixLinks(node)
150
151 for child_node in node.children:
152 self._Traverse(child_node)
153
154 # pylint: disable=arguments-differ
155 def apply(self):
156 """Applies this transform on document tree."""
157 self._Traverse(self.document)
158
159
160 # pylint: invalid-name
161 def setup(app):
162 """Called at Sphinx initialization.
163
164 Args:
165 app (sphinx.application.Sphinx): Sphinx application.
166 """
167 # Triggers sphinx-apidoc to generate API documentation.
168 app.connect('builder-inited', RunSphinxAPIDoc)
169 app.add_config_value(
170 'recommonmark_config', {'enable_auto_toc_tree': True}, True)
171 app.add_transform(MarkdownLinkFixer)