Fixed non-deterministic order of categories in blog plugin

This commit is contained in:
squidfunk 2023-11-24 09:42:57 +01:00
parent d43626a838
commit 058b32f2d3
No known key found for this signature in database
GPG Key ID: 5ED40BC4F9C436DF
4 changed files with 46 additions and 26 deletions

View File

@ -138,13 +138,17 @@ class BlogPlugin(BasePlugin[BlogConfig]):
# Generate views for archive # Generate views for archive
if self.config.archive: if self.config.archive:
views = self._generate_archive(config, files) self.blog.views.extend(
self.blog.views.extend(views) self._generate_archive(config, files)
)
# Generate views for categories # Generate views for categories
if self.config.categories: if self.config.categories:
views = self._generate_categories(config, files) self.blog.views.extend(sorted(
self.blog.views.extend(views) self._generate_categories(config, files),
key = lambda view: view.name,
reverse = False
))
# Generate pages for views # Generate pages for views
if self.config.pagination: if self.config.pagination:
@ -573,10 +577,9 @@ class BlogPlugin(BasePlugin[BlogConfig]):
self._save_to_file(file.abs_src_path, f"# {name}") self._save_to_file(file.abs_src_path, f"# {name}")
file.inclusion = InclusionLevel.EXCLUDED file.inclusion = InclusionLevel.EXCLUDED
# Create and yield view - we don't explicitly set the title of # Create and yield view
# the view, so authors can override them in the page's content
if not isinstance(file.page, Archive): if not isinstance(file.page, Archive):
yield Archive(None, file, config) yield Archive(name, file, config)
# Assign post to archive # Assign post to archive
assert isinstance(file.page, Archive) assert isinstance(file.page, Archive)
@ -610,10 +613,9 @@ class BlogPlugin(BasePlugin[BlogConfig]):
self._save_to_file(file.abs_src_path, f"# {name}") self._save_to_file(file.abs_src_path, f"# {name}")
file.inclusion = InclusionLevel.EXCLUDED file.inclusion = InclusionLevel.EXCLUDED
# Create and yield view - we don't explicitly set the title of # Create and yield view
# the view, so authors can override them in the page's content
if not isinstance(file.page, Category): if not isinstance(file.page, Category):
yield Category(None, file, config) yield Category(name, file, config)
# Assign post to category and vice versa # Assign post to category and vice versa
assert isinstance(file.page, Category) assert isinstance(file.page, Category)

View File

@ -212,10 +212,18 @@ class Excerpt(Page):
# View # View
class View(Page): class View(Page):
# Parent view
parent: View | Section
# Initialize view # Initialize view
def __init__(self, title: str | None, file: File, config: MkDocsConfig): def __init__(self, name: str | None, file: File, config: MkDocsConfig):
super().__init__(title, file, config) super().__init__(None, file, config)
self.parent: View | Section
# Initialize name of the view - note that views never pass a title to
# the parent constructor, so the author can always override the title
# that is used for rendering. However, for some purposes, like for
# example sorting, we need something to compare.
self.name = name
# Initialize posts and views # Initialize posts and views
self.posts: list[Post] = [] self.posts: list[Post] = []

View File

@ -138,13 +138,17 @@ class BlogPlugin(BasePlugin[BlogConfig]):
# Generate views for archive # Generate views for archive
if self.config.archive: if self.config.archive:
views = self._generate_archive(config, files) self.blog.views.extend(
self.blog.views.extend(views) self._generate_archive(config, files)
)
# Generate views for categories # Generate views for categories
if self.config.categories: if self.config.categories:
views = self._generate_categories(config, files) self.blog.views.extend(sorted(
self.blog.views.extend(views) self._generate_categories(config, files),
key = lambda view: view.name,
reverse = False
))
# Generate pages for views # Generate pages for views
if self.config.pagination: if self.config.pagination:
@ -573,10 +577,9 @@ class BlogPlugin(BasePlugin[BlogConfig]):
self._save_to_file(file.abs_src_path, f"# {name}") self._save_to_file(file.abs_src_path, f"# {name}")
file.inclusion = InclusionLevel.EXCLUDED file.inclusion = InclusionLevel.EXCLUDED
# Create and yield view - we don't explicitly set the title of # Create and yield view
# the view, so authors can override them in the page's content
if not isinstance(file.page, Archive): if not isinstance(file.page, Archive):
yield Archive(None, file, config) yield Archive(name, file, config)
# Assign post to archive # Assign post to archive
assert isinstance(file.page, Archive) assert isinstance(file.page, Archive)
@ -610,10 +613,9 @@ class BlogPlugin(BasePlugin[BlogConfig]):
self._save_to_file(file.abs_src_path, f"# {name}") self._save_to_file(file.abs_src_path, f"# {name}")
file.inclusion = InclusionLevel.EXCLUDED file.inclusion = InclusionLevel.EXCLUDED
# Create and yield view - we don't explicitly set the title of # Create and yield view
# the view, so authors can override them in the page's content
if not isinstance(file.page, Category): if not isinstance(file.page, Category):
yield Category(None, file, config) yield Category(name, file, config)
# Assign post to category and vice versa # Assign post to category and vice versa
assert isinstance(file.page, Category) assert isinstance(file.page, Category)

View File

@ -212,10 +212,18 @@ class Excerpt(Page):
# View # View
class View(Page): class View(Page):
# Parent view
parent: View | Section
# Initialize view # Initialize view
def __init__(self, title: str | None, file: File, config: MkDocsConfig): def __init__(self, name: str | None, file: File, config: MkDocsConfig):
super().__init__(title, file, config) super().__init__(None, file, config)
self.parent: View | Section
# Initialize name of the view - note that views never pass a title to
# the parent constructor, so the author can always override the title
# that is used for rendering. However, for some purposes, like for
# example sorting, we need something to compare.
self.name = name
# Initialize posts and views # Initialize posts and views
self.posts: list[Post] = [] self.posts: list[Post] = []