Skip to content

Commit

Permalink
doc/common/extensions: Add requirements directive.
Browse files Browse the repository at this point in the history
Displays compatibility for each hub depending on its features.
  • Loading branch information
laurensvalk committed Jul 26, 2021
1 parent 1f5eb17 commit cefa782
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/common/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
'sphinx.ext.mathjax',
'color',
'classlink',
'requirements',
]

# Add any paths that contain templates here, relative to this directory.
Expand Down
79 changes: 79 additions & 0 deletions doc/common/extensions/requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from docutils import nodes
from docutils.parsers.rst import Directive


# Base feature set.
FEATURES_SMALL = set()

# Medium feature set.
FEATURES_MEDIUM = FEATURES_SMALL | {
'pybricks-geometry',
'pybricks-iodevices',
'stm32-extra',
'stm32-float',
}

# Large feature set.
FEATURES_LARGE = FEATURES_MEDIUM | set()

# Features per hub.
HUB_FEATURES = {
'movehub': FEATURES_SMALL,
'cityhub': FEATURES_MEDIUM,
'technichub': FEATURES_MEDIUM,
'primehub': FEATURES_LARGE,
'inventorhub': FEATURES_LARGE,
}


class PybricksRequirementsDirective(Directive):

required_arguments = 1
optional_arguments = 10

def run(self):
# Get requirements from sphinx-directive.
requirements = set(self.arguments)

# Check compatibility for all hubs.
compatible = {
hub: requirements <= features
for hub, features in HUB_FEATURES.items()
}

# Table cells with hub images.
hub_cells = ["<th>{0}</th>".format(hub) for hub in compatible]

# Table cells with checkmark or cross.
compat_cells = [
"<td>✔️</td>" if compat else "<td>❌</td>"
for hub, compat in compatible.items()
]

# Generate full table.
html = """
<table>
<thead>
<tr>
{0}
</tr>
</thead>
<tbody>
<tr>
{1}
</tr>
</tbody>
</table>
""".format("".join(hub_cells), "".join(compat_cells))

# Return the node.
node = nodes.raw('', html, format="html")
return [node]


def setup(app):
app.add_directive_to_domain(
'py',
'pybricks-requirements',
PybricksRequirementsDirective
)
3 changes: 1 addition & 2 deletions doc/main/micropython/umath.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
:mod:`umath <umath>` -- Math functions
============================================================

This module is available on the the City Hub, Technic Hub,
Prime Hub, and Inventor Hub.
.. pybricks-requirements:: stm32-extra stm32-float

This MicroPython module is similar to the `math module`_ in Python.

Expand Down

0 comments on commit cefa782

Please sign in to comment.