Allow directories in EXTRA_PATH_METADATA

Metadata applied to a directory will apply to all files under
it. In case of conflicts, child paths beat parent paths, so metadata
applied to `dir/subdir/file.md` will take precedence over that applied
to `dir/subdir`, which will take precedence over just `dir`.
This commit is contained in:
Andrew Vant 2017-07-16 17:17:51 -04:00
commit ce9f3d55a3
3 changed files with 48 additions and 3 deletions

View file

@ -698,8 +698,20 @@ def path_metadata(full_path, source_path, settings=None):
if settings.get('DEFAULT_DATE', None) == 'fs':
metadata['date'] = SafeDatetime.fromtimestamp(
os.stat(full_path).st_mtime)
metadata.update(settings.get('EXTRA_PATH_METADATA', {}).get(
source_path, {}))
# Apply EXTRA_PATH_METADATA for the source path and the paths of any
# parent directories. Sorting EPM first ensures that the most specific
# path wins conflicts.
epm = settings.get('EXTRA_PATH_METADATA', {})
for path, meta in sorted(epm.items()):
# Enforce a trailing slash when checking for parent directories.
# This prevents false positives when one file or directory's name
# is a prefix of another's.
dirpath = os.path.join(path, '')
if source_path == path or source_path.startswith(dirpath):
metadata.update(meta)
return metadata