Skip to content

Commit

Permalink
Fixed crash of blog plugin when pagination is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed Nov 23, 2023
1 parent 0fd2a39 commit ae05629
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 98 deletions.
38 changes: 31 additions & 7 deletions material/plugins/blog/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

from babel.dates import format_date
from datetime import datetime
from jinja2 import pass_context
from jinja2.runtime import Context
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.exceptions import PluginError
from mkdocs.plugins import BasePlugin, event_priority
Expand All @@ -35,6 +37,7 @@
from mkdocs.structure.nav import Navigation, Section
from mkdocs.structure.pages import Page
from mkdocs.utils import copy_file, get_relative_url
from mkdocs.utils.templates import url_filter
from paginate import Page as Pagination
from shutil import rmtree
from tempfile import mkdtemp
Expand All @@ -44,7 +47,6 @@
from .config import BlogConfig
from .readtime import readtime
from .structure import Archive, Category, Excerpt, Post, View
from .templates import url_filter

# -----------------------------------------------------------------------------
# Classes
Expand Down Expand Up @@ -148,7 +150,6 @@ def on_files(self, files, *, config):
if self.config.pagination:
for view in self._resolve_views(self.blog):
for page in self._generate_pages(view, config, files):
page.file.inclusion = InclusionLevel.EXCLUDED
view.pages.append(page)

# Ensure that entrypoint is always included in navigation
Expand Down Expand Up @@ -180,6 +181,7 @@ def on_nav(self, nav, *, config, files):

# Revert temporary exclusion of views from navigation
for view in self._resolve_views(self.blog):
view.file.inclusion = self.blog.file.inclusion
for page in view.pages:
page.file.inclusion = self.blog.file.inclusion

Expand Down Expand Up @@ -298,9 +300,25 @@ def on_env(self, env, *, config, files):
def date_filter(date: datetime):
return self._format_date_for_post(date, config)

# Patch URL template filter to add support for paginated views, i.e.,
# that paginated views never link to themselves but to the main view
@pass_context
def url_filter_with_pagination(context: Context, url: str | None):
page = context["page"]

# If the current page is a view, check if the URL links to the page
# itself, and replace it with the URL of the main view
if isinstance(page, View):
view = self._resolve_original(page)
if page.url == url:
url = view.url

# Forward to original template filter
return url_filter(context, url)

# Register custom template filters
env.filters["date"] = date_filter
env.filters["url"] = url_filter
env.filters["url"] = url_filter_with_pagination

# Prepare view for rendering (run latest) - views are rendered last, as we
# need to mutate the navigation to account for pagination. The main problem
Expand Down Expand Up @@ -527,7 +545,7 @@ def _resolve_siblings(self, item: StructureItem, nav: Navigation):

# Resolve original page or view (e.g. for paginated views)
def _resolve_original(self, page: Page):
if isinstance(page, View):
if isinstance(page, View) and page.pages:
return page.pages[0]
else:
return page
Expand All @@ -550,8 +568,10 @@ def _generate_archive(self, config: MkDocsConfig, files: Files):
file = self._path_to_file(path, config)
files.append(file)

# Create file in temporary directory
# Create file in temporary directory and temporarily remove
# from navigation, as we'll add it at a specific location
self._save_to_file(file.abs_src_path, f"# {name}")
file.inclusion = InclusionLevel.EXCLUDED

# Create and yield view - we don't explicitly set the title of
# the view, so authors can override them in the page's content
Expand Down Expand Up @@ -585,8 +605,10 @@ def _generate_categories(self, config: MkDocsConfig, files: Files):
file = self._path_to_file(path, config)
files.append(file)

# Create file in temporary directory
# Create file in temporary directory and temporarily remove
# from navigation, as we'll add it at a specific location
self._save_to_file(file.abs_src_path, f"# {name}")
file.inclusion = InclusionLevel.EXCLUDED

# Create and yield view - we don't explicitly set the title of
# the view, so authors can override them in the page's content
Expand Down Expand Up @@ -615,8 +637,10 @@ def _generate_pages(self, view: View, config: MkDocsConfig, files: Files):
file = self._path_to_file(path, config)
files.append(file)

# Copy file to temporary directory
# Copy file to temporary directory and temporarily remove
# from navigation, as we'll add it at a specific location
copy_file(view.file.abs_src_path, file.abs_src_path)
file.inclusion = InclusionLevel.EXCLUDED

# Create and yield view
if not isinstance(file.page, View):
Expand Down
42 changes: 0 additions & 42 deletions material/plugins/blog/templates/__init__.py

This file was deleted.

38 changes: 31 additions & 7 deletions src/plugins/blog/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

from babel.dates import format_date
from datetime import datetime
from jinja2 import pass_context
from jinja2.runtime import Context
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.exceptions import PluginError
from mkdocs.plugins import BasePlugin, event_priority
Expand All @@ -35,6 +37,7 @@
from mkdocs.structure.nav import Navigation, Section
from mkdocs.structure.pages import Page
from mkdocs.utils import copy_file, get_relative_url
from mkdocs.utils.templates import url_filter
from paginate import Page as Pagination
from shutil import rmtree
from tempfile import mkdtemp
Expand All @@ -44,7 +47,6 @@
from .config import BlogConfig
from .readtime import readtime
from .structure import Archive, Category, Excerpt, Post, View
from .templates import url_filter

# -----------------------------------------------------------------------------
# Classes
Expand Down Expand Up @@ -148,7 +150,6 @@ def on_files(self, files, *, config):
if self.config.pagination:
for view in self._resolve_views(self.blog):
for page in self._generate_pages(view, config, files):
page.file.inclusion = InclusionLevel.EXCLUDED
view.pages.append(page)

# Ensure that entrypoint is always included in navigation
Expand Down Expand Up @@ -180,6 +181,7 @@ def on_nav(self, nav, *, config, files):

# Revert temporary exclusion of views from navigation
for view in self._resolve_views(self.blog):
view.file.inclusion = self.blog.file.inclusion
for page in view.pages:
page.file.inclusion = self.blog.file.inclusion

Expand Down Expand Up @@ -298,9 +300,25 @@ def on_env(self, env, *, config, files):
def date_filter(date: datetime):
return self._format_date_for_post(date, config)

# Patch URL template filter to add support for paginated views, i.e.,
# that paginated views never link to themselves but to the main view
@pass_context
def url_filter_with_pagination(context: Context, url: str | None):
page = context["page"]

# If the current page is a view, check if the URL links to the page
# itself, and replace it with the URL of the main view
if isinstance(page, View):
view = self._resolve_original(page)
if page.url == url:
url = view.url

# Forward to original template filter
return url_filter(context, url)

# Register custom template filters
env.filters["date"] = date_filter
env.filters["url"] = url_filter
env.filters["url"] = url_filter_with_pagination

# Prepare view for rendering (run latest) - views are rendered last, as we
# need to mutate the navigation to account for pagination. The main problem
Expand Down Expand Up @@ -527,7 +545,7 @@ def _resolve_siblings(self, item: StructureItem, nav: Navigation):

# Resolve original page or view (e.g. for paginated views)
def _resolve_original(self, page: Page):
if isinstance(page, View):
if isinstance(page, View) and page.pages:
return page.pages[0]
else:
return page
Expand All @@ -550,8 +568,10 @@ def _generate_archive(self, config: MkDocsConfig, files: Files):
file = self._path_to_file(path, config)
files.append(file)

# Create file in temporary directory
# Create file in temporary directory and temporarily remove
# from navigation, as we'll add it at a specific location
self._save_to_file(file.abs_src_path, f"# {name}")
file.inclusion = InclusionLevel.EXCLUDED

# Create and yield view - we don't explicitly set the title of
# the view, so authors can override them in the page's content
Expand Down Expand Up @@ -585,8 +605,10 @@ def _generate_categories(self, config: MkDocsConfig, files: Files):
file = self._path_to_file(path, config)
files.append(file)

# Create file in temporary directory
# Create file in temporary directory and temporarily remove
# from navigation, as we'll add it at a specific location
self._save_to_file(file.abs_src_path, f"# {name}")
file.inclusion = InclusionLevel.EXCLUDED

# Create and yield view - we don't explicitly set the title of
# the view, so authors can override them in the page's content
Expand Down Expand Up @@ -615,8 +637,10 @@ def _generate_pages(self, view: View, config: MkDocsConfig, files: Files):
file = self._path_to_file(path, config)
files.append(file)

# Copy file to temporary directory
# Copy file to temporary directory and temporarily remove
# from navigation, as we'll add it at a specific location
copy_file(view.file.abs_src_path, file.abs_src_path)
file.inclusion = InclusionLevel.EXCLUDED

# Create and yield view
if not isinstance(file.page, View):
Expand Down
42 changes: 0 additions & 42 deletions src/plugins/blog/templates/__init__.py

This file was deleted.

0 comments on commit ae05629

Please sign in to comment.