-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically generate API overview documentation #61994
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,8 @@ | |
|
||
@brief Modem APIs | ||
@defgroup modem Modem APIs | ||
@since 3.5 | ||
@version 0.1.0 | ||
@{ | ||
@} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
# Copyright (c) 2023 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import doxmlparser | ||
|
||
from docutils import nodes | ||
from doxmlparser.compound import DoxCompoundKind | ||
from pathlib import Path | ||
from sphinx.application import Sphinx | ||
from sphinx.util.docutils import SphinxDirective | ||
from typing import Any, Dict | ||
|
||
|
||
class ApiOverview(SphinxDirective): | ||
""" | ||
This is a Zephyr directive to generate a table containing an overview | ||
of all APIs. This table will show the API name, version and since which | ||
version it is present - all information extracted from Doxygen XML output. | ||
|
||
It is exclusively used by the doc/develop/api/overview.rst page. | ||
|
||
Configuration options: | ||
|
||
api_overview_doxygen_xml_dir: Doxygen xml output directory | ||
api_overview_doxygen_base_url: Doxygen base html directory | ||
""" | ||
|
||
def run(self): | ||
return [self.env.api_overview_table] | ||
|
||
|
||
def get_group(innergroup, all_groups): | ||
try: | ||
return [ | ||
g | ||
for g in all_groups | ||
if g.get_compounddef()[0].get_id() == innergroup.get_refid() | ||
][0] | ||
except IndexError as e: | ||
raise Exception(f"Unexpected group {innergroup.get_refid()}") from e | ||
|
||
|
||
def visit_group(app, group, all_groups, rows, indent=0): | ||
version = since = "" | ||
github_uri = "https://github.com/zephyrproject-rtos/zephyr/releases/tag/" | ||
cdef = group.get_compounddef()[0] | ||
|
||
ssects = [ | ||
s for p in cdef.get_detaileddescription().get_para() for s in p.get_simplesect() | ||
] | ||
for sect in ssects: | ||
if sect.get_kind() == "since": | ||
since = sect.get_para()[0].get_valueOf_() | ||
elif sect.get_kind() == "version": | ||
version = sect.get_para()[0].get_valueOf_() | ||
|
||
if since: | ||
since_url = nodes.inline() | ||
reference = nodes.reference(text=f"v{since.strip()}.0", refuri=f"{github_uri}/v{since.strip()}.0") | ||
reference.attributes["internal"] = True | ||
since_url += reference | ||
else: | ||
since_url = nodes.Text("") | ||
|
||
url_base = Path(app.config.api_overview_doxygen_base_url) | ||
url = url_base / f"{cdef.get_id()}.html" | ||
|
||
title = cdef.get_title() | ||
|
||
row_node = nodes.row() | ||
|
||
# Next entry will contain the spacer and the link with API name | ||
entry = nodes.entry() | ||
span = nodes.Text("".join(["\U000000A0"] * indent)) | ||
entry += span | ||
|
||
# API name with link | ||
inline = nodes.inline() | ||
reference = nodes.reference(text=title, refuri=str(url)) | ||
reference.attributes["internal"] = True | ||
inline += reference | ||
entry += inline | ||
row_node += entry | ||
|
||
version_node = nodes.Text(version) | ||
# Finally, add version and since | ||
for cell in [version_node, since_url]: | ||
entry = nodes.entry() | ||
entry += cell | ||
row_node += entry | ||
rows.append(row_node) | ||
|
||
for innergroup in cdef.get_innergroup(): | ||
visit_group( | ||
app, get_group(innergroup, all_groups), all_groups, rows, indent + 6 | ||
) | ||
|
||
|
||
def parse_xml_dir(dir_name): | ||
groups = [] | ||
root = doxmlparser.index.parse(Path(dir_name) / "index.xml", True) | ||
for compound in root.get_compound(): | ||
if compound.get_kind() == DoxCompoundKind.GROUP: | ||
file_name = Path(dir_name) / f"{compound.get_refid()}.xml" | ||
groups.append(doxmlparser.compound.parse(file_name, True)) | ||
|
||
return groups | ||
|
||
|
||
def generate_table(app, toplevel, groups): | ||
table = nodes.table() | ||
tgroup = nodes.tgroup() | ||
|
||
thead = nodes.thead() | ||
thead_row = nodes.row() | ||
for header_name in ["API", "Version", "Available in Zephyr Since"]: | ||
colspec = nodes.colspec() | ||
tgroup += colspec | ||
|
||
entry = nodes.entry() | ||
entry += nodes.Text(header_name) | ||
thead_row += entry | ||
thead += thead_row | ||
tgroup += thead | ||
|
||
rows = [] | ||
tbody = nodes.tbody() | ||
for t in toplevel: | ||
visit_group(app, t, groups, rows) | ||
tbody.extend(rows) | ||
tgroup += tbody | ||
|
||
table += tgroup | ||
|
||
return table | ||
|
||
|
||
def sync_contents(app: Sphinx) -> None: | ||
if app.config.doxyrunner_outdir: | ||
doxygen_out_dir = Path(app.config.doxyrunner_outdir) | ||
else: | ||
doxygen_out_dir = Path(app.outdir) / "_doxygen" | ||
|
||
doxygen_xml_dir = doxygen_out_dir / "xml" | ||
groups = parse_xml_dir(doxygen_xml_dir) | ||
|
||
toplevel = [ | ||
g | ||
for g in groups | ||
if g.get_compounddef()[0].get_id() | ||
not in [ | ||
i.get_refid() | ||
for h in [j.get_compounddef()[0].get_innergroup() for j in groups] | ||
for i in h | ||
] | ||
] | ||
|
||
app.builder.env.api_overview_table = generate_table(app, toplevel, groups) | ||
|
||
|
||
def setup(app) -> Dict[str, Any]: | ||
app.add_config_value("api_overview_doxygen_xml_dir", "html/doxygen/xml", "env") | ||
app.add_config_value("api_overview_doxygen_base_url", "../../doxygen/html", "env") | ||
|
||
app.add_directive("api-overview-table", ApiOverview) | ||
|
||
app.connect("builder-inited", sync_contents) | ||
|
||
return { | ||
"version": "0.1", | ||
"parallel_read_safe": True, | ||
"parallel_write_safe": True, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when version (... or rather status? :)) is not set, should we by default show the same value as the parent group?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, as there's a risk of implying something is more stable than it is.