Skip to content

Commit 2d71645

Browse files
committed
Fixed generation of entrypoint in blog plugin
1 parent 382101d commit 2d71645

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

material/plugins/blog/plugin.py

+26-18
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ def on_files(self, files, *, config):
125125
file.abs_dest_path = os.path.join(site, file.dest_path)
126126
file.url = file.url.replace(path, root)
127127

128+
# Generate entrypoint, if it does not exist yet
129+
self._generate(files, config)
130+
128131
# Resolve and load posts and generate indexes (run later) - we resolve all
129132
# posts after the navigation is constructed in order to allow other plugins
130133
# to alter the navigation (e.g. awesome-pages) before we start to add pages
@@ -332,24 +335,12 @@ def _is_excluded(self, post: Post):
332335

333336
# -------------------------------------------------------------------------
334337

335-
# Resolve entrypoint - the entrypoint of the blog hosts all posts, sorted
336-
# by descending date. The entrypoint must always be present, even if there
337-
# are no posts, and is automatically created if it does not exist yet. Note
338-
# that posts might be paginated, but this is configurable by the author.
338+
# Resolve entrypoint - the entrypoint of the blog must have been created
339+
# if it did not exist before, and hosts all posts sorted by descending date
339340
def _resolve(self, files: Files, config: MkDocsConfig, nav: Navigation):
340341
path = os.path.join(self.config.blog_dir, "index.md")
341342
path = os.path.normpath(path)
342343

343-
# Create entrypoint, if it does not exist
344-
docs = os.path.relpath(config.docs_dir)
345-
file = os.path.join(docs, path)
346-
if not os.path.isfile(file):
347-
self._save_to_file(file, "# Blog\n\n")
348-
349-
# Append entrypoint to files - note that the entrypoint is added to
350-
# the docs directory, so we need to set the temporary flag to false
351-
files.append(self._path_to_file(path, config, temp = False))
352-
353344
# Obtain entrypoint page
354345
file = files.get_file_from_path(path)
355346
page = file.page
@@ -525,6 +516,23 @@ def _attach_to(self, view: View, section: Section, nav: Navigation):
525516

526517
# -------------------------------------------------------------------------
527518

519+
# Generate entrypoint - the entrypoint must always be present, and thus is
520+
# created before the navigation is constructed if it does not exist yet
521+
def _generate(self, files: Files, config: MkDocsConfig):
522+
path = os.path.join(self.config.blog_dir, "index.md")
523+
path = os.path.normpath(path)
524+
525+
# Create entrypoint, if it does not exist - note that the entrypoint is
526+
# added to the docs directory, not to the temporary directory
527+
docs = os.path.relpath(config.docs_dir)
528+
file = os.path.join(docs, path)
529+
if not os.path.isfile(file):
530+
file = self._path_to_file(path, config, temp = False)
531+
self._save_to_file(file.abs_src_path, "# Blog\n\n")
532+
533+
# Append entrypoint to files
534+
files.append(file)
535+
528536
# Generate views for archive - analyze posts and generate the necessary
529537
# views, taking the date format provided by the author into account
530538
def _generate_archive(self, config: MkDocsConfig, files: Files):
@@ -539,11 +547,11 @@ def _generate_archive(self, config: MkDocsConfig, files: Files):
539547
file = files.get_file_from_path(path)
540548
if not file:
541549
file = self._path_to_file(path, config)
542-
files.append(file)
550+
self._save_to_file(file.abs_src_path, f"# {name}")
543551

544552
# Create and yield archive view
545-
self._save_to_file(file.abs_src_path, f"# {name}")
546553
yield Archive(name, file, config)
554+
files.append(file)
547555

548556
# Assign post to archive
549557
assert isinstance(file.page, Archive)
@@ -570,11 +578,11 @@ def _generate_categories(self, config: MkDocsConfig, files: Files):
570578
file = files.get_file_from_path(path)
571579
if not file:
572580
file = self._path_to_file(path, config)
573-
files.append(file)
581+
self._save_to_file(file.abs_src_path, f"# {name}")
574582

575583
# Create and yield category view
576-
self._save_to_file(file.abs_src_path, f"# {name}")
577584
yield Category(name, file, config)
585+
files.append(file)
578586

579587
# Assign post to category and vice versa
580588
assert isinstance(file.page, Category)

src/plugins/blog/plugin.py

+26-18
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ def on_files(self, files, *, config):
125125
file.abs_dest_path = os.path.join(site, file.dest_path)
126126
file.url = file.url.replace(path, root)
127127

128+
# Generate entrypoint, if it does not exist yet
129+
self._generate(files, config)
130+
128131
# Resolve and load posts and generate indexes (run later) - we resolve all
129132
# posts after the navigation is constructed in order to allow other plugins
130133
# to alter the navigation (e.g. awesome-pages) before we start to add pages
@@ -332,24 +335,12 @@ def _is_excluded(self, post: Post):
332335

333336
# -------------------------------------------------------------------------
334337

335-
# Resolve entrypoint - the entrypoint of the blog hosts all posts, sorted
336-
# by descending date. The entrypoint must always be present, even if there
337-
# are no posts, and is automatically created if it does not exist yet. Note
338-
# that posts might be paginated, but this is configurable by the author.
338+
# Resolve entrypoint - the entrypoint of the blog must have been created
339+
# if it did not exist before, and hosts all posts sorted by descending date
339340
def _resolve(self, files: Files, config: MkDocsConfig, nav: Navigation):
340341
path = os.path.join(self.config.blog_dir, "index.md")
341342
path = os.path.normpath(path)
342343

343-
# Create entrypoint, if it does not exist
344-
docs = os.path.relpath(config.docs_dir)
345-
file = os.path.join(docs, path)
346-
if not os.path.isfile(file):
347-
self._save_to_file(file, "# Blog\n\n")
348-
349-
# Append entrypoint to files - note that the entrypoint is added to
350-
# the docs directory, so we need to set the temporary flag to false
351-
files.append(self._path_to_file(path, config, temp = False))
352-
353344
# Obtain entrypoint page
354345
file = files.get_file_from_path(path)
355346
page = file.page
@@ -525,6 +516,23 @@ def _attach_to(self, view: View, section: Section, nav: Navigation):
525516

526517
# -------------------------------------------------------------------------
527518

519+
# Generate entrypoint - the entrypoint must always be present, and thus is
520+
# created before the navigation is constructed if it does not exist yet
521+
def _generate(self, files: Files, config: MkDocsConfig):
522+
path = os.path.join(self.config.blog_dir, "index.md")
523+
path = os.path.normpath(path)
524+
525+
# Create entrypoint, if it does not exist - note that the entrypoint is
526+
# added to the docs directory, not to the temporary directory
527+
docs = os.path.relpath(config.docs_dir)
528+
file = os.path.join(docs, path)
529+
if not os.path.isfile(file):
530+
file = self._path_to_file(path, config, temp = False)
531+
self._save_to_file(file.abs_src_path, "# Blog\n\n")
532+
533+
# Append entrypoint to files
534+
files.append(file)
535+
528536
# Generate views for archive - analyze posts and generate the necessary
529537
# views, taking the date format provided by the author into account
530538
def _generate_archive(self, config: MkDocsConfig, files: Files):
@@ -539,11 +547,11 @@ def _generate_archive(self, config: MkDocsConfig, files: Files):
539547
file = files.get_file_from_path(path)
540548
if not file:
541549
file = self._path_to_file(path, config)
542-
files.append(file)
550+
self._save_to_file(file.abs_src_path, f"# {name}")
543551

544552
# Create and yield archive view
545-
self._save_to_file(file.abs_src_path, f"# {name}")
546553
yield Archive(name, file, config)
554+
files.append(file)
547555

548556
# Assign post to archive
549557
assert isinstance(file.page, Archive)
@@ -570,11 +578,11 @@ def _generate_categories(self, config: MkDocsConfig, files: Files):
570578
file = files.get_file_from_path(path)
571579
if not file:
572580
file = self._path_to_file(path, config)
573-
files.append(file)
581+
self._save_to_file(file.abs_src_path, f"# {name}")
574582

575583
# Create and yield category view
576-
self._save_to_file(file.abs_src_path, f"# {name}")
577584
yield Category(name, file, config)
585+
files.append(file)
578586

579587
# Assign post to category and vice versa
580588
assert isinstance(file.page, Category)

0 commit comments

Comments
 (0)