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

Added config param 'excluded dirs', to exclude whole dir from check by relative path #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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 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 `[]`.
* `excluded_dirs`: List of relative dir paths the plugin should **not** process (blacklist). The list of paths relative to `docs_dir`. All directory will be ignored including all nested dirs and files.
* `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 @@ -56,14 +57,17 @@ INFO - The following files exist in the docs directory, but may be unused:
```yml
plugins:
- unused_files:
dir: images
file_types:
- png
- jpg
- svg
excluded_files:
- favicon.png
- foo-*.jpg
strict: true
enabled: !ENV [CI, false]
dir: images
file_types:
- png
- jpg
- svg
excluded_files:
- favicon.png
- foo-*.jpg
excluded_dirs:
- assets/somedir
- some/ingored/dir
strict: true
enabled: !ENV [CI, false]
```
45 changes: 37 additions & 8 deletions mkdocs_unused_files/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

log = logging.getLogger('mkdocs')


class UnusedFilesPlugin(BasePlugin):

file_list = []

config_scheme = (
('dir', config_options.Type(str, default='')),
('file_types',config_options.Type((str, list), default=[])),
('file_types', config_options.Type((str, list), default=[])),
('excluded_files', config_options.Type((str, list), default=[])),
('excluded_dirs', config_options.Type((str, list), default=[])),
('strict', config_options.Type(bool, default=False)),
('enabled', config_options.Type(bool, default=True)),
)
Expand All @@ -36,26 +38,50 @@ def on_startup(self, *, command, dirty):
if not self.config['enabled']:
return
# Disable plugin when the documentation is served, i.e., "mkdocs serve" is used
if command == "serve":
if command == 'serve':
self.config['enabled'] = False
log.info("Unused-files plugin disabled while MkDocs is running in 'serve' mode.")
log.info(
"Unused-files plugin disabled while MkDocs is running in 'serve' mode."
)

def on_files(self, files, config):
dir = os.path.join(config.docs_dir, self.config['dir'])
# Get all files in directory
# Prepare list of excluded dirs' relative paths, according to
# platform path separator
excluded_dirs = [
os.path.join(*entry.split('/'))
for entry in self.config['excluded_dirs']
]
# Iterate recursively in directory
for path, _, files in os.walk(dir):
# Check and exclude whole dir if it is in excluded dirs
rel_dir_path = os.path.relpath(path, config.docs_dir)
is_excluded = False
for entry in excluded_dirs:
if rel_dir_path.startswith(entry):
is_excluded = True
break

if is_excluded:
continue

for file in files:
# Add all files with the given types to file_list
# If no types were given, add all files except Markdown files
if not file.endswith("md") and self._matches_type(file):
if not file.endswith('md') and self._matches_type(file):
# 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))
entry = os.path.normpath(
os.path.join(
os.path.relpath(path, config.docs_dir), file
)
)
# Check whether file is excluded
is_excluded = False
for excluded_file in self.config['excluded_files']:
if fnmatch(file, excluded_file):
is_excluded = True
break
if is_excluded:
continue
self.file_list.append(entry)
Expand Down Expand Up @@ -83,5 +109,8 @@ def on_post_build(self, config):
if self.config['strict']:
logger = log.warning
if self.file_list:
logger('The following files exist in the docs directory, but may be unused:\n - {}'.format('\n - '.join(self.file_list)))

logger(
'The following files exist in the docs directory, but may be unused:\n - {}'.format(
'\n - '.join(self.file_list)
)
)
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.2.0',
version='0.3.0',
description='An MkDocs plugin to find unused (orphaned) files in your project.',
long_description='',
keywords='mkdocs',
Expand Down