| 1 |
"""customize events for logo management. |
| 2 |
|
| 3 |
we use one event to copy over custom logo images to _static |
| 4 |
and another even to link them in the html context |
| 5 |
""" |
| 6 |
|
| 7 |
from functools import partial |
| 8 |
from pathlib import Path |
| 9 |
|
| 10 |
from docutils.nodes import Node |
| 11 |
from sphinx.application import Sphinx |
| 12 |
from sphinx.errors import ExtensionError |
| 13 |
from sphinx.util import isurl |
| 14 |
from sphinx.util.fileutil import copy_asset_file |
| 15 |
|
| 16 |
from .utils import get_theme_options_dict, maybe_warn |
| 17 |
|
| 18 |
|
| 19 |
def setup_logo_path( |
| 20 |
app: Sphinx, pagename: str, templatename: str, context: dict, doctree: Node |
| 21 |
) -> None: |
| 22 |
"""Set up relative paths to logos in our HTML templates. |
| 23 |
|
| 24 |
In Sphinx, the context["logo"] is a path to the `html_logo` image now in the output |
| 25 |
`_static` folder. |
| 26 |
|
| 27 |
If logo["image_light"] and logo["image_dark"] are given, we must modify them to |
| 28 |
follow the same pattern. They have already been copied to the output folder |
| 29 |
in the `update_config` event. |
| 30 |
""" |
| 31 |
# get information from the context "logo_url" for sphinx>=6, "logo" sphinx<6 |
| 32 |
pathto = context.get("pathto") |
| 33 |
logo = context.get("logo_url") or context.get("logo") |
| 34 |
theme_logo = context.get("theme_logo", {}) |
| 35 |
|
| 36 |
# Define the final path to logo images in the HTML context |
| 37 |
theme_logo["image_relative"] = {} |
| 38 |
for kind in ["light", "dark"]: |
| 39 |
image_kind_logo = theme_logo.get(f"image_{kind}") |
| 40 |
|
| 41 |
# If it's a URL the "relative" path is just the URL |
| 42 |
# else we need to calculate the relative path to a local file |
| 43 |
if image_kind_logo: |
| 44 |
if not isurl(image_kind_logo): |
| 45 |
image_kind_name = Path(image_kind_logo).name |
| 46 |
image_kind_logo = pathto(f"_static/{image_kind_name}", resource=True) |
| 47 |
theme_logo["image_relative"][kind] = image_kind_logo |
| 48 |
|
| 49 |
# If there's no custom logo for this kind, just use `html_logo` |
| 50 |
# If `logo` is also None, then do not add this key to context. |
| 51 |
elif isinstance(logo, str) and len(logo) > 0: |
| 52 |
theme_logo["image_relative"][kind] = logo |
| 53 |
|
| 54 |
# Update our context logo variables with the new image paths |
| 55 |
context["theme_logo"] = theme_logo |
| 56 |
|
| 57 |
|
| 58 |
def copy_logo_images(app: Sphinx, exception=None) -> None: |
| 59 |
""" |
| 60 |
Copy logo image to the _static directory. |
| 61 |
|
| 62 |
If logo image paths are given, copy them to the `_static` folder. |
| 63 |
Then we can link to them directly in an html_page_context event. |
| 64 |
""" |
| 65 |
warning = partial(maybe_warn, app) |
| 66 |
logo = get_theme_options_dict(app).get("logo", {}) |
| 67 |
staticdir = Path(app.builder.outdir) / "_static" |
| 68 |
assert staticdir.is_absolute() |
| 69 |
for kind in ["light", "dark"]: |
| 70 |
path_image = logo.get(f"image_{kind}") |
| 71 |
if not path_image or isurl(path_image): |
| 72 |
continue |
| 73 |
if (staticdir / Path(path_image).name).exists(): |
| 74 |
# file already exists in static dir e.g. because a theme has |
| 75 |
# bundled the logo and installed it there |
| 76 |
continue |
| 77 |
full_logo_path = Path(app.srcdir) / path_image |
| 78 |
assert full_logo_path.is_absolute() |
| 79 |
if not full_logo_path.exists(): |
| 80 |
warning(f"Path to {kind} image logo does not exist: {path_image}") |
| 81 |
# Ensure templates cannot be passed for logo path to avoid security |
| 82 |
# vulnerability |
| 83 |
if path_image.lower().endswith("_t"): |
| 84 |
raise ExtensionError( |
| 85 |
f"The {kind} logo path '{path_image}' looks like a Sphinx template; " |
| 86 |
"please provide a static logo image." |
| 87 |
) |
| 88 |
copy_asset_file(str(full_logo_path), staticdir) |