Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BlogPlugin: handling of generated blog posts #7583

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

KonstantinNosov
Copy link

As it has been discussed in #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.

@KonstantinNosov KonstantinNosov marked this pull request as ready for review October 4, 2024 10:39
@squidfunk
Copy link
Owner

squidfunk commented Oct 4, 2024

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?

@KonstantinNosov
Copy link
Author

KonstantinNosov commented Oct 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants