Skip to content

Commit

Permalink
610 - fixes how num of pages (#614)
Browse files Browse the repository at this point in the history
* fixes how num of pages

* Format and lint with Ruff

---------

Co-authored-by: Ruff <ruff-action@users.noreply.github.com>
  • Loading branch information
kjaymiller and Ruff committed Feb 24, 2024
1 parent 984fdcf commit 438154f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/render_engine/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __init__(
template_vars: dict[str, any],
routes: list[str | pathlib.Path],
archive_index: int = 0,
num_archive_pages: int = 1,
is_index: bool = False,
plugin_manager: PluginManager | None = None,
template: str | jinja2.Template = "archive.html",
Expand Down
12 changes: 8 additions & 4 deletions src/render_engine/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@ def __init__(
if getattr(self, "items_per_page", False):
self.has_archive = True
self.title = self._title
self.template_vars = getattr(self, "template_vars", {})

def iter_content_path(self):
"""Iterate through in the collection's content path."""

return flatten([pathlib.Path(self.content_path).glob(suffix) for suffix in self.include_suffixes])

def _generate_content_from_modified_pages(self) -> typing.Generator[Page, None, None]:
def _generate_content_from_modified_pages(
self,
) -> typing.Generator[Page, None, None]:
"""
Check git status for newly created and modified files.
Returns the Page objects for the files in the content path
Expand Down Expand Up @@ -163,17 +166,18 @@ def archives(self) -> typing.Generator[Archive, None, None]:

if items_per_page != len(sorted_pages):
archives.extend(list(batched(sorted_pages, items_per_page)))
num_archive_pages = len(archives)
self.template_vars["num_of_pages"] = len(archives) - 1 / items_per_page
else:
self.template_vars["num_of_pages"] = 1

for index, pages in enumerate(archives):
yield Archive(
pages=pages,
template=getattr(self, "archive_template", None),
template_vars=getattr(self, "template_vars", {}),
template_vars=self.template_vars,
title=self._title,
routes=self.routes,
archive_index=index,
num_archive_pages=num_archive_pages,
plugin_manager=getattr(self, "plugin_manager", None),
is_index=not index,
)
Expand Down
26 changes: 25 additions & 1 deletion tests/test_archive.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from render_engine.collection import Collection
from render_engine.archive import Archive
from render_engine.page import Page

Expand Down Expand Up @@ -35,13 +36,36 @@ def test_archive_slug_name_with_pages(title, expected_slug):
template="",
routes=["./"],
archive_index=1,
num_archive_pages=2,
template_vars={},
)

assert archive._slug == expected_slug


@pytest.mark.parametrize(
"_items_per_page, num_of_pages",
[(1, 3), (3, 1)],
)
def test_archive_num_of_pages(_items_per_page: int, num_of_pages: int):
"""Tests that the number of pages in an archive is equal to the number of pages in the archive"""

class TestCollection(Collection):
pages = [Page(), Page(), Page()]
has_archive = True
items_per_page = _items_per_page

collection = TestCollection()
assert list(collection.archives)[0].template_vars["num_of_pages"] == num_of_pages

if _items_per_page < 1:
assert (
list(collection.archives)[1].template_vars["num_of_pages"] == num_of_pages
)
assert (
list(collection.archives)[2].template_vars["num_of_pages"] == num_of_pages
)


def test_archive_template_is_archive_html():
"""Tests that the template is set to archive.html"""
archive = Archive(
Expand Down

0 comments on commit 438154f

Please sign in to comment.