-
Notifications
You must be signed in to change notification settings - Fork 0
/
lektor_minify_html.py
33 lines (26 loc) · 1017 Bytes
/
lektor_minify_html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from lektor.pluginsystem import Plugin
import minify_html
import re
class MinifyHtmlPlugin(Plugin):
name = 'Lektor minify html'
description = u'Minify content using minify-html'
def is_valid_minify(self, filename: str) -> bool:
regex = r".+\.(html|js|css)$"
return bool(re.match(regex, filename))
def minify(self, source: str) -> str:
return minify_html.minify(source, minify_js=True, minify_css=True)
def on_after_build(self, builder, **extra) -> None:
if not len(extra["prog"].artifacts):
return
dst_filename = extra["prog"].artifacts[0].dst_filename
if not self.is_valid_minify(dst_filename):
return
try:
with open(dst_filename, 'r+') as file:
html_content = file.read()
file.seek(0)
file.write(self.minify(html_content))
file.truncate()
except UnicodeDecodeError:
# Minification is skipped.
return