Fixed edge cases for standalone case when using blog plugin

This commit is contained in:
squidfunk 2023-08-22 07:56:33 +02:00
parent a0cb4a7230
commit 62d4a16067
No known key found for this signature in database
GPG Key ID: 5ED40BC4F9C436DF
2 changed files with 48 additions and 50 deletions

View File

@ -161,7 +161,7 @@ class BlogPlugin(BasePlugin[BlogConfig]):
# Attach and link views for archive # Attach and link views for archive
title = self._translate(self.config.archive_name, config) title = self._translate(self.config.archive_name, config)
self._attach_to(self.blog.parent, Section(title, views), nav) self._attach_to(self.blog, Section(title, views), nav)
# Generate and attach views for categories # Generate and attach views for categories
if self.config.categories: if self.config.categories:
@ -170,7 +170,7 @@ class BlogPlugin(BasePlugin[BlogConfig]):
# Attach and link views for categories # Attach and link views for categories
title = self._translate(self.config.categories_name, config) title = self._translate(self.config.categories_name, config)
self._attach_to(self.blog.parent, Section(title, views), nav) self._attach_to(self.blog, Section(title, views), nav)
# Paginate generated views, if enabled # Paginate generated views, if enabled
if self.config.pagination: if self.config.pagination:
@ -280,20 +280,19 @@ class BlogPlugin(BasePlugin[BlogConfig]):
main = page.parent main = page.parent
# If this page is a view, and the parent page is a view as well, we got # If this page is a view, and the parent page is a view as well, we got
# a paginated view and need to update the parent view in the navigation. # a paginated view and need to replace the parent with the current view.
# Paginated views are always rendered last, which is why we can safely # Paginated views are always rendered at the end of the build, which is
# mutate the navigation at this point # why we can safely mutate the navigation at this point
if isinstance(main, View): if isinstance(main, View):
assert isinstance(main.parent, Section)
# Replace view in navigation and rewire view - the current view in
# the navigation becomes the main view, thus the entire chain moves
# one level up. It's essential that the rendering order is linear,
# or else we might end up with a broken navigation.
at = main.parent.children.index(main)
main.parent.children[at] = page
page.parent = main.parent page.parent = main.parent
# Replace view in navigation and rewire it - the current view in the
# navigation becomes the main view, thus the entire chain moves one
# level up. It's essential that the rendering order is linear, or
# else we might end up with a broken navigation.
items = self._resolve_siblings(main, nav)
items[items.index(main)] = page
# Render excerpts and perpare pagination # Render excerpts and perpare pagination
posts, pagination = self._render(page) posts, pagination = self._render(page)
@ -364,7 +363,7 @@ class BlogPlugin(BasePlugin[BlogConfig]):
]) ])
# Update entrypoint in navigation # Update entrypoint in navigation
for items in [self._resolve_items(view.parent, nav), nav.pages]: for items in [self._resolve_siblings(view, nav), nav.pages]:
items[items.index(page)] = view items[items.index(page)] = view
# Return view # Return view
@ -484,10 +483,10 @@ class BlogPlugin(BasePlugin[BlogConfig]):
assert isinstance(page, View) assert isinstance(page, View)
yield page yield page
# Resolve children of a navigation item # Resolve siblings of a navigation item
def _resolve_items(self, item: StructureItem, nav: Navigation): def _resolve_siblings(self, item: StructureItem, nav: Navigation):
if isinstance(item, Section): if isinstance(item.parent, Section):
return item.children return item.parent.children
else: else:
return nav.items return nav.items
@ -505,14 +504,14 @@ class BlogPlugin(BasePlugin[BlogConfig]):
# Attach a section to the given parent section, make sure it's pages are # Attach a section to the given parent section, make sure it's pages are
# part of the navigation, and ensure all pages are linked correctly # part of the navigation, and ensure all pages are linked correctly
def _attach_to(self, parent: Section, section: Section, nav: Navigation): def _attach_to(self, view: View, section: Section, nav: Navigation):
section.parent = parent section.parent = view.parent
# Determine the parent section to attach the section to, which might be # Resolve siblings, which are the children of the parent section, or
# the top-level navigation, if no parent section was given. Note, that # the top-level list of navigation items if the view is at the root of
# it's currently not possible to chose the position of a section, but # the project, and append the given section to it. It's currently not
# we might add support for this in the future. # possible to chose the position of a section.
items = self._resolve_items(parent, nav) items = self._resolve_siblings(view, nav)
items.append(section) items.append(section)
# Find last sibling that is a page, skipping sections, as we need to # Find last sibling that is a page, skipping sections, as we need to

View File

@ -161,7 +161,7 @@ class BlogPlugin(BasePlugin[BlogConfig]):
# Attach and link views for archive # Attach and link views for archive
title = self._translate(self.config.archive_name, config) title = self._translate(self.config.archive_name, config)
self._attach_to(self.blog.parent, Section(title, views), nav) self._attach_to(self.blog, Section(title, views), nav)
# Generate and attach views for categories # Generate and attach views for categories
if self.config.categories: if self.config.categories:
@ -170,7 +170,7 @@ class BlogPlugin(BasePlugin[BlogConfig]):
# Attach and link views for categories # Attach and link views for categories
title = self._translate(self.config.categories_name, config) title = self._translate(self.config.categories_name, config)
self._attach_to(self.blog.parent, Section(title, views), nav) self._attach_to(self.blog, Section(title, views), nav)
# Paginate generated views, if enabled # Paginate generated views, if enabled
if self.config.pagination: if self.config.pagination:
@ -280,20 +280,19 @@ class BlogPlugin(BasePlugin[BlogConfig]):
main = page.parent main = page.parent
# If this page is a view, and the parent page is a view as well, we got # If this page is a view, and the parent page is a view as well, we got
# a paginated view and need to update the parent view in the navigation. # a paginated view and need to replace the parent with the current view.
# Paginated views are always rendered last, which is why we can safely # Paginated views are always rendered at the end of the build, which is
# mutate the navigation at this point # why we can safely mutate the navigation at this point
if isinstance(main, View): if isinstance(main, View):
assert isinstance(main.parent, Section)
# Replace view in navigation and rewire view - the current view in
# the navigation becomes the main view, thus the entire chain moves
# one level up. It's essential that the rendering order is linear,
# or else we might end up with a broken navigation.
at = main.parent.children.index(main)
main.parent.children[at] = page
page.parent = main.parent page.parent = main.parent
# Replace view in navigation and rewire it - the current view in the
# navigation becomes the main view, thus the entire chain moves one
# level up. It's essential that the rendering order is linear, or
# else we might end up with a broken navigation.
items = self._resolve_siblings(main, nav)
items[items.index(main)] = page
# Render excerpts and perpare pagination # Render excerpts and perpare pagination
posts, pagination = self._render(page) posts, pagination = self._render(page)
@ -364,7 +363,7 @@ class BlogPlugin(BasePlugin[BlogConfig]):
]) ])
# Update entrypoint in navigation # Update entrypoint in navigation
for items in [self._resolve_items(view.parent, nav), nav.pages]: for items in [self._resolve_siblings(view, nav), nav.pages]:
items[items.index(page)] = view items[items.index(page)] = view
# Return view # Return view
@ -484,10 +483,10 @@ class BlogPlugin(BasePlugin[BlogConfig]):
assert isinstance(page, View) assert isinstance(page, View)
yield page yield page
# Resolve children of a navigation item # Resolve siblings of a navigation item
def _resolve_items(self, item: StructureItem, nav: Navigation): def _resolve_siblings(self, item: StructureItem, nav: Navigation):
if isinstance(item, Section): if isinstance(item.parent, Section):
return item.children return item.parent.children
else: else:
return nav.items return nav.items
@ -505,14 +504,14 @@ class BlogPlugin(BasePlugin[BlogConfig]):
# Attach a section to the given parent section, make sure it's pages are # Attach a section to the given parent section, make sure it's pages are
# part of the navigation, and ensure all pages are linked correctly # part of the navigation, and ensure all pages are linked correctly
def _attach_to(self, parent: Section, section: Section, nav: Navigation): def _attach_to(self, view: View, section: Section, nav: Navigation):
section.parent = parent section.parent = view.parent
# Determine the parent section to attach the section to, which might be # Resolve siblings, which are the children of the parent section, or
# the top-level navigation, if no parent section was given. Note, that # the top-level list of navigation items if the view is at the root of
# it's currently not possible to chose the position of a section, but # the project, and append the given section to it. It's currently not
# we might add support for this in the future. # possible to chose the position of a section.
items = self._resolve_items(parent, nav) items = self._resolve_siblings(view, nav)
items.append(section) items.append(section)
# Find last sibling that is a page, skipping sections, as we need to # Find last sibling that is a page, skipping sections, as we need to