Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 75 additions & 3 deletions docs/badges_buttons.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,83 @@ The syntax is the same as for the `ref` role.
````
`````

(sd-custom-badge-roles)=

### Custom badge roles

When the **same** badge recurs throughout your documentation — status labels
such as *stable*, *beta* or *deprecated* whose colour and tooltip never change —
define it **once** as a custom role via the `sd_custom_roles` configuration
option, rather than retyping the colour and tooltip at every use site. This is
the recommended pattern for such repeated, semantic badges:

```python
sd_custom_roles = {
"bdg-stable": {
"inherit": "bdg-success",
"tooltip": "A released, supported version",
},
"bdg-beta": {"inherit": "bdg-warning", "tooltip": "Interface may change"},
"bdg-deprecated": {"inherit": "bdg-danger", "tooltip": "Scheduled for removal"},
}
```

Each key is the new role name to register; the value is a dictionary with:

- `inherit` (required): the built-in badge role to inherit from — any of the
`bdg`, `bdg-<color>`, `bdg-<color>-line`, `bdg-link-*` or `bdg-ref-*` roles.
- `tooltip` (optional): a default tooltip, applied as the HTML `title` whenever
the role is used *without* a per-instance `; tooltip` suffix. For a
`bdg-link-*`/`bdg-ref-*` role the baked tooltip applies even to the bare
form (`` {bdg-link-*}`<target>` ``), where the suffix grammar deliberately
refuses to split, and it overrides a reference's automatic title.

Give custom roles a distinguishing prefix (the `bdg-` convention used here is a
good choice): a name that collides with a docutils built-in (`code`, `math`,
`strong`, …) or another extension's role is skipped, so `bdg-`-prefixed names
keep you clear of every such clash.

The new role then behaves exactly like the role it inherits (same rendering,
links and references), with its tooltip baked in:

{bdg-stable}`v2.1` {bdg-beta}`v3.0rc1` {bdg-deprecated}`v1.0`

````{tab-set-code}
```markdown
{bdg-stable}`v2.1` {bdg-beta}`v3.0rc1` {bdg-deprecated}`v1.0`
```
```rst
:bdg-stable:`v2.1` :bdg-beta:`v3.0rc1` :bdg-deprecated:`v1.0`
```
````

A baked `tooltip` is only the *default*: a per-instance `; tooltip` suffix on an
individual badge **overrides** it, so a config-defined role covers the common
case while the suffix stays available for the occasional one-off:

{bdg-stable}`v2.1 ; Long-term support release`

````{tab-set-code}
```markdown
{bdg-stable}`v2.1 ; Long-term support release`
```
```rst
:bdg-stable:`v2.1 ; Long-term support release`
```
````

Custom roles are the badge analogue of
{ref}`custom directives <sd-custom-directives>`. Inheritance is limited to the
badge family listed above; a role whose `inherit` names an unknown badge role,
or whose own name clashes with an existing role, is skipped with a warning.
Editing an entry (or removing it) is picked up on the next build.

### Badge tooltips

Any badge can be given a tooltip (shown on hover, via the HTML `title`
attribute) by appending a `; tooltip` suffix to its text.
This works for every badge family:
For a **one-off** tooltip (shown on hover, via the HTML `title` attribute) —
rather than a repeated one better served by a
{ref}`custom badge role <sd-custom-badge-roles>` — append a `; tooltip` suffix
to any badge's text. This works for every badge family:

{bdg-primary}`stable ; A released, supported version`
{bdg-link-info}`docs <https://example.com> ; Opens the documentation`
Expand Down
8 changes: 8 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
},
}
}
sd_custom_roles = {
"bdg-stable": {
"inherit": "bdg-success",
"tooltip": "A released, supported version",
},
"bdg-beta": {"inherit": "bdg-warning", "tooltip": "Interface may change"},
"bdg-deprecated": {"inherit": "bdg-danger", "tooltip": "Scheduled for removal"},
}

extlinks = {
"pr": ("https://github.com/executablebooks/sphinx-design/pull/%s", "PR #%s"),
Expand Down
6 changes: 6 additions & 0 deletions docs/get_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ sd_hide_title: true
:::
::::

(sd-custom-directives)=

### Creating custom directives

:::{versionadded} 0.6.0
Expand All @@ -86,6 +88,10 @@ The key is the new directive name to add, and the value is a dictionary with the
- `argument`: The default argument (optional, only for directives that take a single argument)
- `options`: A dictionary of default options for the directive (optional)

The same idea is available for roles: `sd_custom_roles` defines reusable badge
roles (e.g. status badges), inheriting a built-in badge role and optionally
baking in a tooltip. See {ref}`Custom badge roles <sd-custom-badge-roles>`.

## Supported browsers

sphinx-design targets [**Baseline** Widely Available](https://web.dev/baseline)
Expand Down
193 changes: 161 additions & 32 deletions sphinx_design/badges_buttons.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
from collections.abc import Sequence
from collections.abc import Iterator, Sequence
import re
from typing import Any

from docutils import nodes
from docutils.parsers.rst import directives
from docutils.parsers.rst import roles as rst_roles
from docutils.parsers.rst.states import Inliner, RSTState
from docutils.utils import unescape
from sphinx import addnodes
from sphinx.application import Sphinx
from sphinx.config import Config
from sphinx.transforms.post_transforms import SphinxPostTransform
from sphinx.util import ws_re
from sphinx.util.docutils import ReferenceRole, SphinxRole
from sphinx.util.logging import getLogger

from sphinx_design.config import WARNING_TYPE, SdConfig
from sphinx_design.shared import SEMANTIC_COLORS, SdDirective, make_choice, text_align

LOGGER = getLogger(__name__)

ROLE_NAME_BADGE_PREFIX = "bdg"
ROLE_NAME_LINK_PREFIX = "bdg-link"
ROLE_NAME_REF_PREFIX = "bdg-ref"
Expand All @@ -25,28 +31,51 @@
# in particular for rounded-pill class etc


def setup_badges_and_buttons(app: Sphinx) -> None:
"""Setup the badge components."""
app.add_role(ROLE_NAME_BADGE_PREFIX, BadgeRole())
app.add_role(ROLE_NAME_LINK_PREFIX, LinkBadgeRole())
app.add_role(ROLE_NAME_REF_PREFIX, XRefBadgeRole())
#: A single built-in badge role, as ``(name, role_class, color, outline)``.
BadgeRoleSpec = tuple[str, type["_TooltipRoleMixin"], "str | None", bool]


def _iter_badge_roles() -> Iterator[BadgeRoleSpec]:
"""Yield the ``(name, role_class, color, outline)`` of every built-in badge role.

This is the single source of truth for the badge role family, used both to
register the roles (:func:`setup_badges_and_buttons`) and to resolve an
``sd_custom_roles`` ``inherit`` target (:func:`setup_custom_roles`).

.. note::

The custom-role ``inherit`` mechanism is deliberately limited to this
badge family in v1. Extending it to the icon roles (``icons.py``) or the
button *roles* would mean yielding those here too, and giving their role
constructors the same ``tooltip`` keyword the badge roles now accept.
"""
yield ROLE_NAME_BADGE_PREFIX, BadgeRole, None, False
yield ROLE_NAME_LINK_PREFIX, LinkBadgeRole, None, False
yield ROLE_NAME_REF_PREFIX, XRefBadgeRole, None, False
for color in SEMANTIC_COLORS:
app.add_role("-".join((ROLE_NAME_BADGE_PREFIX, color)), BadgeRole(color))
app.add_role(
"-".join((ROLE_NAME_BADGE_PREFIX, color, "line")),
BadgeRole(color, outline=True),
)
app.add_role("-".join((ROLE_NAME_LINK_PREFIX, color)), LinkBadgeRole(color))
app.add_role(
yield "-".join((ROLE_NAME_BADGE_PREFIX, color)), BadgeRole, color, False
yield "-".join((ROLE_NAME_BADGE_PREFIX, color, "line")), BadgeRole, color, True
yield "-".join((ROLE_NAME_LINK_PREFIX, color)), LinkBadgeRole, color, False
yield (
"-".join((ROLE_NAME_LINK_PREFIX, color, "line")),
LinkBadgeRole(color, outline=True),
LinkBadgeRole,
color,
True,
)
app.add_role("-".join((ROLE_NAME_REF_PREFIX, color)), XRefBadgeRole(color))
app.add_role(
yield "-".join((ROLE_NAME_REF_PREFIX, color)), XRefBadgeRole, color, False
yield (
"-".join((ROLE_NAME_REF_PREFIX, color, "line")),
XRefBadgeRole(color, outline=True),
XRefBadgeRole,
color,
True,
)


def setup_badges_and_buttons(app: Sphinx) -> None:
"""Setup the badge components."""
for name, role_cls, color, outline in _iter_badge_roles():
app.add_role(name, role_cls(color, outline=outline))

app.add_node(
sd_badge,
html=(visit_sd_badge_html, depart_sd_badge_html),
Expand All @@ -73,6 +102,91 @@ def setup_badges_and_buttons(app: Sphinx) -> None:
app.add_post_transform(BadgeRefTooltipGraft)


#: Marker attribute set on the role instances this extension registers from
#: ``sd_custom_roles``. docutils' role registry (``roles._roles``) is
#: process-global and shared across Sphinx apps, so carrying "ownership" on the
#: stored role object itself lets every ``config-inited`` reconcile *our* roles
#: (refresh or unregister) without a side table that could drift from the
#: registry - e.g. across the many apps a test session builds.
_SD_CUSTOM_ROLE_ATTR = "_sd_custom_role"


def _role_name_taken(name: str) -> bool:
"""Return whether ``name`` already resolves to a role.

docutils looks roles up case-insensitively, so the lower-cased name is
compared against both the locally registered roles (``roles._roles``, the
registry Sphinx's own ``add_role`` consults) *and* docutils' canonical
built-in roles (``roles._role_registry``: ``code``/``math``/``strong``/
``emphasis``/``literal``/... ). Both are long-stable private attributes;
docutils exposes no public query for either.
"""
key = name.lower()
return key in rst_roles._roles or key in rst_roles._role_registry


def setup_custom_roles(app: Sphinx, config: Config) -> None:
"""Register (and reconcile) the custom roles declared in ``sd_custom_roles``.

Parallel to :func:`sphinx_design.shared.setup_custom_directives`: each entry
inherits a built-in sphinx-design *badge* role (v1 scope; see
:func:`_iter_badge_roles`), optionally baking in a default ``tooltip`` used
whenever no per-instance ``; suffix`` overrides it.

The entry shapes have already been validated on ``config-inited``
(see ``sphinx_design.config``); here we additionally check ``inherit``
against the known badge roles. Because it runs on every ``config-inited``
against a process-global registry, the reconciliation policy is:

* a name we registered before is **refreshed** (re-registered with override,
picking up tooltip/inherit edits on an in-process rebuild);
* a name we registered before but now absent from the config is
**unregistered**, so it never leaks into a later build or sibling app;
* a name clashing with a *foreign* role (another extension's, or a docutils
built-in such as ``code``/``strong``) is skipped with a warning.
"""

def _warn(msg: str) -> None:
LOGGER.warning(f"sd_custom_roles: {msg}", type=WARNING_TYPE, subtype="config")

specs = {
name: (role_cls, color, outline)
for name, role_cls, color, outline in _iter_badge_roles()
}
# extension point: to also inherit icon/button roles, yield them from
# ``_iter_badge_roles`` (and give those constructors a ``tooltip`` keyword).

# decide the roles to (re)register: a known ``inherit``, and a name that is
# either already ours (refresh) or free (a foreign clash is skipped)
desired: dict[str, tuple[str, dict[str, Any]]] = {} # lower name -> (name, data)
for name, data in SdConfig.from_sphinx(config).custom_roles.items():
inherit = data["inherit"]
if inherit not in specs:
_warn(f"'{name}.inherit' is an unknown badge role: {inherit}")
continue
existing = rst_roles._roles.get(name.lower())
if not getattr(existing, _SD_CUSTOM_ROLE_ATTR, False) and _role_name_taken(
name
):
_warn(f"'{name}' clashes with an existing role, skipping")
continue
desired[name.lower()] = (name, data)

# unregister any of *our* previously-registered roles dropped from the config
# (the registry is process-global, so this is what stops a stale role
# resolving in a later in-process build or a sibling app)
for key, role in list(rst_roles._roles.items()):
if getattr(role, _SD_CUSTOM_ROLE_ATTR, False) and key not in desired:
del rst_roles._roles[key]

# (re)register, refreshing tooltip/inherit changes via ``override=True``
for name, data in desired.values():
role_cls, color, outline = specs[data["inherit"]]
role = role_cls(color, outline=outline, tooltip=data.get("tooltip"))
setattr(role, _SD_CUSTOM_ROLE_ATTR, True)
app.add_role(name, role, override=True)


def create_bdg_classes(color: str | None, outline: bool) -> list[str]:
"""Create the badge classes."""
classes = [
Expand Down Expand Up @@ -231,6 +345,10 @@ class _TooltipRoleMixin(SphinxRole):
The parsed tooltip (or ``None``) is stored on :attr:`tooltip`, and the
remaining text - with the suffix removed - is forwarded to the base role,
so that ``ReferenceRole``'s title/target parsing still applies to it.

The badge roles share this constructor (``color``/``outline``, plus an
optional baked ``tooltip``), so that they - and any config-defined roles
inheriting them (see :func:`setup_custom_roles`) - are built uniformly.
"""

#: the tooltip parsed from the current role invocation, if any
Expand All @@ -241,6 +359,28 @@ class _TooltipRoleMixin(SphinxRole):
#: form is a target in which ``;`` is a legal character)
tooltip_requires_explicit_target: bool = False

def __init__(
self,
color: str | None = None,
*,
outline: bool = False,
tooltip: str | None = None,
) -> None:
"""Construct a badge role.

:param color: the semantic colour, or ``None`` for the plain badge.
:param outline: whether to use the outline (``-line``) variant.
:param tooltip: a baked-in default tooltip, used when no per-instance
``; suffix`` overrides it (set by config-defined roles). An explicit
suffix always wins. ``None`` for the built-in roles, keeping their
output byte-identical.
"""
super().__init__()
self.color = color
self.outline = outline
#: the baked default tooltip (see the ``tooltip`` parameter)
self.default_tooltip = tooltip

def __call__( # noqa: PLR0913, PLR0917
self,
name: str,
Expand Down Expand Up @@ -278,17 +418,16 @@ def __call__( # noqa: PLR0913, PLR0917
# resolve raw (MyST-style) ``\;`` escapes in the forwarded text; a
# no-op for rST, whose escapes are NUL-encoded and resolved downstream
text = _RAW_ESCAPED_SEMICOLON_RE.sub(";", text)
if self.tooltip is None:
# no per-instance suffix: fall back to any baked default (``None``
# for the built-in roles). A suffix, when present, always wins.
self.tooltip = self.default_tooltip
return super().__call__(name, rawtext, text, lineno, inliner, options, content)


class BadgeRole(_TooltipRoleMixin):
"""Role to display a badge."""

def __init__(self, color: str | None = None, *, outline: bool = False) -> None:
super().__init__()
self.color = color
self.outline = outline

def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]:
"""Run the role."""
node = sd_badge(
Expand All @@ -308,11 +447,6 @@ class LinkBadgeRole(_TooltipRoleMixin, ReferenceRole):
# ``;`` is legal in URLs: tooltips only after ``text <target>``
tooltip_requires_explicit_target = True

def __init__(self, color: str | None = None, *, outline: bool = False) -> None:
super().__init__()
self.color = color
self.outline = outline

def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]:
"""Run the role."""
node = nodes.reference(
Expand All @@ -335,11 +469,6 @@ class XRefBadgeRole(_TooltipRoleMixin, ReferenceRole):
# ``;`` is legal in reference targets: tooltips only after ``text <target>``
tooltip_requires_explicit_target = True

def __init__(self, color: str | None = None, *, outline: bool = False) -> None:
super().__init__()
self.color = color
self.outline = outline

def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]:
"""Run the role."""
options = {
Expand Down
Loading
Loading