File: __init__.py

package info (click to toggle)
sphinx-togglebutton 0.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 444 kB
  • sloc: javascript: 198; python: 143; makefile: 18
file content (107 lines) | stat: -rw-r--r-- 3,155 bytes parent folder | download
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
"""A small sphinx extension to add "toggle" buttons to items."""

import os

from docutils import nodes
from docutils.parsers.rst import Directive, directives
from sphinx.locale import get_translation

MESSAGE_CATALOG_NAME = "togglebutton"
translate = get_translation(MESSAGE_CATALOG_NAME)

__version__ = "0.4.4"


def st_static_path(app):
    static_path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), "_static")
    )
    app.config.html_static_path.append(static_path)


def initialize_js_assets(app, config):
    # Update the global context

    app.add_js_file(
        None, body=f"let toggleHintShow = '{config.togglebutton_hint}';"
    )
    app.add_js_file(
        None, body=f"let toggleHintHide = '{config.togglebutton_hint_hide}';"
    )
    open_print = str(config.togglebutton_open_on_print).lower()
    app.add_js_file(None, body=f"let toggleOpenOnPrint = '{open_print}';")
    app.add_js_file("togglebutton.js")


# This function reads in a variable and inserts it into JavaScript


def insert_custom_selection_config(app):
    # This is a configuration that you've specified for users in `conf.py`

    selector = app.config["togglebutton_selector"]
    js_text = "var togglebuttonSelector = '%s';" % selector
    app.add_js_file(None, body=js_text)


class Toggle(Directive):
    """Hide a block of markup text by wrapping it in a container."""

    optional_arguments = 1
    final_argument_whitespace = True
    has_content = True

    option_spec = {"id": directives.unchanged, "show": directives.flag}

    def run(self):
        self.assert_has_content()
        classes = ["toggle"]
        if "show" in self.options:
            classes.append("toggle-shown")
        parent = nodes.container(classes=classes)
        self.state.nested_parse(self.content, self.content_offset, parent)
        return [parent]


# We connect this function to the step after the builder is initialized


def setup(app):
    # add translations

    package_dir = os.path.abspath(os.path.dirname(__file__))
    locale_dir = os.path.join(package_dir, "translations", "locales")
    app.add_message_catalog(MESSAGE_CATALOG_NAME, locale_dir)

    # Add our static path

    app.connect("builder-inited", st_static_path)

    # Add relevant code to headers

    app.add_css_file("togglebutton.css")

    # Add the string we'll use to select items in the JS
    # Tell Sphinx about this configuration variable

    app.add_config_value(
        "togglebutton_selector", ".toggle, .admonition.dropdown", "html"
    )
    app.add_config_value(
        "togglebutton_hint", f"{translate('Click to show')}", "html"
    )
    app.add_config_value(
        "togglebutton_hint_hide", f"{translate('Click to hide')}", "html"
    )
    app.add_config_value("togglebutton_open_on_print", True, "html")

    # Run the function after the builder is initialized

    app.connect("builder-inited", insert_custom_selection_config)
    app.connect("config-inited", initialize_js_assets)
    app.add_directive("toggle", Toggle)
    return {
        "version": __version__,
        "parallel_read_safe": True,
        "parallel_write_safe": True,
    }