Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve globs using pathlib.glob() #37

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ plugins:

> **Note:** When using `minify_js` or `minify_css`, you don't have to modify the `extra_javascript` or `extra_css` entries
in your `mkdocs.yml` file. The plugins automatically takes care of that.
Both `minify_js` and `minify_css` support the use of **globs** (e.g. `**/*.css`).

[pypi-link]: https://pypi.python.org/pypi/mkdocs-minify-plugin/
[python-image]: https://img.shields.io/pypi/pyversions/mkdocs-minify-plugin?logo=python&logoColor=aaaaaa&labelColor=333333
27 changes: 23 additions & 4 deletions mkdocs_minify_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
An MkDocs plugin to minify HTML, JS or CSS files prior to being written to disk
"""
import hashlib
from pathlib import Path
import os
from typing import Callable, Dict, List, Optional, Tuple, Union

Expand Down Expand Up @@ -82,19 +83,37 @@ def _minify(self, file_type: str, config: MkDocsConfig) -> None:
if not isinstance(file_paths, list):
file_paths = [file_paths]

for file_path in file_paths:
site_file_path: str = f"{config['site_dir']}/{file_path}".replace("\\", "/")
site_dir = Path(config['site_dir'])

file_paths2 = []
for file_path in file_paths.copy():
if "*" in file_path:
glob_parts = file_path.split("*", maxsplit=1)
glob_dir = site_dir / Path(glob_parts[0])
file_path = glob_dir.glob(f"*{glob_parts[1]}")

for glob_file in file_path:
file_paths2.append(glob_file)
else:
file_paths2.append(site_dir / file_path)

# remove duplicates
file_paths2 = list(set(file_paths2))

for file_path in file_paths2:
site_file_path: str = str(file_path.as_posix())
rel_file_path: str = site_file_path.replace(site_dir.as_posix(), "").strip("/")

with open(site_file_path, mode="r+", encoding="utf8") as file:
if self.config["cache_safe"]:
file.write(self.path_to_data[file_path])
file.write(self.path_to_data[rel_file_path])
else:
minified: str = self._minify_file_data_with_func(file.read(), minify_func)
file.seek(0)
file.write(minified)
file.truncate()

file_hash: str = self.path_to_hash.get(file_path, "")
file_hash: str = self.path_to_hash.get(rel_file_path, "")

# Rename to [.hash].min.{file_type}
os.rename(site_file_path, self._minified_asset(site_file_path, file_type, file_hash))
Expand Down
16 changes: 8 additions & 8 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ def test_build_no_extras(mkdocs_build: int, extra_dir: Path, tmp_path: Path) ->
_assert_minified_js(extra_dir / "js" / "script.min.js")
_assert_minified_template_no_extras(
file_path=tmp_path / "site" / "index.html",
file_hash="544b92316ee6219418120cfc0347f6f1b996f93833287777345da06a38f5f05e374d87a1a35e6bee96508169262d8f97",
file_hash="468e45e1d823325ceebfd1c3eaa6e390c9629b43eaefcbdf5ff5d3c77cb358e10737d19d44ab0ad17f66a9935800c135",
)
_assert_minified_template_no_extras(
file_path=tmp_path / "site" / "404.html",
file_hash="91b1fdfb1b0eb78a20992da953b22f4a72b86336bcf7845cd976b5e4a84337b1bdedab7a09f869d9cab80fcb3bd872dc",
file_hash="f8ab14b0fc7865c4eabaa9a4ce0c5db191e7b1fb6ca28e450d070847eb3519d2e6cf64c65607245bdc8f414007014c92",
)


Expand All @@ -76,11 +76,11 @@ def test_build_with_extras(mkdocs_build_with_extras: int, extra_dir: Path, tmp_p
_assert_minified_js(extra_dir / "js" / "script.min.js")
_assert_template_with_extras(
file_path=tmp_path / "site" / "index.html",
file_hash="c102275d47fb49aeda0c2e35c35daf5da1ef5ce96f5360cf329578dc65e70b53249b20017b77bcc2516f672917d75762",
file_hash="b164f41214bc362faf6a43c57be6c783a276ae188450b9f91ba27d4ffd581426e7e7902c9ce1dc93279f4b0badb92ec1",
)
_assert_template_with_extras(
file_path=tmp_path / "site" / "404.html",
file_hash="e4d3e614fa3a73e6c0b4231d443009a2bfa3fcbd5f548b19b58222d04fcd4c5dadf702904952b7ea3f1cc124d3904c77",
file_hash="e2dc0e1cc11452d4d1ec5b22a626a36d134b253e7ac8201b57c88b600a0011e262e9c50bd7a57f1b56dfd3eab08b14cf",
)


Expand Down Expand Up @@ -109,15 +109,15 @@ def test_build_cache_safe(mkdocs_build_cache_safe: int, extra_dir: Path, tmp_pat
file_path=tmp_path / "site" / "index.html",
css_prefix=css_hash_prefix,
js_prefix=js_hash_prefix,
file_hash="315459f1b231791738d6a2be955a469a06a94b9135a776c42cc03250b1b1f3cf9ac0232d9ae34ff7709bf37fa64af558",
file_hash="6ae88614660e302573660221fce668fc9cdbadb8fccf1cb1ec4a912d09518d3755773b3d398dbdef97fa9be2bcc6e322",
minified_extras=False,
minified_html=False,
)
_assert_template_with_extras(
file_path=tmp_path / "site" / "404.html",
css_prefix=css_hash_prefix,
js_prefix=js_hash_prefix,
file_hash="10e73a27d3d134aff777399fb8cb2c044d1108f2b91457b7a84026c9afd23e16719eca9f922f977c33f34dcb69017227",
file_hash="735db5fb6dcfd9bcc09eb287c6303e8c88d7ce8db71d59c1dbb02e837a83836395b508bfbac43652d530f781761e40fc",
minified_extras=False,
minified_html=False,
)
Expand All @@ -136,13 +136,13 @@ def test_build_cache_safe_minified(
file_path=tmp_path / "site" / "index.html",
css_prefix=css_hash_prefix,
js_prefix=js_hash_prefix,
file_hash="4e4abcfcf21f0220799141c9aa67500ff95ade5725351f7f2980a3484dc285c222864b6065bb6ed8d91343e7c0398357",
file_hash="24ebe25956981b302e11251dd82cd1b58087195a2f5fe2fec57534705a66fbabba233a367f6d1431ce28bd4f6d8c5f85",
)
_assert_template_with_extras(
file_path=tmp_path / "site" / "404.html",
css_prefix=css_hash_prefix,
js_prefix=js_hash_prefix,
file_hash="ba1971d561cbd38ece2bb7f45aff9fadf7fe7b766bcd71341af3b99b7109c5c3dc9930c37ea69f05f3c841fc698147b6",
file_hash="76f4c03cd62fb3a2af689cddbb1ff1cb32bee269c329c819df462c203e4f74d88adbd227e582ee7305f8b26a226a9187",
)


Expand Down