From 88b5a27ddf9f2334108825ef3fbac44868cc5d60 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 3 Jan 2013 18:10:08 -0500 Subject: [PATCH 1/2] contents: Page.url_format should expose all metadata I want to add `directory` metadata to each page in `content/pages/` to place my non-article pages by hand: PAGE_URL = '{directory}/{slug}' PAGE_SAVE_AS = '{directory}/{slug}/index.html' To do this, I need the `directory` metadata for formatting the URL. --- pelican/contents.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pelican/contents.py b/pelican/contents.py index d675a2ad..b986a34e 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -114,14 +114,16 @@ class Page(object): @property def url_format(self): - return { + metadata = copy.copy(self.metadata) + metadata.update({ 'slug': getattr(self, 'slug', ''), 'lang': getattr(self, 'lang', 'en'), 'date': getattr(self, 'date', datetime.now()), 'author': getattr(self, 'author', ''), 'category': getattr(self, 'category', self.settings['DEFAULT_CATEGORY']), - } + }) + return metadata def _expand_settings(self, key): fq_key = ('%s_%s' % (self.__class__.__name__, key)).upper() From 733ab8ae6ce59749ca9152bb5ad5cdca50a3e688 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 15 Jan 2013 23:05:48 -0500 Subject: [PATCH 2/2] test_contents: Add tests for metadata export from Page.url_format --- tests/test_contents.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_contents.py b/tests/test_contents.py index a8b9877f..ba94e14e 100644 --- a/tests/test_contents.py +++ b/tests/test_contents.py @@ -99,6 +99,16 @@ class TestPage(unittest.TestCase): page = Page(**self.page_kwargs) self.assertEqual(page.save_as, "pages/foo-bar-fr.html") + def test_metadata_url_format(self): + """Arbitrary metadata should be passed through url_format() + """ + page = Page(**self.page_kwargs) + self.assertIn('summary', page.url_format.keys()) + page.metadata['directory'] = 'test-dir' + page.settings = _DEFAULT_CONFIG.copy() + page.settings['PAGE_SAVE_AS'] = '{directory}/{slug}' + self.assertEqual(page.save_as, 'test-dir/foo-bar') + def test_datetime(self): """If DATETIME is set to a tuple, it should be used to override LOCALE """