|
17 | 17 | #
|
18 | 18 | # SPDX-License-Identifier: MIT
|
19 | 19 |
|
20 |
| -import json |
21 | 20 | import logging
|
22 | 21 | import os
|
| 22 | +import re |
23 | 23 | import subprocess
|
24 | 24 | import sys
|
25 | 25 | import urllib.parse
|
26 | 26 |
|
27 | 27 | import recommonmark
|
| 28 | +from sphinx.transforms import SphinxTransform |
| 29 | +from docutils import nodes |
| 30 | +from sphinx import addnodes |
28 | 31 |
|
29 | 32 | # If extensions (or modules to document with autodoc) are in another directory,
|
30 | 33 | # add these directories to sys.path here. If the directory is relative to the
|
|
84 | 87 | autoapi_add_toctree_entry = False
|
85 | 88 | autoapi_options = ['members', 'undoc-members', 'private-members', 'show-inheritance', 'special-members', 'show-module-summary']
|
86 | 89 | autoapi_template_dir = 'docs/autoapi/templates'
|
| 90 | +autoapi_python_class_content = "both" |
87 | 91 | autoapi_python_use_implicit_namespaces = True
|
88 | 92 | autoapi_root = "shared-bindings"
|
89 | 93 |
|
|
106 | 110 | #
|
107 | 111 | # We don't follow "The short X.Y version" vs "The full version, including alpha/beta/rc tags"
|
108 | 112 | # breakdown, so use the same version identifier for both to avoid confusion.
|
109 |
| -version = release = '0.0.0' |
| 113 | + |
| 114 | +final_version = "" |
| 115 | +git_describe = subprocess.run( |
| 116 | + ["git", "describe", "--dirty", "--tags"], |
| 117 | + stdout=subprocess.PIPE, |
| 118 | + stderr=subprocess.STDOUT, |
| 119 | + encoding="utf-8" |
| 120 | +) |
| 121 | +if git_describe.returncode == 0: |
| 122 | + git_version = re.search( |
| 123 | + r"^\d(?:\.\d){0,2}(?:\-(?:alpha|beta|rc)\.\d+){0,1}", |
| 124 | + str(git_describe.stdout) |
| 125 | + ) |
| 126 | + if git_version: |
| 127 | + final_version = git_version[0] |
| 128 | +else: |
| 129 | + print("Failed to retrieve git version:", git_describe.stdout) |
| 130 | + |
| 131 | +version = release = final_version |
110 | 132 |
|
111 | 133 | # The language for content autogenerated by Sphinx. Refer to documentation
|
112 | 134 | # for a list of supported languages.
|
@@ -423,7 +445,38 @@ def generate_redirects(app):
|
423 | 445 | with open(redirected_filename, 'w') as f:
|
424 | 446 | f.write(TEMPLATE % urllib.parse.quote(to_path, '#/'))
|
425 | 447 |
|
| 448 | + |
| 449 | +class CoreModuleTransform(SphinxTransform): |
| 450 | + default_priority = 870 |
| 451 | + |
| 452 | + def _convert_first_paragraph_into_title(self): |
| 453 | + title = self.document.next_node(nodes.title) |
| 454 | + paragraph = self.document.next_node(nodes.paragraph) |
| 455 | + if not title or not paragraph: |
| 456 | + return |
| 457 | + if isinstance(paragraph[0], nodes.paragraph): |
| 458 | + paragraph = paragraph[0] |
| 459 | + if all(isinstance(child, nodes.Text) for child in paragraph.children): |
| 460 | + for child in paragraph.children: |
| 461 | + title.append(nodes.Text(" \u2013 ")) |
| 462 | + title.append(child) |
| 463 | + paragraph.parent.remove(paragraph) |
| 464 | + |
| 465 | + def _enable_linking_to_nonclass_targets(self): |
| 466 | + for desc in self.document.traverse(addnodes.desc): |
| 467 | + for xref in desc.traverse(addnodes.pending_xref): |
| 468 | + if xref.attributes.get("reftype") == "class": |
| 469 | + xref.attributes.pop("refspecific", None) |
| 470 | + |
| 471 | + def apply(self, **kwargs): |
| 472 | + docname = self.env.docname |
| 473 | + if docname.startswith(autoapi_root) and docname.endswith("/index"): |
| 474 | + self._convert_first_paragraph_into_title() |
| 475 | + self._enable_linking_to_nonclass_targets() |
| 476 | + |
| 477 | + |
426 | 478 | def setup(app):
|
427 | 479 | app.add_css_file("customstyle.css")
|
428 | 480 | app.add_config_value('redirects_file', 'redirects', 'env')
|
429 | 481 | app.connect('builder-inited', generate_redirects)
|
| 482 | + app.add_transform(CoreModuleTransform) |
0 commit comments