Skip to content
Open
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
29 changes: 29 additions & 0 deletions pelican/plugins/minify/minify.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,26 @@ def __init__(self, pelican):
minify_js = pelican.settings.get("JS_MIN", True)
minify_inline_css = pelican.settings.get("INLINE_CSS_MIN", True)
minify_inline_js = pelican.settings.get("INLINE_JS_MIN", True)
minify_skip_files = pelican.settings.get("MINIFY_SKIP_FILES", [])
minify_skip_already_minified = pelican.settings.get(
"MINIFY_SKIP_ALREADY_MINIFIED", True
)

for path, _, files in os.walk(pelican.output_path):
for name in files:
path_file = os.path.join(path, name)

if self.skip_file(
path, name, minify_skip_files, minify_skip_already_minified
):
continue

is_html = fnmatch(name, "*.html")
is_css = fnmatch(name, "*.css")
is_js = fnmatch(name, "*.js")

if is_html and minify_html:
LOGGER.debug(f"[MINIFY] File {path_file} minified")
self.write_to_file(
path_file,
lambda content: self.minify(
Expand All @@ -45,12 +55,14 @@ def __init__(self, pelican):
)

if is_css and minify_css:
LOGGER.debug(f"[MINIFY] File {path_file} minified")
self.write_to_file(
path_file,
lambda content: self.minify(content, True, False),
)

if is_js and minify_js:
LOGGER.debug(f"[MINIFY] File {path_file} minified")
self.write_to_file(
path_file, lambda content: self.minify(content, False, True)
)
Expand All @@ -62,6 +74,23 @@ def minify(self, content, minify_css, minify_js):
minify_js=minify_js,
)

# Exclude files already minified or which name or path is contained into the array of excluded files
def skip_file(self, path, name, skip_files, skip_already_minified) -> bool:
path_or_name = [path, name]

if (skip_already_minified and ".min." in name) or (
skip_files
and any(
exclude_name in real_name
for real_name in path_or_name
for exclude_name in skip_files
)
):
LOGGER.debug(f"[MINIFY] File {name} skipped")
return True

return False

@staticmethod
def write_to_file(path_file, callback):
"""Write the file to disk.
Expand Down