File: __init__.py

package info (click to toggle)
sphinx-copybutton 0.5.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 508 kB
  • sloc: javascript: 321; python: 192; makefile: 21
file content (99 lines) | stat: -rw-r--r-- 3,656 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
"""A small sphinx extension to add "copy" buttons to code blocks."""
from pathlib import Path
from sphinx.util import logging

__version__ = "0.5.2"

logger = logging.getLogger(__name__)


def scb_static_path(app):
    app.config.html_static_path.append(
        str(Path(__file__).parent.joinpath("_static").absolute())
    )


def add_to_context(app, config):
    # Update the global context
    config.html_context.update(
        {"copybutton_prompt_text": config.copybutton_prompt_text}
    )
    config.html_context.update(
        {"copybutton_prompt_is_regexp": config.copybutton_prompt_is_regexp}
    )
    config.html_context.update(
        {"copybutton_only_copy_prompt_lines": config.copybutton_only_copy_prompt_lines}
    )
    config.html_context.update(
        {"copybutton_remove_prompts": config.copybutton_remove_prompts}
    )
    config.html_context.update(
        {"copybutton_copy_empty_lines": config.copybutton_copy_empty_lines}
    )
    config.html_context.update(
        {
            "copybutton_line_continuation_character": (
                config.copybutton_line_continuation_character
            )
        }
    )
    config.html_context.update(
        {"copybutton_here_doc_delimiter": config.copybutton_here_doc_delimiter}
    )

    # Old image path deprecation
    # REMOVE after next release
    if config.copybutton_image_path:
        path = Path(app.srcdir) / config.copybutton_image_path
        logger.warning("copybutton_image_path is deprecated, use copybutton_image_svg")
        if not path.exists():
            raise ValueError("copybutton_img_path does not exist")
        if not path.suffix == ".svg":
            raise ValueError("copybutton_img_path must be an SVG")
        config.copybutton_image_svg = path.read_text()

    config.html_context.update({"copybutton_image_svg": config.copybutton_image_svg})
    config.html_context.update({"copybutton_selector": config.copybutton_selector})
    config.html_context.update(
        {
            "copybutton_format_func": Path(__file__)
            .parent.joinpath("_static", "copybutton_funcs.js")
            .read_text()
            .replace("export function", "function")
        }
    )
    config.html_context.update({"copybutton_exclude": config.copybutton_exclude})


def setup(app):
    logger.verbose("Adding copy buttons to code blocks...")
    # Add our static path
    app.connect("builder-inited", scb_static_path)

    # configuration for this tool
    app.add_config_value("copybutton_prompt_text", "", "html")
    app.add_config_value("copybutton_prompt_is_regexp", False, "html")
    app.add_config_value("copybutton_only_copy_prompt_lines", True, "html")
    app.add_config_value("copybutton_remove_prompts", True, "html")
    app.add_config_value("copybutton_copy_empty_lines", True, "html")
    app.add_config_value("copybutton_line_continuation_character", "", "html")
    app.add_config_value("copybutton_here_doc_delimiter", "", "html")
    app.add_config_value("copybutton_image_svg", "", "html")
    app.add_config_value("copybutton_selector", "div.highlight pre", "html")
    app.add_config_value("copybutton_exclude", ".linenos", "html")

    # DEPRECATE THIS AFTER THE NEXT RELEASE
    app.add_config_value("copybutton_image_path", "", "html")

    # Add configuration value to the template
    app.connect("config-inited", add_to_context)

    # Add relevant code to headers
    app.add_css_file("copybutton.css")
    app.add_js_file("clipboard.min.js")
    app.add_js_file("copybutton.js")
    return {
        "version": __version__,
        "parallel_read_safe": True,
        "parallel_write_safe": True,
    }