Source code for xanadu_sphinx_theme.directives.details

"""This module implements the ``details`` reST directive."""

from inspect import cleandoc

from docutils import nodes
from docutils.parsers.rst import Directive, directives
from docutils.statemachine import StringList

TEMPLATE = cleandoc(
    """
    .. raw:: html

        <a
          class="details-header collapse-header"
          data-bs-toggle="collapse"
          href="#{href}"
          aria-expanded="false"
          aria-controls="details"
        >
            <h2>{title}</h2>
            <i class="bx bx-chevron-down rotate"></i>
        </a>
        <div class="collapse" id="{href}">

    {content}

    .. raw:: html

        </div>
    """
)


def lower_and_hyphenize(string):
    """Turns a string into lower case and replaces spaces by hyphens."""
    return string.lower().replace(" ", "-")


[docs]class DetailsDirective(Directive): """Creates a collapsable Details section.""" required_arguments = 0 optional_arguments = 0 final_argument_whitespace = False option_spec = {"title": directives.unchanged, "href": lower_and_hyphenize} has_content = True add_index = False
[docs] def run(self): title = self.options.get("title", "Usage Details") href = self.options.get("href", lower_and_hyphenize(title)) rst = TEMPLATE.format(title=title, content="\n".join(self.content), href=href) string_list = StringList(rst.split("\n")) node = nodes.tbody() self.state.nested_parse(string_list, self.content_offset, node) return [node]