BlogPlugin: handling of generated blog posts
As it has been discussed in https://github.com/squidfunk/mkdocs-material/issues/7545 I've modified blog plugin code to handle generated posts.
Now if file Post instance is being created from generated content, it reads content with file.string_content property (Implementation from master branch used to crash in this case since tried to load non existing file from file system).
Error messages are also changed to reference path and plugin that has generated the file.
For case where file is loaded form filesystem the behavior and messages are kept as is to ensure backward compatibility.
Thanks! Great work! I'm currently tight on time, but I'll try to get back to this ☺️ Can you maybe provide a minimal reproduction that we can run to see it in action?
a minimum code generating blog post. Should fail without my changes and work with them
POST_CONTENT="""---
date:
created: {date}
tags:
- generated
title: Generated Blog Post
categories:
- test
---
This post is generated
"""
class BlogPostGeneratingPlugin(BasePlugin):
def on_files(self, files: Files, /, *, config: MkDocsConfig) -> Files | None:
file = File.generated(config=config, src_uri="blog/posts/generated.md", content=POST_CONTENT.format(date=datetime.now()))
files.append(file)
return files
Same as ready to launch folder with post generating hook generated posts.zip
And to illustrate the issue PR solves here is the exception mkdocs throws without my changes
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "D:\tmp\generated posts\venv\Scripts\mkdocs.exe\__main__.py", line 7, in <module>
File "D:\tmp\generated posts\venv\Lib\site-packages\click\core.py", line 1157, in __call__
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\tmp\generated posts\venv\Lib\site-packages\click\core.py", line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "D:\tmp\generated posts\venv\Lib\site-packages\click\core.py", line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\tmp\generated posts\venv\Lib\site-packages\click\core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\tmp\generated posts\venv\Lib\site-packages\click\core.py", line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\tmp\generated posts\venv\Lib\site-packages\mkdocs\__main__.py", line 272, in serve_command
serve.serve(**kwargs)
File "D:\tmp\generated posts\venv\Lib\site-packages\mkdocs\commands\serve.py", line 85, in serve
builder(config)
File "D:\tmp\generated posts\venv\Lib\site-packages\mkdocs\commands\serve.py", line 67, in builder
build(config, serve_url=None if is_clean else serve_url, dirty=is_dirty)
File "D:\tmp\generated posts\venv\Lib\site-packages\mkdocs\commands\build.py", line 292, in build
files = config.plugins.on_files(files, config=config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\tmp\generated posts\venv\Lib\site-packages\mkdocs\plugins.py", line 593, in on_files
return self.run_event('files', files, config=config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\tmp\generated posts\venv\Lib\site-packages\mkdocs\plugins.py", line 566, in run_event
result = method(item, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^
File "D:\tmp\generated posts\venv\Lib\site-packages\material\plugins\blog\plugin.py", line 133, in on_files
self.blog.posts = sorted(
^^^^^^^
File "D:\tmp\generated posts\venv\Lib\site-packages\material\plugins\blog\plugin.py", line 443, in _resolve_posts
post = self._resolve_post(file, config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\tmp\generated posts\venv\Lib\site-packages\material\plugins\blog\plugin.py", line 404, in _resolve_post
post = Post(file, config)
^^^^^^^^^^^^^^^^^^
File "D:\tmp\generated posts\venv\Lib\site-packages\material\plugins\blog\structure\__init__.py", line 57, in __init__
path = os.path.relpath(file.abs_src_path, docs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen ntpath>", line 760, in relpath
TypeError: expected str, bytes or os.PathLike object, not NoneType
@squidfunk is there a chance for this PR to be accepted? I have a hack to workaround the issue in my project and either need to invest time to cover it with tests and improve code if it is permanent or just wait till it is fixed in mkdocs-material and remove...
Thanks again for your time investment and efforts here. We currently have a feature freeze due to the foundational work that's happening right now. Since this PR improves deeper customizations and integrations rather than common use cases, we're not considering it for merge in the light of our current efforts.
I'm sorry for any inconveniences caused. We'll come back to it after the next announcement.
No worries. It is important to keep focus.
Material for MkDocs is now in maintenance mode. Please understand that we don't consider merging this PR anymore.