From 2b39fb1b9a361150c79ba12bcd01eba7d62bb8e0 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 28 Jun 2022 22:59:17 +0100 Subject: [PATCH] Bake pygments version into style URL It's a hack, but it does work --- website/contrib/code_block/tests.py | 9 +++++++++ website/contrib/code_block/urls.py | 6 +++++- website/contrib/code_block/utils.py | 4 ++++ website/contrib/code_block/views.py | 8 ++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 website/contrib/code_block/utils.py diff --git a/website/contrib/code_block/tests.py b/website/contrib/code_block/tests.py index 2c5edd8..d398fcd 100644 --- a/website/contrib/code_block/tests.py +++ b/website/contrib/code_block/tests.py @@ -2,6 +2,8 @@ from django.test import TestCase from django.urls import reverse from pygments.styles import get_all_styles +from .utils import PYGMENTS_VERSION, PYGMENTS_VERSION_SLUG + class PygmentsStylesTestCase(TestCase): def test_accessible(self) -> None: @@ -12,9 +14,16 @@ class PygmentsStylesTestCase(TestCase): ) self.assertEqual(response.status_code, 200) self.assertEqual(response["Cache-Control"], "max-age=3600, public") + self.assertEqual(response["ETag"], f'"{PYGMENTS_VERSION}"') def test_unknown_style(self) -> None: response = self.client.get( reverse("static-pygments:styles", args=["not-a-style"]) ) self.assertEqual(response.status_code, 404) + + def test_url_contains_version(self) -> None: + for style in get_all_styles(): + with self.subTest(style=style): + url = reverse("static-pygments:styles", args=[style]) + self.assertIn(PYGMENTS_VERSION_SLUG, url) diff --git a/website/contrib/code_block/urls.py b/website/contrib/code_block/urls.py index 8e61e93..1b9b84a 100644 --- a/website/contrib/code_block/urls.py +++ b/website/contrib/code_block/urls.py @@ -1,7 +1,11 @@ from django.urls import path +from .utils import PYGMENTS_VERSION_SLUG from .views import pygments_styles app_name = "code_block" -urlpatterns = [path(".css", pygments_styles, name="styles")] +urlpatterns = [ + # HACK: Bake the pygments version into the URL, without needing a custom method + path(f".{PYGMENTS_VERSION_SLUG}.css", pygments_styles, name="styles") +] diff --git a/website/contrib/code_block/utils.py b/website/contrib/code_block/utils.py new file mode 100644 index 0000000..acfcabd --- /dev/null +++ b/website/contrib/code_block/utils.py @@ -0,0 +1,4 @@ +from importlib.metadata import version + +PYGMENTS_VERSION = version("pygments") +PYGMENTS_VERSION_SLUG = PYGMENTS_VERSION.replace(".", "-") diff --git a/website/contrib/code_block/views.py b/website/contrib/code_block/views.py index cd79ff5..03ddf4a 100644 --- a/website/contrib/code_block/views.py +++ b/website/contrib/code_block/views.py @@ -1,9 +1,17 @@ from django.http import Http404, HttpRequest, HttpResponse from django.views.decorators.cache import cache_control +from django.views.decorators.http import etag from pygments.formatters.html import HtmlFormatter from pygments.util import ClassNotFound +from .utils import PYGMENTS_VERSION + +def pygments_etag(request: HttpRequest, name: str) -> str: + return PYGMENTS_VERSION + + +@etag(pygments_etag) @cache_control(max_age=3600, public=True) def pygments_styles(request: HttpRequest, name: str) -> HttpResponse: try: