| 1 |
from docutils import nodes |
| 2 |
from docutils.parsers.rst import directives |
| 3 |
from sphinx import addnodes |
| 4 |
from sphinx.application import Sphinx |
| 5 |
from sphinx.util.docutils import ReferenceRole, SphinxRole |
| 6 |
|
| 7 |
from sphinx_design.shared import SEMANTIC_COLORS, SdDirective, make_choice, text_align |
| 8 |
|
| 9 |
ROLE_NAME_BADGE_PREFIX = "bdg" |
| 10 |
ROLE_NAME_LINK_PREFIX = "bdg-link" |
| 11 |
ROLE_NAME_REF_PREFIX = "bdg-ref" |
| 12 |
DIRECTIVE_NAME_BUTTON_LINK = "button-link" |
| 13 |
DIRECTIVE_NAME_BUTTON_REF = "button-ref" |
| 14 |
|
| 15 |
# TODO defining arbitrary classes for badges |
| 16 |
# (maybe split text right of last `;`, then split that by comma) |
| 17 |
# in particular for rounded-pill class etc |
| 18 |
|
| 19 |
|
| 20 |
def setup_badges_and_buttons(app: Sphinx) -> None: |
| 21 |
"""Setup the badge components.""" |
| 22 |
app.add_role(ROLE_NAME_BADGE_PREFIX, BadgeRole()) |
| 23 |
app.add_role(ROLE_NAME_LINK_PREFIX, LinkBadgeRole()) |
| 24 |
app.add_role(ROLE_NAME_REF_PREFIX, XRefBadgeRole()) |
| 25 |
for color in SEMANTIC_COLORS: |
| 26 |
app.add_role("-".join((ROLE_NAME_BADGE_PREFIX, color)), BadgeRole(color)) |
| 27 |
app.add_role( |
| 28 |
"-".join((ROLE_NAME_BADGE_PREFIX, color, "line")), |
| 29 |
BadgeRole(color, outline=True), |
| 30 |
) |
| 31 |
app.add_role("-".join((ROLE_NAME_LINK_PREFIX, color)), LinkBadgeRole(color)) |
| 32 |
app.add_role( |
| 33 |
"-".join((ROLE_NAME_LINK_PREFIX, color, "line")), |
| 34 |
LinkBadgeRole(color, outline=True), |
| 35 |
) |
| 36 |
app.add_role("-".join((ROLE_NAME_REF_PREFIX, color)), XRefBadgeRole(color)) |
| 37 |
app.add_role( |
| 38 |
"-".join((ROLE_NAME_REF_PREFIX, color, "line")), |
| 39 |
XRefBadgeRole(color, outline=True), |
| 40 |
) |
| 41 |
|
| 42 |
app.add_directive(DIRECTIVE_NAME_BUTTON_LINK, ButtonLinkDirective) |
| 43 |
app.add_directive(DIRECTIVE_NAME_BUTTON_REF, ButtonRefDirective) |
| 44 |
|
| 45 |
|
| 46 |
def create_bdg_classes(color: str | None, outline: bool) -> list[str]: |
| 47 |
"""Create the badge classes.""" |
| 48 |
classes = [ |
| 49 |
"sd-sphinx-override", |
| 50 |
"sd-badge", |
| 51 |
] |
| 52 |
if color is None: |
| 53 |
return classes |
| 54 |
if outline: |
| 55 |
classes.extend([f"sd-outline-{color}", f"sd-text-{color}"]) |
| 56 |
else: |
| 57 |
classes.extend([f"sd-bg-{color}", f"sd-bg-text-{color}"]) |
| 58 |
return classes |
| 59 |
|
| 60 |
|
| 61 |
class BadgeRole(SphinxRole): |
| 62 |
"""Role to display a badge.""" |
| 63 |
|
| 64 |
def __init__(self, color: str | None = None, *, outline: bool = False) -> None: |
| 65 |
super().__init__() |
| 66 |
self.color = color |
| 67 |
self.outline = outline |
| 68 |
|
| 69 |
def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]: |
| 70 |
"""Run the role.""" |
| 71 |
node = nodes.inline( |
| 72 |
self.rawtext, |
| 73 |
self.text, |
| 74 |
classes=create_bdg_classes(self.color, self.outline), |
| 75 |
) |
| 76 |
self.set_source_info(node) |
| 77 |
return [node], [] |
| 78 |
|
| 79 |
|
| 80 |
class LinkBadgeRole(ReferenceRole): |
| 81 |
"""Role to display a badge with an external link.""" |
| 82 |
|
| 83 |
def __init__(self, color: str | None = None, *, outline: bool = False) -> None: |
| 84 |
super().__init__() |
| 85 |
self.color = color |
| 86 |
self.outline = outline |
| 87 |
|
| 88 |
def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]: |
| 89 |
"""Run the role.""" |
| 90 |
node = nodes.reference( |
| 91 |
self.rawtext, |
| 92 |
refuri=self.target, |
| 93 |
classes=create_bdg_classes(self.color, self.outline), |
| 94 |
) |
| 95 |
# TODO open in new tab |
| 96 |
self.set_source_info(node) |
| 97 |
# if self.target != self.title: |
| 98 |
# node["reftitle"] = self.target |
| 99 |
node += nodes.inline(self.title, self.title) |
| 100 |
return [node], [] |
| 101 |
|
| 102 |
|
| 103 |
class XRefBadgeRole(ReferenceRole): |
| 104 |
"""Role to display a badge with an internal link.""" |
| 105 |
|
| 106 |
def __init__(self, color: str | None = None, *, outline: bool = False) -> None: |
| 107 |
super().__init__() |
| 108 |
self.color = color |
| 109 |
self.outline = outline |
| 110 |
|
| 111 |
def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]: |
| 112 |
"""Run the role.""" |
| 113 |
options = { |
| 114 |
"classes": create_bdg_classes(self.color, self.outline), |
| 115 |
"reftarget": self.target, |
| 116 |
"refdoc": self.env.docname, |
| 117 |
"refdomain": "", |
| 118 |
"reftype": "any", |
| 119 |
"refexplicit": self.has_explicit_title, |
| 120 |
"refwarn": True, |
| 121 |
} |
| 122 |
node = addnodes.pending_xref(self.rawtext, **options) |
| 123 |
self.set_source_info(node) |
| 124 |
node += nodes.inline(self.title, self.title, classes=["xref", "any"]) |
| 125 |
return [node], [] |
| 126 |
|
| 127 |
|
| 128 |
class _ButtonDirective(SdDirective): |
| 129 |
"""A base button directive.""" |
| 130 |
|
| 131 |
required_arguments = 1 |
| 132 |
optional_arguments = 0 |
| 133 |
final_argument_whitespace = True |
| 134 |
has_content = True |
| 135 |
option_spec = { |
| 136 |
"color": make_choice(SEMANTIC_COLORS), |
| 137 |
"outline": directives.flag, |
| 138 |
"align": text_align, |
| 139 |
# expand to fit parent width |
| 140 |
"expand": directives.flag, |
| 141 |
# make parent also clickable |
| 142 |
"click-parent": directives.flag, |
| 143 |
"tooltip": directives.unchanged_required, |
| 144 |
"shadow": directives.flag, |
| 145 |
# ref button only |
| 146 |
"ref-type": make_choice(["any", "ref", "doc", "myst"]), |
| 147 |
"class": directives.class_option, |
| 148 |
} |
| 149 |
|
| 150 |
def create_ref_node( |
| 151 |
self, rawtext: str, target: str, explicit_title: bool, classes: list[str] |
| 152 |
) -> nodes.Node: |
| 153 |
"""Create the reference node.""" |
| 154 |
raise NotImplementedError |
| 155 |
|
| 156 |
def run_with_defaults(self) -> list[nodes.Node]: |
| 157 |
rawtext = self.arguments[0] |
| 158 |
target = directives.uri(rawtext) |
| 159 |
classes = ["sd-sphinx-override", "sd-btn", "sd-text-wrap"] |
| 160 |
if "color" in self.options: |
| 161 |
if "outline" in self.options: |
| 162 |
classes.append(f"sd-btn-outline-{self.options['color']}") |
| 163 |
else: |
| 164 |
classes.append(f"sd-btn-{self.options['color']}") |
| 165 |
if "click-parent" in self.options: |
| 166 |
classes.append("sd-stretched-link") |
| 167 |
if "shadow" in self.options: |
| 168 |
classes.append("sd-shadow-sm") |
| 169 |
if "class" in self.options: |
| 170 |
classes.extend(self.options["class"]) |
| 171 |
node = self.create_ref_node(rawtext, target, bool(self.content), classes) |
| 172 |
# TODO open in new tab |
| 173 |
self.set_source_info(node) |
| 174 |
if "tooltip" in self.options: |
| 175 |
node["reftitle"] = self.options["tooltip"] # TODO escape HTML |
| 176 |
|
| 177 |
if self.content: |
| 178 |
textnodes, _ = self.state.inline_text( |
| 179 |
"\n".join(self.content), self.lineno + self.content_offset |
| 180 |
) |
| 181 |
content = nodes.inline("", "") |
| 182 |
content.extend(textnodes) |
| 183 |
else: |
| 184 |
content = nodes.inline(target, target) |
| 185 |
node.append(content) |
| 186 |
|
| 187 |
if "expand" in self.options: |
| 188 |
grid_container = nodes.inline(classes=["sd-d-grid"]) |
| 189 |
self.set_source_info(grid_container) |
| 190 |
grid_container += node |
| 191 |
node = grid_container |
| 192 |
|
| 193 |
# `visit_reference` requires that a reference be inside a `TextElement` parent |
| 194 |
container = nodes.paragraph(classes=self.options.get("align", [])) |
| 195 |
self.set_source_info(container) |
| 196 |
container += node |
| 197 |
|
| 198 |
return [container] |
| 199 |
|
| 200 |
|
| 201 |
class ButtonLinkDirective(_ButtonDirective): |
| 202 |
"""A button directive with an external link.""" |
| 203 |
|
| 204 |
def create_ref_node( |
| 205 |
self, rawtext: str, target: str, explicit_title: bool, classes: list[str] |
| 206 |
) -> nodes.Node: |
| 207 |
"""Create the reference node.""" |
| 208 |
return nodes.reference( |
| 209 |
rawtext, |
| 210 |
refuri=target, |
| 211 |
classes=classes, |
| 212 |
) |
| 213 |
|
| 214 |
|
| 215 |
class ButtonRefDirective(_ButtonDirective): |
| 216 |
"""A button directive with an internal link.""" |
| 217 |
|
| 218 |
def create_ref_node( |
| 219 |
self, rawtext: str, target: str, explicit_title: bool, classes: list[str] |
| 220 |
) -> nodes.Node: |
| 221 |
"""Create the reference node.""" |
| 222 |
ref_type = self.options.get("ref-type", "any") |
| 223 |
options = { |
| 224 |
# TODO the presence of classes raises an error if the link cannot be found |
| 225 |
"classes": classes, |
| 226 |
"reftarget": target, |
| 227 |
"refdoc": self.env.docname, |
| 228 |
"refdomain": "std" if ref_type in {"ref", "doc"} else "", |
| 229 |
"reftype": ref_type, |
| 230 |
"refexplicit": explicit_title, |
| 231 |
"refwarn": True, |
| 232 |
} |
| 233 |
return addnodes.pending_xref(rawtext, **options) |