|
4 | 4 | import re |
5 | 5 |
|
6 | 6 | import anyascii |
| 7 | +from docutils.parsers.rst import convert_directive_function |
7 | 8 | from jinja2 import Environment, FileSystemLoader, TemplateNotFound |
8 | 9 | import sphinx |
9 | 10 | import sphinx.util |
|
12 | 13 | from sphinx.util.osutil import ensuredir |
13 | 14 | import sphinx.util.logging |
14 | 15 |
|
15 | | -from ..settings import API_ROOT, TEMPLATE_DIR |
| 16 | +from ..settings import API_ROOT, TEMPLATE_DIR, SINGLE_PAGE_LEVELS |
16 | 17 |
|
17 | 18 | LOGGER = sphinx.util.logging.getLogger(__name__) |
18 | 19 |
|
@@ -306,23 +307,67 @@ def create_class(self, data, options=None, **kwargs): |
306 | 307 | """ |
307 | 308 | raise NotImplementedError |
308 | 309 |
|
| 310 | + def output_child_rst(self, obj, obj_parent, detail_dir, single_page_level, source_suffix): |
| 311 | + |
| 312 | + if not obj.display: |
| 313 | + return |
| 314 | + |
| 315 | + obj_child_page_level = SINGLE_PAGE_LEVELS.index(obj.type) |
| 316 | + desired_page_level = SINGLE_PAGE_LEVELS.index(single_page_level) |
| 317 | + needs_single_page = obj_child_page_level <= desired_page_level |
| 318 | + if not needs_single_page: |
| 319 | + return |
| 320 | + |
| 321 | + obj_child_rst = obj.render( |
| 322 | + needs_single_page=needs_single_page, |
| 323 | + ) |
| 324 | + if not obj_child_rst: |
| 325 | + return |
| 326 | + |
| 327 | + ensuredir(os.path.join(detail_dir, obj.short_name)) |
| 328 | + path = os.path.join( |
| 329 | + detail_dir, obj.short_name, f"index{source_suffix}" |
| 330 | + ) |
| 331 | + |
| 332 | + with open(path, "wb+") as obj_child_detail_file: |
| 333 | + obj_child_detail_file.write(obj_child_rst.encode("utf-8")) |
| 334 | + |
| 335 | + for obj_child in obj.children: |
| 336 | + child_detail_dir = os.path.join(detail_dir, obj.name) |
| 337 | + self.output_child_rst(obj_child, obj, child_detail_dir, single_page_level, source_suffix) |
| 338 | + |
| 339 | + |
309 | 340 | def output_rst(self, root, source_suffix): |
| 341 | + # Evaluate which object types should render in a single page |
| 342 | + single_page_level = self.app.config.autoapi_single_page_level |
| 343 | + desired_page_level = SINGLE_PAGE_LEVELS.index(single_page_level) |
| 344 | + single_page_objects = SINGLE_PAGE_LEVELS[:desired_page_level+1] |
| 345 | + |
310 | 346 | for _, obj in status_iterator( |
311 | 347 | self.objects.items(), |
312 | 348 | colorize("bold", "[AutoAPI] ") + "Rendering Data... ", |
313 | 349 | length=len(self.objects), |
314 | 350 | verbosity=1, |
315 | 351 | stringify_func=(lambda x: x[0]), |
316 | 352 | ): |
317 | | - rst = obj.render() |
| 353 | + if not obj.display: |
| 354 | + continue |
| 355 | + |
| 356 | + rst = obj.render(single_page_objects=single_page_objects) |
318 | 357 | if not rst: |
319 | 358 | continue |
320 | 359 |
|
321 | 360 | detail_dir = obj.include_dir(root=root) |
322 | 361 | ensuredir(detail_dir) |
323 | 362 | path = os.path.join(detail_dir, f"index{source_suffix}") |
| 363 | + |
324 | 364 | with open(path, "wb+") as detail_file: |
325 | 365 | detail_file.write(rst.encode("utf-8")) |
| 366 | + |
| 367 | + for obj_child in obj.children: |
| 368 | + self.output_child_rst(obj_child, obj, detail_dir=detail_dir, |
| 369 | + single_page_level=single_page_level, |
| 370 | + source_suffix=source_suffix) |
326 | 371 |
|
327 | 372 | if self.app.config.autoapi_add_toctree_entry: |
328 | 373 | self._output_top_rst(root) |
|
0 commit comments