77# standard library
88import json
99from copy import deepcopy
10+ from dataclasses import asdict
1011from datetime import datetime
1112from email .utils import formatdate
1213from pathlib import Path
3334from mkdocs_rss_plugin .integrations .theme_material_social_plugin import (
3435 IntegrationMaterialSocialCards ,
3536)
36- from mkdocs_rss_plugin .models import PageInformation
37+ from mkdocs_rss_plugin .models import PageInformation , RssFeedBase
3738from mkdocs_rss_plugin .util import Util
3839
3940# ############################################################################
@@ -81,8 +82,8 @@ def on_startup(
8182
8283 self .pages_to_filter : list [PageInformation ] = []
8384 # prepare output feeds
84- self .feed_created : dict = {}
85- self .feed_updated : dict = {}
85+ self .feed_created : RssFeedBase = RssFeedBase ()
86+ self .feed_updated : RssFeedBase = RssFeedBase ()
8687
8788 def on_config (self , config : MkDocsConfig ) -> MkDocsConfig :
8889 """The config event is the first event called on build and
@@ -139,30 +140,30 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig:
139140 self .tpl_folder = DEFAULT_TEMPLATE_FOLDER
140141
141142 # start a feed dictionary using global config vars
142- base_feed = {
143- " author" : config .site_author or None ,
144- " buildDate" : formatdate (get_build_timestamp ()),
145- " copyright" : config .copyright ,
146- " description" : (
143+ base_feed = RssFeedBase (
144+ author = config .site_author or None ,
145+ buildDate = formatdate (get_build_timestamp ()),
146+ copyright = config .copyright ,
147+ description = (
147148 self .config .feed_description
148149 if self .config .feed_description
149150 else config .site_description
150151 ),
151- " entries" : [],
152- " generator" : f"{ __title__ } - v{ __version__ } " ,
153- " html_url" : self .util .get_site_url (mkdocs_config = config ),
154- " language" : self .util .guess_locale (mkdocs_config = config ),
155- " pubDate" : formatdate (get_build_timestamp ()),
156- " repo_url" : config .repo_url ,
157- " title" : (
152+ entries = [],
153+ generator = f"{ __title__ } - v{ __version__ } " ,
154+ html_url = self .util .get_site_url (mkdocs_config = config ),
155+ language = self .util .guess_locale (mkdocs_config = config ),
156+ pubDate = formatdate (get_build_timestamp ()),
157+ repo_url = config .repo_url ,
158+ title = (
158159 self .config .feed_title if self .config .feed_title else config .site_name
159160 ),
160- " ttl" : self .config .feed_ttl ,
161- }
161+ ttl = self .config .feed_ttl ,
162+ )
162163
163164 # feed image
164165 if self .config .image :
165- base_feed [ " logo_url" ] = self .config .image
166+ base_feed . logo_url = self .config .image
166167
167168 # pattern to match pages included in output
168169 self .match_path_pattern = re_compile (self .config .match_path )
@@ -224,29 +225,29 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig:
224225 self .feed_updated = deepcopy (base_feed )
225226
226227 # final feed url
227- if base_feed .get ( " html_url" ) :
228+ if base_feed .html_url :
228229 # concatenate both URLs
229- self .feed_created [ " rss_url" ] = (
230- base_feed .get ( " html_url" ) + self .config .feeds_filenames .rss_created
230+ self .feed_created . rss_url = (
231+ base_feed .html_url + self .config .feeds_filenames .rss_created
231232 )
232- self .feed_updated [ " rss_url" ] = (
233- base_feed .get ( " html_url" ) + self .config .feeds_filenames .rss_updated
233+ self .feed_updated . rss_url = (
234+ base_feed .html_url + self .config .feeds_filenames .rss_updated
234235 )
235- self .feed_created [ " json_url" ] = (
236- base_feed .get ( " html_url" ) + self .config .feeds_filenames .json_created
236+ self .feed_created . json_url = (
237+ base_feed .html_url + self .config .feeds_filenames .json_created
237238 )
238- self .feed_updated [ " json_url" ] = (
239- base_feed .get ( " html_url" ) + self .config .feeds_filenames .json_updated
239+ self .feed_updated . json_url = (
240+ base_feed .html_url + self .config .feeds_filenames .json_updated
240241 )
241242 else :
242243 logger .error (
243244 "The variable `site_url` is not set in the MkDocs "
244245 "configuration file whereas a URL is mandatory to publish. "
245246 "See: https://validator.w3.org/feed/docs/rss2.html#requiredChannelElements"
246247 )
247- self .feed_created [ " rss_url" ] = self .feed_updated [ " json_url" ] = (
248- self .feed_updated [ " rss_url" ]
249- ) = self .feed_updated [ " json_url" ] = None
248+ self .feed_created . rss_url = self .feed_updated . json_url = (
249+ self .feed_updated . rss_url
250+ ) = self .feed_updated . json_url = None
250251
251252 # ending event
252253 return config
@@ -371,7 +372,7 @@ def on_post_build(self, config: config_options.Config) -> None:
371372 )
372373
373374 # created items
374- self .feed_created .get ( " entries" ) .extend (
375+ self .feed_created .entries .extend (
375376 self .util .filter_pages (
376377 pages = self .pages_to_filter ,
377378 attribute = "created" ,
@@ -380,7 +381,7 @@ def on_post_build(self, config: config_options.Config) -> None:
380381 )
381382
382383 # updated items
383- self .feed_updated .get ( " entries" ) .extend (
384+ self .feed_updated .entries .extend (
384385 self .util .filter_pages (
385386 pages = self .pages_to_filter ,
386387 attribute = "updated" ,
@@ -419,7 +420,7 @@ def on_post_build(self, config: config_options.Config) -> None:
419420 # write feeds to files stripping out spaces and new lines
420421 with out_feed_created .open (mode = "w" , encoding = "UTF8" ) as fifeed_created :
421422 prev_char = ""
422- for char in template .render (feed = self .feed_created ):
423+ for char in template .render (feed = asdict ( self .feed_created ) ):
423424 if char == "\n " :
424425 continue
425426 if char == " " and prev_char == " " :
@@ -429,7 +430,7 @@ def on_post_build(self, config: config_options.Config) -> None:
429430 fifeed_created .write (char )
430431
431432 with out_feed_updated .open (mode = "w" , encoding = "UTF8" ) as fifeed_updated :
432- for char in template .render (feed = self .feed_updated ):
433+ for char in template .render (feed = asdict ( self .feed_updated ) ):
433434 if char == "\n " :
434435 prev_char = char
435436 continue
@@ -443,14 +444,14 @@ def on_post_build(self, config: config_options.Config) -> None:
443444 if self .config .json_feed_enabled :
444445 with out_json_created .open (mode = "w" , encoding = "UTF8" ) as fp :
445446 json .dump (
446- self .util .feed_to_json (self .feed_created ),
447+ self .util .feed_to_json (asdict ( self .feed_created ) ),
447448 fp ,
448449 indent = 4 if self .config .pretty_print else None ,
449450 )
450451
451452 with out_json_updated .open (mode = "w" , encoding = "UTF8" ) as fp :
452453 json .dump (
453- self .util .feed_to_json (self .feed_updated , updated = True ),
454+ self .util .feed_to_json (asdict ( self .feed_updated ) , updated = True ),
454455 fp ,
455456 indent = 4 if self .config .pretty_print else None ,
456457 )
0 commit comments