10
10
from datetime import datetime
11
11
from email .utils import formatdate
12
12
from pathlib import Path
13
- from re import compile
13
+ from re import compile as re_compile
14
14
15
15
# 3rd party
16
16
from jinja2 import Environment , FileSystemLoader , select_autoescape
17
17
from mkdocs .config import config_options
18
+ from mkdocs .config .defaults import MkDocsConfig
18
19
from mkdocs .exceptions import PluginError
19
20
from mkdocs .plugins import BasePlugin , event_priority , get_plugin_logger
21
+ from mkdocs .structure .files import Files
20
22
from mkdocs .structure .pages import Page
21
23
from mkdocs .utils import get_build_timestamp
22
24
@@ -55,30 +57,28 @@ class GitRssPlugin(BasePlugin[RssPluginConfig]):
55
57
56
58
def __init__ (self ):
57
59
"""Instanciation."""
58
- # dates source
59
- self .src_date_created = self .src_date_updated = "git"
60
- self .meta_datetime_format : str | None = None
61
- self .meta_default_timezone : str = "UTC"
62
- self .meta_default_time : datetime .time | None = None
63
60
# pages storage
64
61
self .pages_to_filter : list = []
65
62
# prepare output feeds
66
63
self .feed_created : dict = {}
67
64
self .feed_updated : dict = {}
68
65
69
- def on_config (self , config : config_options . Config ) -> dict :
66
+ def on_config (self , config : MkDocsConfig ) -> MkDocsConfig :
70
67
"""The config event is the first event called on build and
71
68
is run immediately after the user configuration is loaded and validated.
72
69
Any alterations to the config should be made here.
73
- https://www.mkdocs.org/user-guide/plugins/#on_config
74
70
75
- :param config: global configuration object
76
- :type config: config_options.Config
71
+ See: https://www.mkdocs.org/user-guide/plugins/#on_config
77
72
78
- :raises FileExistsError: if the template for the RSS feed is not found
73
+ Args:
74
+ config (config_options.Config): global configuration object
79
75
80
- :return: plugin configuration object
81
- :rtype: dict
76
+ Raises:
77
+ FileExistsError: if the template for the RSS feed is not found
78
+ PluginError: if the 'date_from_meta.default_time' format does not comply
79
+
80
+ Returns:
81
+ MkDocsConfig: global configuration object
82
82
"""
83
83
# Skip if disabled
84
84
if not self .config .enabled :
@@ -131,48 +131,56 @@ def on_config(self, config: config_options.Config) -> dict:
131
131
base_feed ["logo_url" ] = self .config .image
132
132
133
133
# pattern to match pages included in output
134
- self .match_path_pattern = compile (self .config .match_path )
134
+ self .match_path_pattern = re_compile (self .config .match_path )
135
135
136
136
# date handling
137
- if self .config .date_from_meta is not None :
138
- self .src_date_created = self .config .date_from_meta .get ("as_creation" , False )
139
- self .src_date_updated = self .config .date_from_meta .get ("as_update" , False )
140
- self .meta_datetime_format = self .config .date_from_meta .get (
141
- "datetime_format" , "%Y-%m-%d %H:%M"
137
+ if (
138
+ self .config .date_from_meta .as_creation == "git"
139
+ and self .config .date_from_meta .as_update == "git"
140
+ ):
141
+ logger .debug ("Dates will be retrieved from git log." )
142
+ elif any (
143
+ [
144
+ isinstance (self .config .date_from_meta .as_creation , bool ),
145
+ isinstance (self .config .date_from_meta .as_update , bool ),
146
+ ]
147
+ ):
148
+ deprecation_msg = (
149
+ "Since version 1.13, using a boolean for "
150
+ "'date_from_meta.as_creation' and 'date_from_meta.as_update' is "
151
+ "deprecated. Please update your "
152
+ "`rss` plugin settings in your Mkdocs configuration "
153
+ f"({ config .config_file_path } ) by using a str or removing the value if "
154
+ "you were using `False`., "
142
155
)
143
- self .meta_default_timezone = self .config .date_from_meta .get (
144
- "default_timezone" , "UTC"
156
+ logger .warning (DeprecationWarning (deprecation_msg ))
157
+ self .config .date_from_meta .as_creation = (
158
+ self .config .date_from_meta .as_update
159
+ ) = "git"
160
+
161
+ # check if default time complies with expected format
162
+ try :
163
+ self .config .date_from_meta .default_time = datetime .strptime (
164
+ self .config .date_from_meta .default_time , "%H:%M"
145
165
)
146
- self .meta_default_time = self .config .date_from_meta .get (
147
- "default_time" , None
166
+ except ValueError as err :
167
+ raise PluginError (
168
+ "Config error: `date_from_meta.default_time` value "
169
+ f"'{ self .config .date_from_meta .default_time } ' format doesn't match the "
170
+ f"expected format %H:%M. Trace: { err } "
148
171
)
149
- if self .meta_default_time :
150
- try :
151
- self .meta_default_time = datetime .strptime (
152
- self .meta_default_time , "%H:%M"
153
- )
154
- except ValueError as err :
155
- raise PluginError (
156
- "Config error: `date_from_meta.default_time` value "
157
- f"'{ self .meta_default_time } ' format doesn't match the expected "
158
- f"format %H:%M. Trace: { err } "
159
- )
160
- else :
161
- self .meta_default_time = datetime .min
162
172
163
- if self .config .use_git :
164
- logger .debug (
165
- "Dates will be retrieved FIRSTLY from page meta (yaml "
166
- "frontmatter). The git log will be used as fallback."
167
- )
168
- else :
169
- logger .debug (
170
- "Dates will be retrieved ONLY from page meta (yaml "
171
- "frontmatter). The build date will be used as fallback, without any "
172
- "call to Git."
173
- )
173
+ if self .config .use_git :
174
+ logger .debug (
175
+ "Dates will be retrieved FIRSTLY from page meta (yaml "
176
+ "frontmatter). The git log will be used as fallback."
177
+ )
174
178
else :
175
- logger .debug ("Dates will be retrieved from git log." )
179
+ logger .debug (
180
+ "Dates will be retrieved ONLY from page meta (yaml "
181
+ "frontmatter). The build date will be used as fallback, without any "
182
+ "call to Git."
183
+ )
176
184
177
185
# create 2 final dicts
178
186
self .feed_created = deepcopy (base_feed )
@@ -199,34 +207,32 @@ def on_config(self, config: config_options.Config) -> dict:
199
207
"configuration file whereas a URL is mandatory to publish. "
200
208
"See: https://validator.w3.org/feed/docs/rss2.html#requiredChannelElements"
201
209
)
202
- self .feed_created ["rss_url" ] = self .feed_updated ["rss_url" ] = None
210
+ self .feed_created ["rss_url" ] = self .feed_updated ["json_url" ] = (
211
+ self .feed_updated ["rss_url" ]
212
+ ) = self .feed_updated ["json_url" ] = None
203
213
204
214
# ending event
205
215
return config
206
216
207
217
@event_priority (priority = - 75 )
208
218
def on_page_content (
209
- self , html : str , page : Page , config : config_options . Config , files
219
+ self , html : str , page : Page , config : MkDocsConfig , files : Files
210
220
) -> str | None :
211
- """The page_content event is called after the Markdown text is rendered
212
- to HTML (but before being passed to a template) and can be used to alter
213
- the HTML body of the page.
221
+ """The page_content event is called after the Markdown text is rendered to HTML
222
+ (but before being passed to a template) and can be used to alter the HTML
223
+ body of the page.
214
224
215
- https://www.mkdocs.org/user-guide/plugins/#on_page_content
225
+ See: https://www.mkdocs.org/user-guide/plugins/#on_page_content
216
226
217
- :param html: HTML rendered from Markdown source as string
218
- :type html: str
219
- :param page: mkdocs.nav.Page instance
220
- :type page: Page
221
- :param config: global configuration object
222
- :type config: config_options.Config
223
- :param files: global navigation object
224
- :type files: [type]
227
+ Args:
228
+ html (str): HTML rendered from Markdown source as string
229
+ page (Page): `mkdocs.structure.pages.Page` instance
230
+ config (MkDocsConfig): global configuration object
231
+ files (Files): global files collection
225
232
226
- :return: HTML rendered from Markdown source as string
227
- :rtype: str
233
+ Returns:
234
+ Optional[ str]: HTML rendered from Markdown source as string
228
235
"""
229
-
230
236
# Skip if disabled
231
237
if not self .config .enabled :
232
238
return
@@ -243,11 +249,11 @@ def on_page_content(
243
249
# retrieve dates from git log
244
250
page_dates = self .util .get_file_dates (
245
251
in_page = page ,
246
- source_date_creation = self .src_date_created ,
247
- source_date_update = self .src_date_updated ,
248
- meta_datetime_format = self .meta_datetime_format ,
249
- meta_default_timezone = self .meta_default_timezone ,
250
- meta_default_time = self .meta_default_time ,
252
+ source_date_creation = self .config . date_from_meta . as_creation ,
253
+ source_date_update = self .config . date_from_meta . as_update ,
254
+ meta_datetime_format = self .config . date_from_meta . datetime_format ,
255
+ meta_default_timezone = self .config . date_from_meta . default_timezone ,
256
+ meta_default_time = self .config . date_from_meta . default_time ,
251
257
)
252
258
253
259
# handle custom URL parameters
0 commit comments