Pelican plugin wrapper for 'web-compressor'
Project description
Pelican plugin for web-compressor
pelican-compress optimizes any Pelican site's output - using sane defaults & insane tools.
Installation
It's available from PyPi using a package manager of your choice:
# Using 'pip'
pip install pelican-compressor
# Using 'poetry'
poetry add pelican-compressor
Getting started
Using pelican-compressor is straightforward:
Plugin
After installing it, the following features are enabled by default:
- minification of HTML files & web assets, such as CSS/JS, JSON & XML files
- cachebusting assets, such as CSS/JS, JSON & XML files, fonts and images
- creating SRI hashes for
link&scripttags
See 'Configuration' below for full control over these processes and even more features, such as
- optimizing all JPEG & PNG images
- converting them to modern file formats such as AVIF/WebP
- building CSP directives (including nonces for inline scripts & styles)
Commandline
Installing pelican-compressor also gives you access to webcompr, which might be interesting for more flexibility.
Configuration
The following settings need to be prepended by COMPRESSOR_ to work, eg for enabling AVIF/WebP generation, define COMPRESSOR_ENABLE_MODERN_FORMATS = True in your settings (most likely pelicanconf.py).
General
| Setting | Type | Default | Description |
|---|---|---|---|
BLOCK_LIST |
list |
[] |
Filenames to be excluded |
Asset minification
| Setting | Type | Default | Description |
|---|---|---|---|
ENABLE_MINIFY |
bool |
true |
Enables/disables feature |
MINIFY_MEDIATYPES |
list or re.Pattern |
HTML, CSS, JS, SVG | Files to be minified |
MINIFY_OPTIONS |
dict |
see below | Minification settings |
For available minification settings, see here.
Image optimizations
| Setting | Type | Default | Description |
|---|---|---|---|
ENABLE_IMAGEOPTIM |
bool |
false |
Enables/disables feature |
IMAGEOPTIM_MEDIATYPES |
list or re.Pattern |
JPG/PNG | Files to be optimized |
JPEG_QUALITY |
int |
85 |
JPEG output quality |
STRIP_METADATA |
bool |
true |
Whether to remove metadata |
Asset hashing / "cachebusting"
| Setting | Type | Default | Description |
|---|---|---|---|
ENABLE_HASHING |
bool |
true |
Enables/disables feature |
HASHING_MEDIATYPES |
list or re.Pattern |
CSS/JS files, fonts & images | Files to be hashed |
HASH_LENGTH |
int |
10 |
Length of appended hash string |
HASH_MODIFIED |
bool |
false |
Hash modified time or file contents |
AVIF / WebP generation
| Setting | Type | Default | Description |
|---|---|---|---|
ENABLE_MODERN_FORMATS |
bool |
false |
Enables/disables feature |
AVIF_SETTINGS |
dict |
{"quality": 90} |
AVIF settings (see below) |
WEBP_SETTINGS |
dict |
{"method": 6} |
WebP settings (see below) |
Available WebP settings:
losslessqualitymethodicc_profileexif
For more information, see Pillow docs.
Available AVIF settings:
advancedalpha_premultipliedappend_imagescodecdurationexificc_profileqmaxqminqualityrangespeedsubsamplingtile_rowsxmp/XML:com.adobe.xmp
Note: AVIF generation is provided through a plugin, which hasn't made it into Pillow's core yet, so consider this feature as being experimental!
For more information (eg default values), check out the info.get() calls inside the _save() method, courtesy of the pillow_avif plugin.
Subresource integrity
| Setting | Type | Default | Description |
|---|---|---|---|
ENABLE_SRI |
str |
true |
Enables/disables feature |
SRI_DIGEST |
str |
'sha512' |
Cryptographic digest to use |
Content security policy
| Setting | Type | Default | Description |
|---|---|---|---|
ENABLE_CSP |
bool |
false |
Enables/disables feature |
CSP_DIGEST |
str |
'sha512' |
Cryptographic digest to use |
CSP_NONCE |
str |
uuid.uuid4().hex |
Random nonce to be used |
CSP_DIRECTIVES |
dict |
{} |
CSP directives (see below) |
When creating a content security policy, you might want to start with something restrictive and go from there:
COMPRESSOR_ENABLE_CSP = True
COMPRESSOR_CSP_DIRECTIVES = {
"default-src": "none",
"script-src": "'strict-dynamic'",
"object-src": "none",
"base-uri": "none",
},
Hooks
It's also prossible to assign functions to be called right before this plugin (PRE_HOOK) or immediately thereafter (POST_HOOK), eg for creating image variants (such as placeholders for lazyloading). Each function receives the argument asset, which is a tuple consisting of the pathlib.Path to each asset file and a str representing its MIME type:
# pelicanconf.py
COMPRESSOR_PRE_HOOK = lambda asset: (
# Unpack asset file & mediatype
file_path, mime_type = asset
# If image file ..
if mime_type == "image/jpeg":
# .. do something
)
Roadmap
- Update
README.md - Add tests
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pelican-compressor-0.2.1.tar.gz.
File metadata
- Download URL: pelican-compressor-0.2.1.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.1 CPython/3.10.4 Linux/5.19.0-76051900-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5889cd1ea6cbe6a2c5482e83de89f0baabcb6becdcf6a06d0a339b05d829b40d
|
|
| MD5 |
8351727ff03582667ca90cd4465e521e
|
|
| BLAKE2b-256 |
4ddd75f9916482d58f9e98659d6415fc87a7f329a6d3dc6ef74a58d89a29a58f
|
File details
Details for the file pelican_compressor-0.2.1-py3-none-any.whl.
File metadata
- Download URL: pelican_compressor-0.2.1-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.1 CPython/3.10.4 Linux/5.19.0-76051900-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b86b7af75c175daabf0305739639e38f44050dbeb41d989590b709da39af4024
|
|
| MD5 |
ca8daf68e9c63475eb32df6f805d6cc7
|
|
| BLAKE2b-256 |
f750a5633edd3e6030d5e2f9a3ccea9aedd49fcc5e8472d0975908f90d403528
|