Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Append a hash ?digest to CSS files for cache-busting#108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
hugovk merged 1 commit intopython:mainfromhugovk:add-hash-digest-to-css
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletionspython_docs_theme/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,62 @@
import hashlib
import os
from functools import lru_cache
from pathlib import Path
from typing import Any, Dict, List

import sphinx.application
from sphinx.builders.html import StandaloneHTMLBuilder

THEME_PATH = Path(__file__).parent.resolve()


@lru_cache(maxsize=None)
def _asset_hash(path: str) -> str:
"""Append a `?digest=` to an url based on the file content."""
full_path = THEME_PATH / path.replace("_static/", "static/")
digest = hashlib.sha1(full_path.read_bytes()).hexdigest()

return f"{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(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:
if app.config.html_theme != "python_docs_theme":
return

assert isinstance(app.builder, StandaloneHTMLBuilder)

if "css_files" in context:
if "_static/pydoctheme.css" not in context["css_files"]:
raise ValueError(
"This documentation is not using `pydoctheme.css` as the stylesheet. "
"If you have set `html_style` in your conf.py file, remove it."
)

_add_asset_hashes(
context["css_files"],
["_static/pydoctheme.css"],
)


def setup(app):
current_dir = os.path.abspath(os.path.dirname(__file__))
app.add_html_theme(
'python_docs_theme', current_dir)

app.connect("html-page-context", _html_page_context)

return {
'parallel_read_safe': True,
'parallel_write_safe': True,
Expand Down
2 changes: 1 addition & 1 deletionpython_docs_theme/theme.conf
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
[theme]
inherit = default
stylesheet = pydoctheme.css?2022.1
stylesheet = pydoctheme.css
pygments_style = default

[options]
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp