Skip to content

Commit

Permalink
Fix context variable precedence.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeanwallace committed Mar 27, 2024
1 parent 3d1c675 commit b69d0b7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 11 deletions.
25 changes: 15 additions & 10 deletions jinjabread/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def generate(self):
continue
try:
page = self.match_page(content_path)
except errors.NoPageMatchedError:
except errors.PageNotMatchedError:
continue
page.generate()

Expand All @@ -69,8 +69,9 @@ def make_page(self, site, content_path):


class Page:
def __init__(self, *, glob_pattern=None):
def __init__(self, *, glob_pattern=None, context=None):
self.glob_pattern = glob_pattern or "**/*"
self.context = context or {}

def setup(self, site, content_path):
self.site = site
Expand All @@ -94,13 +95,13 @@ def _get_sibling_context_list(self):
try:
index_path = find_index_file(path)
page = self.site.match_page(index_path)
except (FileNotFoundError, errors.NoPageMatchedError):
except (FileNotFoundError, errors.PageNotMatchedError):
continue
context_list.append(page.get_context())
continue
try:
page = self.site.match_page(path)
except errors.NoPageMatchedError:
except errors.PageNotMatchedError:
continue
context_list.append(page.get_context())
return context_list
Expand All @@ -111,10 +112,14 @@ def get_context(self):
url_path = "/" + relative_path.parent.as_posix() + "/"
else:
url_path = "/" + relative_path.with_suffix("").as_posix()
context = self.site.config.context | {
"file_path": relative_path.as_posix(),
"url_path": url_path,
}
context = (
self.site.config.context
| self.context
| {
"file_path": relative_path.as_posix(),
"url_path": url_path,
}
)
if self.content_path.stem == "index":
context["pages"] = self._get_sibling_context_list()
return context
Expand All @@ -135,10 +140,10 @@ def generate(self):

class MarkdownPage(Page):

def __init__(self, *, layout_name, glob_pattern=None):
def __init__(self, *, layout_name, glob_pattern=None, **kwargs):
self.layout_name = layout_name
self.markdown = markdown.Markdown(extensions=["full_yaml_metadata"])
super().__init__(glob_pattern=glob_pattern or "**/*.md")
super().__init__(glob_pattern=glob_pattern or "**/*.md", **kwargs)

def get_output_path(self):
return super().get_output_path().with_suffix(Path(self.layout_name).suffix)
Expand Down
2 changes: 1 addition & 1 deletion jinjabread/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ class Error(Exception):
pass


class NoPageMatchedError(Error):
class PageNotMatchedError(Error):
pass
64 changes: 64 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,70 @@ def test_copy_static_directory(self):

self.assertTrue(Path("public/static/dummy.jpg").exists())

def test_context_variable_precedence(self):
with (self.working_dir / "jinjabread.toml").open("w") as file:
file.write(
"""
[context]
fee = "fie"
foe = "fum"
[[pages]]
type = "jinjabread.Page"
glob_pattern = "**/*.txt"
[pages.context]
foe = "foo"
[[pages]]
type = "jinjabread.Page"
glob_pattern = "**/*.csv"
[pages.context]
foe = "bar"
[[pages]]
type = "jinjabread.Page"
glob_pattern = "**/*.html"
"""
)

config = jinjabread.Config.load()
site = jinjabread.Site(config)

txt_page = site.match_page(Path("content/test.txt"))
self.assertDictEqual(
{
"file_path": "test.txt",
"url_path": "/test",
"fee": "fie",
"foe": "foo",
},
txt_page.get_context(),
)

csv_page = site.match_page(Path("content/test.csv"))
self.assertDictEqual(
{
"file_path": "test.csv",
"url_path": "/test",
"fee": "fie",
"foe": "bar",
},
csv_page.get_context(),
)

html_page = site.match_page(Path("content/test.html"))
self.assertDictEqual(
{
"file_path": "test.html",
"url_path": "/test",
"fee": "fie",
"foe": "fum",
},
html_page.get_context(),
)


class NewSiteTest(TestTempWorkingDirMixin, unittest.TestCase):

Expand Down

0 comments on commit b69d0b7

Please sign in to comment.