| 1 |
"""Configuration file for the Sphinx documentation builder.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import datetime |
| 6 |
import os |
| 7 |
import re |
| 8 |
import shutil |
| 9 |
import typing as t |
| 10 |
from importlib.metadata import metadata |
| 11 |
from importlib.util import find_spec |
| 12 |
from pathlib import Path |
| 13 |
|
| 14 |
import sphinx_rtd_theme |
| 15 |
|
| 16 |
if t.TYPE_CHECKING: |
| 17 |
import sphinx.ext.autodoc |
| 18 |
from sphinx.application import Sphinx |
| 19 |
|
| 20 |
|
| 21 |
needs_sphinx = "3.1" |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
NOT_LOADED_MSGS = [] |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
project = "rstcheck_core" |
| 30 |
author = "Steven Myint <[email protected]>" |
| 31 |
GH_REPO_LINK = "https://github.com/rstcheck/rstcheck-core" |
| 32 |
CREATION_YEAR = 2013 |
| 33 |
CURRENT_YEAR = f"{datetime.datetime.now(tz=datetime.UTC).date().year}" |
| 34 |
copyright = ( |
| 35 |
f"{CREATION_YEAR}{('-' + CURRENT_YEAR) if CURRENT_YEAR != CREATION_YEAR else ''}, " |
| 36 |
f"{author} and AUTHORS" |
| 37 |
) |
| 38 |
RSTCHECK_CORE_VERSION = metadata(project)["Version"] |
| 39 |
release = RSTCHECK_CORE_VERSION |
| 40 |
version_parts = re.search(r"^v?(?P<version>\d+\.\d+)\.\d+[-.]?(?P<tag>[a-z]*)[\.]?\d*", release) |
| 41 |
|
| 42 |
version = None if not version_parts else version_parts.group("version") |
| 43 |
|
| 44 |
RELEASE_LEVEL = None if not version_parts else version_parts.group("tag") |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
extensions: list[str] = [] |
| 49 |
today_fmt = "%Y-%m-%d" |
| 50 |
exclude_patterns: list[str] = [] |
| 51 |
|
| 52 |
|
| 53 |
html_static_path = ["_static"] if Path("_static").exists() else [] |
| 54 |
templates_path = ["_templates"] if Path("_templates").exists() else [] |
| 55 |
|
| 56 |
rst_prolog = """ |
| 57 |
.. ifconfig:: RELEASE_LEVEL in ('alpha', 'beta', 'rc') |
| 58 |
|
| 59 |
.. warning:: |
| 60 |
The here documented release |release| is a pre-release. |
| 61 |
Keep in mind that breaking changes can occur till the final release. |
| 62 |
|
| 63 |
You may want to use the latest stable release instead. |
| 64 |
""" |
| 65 |
|
| 66 |
rst_epilog = """ |
| 67 |
.. |br| raw:: html |
| 68 |
|
| 69 |
<br/> |
| 70 |
""" |
| 71 |
|
| 72 |
tls_cacerts = os.getenv("SSL_CERT_FILE") |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
extensions.append("m2r2") |
| 77 |
source_suffix = [".rst", ".md"] |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
linkcheck_workers = 1 |
| 83 |
linkcheck_retries = 5 |
| 84 |
linkcheck_timeout = 30 |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
extensions.append("sphinx.ext.duration") |
| 90 |
extensions.append("sphinx.ext.coverage") |
| 91 |
coverage_write_headline = False |
| 92 |
coverage_show_missing_items = True |
| 93 |
extensions.append("sphinx.ext.doctest") |
| 94 |
|
| 95 |
|
| 96 |
extensions.append("sphinx.ext.autosectionlabel") |
| 97 |
autosectionlabel_prefix_document = True |
| 98 |
autosectionlabel_maxdepth = 2 |
| 99 |
extensions.append("sphinx.ext.ifconfig") |
| 100 |
extensions.append("sphinx.ext.viewcode") |
| 101 |
|
| 102 |
|
| 103 |
extensions.append("sphinx.ext.intersphinx") |
| 104 |
|
| 105 |
intersphinx_mapping = { |
| 106 |
"python": ("https://docs.python.org/3/", None), |
| 107 |
"pattern": ("https://docs.python.org/3/library/", "objects.pattern.inv"), |
| 108 |
"pydantic": ("https://docs.pydantic.dev/latest/", "objects.pydantic.inv"), |
| 109 |
"sphinx": ("https://www.sphinx-doc.org/en/master/extdev/", "objects.sphinx.inv"), |
| 110 |
} |
| 111 |
|
| 112 |
extensions.append("sphinx.ext.extlinks") |
| 113 |
extlinks = { |
| 114 |
"repo": (f"{GH_REPO_LINK}/%s", "Repo's %s"), |
| 115 |
"issue": (f"{GH_REPO_LINK}/issues/%s", "#%s"), |
| 116 |
"pull": (f"{GH_REPO_LINK}/pull/%s", "PR#%s"), |
| 117 |
"user": ("https://github.com/%s", "@%s"), |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
apidoc_module_dir = f"../../src/{project}/" |
| 123 |
apidoc_output_dir = "autoapidoc" |
| 124 |
apidoc_toc_file = False |
| 125 |
apidoc_separate_modules = False |
| 126 |
apidoc_module_first = True |
| 127 |
apidoc_extra_args = [] |
| 128 |
if Path("_apidoc_templates").is_dir(): |
| 129 |
apidoc_extra_args += ["--templatedir", "apidoc_templates"] |
| 130 |
autoclass_content = "both" |
| 131 |
|
| 132 |
|
| 133 |
if find_spec("sphinxcontrib.apidoc") is not None: |
| 134 |
extensions.append("sphinxcontrib.apidoc") |
| 135 |
if Path(apidoc_output_dir).is_dir(): |
| 136 |
shutil.rmtree(apidoc_output_dir) |
| 137 |
else: |
| 138 |
NOT_LOADED_MSGS.append("## 'sphinxcontrib-apidoc' extension not loaded - not installed") |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
extensions.append("sphinx.ext.autodoc") |
| 143 |
autodoc_typehints = "description" |
| 144 |
autodoc_member_order = "bysource" |
| 145 |
autodoc_mock_imports: list[str] = [] |
| 146 |
autodoc_default_options = {"members": True} |
| 147 |
|
| 148 |
|
| 149 |
def _remove_module_docstring( |
| 150 |
app: Sphinx, |
| 151 |
what: str, |
| 152 |
name: str, |
| 153 |
obj: t.Any, |
| 154 |
options: sphinx.ext.autodoc.Options, |
| 155 |
lines: list[str], |
| 156 |
) -> None: |
| 157 |
"""Remove module docstring.""" |
| 158 |
if what == "module": |
| 159 |
del lines[:] |
| 160 |
|
| 161 |
|
| 162 |
if find_spec("sphinx_autodoc_typehints") is not None: |
| 163 |
extensions.append("sphinx_autodoc_typehints") |
| 164 |
else: |
| 165 |
NOT_LOADED_MSGS.append("## 'sphinx-autodoc-typehints' extension not loaded - not installed") |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
spelling_word_list_filename = "spelling_dict.txt" |
| 170 |
spelling_show_suggestions = True |
| 171 |
spelling_exclude_patterns = ["autoapi/**", "autoapidoc/**"] |
| 172 |
|
| 173 |
if find_spec("sphinxcontrib.spelling") is not None and os.environ.get("SPHINX_SPELLING") == "true": |
| 174 |
extensions.append("sphinxcontrib.spelling") |
| 175 |
else: |
| 176 |
NOT_LOADED_MSGS.append("## 'sphinxcontrib-spelling' extension not loaded - not installed") |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
extensions.append("sphinx_rtd_theme") |
| 182 |
html_theme = "sphinx_rtd_theme" |
| 183 |
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] |
| 184 |
html_theme_options = {"style_external_links": True, "navigation_depth": 5} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
html_last_updated_fmt = today_fmt |
| 189 |
html_show_sourcelink = True |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
def setup(app: Sphinx) -> None: |
| 194 |
"""Connect custom func to sphinx events.""" |
| 195 |
app.connect("autodoc-process-docstring", _remove_module_docstring) |
| 196 |
|
| 197 |
app.add_config_value("RELEASE_LEVEL", "", "env") |
| 198 |
|
| 199 |
|
| 200 |
for msg in NOT_LOADED_MSGS: |
| 201 |
print(msg) |