From 872fd4fc824676899df6721af797b3ed694dde22 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 23 Jun 2024 21:35:13 +0100 Subject: [PATCH] Minify pygments styles --- requirements.txt | 1 + website/contrib/code_block/views.py | 19 +++++++------------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/requirements.txt b/requirements.txt index 5f5d92c..0ed35ce 100644 --- a/requirements.txt +++ b/requirements.txt @@ -30,6 +30,7 @@ wagtail-lite-youtube-embed==0.1.0 django-minify-html==1.7.1 metadata-parser==0.12.1 django-tasks==0.2.0 +lightningcss==0.2.0 # DRF OpenAPI dependencies uritemplate diff --git a/website/contrib/code_block/views.py b/website/contrib/code_block/views.py index d45e3e5..0345d35 100644 --- a/website/contrib/code_block/views.py +++ b/website/contrib/code_block/views.py @@ -1,21 +1,16 @@ +import lightningcss from django.http import HttpRequest, HttpResponse -from django.utils.datastructures import OrderedSet from django.views.decorators.cache import cache_control from pygments.formatters.html import HtmlFormatter @cache_control(max_age=3600) def pygments_styles(request: HttpRequest) -> HttpResponse: - default_styles = ( - HtmlFormatter(style="default") - .get_style_defs("html:not(.dark-mode) .highlight") - .split("\n") + default_styles = HtmlFormatter(style="default").get_style_defs( + "html:not(.dark-mode) .highlight" ) - dark_styles = ( - HtmlFormatter(style="monokai") - .get_style_defs("html.dark-mode .highlight") - .split("\n") - ) - return HttpResponse( - "".join(OrderedSet(default_styles + dark_styles)), content_type="text/css" + dark_styles = HtmlFormatter(style="monokai").get_style_defs( + "html.dark-mode .highlight" ) + compressed = lightningcss.process_stylesheet(default_styles + dark_styles) + return HttpResponse(compressed, content_type="text/css")