Skip to content

Commit f4ffc85

Browse files
authored
Merge pull request #22 from pydsigner/minify
Minification Steps
2 parents 2a0ac85 + 1af1069 commit f4ffc85

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ markdown = ["anchovy[jinja]", "markdown_it_py>=3.0.0"]
3434
css = ["tinycss2>=1.1.1"]
3535
pretty = ["rich>=12.5.1"]
3636
pillow = ["Pillow>=9.2.0"]
37-
web = ["anchovy[markdown]", "anchovy[css]", "anchovy[pillow]"]
37+
minify = ["csscompressor>=0.9.5", "minify-html-onepass>=0.11.1"]
38+
web = ["anchovy[markdown]", "anchovy[css]", "anchovy[pillow]", "anchovy[minify]"]
3839
base = ["anchovy[web]", "anchovy[pretty]"]
3940
# Currently, [all] is the same as [base]; this will change in the future if
4041
# heavy dependencies for non-core Steps are added.

src/anchovy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
)
1010
from .images import CWebPStep, ImageMagickStep, IMThumbnailStep, PillowStep, OptipngStep
1111
from .jinja import JinjaMarkdownStep, JinjaRenderStep
12+
from .minify import CSSMinifierStep, HTMLMinifierStep, ResourcePackerStep
1213
from .paths import DirPathCalc, OutputDirPathCalc, REMatcher, WorkingDirPathCalc
1314
from .simple import DirectCopyStep

src/anchovy/jinja.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class JinjaRenderStep(Step):
2323

2424
@classmethod
2525
def get_dependencies(cls):
26-
return super().get_dependencies() | {
26+
return {
2727
pip_dependency('jinja2'),
2828
}
2929

src/anchovy/minify.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import shutil
2+
from pathlib import Path
3+
4+
from .core import ContextDir, Step
5+
from .dependencies import pip_dependency
6+
7+
8+
class ResourcePackerStep(Step):
9+
def __init__(self, source_dir: ContextDir = 'input_dir'):
10+
self.source_dir: ContextDir = source_dir
11+
12+
def __call__(self, path: Path, output_paths: list[Path]):
13+
parent_dir = self.context[self.source_dir]
14+
data = '\n\n'.join(
15+
(parent_dir / filename.strip()).read_text('utf-8')
16+
for filename in path.open() if filename
17+
)
18+
19+
for o_path in output_paths:
20+
o_path.parent.mkdir(parents=True, exist_ok=True)
21+
output_paths[0].write_text(data, 'utf-8')
22+
for o_path in output_paths[1:]:
23+
shutil.copy(output_paths[0], o_path)
24+
25+
26+
class CSSMinifierStep(Step):
27+
@classmethod
28+
def get_dependencies(cls):
29+
return {
30+
pip_dependency('csscompressor'),
31+
}
32+
33+
def __call__(self, path: Path, output_paths: list[Path]):
34+
import csscompressor
35+
data = csscompressor.compress(path.read_text('utf-8'))
36+
for o_path in output_paths:
37+
o_path.parent.mkdir(parents=True, exist_ok=True)
38+
output_paths[0].write_text(data, 'utf-8')
39+
for o_path in output_paths[1:]:
40+
shutil.copy(output_paths[0], o_path)
41+
42+
43+
class HTMLMinifierStep(Step):
44+
@classmethod
45+
def get_dependencies(cls):
46+
return {
47+
pip_dependency('minify-html-onepass', check_name='minify_html_onepass'),
48+
}
49+
50+
def __call__(self, path: Path, output_paths: list[Path]):
51+
import minify_html_onepass
52+
data = minify_html_onepass.minify(path.read_text('utf-8'))
53+
for o_path in output_paths:
54+
o_path.parent.mkdir(parents=True, exist_ok=True)
55+
output_paths[0].write_text(data, 'utf-8')
56+
for o_path in output_paths[1:]:
57+
shutil.copy(output_paths[0], o_path)

0 commit comments

Comments
 (0)