Skip to content

Commit aaa2b52

Browse files
authored
Merge pull request #38 from pydsigner/general_enhancements
General enhancements
2 parents d49e9ad + 3bcad39 commit aaa2b52

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

pyproject.toml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["setuptools>=61.0", "setuptools-scm"]
2+
requires = ["setuptools>=64.0", "setuptools-scm"]
33
build-backend = "setuptools.build_meta"
44

55
[project]
@@ -28,13 +28,16 @@ classifiers = [
2828
]
2929
dynamic = ["version"]
3030

31+
[project.urls]
32+
Source = "https://github.com/pydsigner/anchovy"
33+
3134
[project.optional-dependencies]
3235
jinja = ["Jinja2>=3.1.2"]
3336
markdown = ["anchovy[jinja]", "markdown_it_py>=3.0.0"]
3437
css = ["tinycss2>=1.1.1"]
3538
pretty = ["rich>=12.5.1"]
3639
pillow = ["Pillow>=9.2.0"]
37-
minify = ["csscompressor>=0.9.5", "minify-html-onepass>=0.11.1"]
40+
minify = ["lightningcss>=0.1.1,<1.0", "minify-html-onepass>=0.11.1"]
3841
web = ["anchovy[markdown]", "anchovy[css]", "anchovy[pillow]", "anchovy[minify]"]
3942
base = ["anchovy[web]", "anchovy[pretty]"]
4043
# Currently, [all] is the same as [base]; this will change in the future if
@@ -43,7 +46,12 @@ all = ["anchovy[base]"]
4346
# Includes all possible dependencies, including fallbacks that will not be used
4447
# under normal circumstances, for linting purposes. End users are intended to
4548
# ignore this option.
46-
cq = ["anchovy[all]", "commonmark>=0.9.1", "markdown>=3.4.3", "mistletoe>=1.1.0", "tqdm>=4.65.0"]
49+
cq = [
50+
"anchovy[all]",
51+
"commonmark>=0.9.1", "markdown>=3.4.3", "mistletoe>=1.1.0",
52+
"tqdm>=4.65.0",
53+
"minify-html>=0.11.1",
54+
]
4755

4856
[project.scripts]
4957
anchovy = "anchovy.cli:main"

src/anchovy/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ def main():
176176
context: Context | None = namespace.get('CONTEXT')
177177
else:
178178
label: str = f'-m {args.module.__name__}'
179-
settings: InputBuildSettings | None = getattr(args.module, 'SETTINGS')
180-
rules: list[Rule] | None = getattr(args.module, 'RULES')
181-
context: Context | None = getattr(args.module, 'CONTEXT')
179+
settings: InputBuildSettings | None = getattr(args.module, 'SETTINGS', None)
180+
rules: list[Rule] | None = getattr(args.module, 'RULES', None)
181+
context: Context | None = getattr(args.module, 'CONTEXT', None)
182182

183183
if args.audit_steps:
184184
audit_rules = context.rules if context else rules

src/anchovy/css/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44

55
from ..core import Step
6-
from ..dependencies import Dependency, import_install_check
6+
from ..dependencies import pip_dependency
77

88

99
class AnchovyCSSStep(Step):
@@ -13,9 +13,9 @@ class AnchovyCSSStep(Step):
1313
encoding = 'utf-8'
1414

1515
@classmethod
16-
def get_dependencies(cls) -> set[Dependency]:
17-
return super().get_dependencies() | {
18-
Dependency('tinycss2', 'pip', import_install_check),
16+
def get_dependencies(cls):
17+
return {
18+
pip_dependency('tinycss2'),
1919
}
2020

2121
def __call__(self, path: Path, output_paths: list[Path]):

src/anchovy/minify.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def __init__(self, source_dir: ContextDir = 'input_dir'):
1414
def __call__(self, path: Path, output_paths: list[Path]):
1515
parent_dir = self.context[self.source_dir]
1616
data = '\n\n'.join(
17-
(parent_dir / filename.strip()).read_text(self.encoding)
18-
for filename in path.open() if filename
17+
(parent_dir / f).read_text(self.encoding)
18+
for filename in path.open() if (f := filename.strip()) and not f.startswith('#')
1919
)
2020

2121
for o_path in output_paths:
@@ -27,15 +27,37 @@ def __call__(self, path: Path, output_paths: list[Path]):
2727

2828
class CSSMinifierStep(Step):
2929
encoding = 'utf-8'
30+
3031
@classmethod
3132
def get_dependencies(cls):
3233
return {
33-
pip_dependency('csscompressor'),
34+
pip_dependency('lightningcss')
3435
}
3536

37+
def __init__(self,
38+
error_recovery: bool = False,
39+
parser_flags: dict[str, bool] | None = None,
40+
unused_symbols: set[str] | None = None,
41+
browsers_list: list[str] | None = ['defaults'],
42+
minify: bool = True):
43+
44+
self.error_recovery = error_recovery
45+
self.parser_flags = parser_flags or {}
46+
self.unused_symbols = unused_symbols
47+
self.browsers_list = browsers_list
48+
self.minify = minify
49+
3650
def __call__(self, path: Path, output_paths: list[Path]):
37-
import csscompressor
38-
data = csscompressor.compress(path.read_text(self.encoding))
51+
import lightningcss
52+
data = lightningcss.process_stylesheet(
53+
path.read_text(self.encoding),
54+
filename=str(path),
55+
error_recovery=self.error_recovery,
56+
parser_flags=lightningcss.calc_parser_flags(**self.parser_flags),
57+
unused_symbols=self.unused_symbols,
58+
browsers_list=self.browsers_list,
59+
minify=self.minify
60+
)
3961
for o_path in output_paths:
4062
o_path.parent.mkdir(parents=True, exist_ok=True)
4163
output_paths[0].write_text(data, self.encoding)
@@ -45,16 +67,30 @@ def __call__(self, path: Path, output_paths: list[Path]):
4567

4668
class HTMLMinifierStep(Step):
4769
encoding = 'utf-8'
70+
minify_css = False
71+
minify_js = False
4872

4973
@classmethod
5074
def get_dependencies(cls):
5175
return {
52-
pip_dependency('minify-html-onepass', check_name='minify_html_onepass'),
76+
(
77+
pip_dependency('minify-html-onepass', check_name='minify_html_onepass')
78+
| pip_dependency('minify-html', check_name='minify_html')
79+
),
5380
}
5481

5582
def __call__(self, path: Path, output_paths: list[Path]):
56-
import minify_html_onepass
57-
data = minify_html_onepass.minify(path.read_text(self.encoding))
83+
params = {'minify_css': self.minify_css, 'minify_js': self.minify_js}
84+
try:
85+
from minify_html_onepass import minify
86+
except ImportError:
87+
from minify_html import minify
88+
params |= {
89+
'do_not_minify_doctype': True,
90+
'ensure_spec_compliant_unquoted_attribute_values': True,
91+
'keep_spaces_between_attributes': True,
92+
}
93+
data = minify(path.read_text(self.encoding), **params)
5894
for o_path in output_paths:
5995
o_path.parent.mkdir(parents=True, exist_ok=True)
6096
output_paths[0].write_text(data, self.encoding)

0 commit comments

Comments
 (0)