-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeming.py
More file actions
160 lines (135 loc) · 6.16 KB
/
theming.py
File metadata and controls
160 lines (135 loc) · 6.16 KB
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
"""Theming support for HTML builders."""
from __future__ import annotations
__all__ = ('Theme', 'HTMLThemeFactory')
import configparser
import contextlib
import os
import shutil
import sys
import tempfile
from importlib.metadata import entry_points
from os import path
from typing import TYPE_CHECKING, Any
from zipfile import ZipFile
from sphinx import package_dir
from sphinx.config import check_confval_types as _config_post_init
from sphinx.errors import ThemeError
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.osutil import ensuredir
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
if TYPE_CHECKING:
from collections.abc import Callable
from typing import TypedDict
from typing_extensions import Required
from sphinx.application import Sphinx
class _ThemeToml(TypedDict, total=False):
theme: Required[_ThemeTomlTheme]
options: dict[str, str]
class _ThemeTomlTheme(TypedDict, total=False):
inherit: Required[str]
stylesheets: list[str]
sidebars: list[str]
pygments_style: _ThemeTomlThemePygments
class _ThemeTomlThemePygments(TypedDict, total=False):
default: str
dark: str
logger = logging.getLogger(__name__)
_NO_DEFAULT = object()
_THEME_TOML = 'theme.toml'
_THEME_CONF = 'theme.conf'
class Theme:
"""A Theme is a set of HTML templates and configurations.
This class supports both theme directory and theme archive (zipped theme).
"""
def __init__(self, name: str, *, configs: dict[str, _ConfigFile], paths: list[str], tmp_dirs: list[str]) -> None:
self.name = name
self._dirs = tuple(paths)
self._tmp_dirs = tmp_dirs
options: dict[str, Any] = {}
self.stylesheets: tuple[str, ...] = ()
self.sidebar_templates: tuple[str, ...] = ()
self.pygments_style_default: str | None = None
self.pygments_style_dark: str | None = None
for config in reversed(configs.values()):
options |= config.options
if config.stylesheets is not None:
self.stylesheets = config.stylesheets
if config.sidebar_templates is not None:
self.sidebar_templates = config.sidebar_templates
if config.pygments_style_default is not None:
self.pygments_style_default = config.pygments_style_default
if config.pygments_style_dark is not None:
self.pygments_style_dark = config.pygments_style_dark
self._options = options
def get_theme_dirs(self) -> list[str]:
"""Return a list of theme directories, beginning with this theme's,
then the base theme's, then that one's base theme's, etc.
"""
pass
def get_config(self, section: str, name: str, default: Any=_NO_DEFAULT) -> Any:
"""Return the value for a theme configuration setting, searching the
base theme chain.
"""
pass
def get_options(self, overrides: dict[str, Any] | None=None) -> dict[str, Any]:
"""Return a dictionary of theme options and their values."""
pass
def _cleanup(self) -> None:
"""Remove temporary directories."""
pass
class HTMLThemeFactory:
"""A factory class for HTML Themes."""
def __init__(self, app: Sphinx) -> None:
self._app = app
self._themes = app.registry.html_themes
self._entry_point_themes: dict[str, Callable[[], None]] = {}
self._load_builtin_themes()
if getattr(app.config, 'html_theme_path', None):
self._load_additional_themes(app.config.html_theme_path)
self._load_entry_point_themes()
def _load_builtin_themes(self) -> None:
"""Load built-in themes."""
pass
def _load_additional_themes(self, theme_paths: list[str]) -> None:
"""Load additional themes placed at specified directories."""
pass
def _load_entry_point_themes(self) -> None:
"""Try to load a theme with the specified name.
This uses the ``sphinx.html_themes`` entry point from package metadata.
"""
pass
@staticmethod
def _find_themes(theme_path: str) -> dict[str, str]:
"""Search themes from specified directory."""
pass
def create(self, name: str) -> Theme:
"""Create an instance of theme."""
pass
def _is_archived_theme(filename: str, /) -> bool:
"""Check whether the specified file is an archived theme file or not."""
pass
def _extract_zip(filename: str, target_dir: str, /) -> None:
"""Extract zip file to target directory."""
pass
class _ConfigFile:
__slots__ = ('stylesheets', 'sidebar_templates', 'pygments_style_default', 'pygments_style_dark', 'options')
def __init__(self, stylesheets: tuple[str, ...] | None, sidebar_templates: tuple[str, ...] | None, pygments_style_default: str | None, pygments_style_dark: str | None, options: dict[str, str]) -> None:
self.stylesheets: tuple[str, ...] | None = stylesheets
self.sidebar_templates: tuple[str, ...] | None = sidebar_templates
self.pygments_style_default: str | None = pygments_style_default
self.pygments_style_dark: str | None = pygments_style_dark
self.options: dict[str, str] = options.copy()
def __repr__(self) -> str:
return f'{self.__class__.__qualname__}(stylesheets={self.stylesheets!r}, sidebar_templates={self.sidebar_templates!r}, pygments_style_default={self.pygments_style_default!r}, pygments_style_dark={self.pygments_style_dark!r}, options={self.options!r})'
def __eq__(self, other: object) -> bool:
if isinstance(other, _ConfigFile):
return self.stylesheets == other.stylesheets and self.sidebar_templates == other.sidebar_templates and (self.pygments_style_default == other.pygments_style_default) and (self.pygments_style_dark == other.pygments_style_dark) and (self.options == other.options)
return NotImplemented
def __hash__(self) -> int:
return hash((self.__class__.__qualname__, self.stylesheets, self.sidebar_templates, self.pygments_style_default, self.pygments_style_dark, self.options))
if __name__ == '__main__':
raise SystemExit(_migrate_conf_to_toml(sys.argv[1:]))