flask-squeeze 2.1.3__tar.gz → 2.2.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: flask-squeeze
3
- Version: 2.1.3
3
+ Version: 2.2.0
4
4
  Summary: Compress and minify Flask responses!
5
5
  Home-page: https://github.com/mkrd/Flask-Squeeze
6
6
  License: MIT
@@ -20,7 +20,6 @@ Classifier: Programming Language :: Python :: 3
20
20
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
21
  Requires-Dist: brotli (>=1,<2)
22
22
  Requires-Dist: flask (>1)
23
- Requires-Dist: htmlmin (>=0.1,<0.2)
24
23
  Requires-Dist: rcssmin (>=1,<2)
25
24
  Requires-Dist: rjsmin (>=1,<2)
26
25
  Project-URL: Repository, https://github.com/mkrd/Flask-Squeeze
@@ -6,9 +6,6 @@ import secrets
6
6
  import random
7
7
 
8
8
  from flask import Flask, Response, request
9
- from rjsmin import jsmin
10
- from rcssmin import cssmin
11
- import htmlmin
12
9
  import brotli
13
10
 
14
11
  from .cache import (
@@ -32,6 +29,12 @@ from .utils import (
32
29
  Minifcation,
33
30
  )
34
31
 
32
+ from .minifiers import (
33
+ minify_html,
34
+ minify_css,
35
+ minify_js,
36
+ )
37
+
35
38
 
36
39
 
37
40
  class Squeeze(object):
@@ -93,11 +96,11 @@ class Squeeze(object):
93
96
 
94
97
  with ctx_add_debug_header("X-Flask-Squeeze-Minify-Duration", response):
95
98
  if self.minify_choice == Minifcation.html:
96
- minified = htmlmin.main.minify(data)
99
+ minified = minify_html(data)
97
100
  elif self.minify_choice == Minifcation.css:
98
- minified = cssmin(data, keep_bang_comments=False)
101
+ minified = minify_css(data)
99
102
  elif self.minify_choice == Minifcation.js:
100
- minified = jsmin(data, keep_bang_comments=False)
103
+ minified = minify_js(data)
101
104
  minified = minified.encode("utf-8")
102
105
 
103
106
  log(3, f"Minify ratio: {len(data) / len(minified):.2f}x")
@@ -0,0 +1,82 @@
1
+ from html.parser import HTMLParser
2
+ import rjsmin
3
+ import rcssmin
4
+
5
+
6
+ class MinifyHTMLParser(HTMLParser):
7
+ def __init__(self):
8
+ super().__init__()
9
+ self.minified_html = []
10
+
11
+ def write(self, data: str) -> None:
12
+ self.minified_html.append(data)
13
+
14
+ def get_minified_html(self) -> str:
15
+ return "".join(self.minified_html)
16
+
17
+ def handle_decl(self, decl: str) -> None:
18
+ self.write(f"<!{decl}>")
19
+
20
+ def unknown_decl(self, data: str) -> None:
21
+ self.write(f"<!{data}>")
22
+
23
+ def handle_pi(self, data):
24
+ self.write(f"<?{data}>")
25
+
26
+ def handle_startendtag(self, tag, attrs):
27
+ self.add_tag(tag, attrs, "/>")
28
+
29
+ def handle_entityref(self, name):
30
+ self.write(f"&{name};")
31
+
32
+ def handle_charref(self, name):
33
+ self.write(f"&#x{name};")
34
+
35
+ def handle_starttag(self, tag, attrs):
36
+ self.add_tag(tag, attrs, ">")
37
+
38
+ # TODO Rename this here and in `handle_startendtag` and `handle_starttag`
39
+ def add_tag(self, tag, attrs, end_tag):
40
+ self.write(f"<{tag}")
41
+ for attr in attrs:
42
+ self.write(f' {attr[0]}')
43
+ if attr[1] is not None:
44
+ self.write(f'="{attr[1]}"')
45
+ self.write(end_tag)
46
+
47
+ def handle_endtag(self, tag):
48
+ self.write(f"</{tag}>")
49
+
50
+ def handle_data(self, data):
51
+ if self.lasttag == "style":
52
+ self.write(minify_css(data).strip())
53
+ elif self.lasttag == "script":
54
+ self.write(minify_js(data).strip())
55
+ elif self.lasttag in ["textarea", "pre", "code"]:
56
+ self.write(data)
57
+ else:
58
+ self.write(data.strip())
59
+
60
+
61
+
62
+ def minify_html(html_text: str) -> str:
63
+ """
64
+ Minifies HTML by removing white space and comments.
65
+ Additionally it uses minify_css and minify_js functions
66
+ to minify css in style tags and js in script tags
67
+ respectively.
68
+ """
69
+
70
+ parser = MinifyHTMLParser()
71
+ parser.feed(html_text)
72
+ return parser.get_minified_html()
73
+
74
+
75
+
76
+ def minify_css(data: str) -> str:
77
+ return rcssmin.cssmin(data, keep_bang_comments=False)
78
+
79
+
80
+
81
+ def minify_js(data: str) -> str:
82
+ return rjsmin.jsmin(data, keep_bang_comments=False)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "flask-squeeze"
3
- version = "2.1.3"
3
+ version = "2.2.0"
4
4
  repository = "https://github.com/mkrd/Flask-Squeeze"
5
5
  description = "Compress and minify Flask responses!"
6
6
  readme = "README.md"
@@ -21,7 +21,6 @@ flask = ">1"
21
21
  brotli = "^1"
22
22
  rjsmin = "^1"
23
23
  rcssmin = "^1"
24
- htmlmin = "^0.1"
25
24
 
26
25
  [tool.poetry.dev-dependencies]
27
26
  pycodestyle = "^2.10.0"
File without changes
File without changes