Skip to content

Commit 560d706

Browse files
authored
fix: deprecation of Theme._vars by using config attributes (#212)
Supersedes #206 Closes #205
2 parents e31831b + d0903cf commit 560d706

5 files changed

Lines changed: 117 additions & 10 deletions

File tree

mkdocs_rss_plugin/util.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -612,16 +612,48 @@ def guess_locale(mkdocs_config: Config) -> str or None:
612612
# MkDocs locale settings - might be added in future mkdocs versions
613613
# see: https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/24
614614
if mkdocs_config.get("locale"):
615+
logger.warning(
616+
DeprecationWarning(
617+
"[rss-plugin] Mkdocs does not support locale option at the "
618+
"configuration root but under theme sub-configuration. It won't be "
619+
"supported anymore by the plugin in the next version."
620+
)
621+
)
615622
return mkdocs_config.get("locale")
616623

617-
# Some themes implement a locale or a language setting
618-
if "theme" in mkdocs_config and "locale" in mkdocs_config.get("theme"):
619-
locale = mkdocs_config.get("theme")._vars.get("locale")
620-
return f"{locale.language}-{locale.territory}"
621-
elif "theme" in mkdocs_config and "language" in mkdocs_config.get("theme"):
622-
return mkdocs_config.get("theme")._vars.get("language")
623-
else:
624-
return None
624+
# Some themes implement a locale or a language settings
625+
if "theme" in mkdocs_config:
626+
if (
627+
mkdocs_config.theme.name == "material"
628+
and "language" in mkdocs_config.theme
629+
):
630+
# TODO: remove custom behavior when Material theme switches to locale
631+
# see: https://github.com/squidfunk/mkdocs-material/discussions/6453
632+
logger.debug(
633+
"[rss plugin] Language detected in Material theme "
634+
f"('{mkdocs_config.theme.name}') settings: "
635+
f"{mkdocs_config.theme.get('language')}"
636+
)
637+
return mkdocs_config.theme.get("language")
638+
639+
elif "locale" in mkdocs_config.theme:
640+
locale = mkdocs_config.theme.locale
641+
logger.debug(
642+
"[rss plugin] Locale detected in theme "
643+
f"('{mkdocs_config.theme.name}') settings: {locale=}"
644+
)
645+
return (
646+
f"{locale.language}-{locale.territory}"
647+
if locale.territory
648+
else f"{locale.language}"
649+
)
650+
else:
651+
logger.debug(
652+
"[rss plugin] Nor locale or language detected in theme settings "
653+
f"('{mkdocs_config.theme.name}')."
654+
)
655+
656+
return None
625657

626658
@staticmethod
627659
def filter_pages(pages: list, attribute: str, length: int) -> list:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
site_name: Test RSS Plugin
2+
site_description: Test a language code set in with territory
3+
site_url: https://guts.github.io/mkdocs-rss-plugin
4+
5+
plugins:
6+
- rss
7+
8+
theme:
9+
name: material
10+
locale: en_US
11+
language: fr # custom setting for historical reason - see: https://github.com/squidfunk/mkdocs-material/discussions/6453
File renamed without changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
site_name: Test RSS Plugin
2+
site_description: Test a language code with territory
3+
site_url: https://guts.github.io/mkdocs-rss-plugin
4+
5+
use_directory_urls: true
6+
7+
plugins:
8+
- rss
9+
10+
theme:
11+
name: mkdocs
12+
locale: fr

tests/test_build.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,12 @@ def test_simple_build_item_length_unlimited(self):
330330
len(feed_item.description), 150, feed_item.title
331331
)
332332

333-
def test_simple_build_lang_with_territory(self):
333+
def test_simple_build_locale_with_territory(self):
334334
with tempfile.TemporaryDirectory() as tmpdirname:
335335
cli_result = self.build_docs_setup(
336336
testproject_path="docs",
337337
mkdocs_yml_filepath=Path(
338-
"tests/fixtures/mkdocs_lang_with_territory.yml"
338+
"tests/fixtures/mkdocs_locale_with_territory.yml"
339339
),
340340
output_path=tmpdirname,
341341
strict=True,
@@ -356,6 +356,58 @@ def test_simple_build_lang_with_territory(self):
356356
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
357357
self.assertEqual(feed_parsed.feed.get("language"), "en-US")
358358

359+
def test_simple_build_locale_without_territory(self):
360+
with tempfile.TemporaryDirectory() as tmpdirname:
361+
cli_result = self.build_docs_setup(
362+
testproject_path="docs",
363+
mkdocs_yml_filepath=Path(
364+
"tests/fixtures/mkdocs_locale_without_territory.yml"
365+
),
366+
output_path=tmpdirname,
367+
strict=True,
368+
)
369+
370+
if cli_result.exception is not None:
371+
e = cli_result.exception
372+
logger.debug(format_exception(type(e), e, e.__traceback__))
373+
374+
self.assertEqual(cli_result.exit_code, 0)
375+
self.assertIsNone(cli_result.exception)
376+
377+
# created items
378+
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
379+
self.assertEqual(feed_parsed.feed.get("language"), "fr")
380+
381+
# updated items
382+
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
383+
self.assertEqual(feed_parsed.feed.get("language"), "fr")
384+
385+
def test_simple_build_language_specific_material(self):
386+
with tempfile.TemporaryDirectory() as tmpdirname:
387+
cli_result = self.build_docs_setup(
388+
testproject_path="docs",
389+
mkdocs_yml_filepath=Path(
390+
"tests/fixtures/mkdocs_language_specific_material.yml"
391+
),
392+
output_path=tmpdirname,
393+
strict=True,
394+
)
395+
396+
if cli_result.exception is not None:
397+
e = cli_result.exception
398+
logger.debug(format_exception(type(e), e, e.__traceback__))
399+
400+
self.assertEqual(cli_result.exit_code, 0)
401+
self.assertIsNone(cli_result.exception)
402+
403+
# created items
404+
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
405+
self.assertEqual(feed_parsed.feed.get("language"), "fr")
406+
407+
# updated items
408+
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
409+
self.assertEqual(feed_parsed.feed.get("language"), "fr")
410+
359411
def test_simple_build_pretty_print_enabled(self):
360412
with tempfile.TemporaryDirectory() as tmpdirname:
361413
cli_result = self.build_docs_setup(

0 commit comments

Comments
 (0)