File: dropdown.py

package info (click to toggle)
sphinx-design 0.6.1-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 10,680 kB
  • sloc: python: 2,086; xml: 900; javascript: 56; sh: 8; makefile: 3
file content (237 lines) | stat: -rw-r--r-- 8,007 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""Originally Adapted from sphinxcontrib.details.directive"""

from docutils import nodes
from docutils.parsers.rst import directives
from sphinx.application import Sphinx
from sphinx.transforms.post_transforms import SphinxPostTransform

from sphinx_design.shared import (
    SEMANTIC_COLORS,
    SdDirective,
    create_component,
    is_component,
    make_choice,
    margin_option,
)

from ._compat import findall
from .icons import get_octicon, list_octicons


def setup_dropdown(app: Sphinx) -> None:
    app.add_node(dropdown_main, html=(visit_dropdown_main, depart_dropdown_main))
    app.add_node(dropdown_title, html=(visit_dropdown_title, depart_dropdown_title))
    app.add_directive("dropdown", DropdownDirective)
    app.add_post_transform(DropdownHtmlTransform)


class dropdown_main(nodes.Element, nodes.General):  # noqa: N801
    pass


class dropdown_title(nodes.TextElement, nodes.General):  # noqa: N801
    pass


def visit_dropdown_main(self, node):
    if node.get("opened"):
        self.body.append(self.starttag(node, "details", open="open"))
    else:
        self.body.append(self.starttag(node, "details"))


def depart_dropdown_main(self, node):
    self.body.append("</details>")


def visit_dropdown_title(self, node):
    self.body.append(self.starttag(node, "summary"))


def depart_dropdown_title(self, node):
    self.body.append("</summary>")


class DropdownDirective(SdDirective):
    """A directive to generate a collapsible container.

    Note: This directive generates a single container,
    for the title (optional) and content::

        <container design_component="dropdown" has_title=True>
            <rubric>
                ...title nodes
        ...content nodes

    This allows for a default rendering in non-HTML outputs.

    The ``DropdownHtmlTransform`` then transforms this container
    into the HTML specific structure.
    """

    optional_arguments = 1  # title of dropdown
    final_argument_whitespace = True
    has_content = True
    option_spec = {
        "open": directives.flag,  # make open by default
        "color": make_choice(SEMANTIC_COLORS),
        "icon": make_choice(list_octicons()),
        "chevron": make_choice(
            ["right-down", "down-up"]
        ),  # chevron direction closed-open
        "animate": make_choice(("fade-in", "fade-in-slide-down")),
        "margin": margin_option,
        "name": directives.unchanged,
        "class-container": directives.class_option,
        "class-title": directives.class_option,
        "class-body": directives.class_option,
    }

    def run_with_defaults(self) -> list[nodes.Node]:
        # default classes
        classes = {
            "container_classes": self.options.get("margin", ["sd-mb-3"])
            + self.options.get("class-container", []),
            "title_classes": self.options.get("class-title", []),
            "body_classes": self.options.get("class-body", []),
        }

        # add color classes
        title_color = self.options.get("color")
        if title_color:
            classes["title_classes"].extend(
                [f"sd-bg-{title_color}", f"sd-bg-text-{title_color}"]
            )

        # add animation classes
        if (
            "animate" in self.options
            and ("sd-" + self.options["animate"]) not in classes["container_classes"]
        ):
            classes["container_classes"].append("sd-" + self.options["animate"])

        container = create_component(
            "dropdown",
            opened="open" in self.options,
            type="dropdown",
            has_title=len(self.arguments) > 0,
            icon=self.options.get("icon"),
            chevron=self.options.get("chevron"),
            **classes,
        )
        self.set_source_info(container)
        if self.arguments:
            textnodes, messages = self.state.inline_text(self.arguments[0], self.lineno)
            title_node = nodes.rubric(self.arguments[0], "", *textnodes)
            container += title_node
            container += messages
            # where possible we add the target to the title node,
            # so that it can be used as the reference text
            self.add_name(title_node)
        else:
            self.add_name(container)
        self.state.nested_parse(self.content, self.content_offset, container)
        return [container]


class DropdownHtmlTransform(SphinxPostTransform):
    """Transform dropdown containers into the HTML specific AST structures::

    <details class="sd-sphinx-override sd-dropdown sd-card">
        <summary class="sd-summary-title sd-card-header">
            ...title nodes
        <div class="sd-summary-content sd-card-body">
            ...content nodes

    """

    default_priority = 199
    formats = ("html",)

    def run(self) -> None:
        """Run the transform"""
        document: nodes.document = self.document
        for node in findall(document)(lambda node: is_component(node, "dropdown")):
            # TODO option to not have card css (but requires more formatting)
            use_card = True

            marker_type = (
                "chevron-down" if node["chevron"] == "down-up" else "chevron-right"
            )
            state_marker = nodes.inline(
                "",
                "",
                nodes.raw(
                    "",
                    nodes.Text(get_octicon(marker_type, height="1.5em")),
                    format="html",
                ),
                classes=["sd-summary-state-marker", f"sd-summary-{marker_type}"],
            )

            newnode = dropdown_main(
                opened=node["opened"],
                classes=["sd-sphinx-override", "sd-dropdown"]
                + (["sd-card"] if use_card else ["sd-d-flex-column"])
                + node["container_classes"],
            )

            if node["has_title"]:
                title_text_children = node[0].children
                if node[0].get("ids"):
                    newnode["ids"] += node[0]["ids"]
                body_children = node[1:]
            else:
                title_text_children = [
                    nodes.raw(
                        "...",
                        nodes.Text(
                            get_octicon(
                                "kebab-horizontal", height="1.5em", classes=["no-title"]
                            )
                        ),
                        format="html",
                    )
                ]
                body_children = node.children
            title_text_node = nodes.inline(
                "",
                "",
                *title_text_children,
                classes=["sd-summary-text"],
            )
            title_children = [title_text_node, state_marker]
            if node["icon"]:
                title_children.insert(
                    0,
                    nodes.raw(
                        "",
                        get_octicon(node["icon"], height="1em"),
                        classes=["sd-summary-icon"],
                        format="html",
                    ),
                )

            newnode += dropdown_title(
                "",
                "",
                *title_children,
                classes=["sd-summary-title"]
                + (["sd-card-header"] if use_card else [])
                + node["title_classes"],
            )
            body_node = create_component(
                "dropdown-body",
                classes=["sd-summary-content"]
                + (["sd-card-body"] if use_card else [])
                + node["body_classes"],
                children=body_children,
            )
            if use_card:
                for para in findall(body_node)(nodes.paragraph):
                    para["classes"] = ([] if "classes" in para else para["classes"]) + [
                        "sd-card-text"
                    ]
            newnode += body_node
            # newnode += open_marker
            node.replace_self(newnode)