Minify pygments styles

This commit is contained in:
Jake Howard 2024-06-23 21:35:13 +01:00
parent fffd41dc82
commit 872fd4fc82
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 8 additions and 12 deletions

View file

@ -30,6 +30,7 @@ wagtail-lite-youtube-embed==0.1.0
django-minify-html==1.7.1 django-minify-html==1.7.1
metadata-parser==0.12.1 metadata-parser==0.12.1
django-tasks==0.2.0 django-tasks==0.2.0
lightningcss==0.2.0
# DRF OpenAPI dependencies # DRF OpenAPI dependencies
uritemplate uritemplate

View file

@ -1,21 +1,16 @@
import lightningcss
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.utils.datastructures import OrderedSet
from django.views.decorators.cache import cache_control from django.views.decorators.cache import cache_control
from pygments.formatters.html import HtmlFormatter from pygments.formatters.html import HtmlFormatter
@cache_control(max_age=3600) @cache_control(max_age=3600)
def pygments_styles(request: HttpRequest) -> HttpResponse: def pygments_styles(request: HttpRequest) -> HttpResponse:
default_styles = ( default_styles = HtmlFormatter(style="default").get_style_defs(
HtmlFormatter(style="default") "html:not(.dark-mode) .highlight"
.get_style_defs("html:not(.dark-mode) .highlight")
.split("\n")
) )
dark_styles = ( dark_styles = HtmlFormatter(style="monokai").get_style_defs(
HtmlFormatter(style="monokai") "html.dark-mode .highlight"
.get_style_defs("html.dark-mode .highlight")
.split("\n")
)
return HttpResponse(
"".join(OrderedSet(default_styles + dark_styles)), content_type="text/css"
) )
compressed = lightningcss.process_stylesheet(default_styles + dark_styles)
return HttpResponse(compressed, content_type="text/css")