-
Notifications
You must be signed in to change notification settings - Fork 7
/
__init__.py
317 lines (251 loc) · 9.92 KB
/
__init__.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# Copyright (c) 2021 Pradyun Gedam
# Licensed under MIT License
# SPDX-License-Identifier: MIT
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
"""A clean and capable Sphinx documentation theme."""
__version__ = "2021.10.09.dev1"
import hashlib
import logging
import os
from functools import lru_cache
from pathlib import Path
from typing import Any, Dict, List, Optional
import sphinx.application
import sphinx.config
from bs4 import BeautifulSoup
from pygments.formatters import HtmlFormatter
from pygments.style import Style
from pygments.token import Text
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.highlighting import PygmentsBridge
from .errors import LutraError
from .navigation import should_hide_toc, trim_sidebar_navigation
THEME_PATH = (Path(__file__).parent / "theme" / "lutra").resolve()
logger = logging.getLogger(__name__)
# GLOBAL STATE
_KNOWN_STYLES_IN_USE: Dict[str, Optional[Style]] = {
"light": None,
"dark": None,
}
# TODO: Investigate if we can use a posttransform to achieve this instead.
def wrap_elements_that_can_get_too_wide(content: str) -> str:
"""Wrap the elements that could get too wide, with a div to allow controlling width.
- <table>
- [class=math]
"""
soup = BeautifulSoup(content, "html.parser")
for table in soup.find_all("table"):
table_wrapper = soup.new_tag("div", attrs={"class": "table-wrapper"})
table.replace_with(table_wrapper)
table_wrapper.append(table)
for math in soup.find_all("div", class_="math"):
wrapper = soup.new_tag("div", attrs={"class": "math-wrapper"})
math.replace_with(wrapper)
wrapper.append(math)
return str(soup)
def get_pygments_style_colors(
style: Style, *, fallbacks: Dict[str, str]
) -> Dict[str, str]:
"""Get background/foreground colors for given pygments style."""
background = style.background_color
text_colors = style.style_for_token(Text)
foreground = text_colors["color"]
if not background:
background = fallbacks["background"]
if not foreground:
foreground = fallbacks["foreground"]
else:
foreground = f"#{foreground}"
return {"background": background, "foreground": foreground}
@lru_cache(maxsize=2)
def get_colors_for_codeblocks(
highlighter: PygmentsBridge, *, fg: str, bg: str
) -> Dict[str, str]:
"""Get background/foreground colors for given pygments style."""
return get_pygments_style_colors(
highlighter.formatter_args["style"],
fallbacks={
"foreground": fg,
"background": bg,
},
)
@lru_cache
def _asset_hash(path: str) -> str:
"""Append a `?digest=` to an url based on the file content."""
full_path = THEME_PATH / "static" / path
digest = hashlib.md5(full_path.read_bytes()).hexdigest()
return f"_static/{path}?digest={digest}"
def _add_asset_hashes(static: List[str], add_digest_to: List[str]) -> None:
for asset in add_digest_to:
index = static.index("_static/" + asset)
static[index].filename = _asset_hash(asset) # type: ignore
def _html_page_context(
app: sphinx.application.Sphinx,
pagename: str,
templatename: str,
context: Dict[str, Any],
doctree: Any,
) -> None:
assert app.builder
if "css_files" in context:
_add_asset_hashes(
context["css_files"],
["styles/lutra.css", "styles/lutra-extensions.css"],
)
if "scripts" in context:
_add_asset_hashes(
context["scripts"],
["scripts/lutra.js"],
)
# Basic constants
context["lutra_version"] = __version__
# Values computed from page-level context.
toctree = context["toctree"]
toctree_html = toctree(includehidden=True, titles_only=True, maxdepth=-1)
was_trimmed, toctree_html = trim_sidebar_navigation(
toctree_html, style=context["theme_navigation_style"]
)
context["lutra_trimmed_toctree"] = was_trimmed
context["lutra_toctree_html"] = toctree_html
context["lutra_hide_toc"] = should_hide_toc(context)
context["lutra_modified"] = (
app.builder.config.lutra_modified or app.config.html_theme != "lutra"
)
# Inject information about styles
context["lutra_pygments"] = {
"light": get_pygments_style_colors(
_KNOWN_STYLES_IN_USE["light"],
fallbacks=dict(
foreground="black",
background="white",
),
),
"dark": get_pygments_style_colors(
_KNOWN_STYLES_IN_USE["dark"],
fallbacks=dict(
foreground="white",
background="black",
),
),
}
# Patch the content
if "body" in context:
context["body"] = wrap_elements_that_can_get_too_wide(context["body"])
def _builder_inited(app: sphinx.application.Sphinx) -> None:
if not isinstance(app.builder, StandaloneHTMLBuilder):
raise LutraError(
reference="used-with-non-html-build",
message="Lutra is being used as an extension for a non-HTML build!",
hint_stmt=(
"Did you list 'lutra' in the `extensions` in conf.py? "
"If so, please remove it, since that will enable it for all Sphinx "
"builders (rather than just HTML builders) which does not work."
),
)
if app.config.html_sidebars:
raise LutraError(
reference="using-html_sidebars",
message="Lutra does not support the html_sidebars configuration.",
hint_stmt="See http://pradyunsg.me/lutra/customisation/sidebar/",
)
# Our `lutra.js` file needs to be loaded as soon as possible.
app.add_js_file("scripts/lutra.js", priority=200)
# 500 is the default priority for extensions, we want this after this.
app.add_css_file("styles/lutra-extensions.css", priority=600)
builder = app.builder
assert builder, "what?"
assert (
builder.highlighter is not None
), "there should be a default style known to Sphinx"
assert (
builder.dark_highlighter is None
), "there shouldn't be a dark style known to Sphinx"
update_known_styles_state(app)
def _update_default(key: str, /, *, new_default: Any) -> None:
app.config.values[key] = (new_default, *app.config.values[key][1:])
# Change the default permalinks icon
_update_default("html_permalinks_icon", new_default="#")
def update_known_styles_state(app: sphinx.application.Sphinx) -> None:
"""Update a global store of known styles of this application."""
global _KNOWN_STYLES_IN_USE
_KNOWN_STYLES_IN_USE = {
"light": _get_light_style(app),
"dark": _get_dark_style(app),
}
def _get_light_style(app: sphinx.application.Sphinx) -> Style:
return app.builder.highlighter.formatter_args["style"]
def _get_dark_style(app: sphinx.application.Sphinx) -> Style:
try:
dark_style = app.config._raw_config["pygments_dark_style"]
except KeyError:
dark_style = app.config.pygments_dark_style
return PygmentsBridge("html", dark_style).formatter_args["style"]
def get_pygments_stylesheet() -> str:
"""Generate the theme-specific pygments.css.
There is no way to tell Sphinx how the theme handles dark mode; at this time.
"""
light_formatter = HtmlFormatter(style=_KNOWN_STYLES_IN_USE["light"])
dark_formatter = HtmlFormatter(style=_KNOWN_STYLES_IN_USE["dark"])
light_prefix = "html:not(.dark) .highlight"
dark_prefix = "html.dark .highlight"
# This section uses the "internal" methods of HtmlFormatter, to get the styles
# such that they're namespaced. This helps ensure that they only apply on the
# relevant styles.
lines = []
lines.extend(
[
f"{light_prefix} {line}"
for line in light_formatter.get_linenos_style_defs()
if not line.startswith("pre {")
]
)
lines.extend(light_formatter.get_background_style_defs(light_prefix))
lines.extend(light_formatter.get_token_style_defs(light_prefix))
lines.extend(
[
f"{dark_prefix} {line}"
for line in dark_formatter.get_linenos_style_defs()
if not line.startswith("pre {")
]
)
lines.extend(dark_formatter.get_background_style_defs(dark_prefix))
lines.extend(dark_formatter.get_token_style_defs(dark_prefix))
return "\n".join(lines)
def _build_finished(
app: sphinx.application.Sphinx,
exception: Optional[Exception],
) -> None:
if exception is not None:
return
# We overwrite the default pygments.css file, because this theme has different
# needs from it, compared to what Sphinx generates.
assert app.builder
with open(os.path.join(app.builder.outdir, "_static", "pygments.css"), "w") as f:
f.write(get_pygments_stylesheet())
def setup(app: sphinx.application.Sphinx) -> Dict[str, Any]:
"""Entry point for sphinx theming."""
app.require_sphinx("4.0")
app.add_config_value(
"pygments_dark_style",
default="lutra.styles.StyloDarkStyle",
rebuild="env",
types=[str],
)
app.add_config_value("lutra_modified", default=False, rebuild="env", types=[bool])
app.add_html_theme("lutra", str(THEME_PATH))
app.connect("html-page-context", _html_page_context)
app.connect("builder-inited", _builder_inited)
app.connect("build-finished", _build_finished)
app.setup_extension("sphinxext.opengraph")
return {
"parallel_read_safe": True,
"parallel_write_safe": True,
"version": __version__,
}