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

feature: allow multiple instances #280

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mkdocs_rss_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
class GitRssPlugin(BasePlugin[RssPluginConfig]):
"""Main class for MkDocs plugin."""

# allow to set the plugin multiple times in the same mkdocs config
supports_multiple_instances = True

def __init__(self):
"""Instanciation."""
# dates source
Expand Down
25 changes: 18 additions & 7 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
from mkdocs.config import load_config
from mkdocs.config.base import Config

# package
from mkdocs_rss_plugin.plugin import GitRssPlugin

# #############################################################################
# ########## Classes ###############
# ##################################
Expand All @@ -45,19 +48,27 @@ def get_plugin_config_from_mkdocs(
# instanciate plugin
cfg_mkdocs = load_config(str(mkdocs_yml_filepath.resolve()))

plugins = cfg_mkdocs.get("plugins")
if "rss" not in plugins:
plugins = cfg_mkdocs.plugins
rss_plugin_instances = [
plg for plg in plugins.items() if isinstance(plg[1], GitRssPlugin)
]
if not len(rss_plugin_instances):
logging.warning(
f"Plugin {plugin_name} is not part of enabled plugin in the MkDocs "
"configuration file: {mkdocs_yml_filepath}"
)
return {}
plugin_loaded = plugins.get("rss")
return cfg_mkdocs

if len(rss_plugin_instances) == 1:
plugin = rss_plugin_instances[0][1]
self.assertIsInstance(plugin, GitRssPlugin)
elif len(rss_plugin_instances) >= 1:
plugin = rss_plugin_instances[1][1]
self.assertIsInstance(plugin, GitRssPlugin)

cfg = plugin_loaded.on_config(cfg_mkdocs)
logging.info("Fixture configuration loaded: " + str(cfg))
logging.info(f"Fixture configuration loaded: {plugin.on_config(cfg_mkdocs)}")

return plugin_loaded.config
return plugin.config

def build_docs_setup(
self,
Expand Down
20 changes: 20 additions & 0 deletions tests/dev/dev_load_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from mkdocs.config import load_config

from mkdocs_rss_plugin.plugin import GitRssPlugin

mkdocs_cfg = load_config(config_file="tests/fixtures/mkdocs_multiple_instances.yml")

print(mkdocs_cfg.plugins.keys())
rss_instances = [
plg for plg in mkdocs_cfg.plugins.items() if isinstance(plg[1], GitRssPlugin)
]
print(len(rss_instances))

for plg in mkdocs_cfg.plugins.items():
print(plg)
print(isinstance(plg[1], GitRssPlugin))
print(type(plg))

rss_instance_1 = plg[1]
print(dir(rss_instance_1))
print(rss_instance_1.on_config(mkdocs_cfg))
3 changes: 2 additions & 1 deletion tests/fixtures/docs/blog/posts/sample_blog_post.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
date: 2023-02-12
authors: [guts]
authors:
- guts
categories:
- Blog
---
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/mkdocs_multiple_instances.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
site_name: Test Mkdocs with multiple RSS plugin instances
site_description: Multiple RSS plugin in a single mkdocs
site_url: https://guts.github.io/mkdocs-rss-plugin

plugins:
- rss
- rss:
feeds_filenames:
json_created: blog.json
json_updated: blog-updated.json
rss_created: blog.xml
rss_updated: blog-updated.xml
match_path: "blog/.*"

theme:
name: material
45 changes: 45 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,51 @@ def test_simple_build_custom_output_basename(self):
)
self.assertEqual(feed_parsed.bozo, 0)

def test_simple_build_multiple_instances(self):
config = self.get_plugin_config_from_mkdocs(
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_multiple_instances.yml"),
plugin_name="rss",
)

with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path(
"tests/fixtures/mkdocs_multiple_instances.yml"
),
output_path=tmpdirname,
strict=True,
)

if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))

self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(
Path(tmpdirname) / config.feeds_filenames.rss_created
)
self.assertEqual(feed_parsed.bozo, 0)

# updated items
feed_parsed = feedparser.parse(
Path(tmpdirname) / config.feeds_filenames.rss_updated
)
self.assertEqual(feed_parsed.bozo, 0)

# created items - blog
feed_parsed = feedparser.parse(Path(tmpdirname).joinpath("blog.xml"))
self.assertEqual(feed_parsed.bozo, 0)

# updated items - blog
feed_parsed = feedparser.parse(
Path(tmpdirname).joinpath("blog-updated.xml")
)
self.assertEqual(feed_parsed.bozo, 0)

def test_simple_build_pretty_print_enabled(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
Expand Down