9
0
mirror of https://github.com/donlon/cloudflare-error-page.git synced 2025-12-23 08:49:25 +00:00

python: separate css from html template

This commit is contained in:
Anthony Donlon
2025-12-22 22:28:46 +08:00
parent c0e576478a
commit 9fbcba1a34
8 changed files with 37 additions and 125 deletions

View File

@@ -20,7 +20,7 @@ env = Environment(
lstrip_blocks=True, lstrip_blocks=True,
) )
base_template: Template = env.get_template("error.html") base_template: Template = env.get_template("template.html")
class ErrorPageParams(TypedDict): class ErrorPageParams(TypedDict):

File diff suppressed because one or more lines are too long

View File

@@ -14,7 +14,15 @@
<meta name="robots" content="noindex, nofollow" /> <meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width,initial-scale=1" /> <meta name="viewport" content="width=device-width,initial-scale=1" />
{% block html_head %}{% endblock %} {% block html_head %}{% endblock %}
<!-- @INLINE_CSS_HERE@ --> <style>
{% if html_style %}
{# Support custom stylesheet #}
{{ html_style | safe }}
{% else %}
{# Default stylesheet #}
{% include 'main.css' %}
{% endif %}
</style>
</head> </head>
<body> <body>
<div id="cf-wrapper"> <div id="cf-wrapper">

View File

@@ -23,5 +23,8 @@ include = [
"cloudflare_error_page/templates/*", "cloudflare_error_page/templates/*",
] ]
[tool.hatch.build.targets.wheel.hooks.custom]
path = "scripts/hatch_build.py"
[tool.hatch.version] [tool.hatch.version]
path = "cloudflare_error_page/__init__.py" path = "cloudflare_error_page/__init__.py"

File diff suppressed because one or more lines are too long

17
scripts/hatch_build.py Normal file
View File

@@ -0,0 +1,17 @@
import os
import sys
import shutil
from pathlib import Path
from typing import Any
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
sys.path.append(os.path.dirname(__file__))
from inline_resources import generate_inlined_css
class CustomBuildHook(BuildHookInterface):
def initialize(self, version: str, build_data: dict[str, Any]):
generate_inlined_css()
src = Path(self.root) / 'resources' / 'styles' / 'main.css'
dst = Path(self.root) / 'cloudflare_error_page' / 'templates'
shutil.copy(src, dst)

View File

@@ -4,7 +4,7 @@ from urllib.parse import quote
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
resources_folder = os.path.join(root,'resources') resources_folder = os.path.join(root, 'resources')
def read_file(path: str) -> str: def read_file(path: str) -> str:
@@ -49,7 +49,7 @@ def inline_css_resource(original_file: str, css_file: str, output_file: str):
write_file(output_file, original_data) write_file(output_file, original_data)
if __name__ == '__main__': def generate_inlined_css():
inline_svg_resources( inline_svg_resources(
os.path.join(resources_folder, 'styles/main-original.css'), os.path.join(resources_folder, 'styles/main-original.css'),
[ [
@@ -61,11 +61,10 @@ if __name__ == '__main__':
], ],
os.path.join(resources_folder, 'styles/main.css'), os.path.join(resources_folder, 'styles/main.css'),
) )
inline_css_resource(
os.path.join(resources_folder, 'templates/error.html'),
os.path.join(resources_folder, 'styles/main.css'), if __name__ == '__main__':
os.path.join(root, 'cloudflare_error_page/templates/error.html'), generate_inlined_css()
)
inline_css_resource( inline_css_resource(
os.path.join(resources_folder, 'templates/error.ejs'), os.path.join(resources_folder, 'templates/error.ejs'),
os.path.join(resources_folder, 'styles/main.css'), os.path.join(resources_folder, 'styles/main.css'),