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

Road to Mkdocs >= 1.4: plugin's configuration #195

Merged
merged 5 commits into from
Dec 5, 2023
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
33 changes: 33 additions & 0 deletions mkdocs_rss_plugin/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#! python3 # noqa: E265

# ############################################################################
# ########## Libraries #############
# ##################################


# 3rd party
from mkdocs.config import config_options
from mkdocs.config.base import Config


# ############################################################################
# ########## Classes ###############
# ##################################
class RssPluginConfig(Config):
"""Configuration for RSS plugin for Mkdocs."""

abstract_chars_count = config_options.Type(int, default=160)
abstract_delimiter = config_options.Type(str, default="<!-- more -->")
categories = config_options.Optional(
config_options.ListOfItems(config_options.Type(str))
)
comments_path = config_options.Optional(config_options.Type(str))
date_from_meta = config_options.Optional(config_options.Type(dict))
enabled = config_options.Type(bool, default=True)
feed_ttl = config_options.Type(int, default=1440)
image = config_options.Optional(config_options.Type(str))
length = config_options.Type(int, default=20)
match_path = config_options.Type(str, default=".*")
pretty_print = config_options.Type(bool, default=False)
url_parameters = config_options.Optional(config_options.Type(dict))
use_git = config_options.Type(bool, default=True)
39 changes: 12 additions & 27 deletions mkdocs_rss_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
from mkdocs.utils import get_build_timestamp

# package modules
from .__about__ import __title__, __uri__, __version__
from .customtypes import PageInformation
from .util import Util
from mkdocs_rss_plugin.__about__ import __title__, __uri__, __version__
from mkdocs_rss_plugin.config import RssPluginConfig
from mkdocs_rss_plugin.customtypes import PageInformation
from mkdocs_rss_plugin.util import Util

# ############################################################################
# ########## Globals #############
Expand All @@ -42,25 +43,9 @@
# ##################################


class GitRssPlugin(BasePlugin):
class GitRssPlugin(BasePlugin[RssPluginConfig]):
"""Main class for MkDocs plugin."""

config_scheme = (
("abstract_chars_count", config_options.Type(int, default=160)),
("abstract_delimiter", config_options.Type(str, default="<!-- more -->")),
("categories", config_options.Type(list, default=None)),
("comments_path", config_options.Type(str, default=None)),
("date_from_meta", config_options.Type(dict, default=None)),
("enabled", config_options.Type(bool, default=True)),
("feed_ttl", config_options.Type(int, default=1440)),
("image", config_options.Type(str, default=None)),
("length", config_options.Type(int, default=20)),
("match_path", config_options.Type(str, default=".*")),
("pretty_print", config_options.Type(bool, default=False)),
("url_parameters", config_options.Type(dict, default=None)),
("use_git", config_options.Type(bool, default=True)),
)

def __init__(self):
"""Instanciation."""
# dates source
Expand Down Expand Up @@ -104,23 +89,23 @@ def on_config(self, config: config_options.Config) -> dict:

# start a feed dictionary using global config vars
base_feed = {
"author": config.get("site_author", None),
"author": config.site_author or None,
"buildDate": formatdate(get_build_timestamp()),
"copyright": config.get("copyright", None),
"description": config.get("site_description", None),
"copyright": config.copyright,
"description": config.site_description,
"entries": [],
"generator": f"{__title__} - v{__version__}",
"html_url": self.util.get_site_url(config),
"language": self.util.guess_locale(config),
"pubDate": formatdate(get_build_timestamp()),
"repo_url": config.get("repo_url", config.get("site_url", None)),
"repo_url": config.repo_url,
"title": config.get("site_name", None),
"ttl": self.config.get("feed_ttl", None),
"ttl": self.config.feed_ttl,
}

# feed image
if self.config.get("image"):
base_feed["logo_url"] = self.config.get("image")
if self.config.image:
base_feed["logo_url"] = self.config.image

# pattern to match pages included in output
self.match_path_pattern = compile(self.config.get("match_path"))
Expand Down
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@


GitPython>=3.1,<3.2
mkdocs>=1.1,<2
mkdocs>=1.4,<2
pytz==2022.* ; python_version < "3.9"
tzdata==2023.* ; python_version >= "3.9" and sys_platform == "win32"
11 changes: 9 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
import unittest
from pathlib import Path

# 3rd party
from mkdocs.config.base import Config

from mkdocs_rss_plugin.config import RssPluginConfig

# plugin target
from mkdocs_rss_plugin.plugin import GitRssPlugin

Expand Down Expand Up @@ -65,15 +68,17 @@ def test_plugin_config_defaults(self):
"feed_ttl": 1440,
"image": None,
"length": 20,
"pretty_print": False,
"match_path": ".*",
"pretty_print": False,
"url_parameters": None,
"use_git": True,
}

# load
plugin = GitRssPlugin()
errors, warnings = plugin.load_config({})
self.assertIsInstance(plugin.config, RssPluginConfig)
self.assertIsInstance(plugin.config, Config)
self.assertEqual(plugin.config, expected)
self.assertEqual(errors, [])
self.assertEqual(warnings, [])
Expand All @@ -90,8 +95,8 @@ def test_plugin_config_image(self):
"feed_ttl": 1440,
"image": self.feed_image,
"length": 20,
"pretty_print": False,
"match_path": ".*",
"pretty_print": False,
"url_parameters": None,
"use_git": True,
}
Expand All @@ -102,6 +107,8 @@ def test_plugin_config_image(self):
# load
plugin = GitRssPlugin()
errors, warnings = plugin.load_config(custom_cfg)
self.assertIsInstance(plugin.config, RssPluginConfig)
self.assertIsInstance(plugin.config, Config)
self.assertEqual(plugin.config, expected)
self.assertEqual(errors, [])
self.assertEqual(warnings, [])
Expand Down