jinja2-htmlmin 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from functools import wraps
|
|
3
|
+
from typing import TypedDict
|
|
4
|
+
|
|
5
|
+
from htmlmin import Minifier
|
|
6
|
+
|
|
7
|
+
TYPE_CHECKING = False
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from collections.abc import Collection
|
|
10
|
+
from typing import TypeVar
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
from typing import Unpack # type: ignore
|
|
14
|
+
except ImportError:
|
|
15
|
+
try:
|
|
16
|
+
from typing_extensions import Unpack # type: ignore
|
|
17
|
+
except ImportError:
|
|
18
|
+
from typing import Any as Unpack
|
|
19
|
+
|
|
20
|
+
from htmlmin.parser import HTMLMinParser
|
|
21
|
+
from jinja2 import BaseLoader
|
|
22
|
+
|
|
23
|
+
_Loader = TypeVar("_Loader", bound=BaseLoader)
|
|
24
|
+
|
|
25
|
+
class _HTMLMinKwargs(TypedDict, total=False):
|
|
26
|
+
remove_comments: bool
|
|
27
|
+
remove_empty_space: bool
|
|
28
|
+
remove_all_empty_space: bool
|
|
29
|
+
reduce_empty_attributes: bool
|
|
30
|
+
reduce_boolean_attributes: bool
|
|
31
|
+
remove_optional_attribute_quotes: bool
|
|
32
|
+
convert_charrefs: bool
|
|
33
|
+
keep_pre: bool
|
|
34
|
+
pre_tags: Collection[str]
|
|
35
|
+
pre_attr: str
|
|
36
|
+
cls: type[HTMLMinParser]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
__version__ = "1.0.0"
|
|
40
|
+
|
|
41
|
+
_ESCAPE_RE = re.compile(r"\{(?:\{.*?\}|%.*?%|#.*?#)\}")
|
|
42
|
+
_UNESCAPE_RE = re.compile(r"__jinja2_htmlmin (\d+)__")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def minify_loader(loader: _Loader, /, **kwargs: "Unpack[_HTMLMinKwargs]") -> _Loader:
|
|
46
|
+
"""
|
|
47
|
+
Enhance a Jinja2 loader to automatically minify HTML templates.
|
|
48
|
+
|
|
49
|
+
Wraps the loader's get_source method to apply HTML minification while
|
|
50
|
+
preserving Jinja2 syntax. The minification occurs at template load time,
|
|
51
|
+
before Jinja2 compilation.
|
|
52
|
+
|
|
53
|
+
:param loader: Jinja2 loader instance to enhance
|
|
54
|
+
:param kwargs: Minifier configuration options (see
|
|
55
|
+
https://htmlmin.readthedocs.io/en/latest/reference.html)
|
|
56
|
+
:return: The same loader instance with minification enabled
|
|
57
|
+
|
|
58
|
+
Example::
|
|
59
|
+
|
|
60
|
+
from jinja2 import FileSystemLoader
|
|
61
|
+
from jinja2_htmlmin import minify_loader
|
|
62
|
+
|
|
63
|
+
loader = minify_loader(
|
|
64
|
+
FileSystemLoader("templates"),
|
|
65
|
+
remove_comments=True,
|
|
66
|
+
remove_empty_space=True,
|
|
67
|
+
remove_all_empty_space=True,
|
|
68
|
+
reduce_boolean_attributes=True,
|
|
69
|
+
)
|
|
70
|
+
"""
|
|
71
|
+
minifier = Minifier(**kwargs)
|
|
72
|
+
|
|
73
|
+
super_get_source = loader.get_source
|
|
74
|
+
|
|
75
|
+
@wraps(super_get_source)
|
|
76
|
+
def get_source(environment, template):
|
|
77
|
+
source, path, up_to_date = super_get_source(environment, template)
|
|
78
|
+
|
|
79
|
+
# Replace Jinja syntax with unique entities
|
|
80
|
+
def repl_escape(match: re.Match[str]) -> str:
|
|
81
|
+
id_ = len(lookup)
|
|
82
|
+
lookup.append(match[0])
|
|
83
|
+
return f"__jinja2_htmlmin {id_:05d}__"
|
|
84
|
+
|
|
85
|
+
lookup: list[str] = []
|
|
86
|
+
source = _ESCAPE_RE.sub(repl_escape, source)
|
|
87
|
+
|
|
88
|
+
# Minify the source
|
|
89
|
+
source = minifier.minify(source)
|
|
90
|
+
|
|
91
|
+
# Restore Jinja syntax
|
|
92
|
+
def repl_unescape(match: re.Match[str]) -> str:
|
|
93
|
+
return lookup[int(match[1])]
|
|
94
|
+
|
|
95
|
+
source = _UNESCAPE_RE.sub(repl_unescape, source)
|
|
96
|
+
|
|
97
|
+
return source, path, up_to_date
|
|
98
|
+
|
|
99
|
+
loader.get_source = get_source
|
|
100
|
+
return loader
|
jinja2_htmlmin/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jinja2-htmlmin
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Automatic HTML minification for Jinja2 templates
|
|
5
|
+
Project-URL: Repository, https://github.com/Zaczero/jinja2-htmlmin
|
|
6
|
+
Project-URL: Issues, https://github.com/Zaczero/jinja2-htmlmin/issues
|
|
7
|
+
Author-email: Kamil Monicz <kamil@monicz.dev>
|
|
8
|
+
License-Expression: 0BSD
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: fastapi,html,htmlmin,jinja,jinja2,minification,minify,optimization,performance,response,starlette,template,web
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Environment :: Web Environment
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: Zero-Clause BSD (0BSD)
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
23
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Classifier: Topic :: Text Processing :: Markup :: HTML
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Python: >=3.9
|
|
28
|
+
Requires-Dist: htmlmin2
|
|
29
|
+
Requires-Dist: jinja2
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
# jinja2-htmlmin
|
|
33
|
+
|
|
34
|
+
[](https://pypi.org/p/jinja2-htmlmin)
|
|
35
|
+
[](https://liberapay.com/Zaczero/)
|
|
36
|
+
[](https://github.com/sponsors/Zaczero)
|
|
37
|
+
|
|
38
|
+
Automatic HTML minification for Jinja2 templates.
|
|
39
|
+
|
|
40
|
+
## Why Use This?
|
|
41
|
+
|
|
42
|
+
- **Zero runtime overhead**: Minification happens once at load time, not on every render
|
|
43
|
+
- **Smaller HTML**: Reduced bandwidth and faster page loads
|
|
44
|
+
- **Drop-in compatibility**: Works with any Jinja2 loader (Flask, Django, FastAPI, etc.)
|
|
45
|
+
- **Broad Python support**: Compatible with Python 3.9+
|
|
46
|
+
- [**Semantic Versioning**](https://semver.org): Predictable, reliable updates
|
|
47
|
+
- [**Zero-Clause BSD**](https://choosealicense.com/licenses/0bsd/): Public domain, use freely anywhere
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install jinja2-htmlmin
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Quick Start
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from jinja2 import Environment, FileSystemLoader
|
|
59
|
+
from jinja2_htmlmin import minify_loader
|
|
60
|
+
|
|
61
|
+
# Wrap any Jinja2 loader
|
|
62
|
+
# See https://htmlmin.readthedocs.io/en/latest/reference.html for options
|
|
63
|
+
env = Environment(
|
|
64
|
+
loader=minify_loader(
|
|
65
|
+
FileSystemLoader("templates"),
|
|
66
|
+
remove_comments=True,
|
|
67
|
+
remove_empty_space=True,
|
|
68
|
+
remove_all_empty_space=True,
|
|
69
|
+
reduce_boolean_attributes=True,
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# Rendered HTML is automatically minified
|
|
74
|
+
html = env.get_template('index.html').render(title='My Page')
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## How It Works
|
|
78
|
+
|
|
79
|
+
1. Template loads normally through your existing loader
|
|
80
|
+
2. Jinja2 syntax is temporarily protected during minification
|
|
81
|
+
3. HTML content is minified using [htmlmin2](https://github.com/wilhelmer/htmlmin)
|
|
82
|
+
4. Jinja2 syntax is restored and template compiles as usual
|
|
83
|
+
5. Jinja2's built-in caching means this happens only once per template
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
jinja2_htmlmin/__init__.py,sha256=ukX4Mr-I9e4cRcJiEiFQUAT7IwJlySRlu0IZ7oVb9PA,2974
|
|
2
|
+
jinja2_htmlmin/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
jinja2_htmlmin-1.0.0.dist-info/METADATA,sha256=mGx4bly8lNaEWIAIFZl0PdKBnd1jWFfE15Tk3nduAcQ,3353
|
|
4
|
+
jinja2_htmlmin-1.0.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
5
|
+
jinja2_htmlmin-1.0.0.dist-info/licenses/LICENSE,sha256=DbMza-HVDBjY0PhEqZbN3a9moBjb3uWKuUkzwcb_wuk,632
|
|
6
|
+
jinja2_htmlmin-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
BSD Zero Clause License
|
|
2
|
+
|
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
4
|
+
purpose with or without fee is hereby granted.
|
|
5
|
+
|
|
6
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
7
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
8
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
9
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
10
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
11
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
12
|
+
PERFORMANCE OF THIS SOFTWARE.
|