From ae2e7ab7ae58580ac87b569e1042eb00c3a2c176 Mon Sep 17 00:00:00 2001 From: Jason Wallace Date: Wed, 27 Mar 2024 09:36:28 +0200 Subject: [PATCH] Fix url_path context variable. --- jinjabread/base.py | 8 ++++-- tests/tests.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/jinjabread/base.py b/jinjabread/base.py index 7ef934b..d3fec04 100644 --- a/jinjabread/base.py +++ b/jinjabread/base.py @@ -108,10 +108,12 @@ def _get_sibling_context_list(self): def get_context(self): relative_path = self.output_path.relative_to(self.site.config.output_dir) - if relative_path.stem == "index": - url_path = "/" + relative_path.parent.as_posix() + "/" - else: + if not relative_path.stem == "index": url_path = "/" + relative_path.with_suffix("").as_posix() + elif not relative_path.parent.name: + url_path = "/" + else: + url_path = "/" + relative_path.parent.as_posix() + "/" context = ( self.site.config.context | self.context diff --git a/tests/tests.py b/tests/tests.py index abc3cba..2b346b4 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -512,6 +512,76 @@ def test_context_variable_precedence(self): html_page.get_context(), ) + def test_context_variables_on_root_index_page(self): + index_file = self.working_dir / "content" / "index.html" + index_file.parent.mkdir(parents=True) + index_file.touch() + + config = jinjabread.Config.load() + site = jinjabread.Site(config) + + index_page = site.match_page(Path("content/index.html")) + self.assertDictEqual( + { + "file_path": "index.html", + "url_path": "/", + "pages": [], + }, + index_page.get_context(), + ) + + def test_context_variables_on_dir_index_page(self): + index_file = self.working_dir / "content" / "posts" / "index.html" + index_file.parent.mkdir(parents=True) + index_file.touch() + + config = jinjabread.Config.load() + site = jinjabread.Site(config) + + index_page = site.match_page(Path("content/posts/index.html")) + self.assertDictEqual( + { + "file_path": "posts/index.html", + "url_path": "/posts/", + "pages": [], + }, + index_page.get_context(), + ) + + def test_context_variables_on_root_page(self): + index_file = self.working_dir / "content" / "about.html" + index_file.parent.mkdir(parents=True) + index_file.touch() + + config = jinjabread.Config.load() + site = jinjabread.Site(config) + + index_page = site.match_page(Path("content/about.html")) + self.assertDictEqual( + { + "file_path": "about.html", + "url_path": "/about", + }, + index_page.get_context(), + ) + + def test_context_variables_on_dir_page(self): + index_file = self.working_dir / "content" / "posts" / "post1.html" + index_file.parent.mkdir(parents=True) + index_file.touch() + + config = jinjabread.Config.load() + site = jinjabread.Site(config) + + index_page = site.match_page(Path("content/posts/post1.html")) + self.assertDictEqual( + { + "file_path": "posts/post1.html", + "url_path": "/posts/post1", + }, + index_page.get_context(), + ) + class NewSiteTest(TestTempWorkingDirMixin, unittest.TestCase):