doc: boards: extensions: store board_catalog in Sphinx domain

Load the board catalog only once at build-inited time so that
it's cached and enabled things like boards, vendors, and socs
to eventually become first class Sphinx objects.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
This commit is contained in:
Benjamin Cabé 2024-10-17 14:48:06 +02:00 committed by Alberto Escolar
parent f75f2bf5d9
commit 534990249d
3 changed files with 23 additions and 11 deletions

View file

@ -578,13 +578,16 @@ class BoardCatalogDirective(SphinxDirective):
if self.env.app.builder.format == "html":
self.env.domaindata["zephyr"]["has_board_catalog"][self.env.docname] = True
# As it is not expected that more than one board-catalog directive is used across
# the documentation, and since the generation is only taking a few seconds, we don't
# store the catalog in the domain data. It might change in the future if the generation
# becomes more expensive.
board_catalog = get_catalog()
domain_data = self.env.domaindata["zephyr"]
renderer = SphinxRenderer([TEMPLATES_DIR])
rendered = renderer.render("board-catalog.html", {"catalog": board_catalog})
rendered = renderer.render(
"board-catalog.html",
{
"boards": domain_data["boards"],
"vendors": domain_data["vendors"],
"socs": domain_data["socs"],
},
)
return [nodes.raw("", rendered, format="html")]
else:
return [nodes.paragraph(text="Board catalog is only available in HTML.")]
@ -804,6 +807,13 @@ def install_static_assets_as_needed(
app.add_js_file("js/board-catalog.js")
def load_board_catalog_into_domain(app: Sphinx) -> None:
board_catalog = get_catalog()
app.env.domaindata["zephyr"]["boards"] = board_catalog["boards"]
app.env.domaindata["zephyr"]["vendors"] = board_catalog["vendors"]
app.env.domaindata["zephyr"]["socs"] = board_catalog["socs"]
def setup(app):
app.add_config_value("zephyr_breathe_insert_related_samples", False, "env")
@ -820,6 +830,8 @@ def setup(app):
"builder-inited",
(lambda app: app.config.html_static_path.append(RESOURCES_DIR.as_posix())),
)
app.connect("builder-inited", load_board_catalog_into_domain)
app.connect("html-page-context", install_static_assets_as_needed)
app.connect("env-updated", compute_sample_categories_hierarchy)

View file

@ -16,7 +16,7 @@
data-vendor="{{ board.vendor }}"
data-socs="{{ board.socs | join(" ") }}"
tabindex="0">
<div class="vendor">{{ catalog.vendors[board.vendor] }}</div>
<div class="vendor">{{ vendors[board.vendor] }}</div>
{% if board.image -%}
<img alt="A picture of the {{ board.full_name }} board"
src="../_images/{{ board.image.split('/')[-1] }}" class="picture" />

View file

@ -38,8 +38,8 @@
<option value="" disabled selected>Select a vendor</option>
{# Only show those vendors that have actual boards in the catalog.
Note: as sorting per vendor name is not feasible in Jinja, the option list is sorted in the JavaScript code later #}
{% for vendor in (catalog.boards | items | map(attribute='1.vendor') | unique ) -%}
<option value="{{ vendor }}">{{ catalog.vendors[vendor] }}</option>
{% for vendor in (boards | items | map(attribute='1.vendor') | unique ) -%}
<option value="{{ vendor }}">{{ vendors[vendor] }}</option>
{% endfor %}
</select>
</div>
@ -82,11 +82,11 @@
<div id="nb-matches" style="text-align: center"></div>
<div id="catalog">
{% for board_name, board in catalog.boards | items | sort(attribute='1.full_name') -%}
{% for board_name, board in boards | items | sort(attribute='1.full_name') -%}
{% include "board-card.html" %}
{% endfor %}
</div>
<script>
socs_data = {{ catalog.socs | tojson }};
socs_data = {{ socs | tojson }};
</script>