Skip to content

Commit

Permalink
Changed behavior of excluded_files option, now accepts wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Wilhelmer committed Jul 17, 2023
1 parent 6e895af commit 74e28c6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ INFO - The following files exist in the docs directory, but may be unused:

* `dir`: The directory where to search for unused files. Path is relative to `docs_dir`. The plugin recurses all subdirectories. For example, if you specify `images` and `docs_dir` is set to `docs`, the plugin searches in `docs/images`, including all subdirectories. Defaults to `docs_dir`.
* `file_types`: List of file types the plugin should process (whitelist). If empty or omitted, all files **except Markdown (md)** files will be processed. Defaults to `[]`.
* `excluded_files`: List of files (relative to `dir`) which are explicitly excluded. Works in combination with `file_types`. Defaults to `[]`.
* `excluded_files`: List of files the plugin should **not** process (blacklist). Works in combination with `file_types`. Entries apply to `dir` and all its subdirectories. Do not specify paths here, only file names. You can use wildcards. For example, `foo-*.jpg` excludes all JPG files prefixed with `foo-` in all directories. Defaults to `[]`.
* `strict`: Elevates the log level to `warning`. This allows you to use MkDocs' strict flag (`mkdocs build -s`) to abort a build if unused files exist. Defaults to `false`.
* `enabled`: This option specifies whether the plugin is enabled when building your project. If you want to switch the plugin off, e.g. for local builds, use an [environment variable](https://www.mkdocs.org/user-guide/configuration/#environment-variables). Defaults to `true`.

Expand All @@ -62,7 +62,8 @@ plugins:
- jpg
- svg
excluded_files:
- css/favicon.png
- favicon.png
- foo-*.jpg
strict: true
enabled: !ENV [CI, false]
```
8 changes: 7 additions & 1 deletion mkdocs_unused_files/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import logging
import urllib.parse
from fnmatch import fnmatch
from mkdocs.config import config_options
from mkdocs.plugins import BasePlugin
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -50,7 +51,12 @@ def on_files(self, files, config):
# Create entry from relative path between full path and docs_dir + filename
# When path and docs_dir are identical, relpath returns ".". We use normpath() to resolve that
entry = os.path.normpath(os.path.join(os.path.relpath(path, config.docs_dir), file))
if entry in self.config['excluded_files']:
# Check whether file is excluded
is_excluded = False
for excluded_file in self.config['excluded_files']:
if fnmatch(file, excluded_file):
is_excluded = True
if is_excluded:
continue
self.file_list.append(entry)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='mkdocs-unused-files',
version='0.1.7',
version='0.2.0',
description='An MkDocs plugin to find unused (orphaned) files in your project.',
long_description='',
keywords='mkdocs',
Expand Down

0 comments on commit 74e28c6

Please sign in to comment.