-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.py
More file actions
277 lines (238 loc) · 10.2 KB
/
Copy pathplugin.py
File metadata and controls
277 lines (238 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""Plugin definition for mkdocs-autoapi."""
# built-in imports
import collections
import os
import tempfile
import urllib.parse
from pathlib import Path
from typing import Optional
# third-party imports
from jinja2 import Environment
from mkdocs.config import Config, config_options
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.exceptions import ConfigurationError, PluginError
from mkdocs.plugins import BasePlugin
from mkdocs.structure.files import Files
from mkdocs.structure.nav import Navigation, Section
from mkdocs.structure.pages import Page
# local imports
from mkdocs_autoapi.autoapi import add_autoapi_nav_entry, create_docs
from mkdocs_autoapi.generate_files.editor import FilesEditor
from mkdocs_autoapi.literate_nav import resolve
from mkdocs_autoapi.logging import get_logger
from mkdocs_autoapi.section_index import rewrite
from mkdocs_autoapi.section_index.section_page import SectionPage
logger = get_logger(name="mkdocs-autoapi")
class AutoApiPluginConfig(Config):
"""Configuration options for plugin."""
autoapi_dir = config_options.Dir(exists=True, default=".")
autoapi_file_patterns = config_options.ListOfItems(
config_options.Type(str),
default=["*.py", "*.pyi"],
)
autoapi_ignore = config_options.ListOfItems(
config_options.Type(str), default=[]
)
autoapi_keep_files = config_options.Type(bool, default=False)
autoapi_generate_api_docs = config_options.Type(bool, default=True)
autoapi_add_nav_entry = config_options.Type((str, bool), default=True)
autoapi_root = config_options.Type(str, default="autoapi")
class AutoApiPlugin(BasePlugin[AutoApiPluginConfig]):
"""Plugin logic definition."""
def on_config(self, config: MkDocsConfig) -> Optional[Config]:
"""Validate the plugin configuration.
# Step 1
1. Check if `mkdocstrings` is included in plugin configuration.
2a. If `mkdocstrings` is included, then validate its configuration.
1. Get the `mkdocstrings` configuration object.
2. If `mkdocstrings` is not enabled, then warn the user.
3. Get the `handlers` configuration.
4. Identify the AutoAPI directory. If the value provided by the
user is a Python package, then get the parent directory.
Otherwise, use the provided value.
5. Check if the AutoAPI directory is included in the paths for
each `mkdocstrings` handler. If not, then warn the user.
2b. If `mkdocstrings` is not included, then warn the user.
3. Return.
Args:
config:
The MkDocs configuration object.
Returns:
The validated plugin configuration.
"""
# Step 1
logger.debug(msg="Validating plugin configuration ...")
is_mkdocstrings_included = "mkdocstrings" in list(config.plugins.keys())
# Step 2a
if is_mkdocstrings_included:
# Step 2a.1
mkdocstrings_configuration = config.plugins["mkdocstrings"].config
# Step 2a.2
if not mkdocstrings_configuration.enabled:
logger.warning(
msg="mkdocstrings is not enabled.\n HINT: Set `enabled: True` in mkdocstrings configuration."
)
# Step 2a.3
mkdocstrings_handlers_configuration = (
mkdocstrings_configuration.handlers
)
if mkdocstrings_handlers_configuration == dict():
mkdocstrings_handlers_configuration = {
mkdocstrings_configuration.default_handler: {"paths": ["."]}
}
# Step 2a.4
autoapi_dir = Path(self.config.autoapi_dir).absolute()
if "__init__.py" in os.listdir(autoapi_dir):
autoapi_dir = autoapi_dir.parent.absolute()
# Step 2a.5
mkdocs_yml_dir = Path(config.config_file_path).parent.absolute()
for handler in mkdocstrings_handlers_configuration.keys():
paths = [
Path(
os.path.abspath(os.path.join(mkdocs_yml_dir, p))
).absolute()
for p in mkdocstrings_handlers_configuration[handler][
"paths"
]
]
if autoapi_dir not in paths:
relative_autoapi_dir = os.path.relpath(
path=autoapi_dir,
start=mkdocs_yml_dir,
).replace("\\", "/")
logger.warning(
msg=f'AutoAPI directory not found in paths for `mkdocstrings` handler "{handler}".\n HINT: Add "{relative_autoapi_dir}" to the `paths` list in the `mkdocstrings` handler configuration.'
)
# Step 2a.6
default_handler = mkdocstrings_configuration.default_handler
if default_handler not in ["python", "vba"]:
raise ConfigurationError(
f"mkdocstrings default handler must be one of ['python', 'python-legacy', 'vba'], not: {handler}"
)
# Step 2b
else:
logger.warning(
msg="mkdocstrings is not included in mkdocs configuration.\n HINT: Add `mkdocstrings` to the `plugins` list in mkdocs configuration file."
)
# Step 3
return config
def on_files(self, files: Files, config: MkDocsConfig) -> Files:
"""Generate autoAPI documentation files.
Steps:
1. Create a temporary directory to store the generated files.
2. Ignore the virtual environment from the documentation if it is
not already ignored.
3. Create the autoAPI documentation files.
4. Store the paths of the generated files.
5. Return the updated files object.
Args:
files:
The MkDocs files object.
config:
The MkDocs configuration.
Returns:
The updated MkDocs files object.
"""
# Step 1
self._dir = tempfile.TemporaryDirectory(
prefix="autoapi",
)
config.update(self.config)
# Step 2
if "venv/**/*" not in self.config.autoapi_ignore:
self.config.autoapi_ignore.append("venv/**/*")
if ".venv/**/*" not in self.config.autoapi_ignore:
self.config.autoapi_ignore.append(".venv/**/*")
# Step 4
with FilesEditor(
files=files,
config=config,
directory=self._dir.name,
) as editor:
try:
if self.config.autoapi_generate_api_docs:
create_docs(config=config)
elif self.config.autoapi_add_nav_entry:
add_autoapi_nav_entry(config=config)
logger.debug(msg="Added AutoAPI section to navigation.")
except Exception as e:
raise PluginError(str(e))
# Step 5
self._edit_paths = dict(editor.edit_paths)
# Step 6
markdown_extensions = config.markdown_extensions
markdown_config = {
"markdown_extensions": markdown_extensions,
"extension_configs": config["mdx_configs"],
"tab_length": 4,
}
# Step 7
config.nav = resolve.resolve_directories_in_nav(
nav_data=config.nav,
files=editor.files,
nav_file_name="summary.md",
implicit_index=False,
markdown_config=markdown_config,
)
self._files = editor.files
# Step 8
return editor.files
def on_nav(self, nav: Navigation, config, files) -> Navigation:
"""Apply plugin-specific transformations to the navigation."""
todo = collections.deque((nav.items,))
while todo:
items = todo.popleft()
for i, section in enumerate(items):
if not isinstance(section, Section) or not section.children:
continue
todo.append(section.children)
page = section.children[0]
if not isinstance(page, Page):
continue
assert not page.children
if not page.title and page.url:
page.__class__ = SectionPage
assert isinstance(page, SectionPage)
page.is_section = page.is_page = True
page.title = section.title
page.parent = section.parent
section.children.pop(0)
page.children = section.children
for child in page.children:
child.parent = page
items[i] = page
self._nav = nav
return nav
def on_env(self, env: Environment, config, files) -> Environment:
"""Apply plugin-specific transformations to the Jinja environment."""
assert env.loader is not None
env.loader = self._loader = rewrite.TemplateRewritingLoader(env.loader)
return env
def on_page_context(self, context, page, config, nav):
"""Apply plugin-specific transformations to a page's context."""
if nav != self._nav:
self._nav = nav
def on_page_content(
self,
html: str,
page: Page,
config: MkDocsConfig,
files: Files,
) -> str:
"""Apply plugin-specific transformations to a page's content."""
if self.config.autoapi_generate_api_docs:
repo_url = config.repo_url
edit_uri = config.edit_uri
src_path = page.file.src_uri
if src_path in self._edit_paths:
path = self._edit_paths.pop(src_path)
if repo_url and edit_uri:
if not edit_uri.startswith(
("?", "#")
) and not repo_url.endswith("/"):
repo_url += "/"
page.edit_url = path and urllib.parse.urljoin(
base=urllib.parse.urljoin(base=repo_url, url=edit_uri),
url=path,
)
return html