mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Merge pull request #2181 from andrew-vant/recursive_epm
Allow directory entries in EXTRA_PATH_METADATA
This commit is contained in:
commit
6fa80ce37d
3 changed files with 48 additions and 3 deletions
|
|
@ -783,7 +783,8 @@ Metadata
|
||||||
|
|
||||||
Extra metadata dictionaries keyed by relative path. Relative paths require
|
Extra metadata dictionaries keyed by relative path. Relative paths require
|
||||||
correct OS-specific directory separators (i.e. / in UNIX and \\ in Windows)
|
correct OS-specific directory separators (i.e. / in UNIX and \\ in Windows)
|
||||||
unlike some other Pelican file settings.
|
unlike some other Pelican file settings. Paths to a directory apply to all
|
||||||
|
files under it. The most-specific path wins conflicts.
|
||||||
|
|
||||||
Not all metadata needs to be :ref:`embedded in source file itself
|
Not all metadata needs to be :ref:`embedded in source file itself
|
||||||
<internal_metadata>`. For example, blog posts are often named following a
|
<internal_metadata>`. For example, blog posts are often named following a
|
||||||
|
|
|
||||||
|
|
@ -698,8 +698,20 @@ def path_metadata(full_path, source_path, settings=None):
|
||||||
if settings.get('DEFAULT_DATE', None) == 'fs':
|
if settings.get('DEFAULT_DATE', None) == 'fs':
|
||||||
metadata['date'] = SafeDatetime.fromtimestamp(
|
metadata['date'] = SafeDatetime.fromtimestamp(
|
||||||
os.stat(full_path).st_mtime)
|
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
|
return metadata
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -273,6 +273,38 @@ class RstReaderTest(ReaderTest):
|
||||||
}
|
}
|
||||||
self.assertDictHasSubset(page.metadata, expected)
|
self.assertDictHasSubset(page.metadata, expected)
|
||||||
|
|
||||||
|
def test_article_extra_path_metadata_recurse(self):
|
||||||
|
parent = "TestCategory"
|
||||||
|
notparent = "TestCategory/article"
|
||||||
|
path = "TestCategory/article_without_category.rst"
|
||||||
|
|
||||||
|
epm = {
|
||||||
|
parent: {'epmr_inherit': parent,
|
||||||
|
'epmr_override': parent, },
|
||||||
|
notparent: {'epmr_bogus': notparent},
|
||||||
|
path: {'epmr_override': path, },
|
||||||
|
}
|
||||||
|
expected_metadata = {
|
||||||
|
'epmr_inherit': parent,
|
||||||
|
'epmr_override': path,
|
||||||
|
}
|
||||||
|
|
||||||
|
page = self.read_file(path=path, EXTRA_PATH_METADATA=epm)
|
||||||
|
self.assertDictHasSubset(page.metadata, expected_metadata)
|
||||||
|
|
||||||
|
# Make sure vars aren't getting "inherited" by mistake...
|
||||||
|
path = "article.rst"
|
||||||
|
page = self.read_file(path=path, EXTRA_PATH_METADATA=epm)
|
||||||
|
for k in expected_metadata.keys():
|
||||||
|
self.assertNotIn(k, page.metadata)
|
||||||
|
|
||||||
|
# Same, but for edge cases where one file's name is a prefix of
|
||||||
|
# another.
|
||||||
|
path = "TestCategory/article_without_category.rst"
|
||||||
|
page = self.read_file(path=path, EXTRA_PATH_METADATA=epm)
|
||||||
|
for k in epm[notparent].keys():
|
||||||
|
self.assertNotIn(k, page.metadata)
|
||||||
|
|
||||||
def test_typogrify(self):
|
def test_typogrify(self):
|
||||||
# if nothing is specified in the settings, the content should be
|
# if nothing is specified in the settings, the content should be
|
||||||
# unmodified
|
# unmodified
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue