Debian Codegraph @

docs/conf.py

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