File: __init__.py

package info (click to toggle)
sphinxcontrib-jquery 4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 520 kB
  • sloc: javascript: 6,986; python: 118; makefile: 3
file content (60 lines) | stat: -rw-r--r-- 2,156 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
from os import makedirs, path
import shutil
import base64
import hashlib

import sphinx

__version__ = "4.1"
version_info = (4, 1)

_ROOT_DIR = path.abspath(path.dirname(__file__))
_FILES = (
    (
        'jquery.js',
        'sha384-vtXRMe3mGCbOeY7l30aIg8H9p3GdeSe4IFlP6G8JMa7o7lXvnz3GFKzPxzJdPfGK',
    ),
    (
        '_sphinx_javascript_frameworks_compat.js',
        'sha384-lSZeSIVKp9myfKbDQ3GkN/KHjUc+mzg17VKDN4Y2kUeBSJioB9QSM639vM9fuY//',
    ),
)


def add_js_files(app, config):
    jquery_installed = getattr(app, "_sphinxcontrib_jquery_installed", False)

    if sphinx.version_info[:2] >= (6, 0) and not jquery_installed:
        makedirs(path.join(app.outdir, '_static'), exist_ok=True)
        for (filename, integrity) in _FILES:
            # The default is not to enable subresource integrity checks, as it
            # does not trigger the hash check but instead blocks the request
            # when viewing documentation locally through the ``file://`` URIs.
            if config.jquery_use_sri:
                if filename == "jquery.js":
                    with open("/usr/share/javascript/jquery/jquery.min.js", "rb") as f:
                        checksum = hashlib.file_digest(f, hashlib.sha384)
                    encoded = base64.b64encode(checksum.digest()).decode("ascii")
                    integrity = f"sha384-{encoded}"
                app.add_js_file(filename, priority=100, integrity=integrity)
            else:
                app.add_js_file(filename, priority=100)
            shutil.copyfile(
                path.join(_ROOT_DIR, filename),
                path.join(app.outdir, '_static', filename)
            )
        app._sphinxcontrib_jquery_installed = True


def setup(app):
    # Configuration value for enabling `subresource integrity`__ (SRI) checks
    # __ https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
    app.add_config_value("jquery_use_sri", default=False, rebuild="html", types=(bool,))

    app.connect('config-inited', add_js_files)

    return {
        "parallel_read_safe": True,
        "parallel_write_safe": True,
        "version": __version__,
    }