Skip to content

Commit

Permalink
Fix url_path context variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeanwallace committed Mar 27, 2024
1 parent d9d2fa9 commit ae2e7ab
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
8 changes: 5 additions & 3 deletions jinjabread/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down

0 comments on commit ae2e7ab

Please sign in to comment.