Skip to content

Commit d0903cf

Browse files
committed
add: handle and test special case of Material theme
see: squidfunk/mkdocs-material#6453
1 parent 13595c8 commit d0903cf

5 files changed

Lines changed: 59 additions & 10 deletions

File tree

mkdocs_rss_plugin/util.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,22 @@ def guess_locale(mkdocs_config: Config) -> str or None:
621621
)
622622
return mkdocs_config.get("locale")
623623

624-
# Some themes implement a locale or a language setting
624+
# Some themes implement a locale or a language settings
625625
if "theme" in mkdocs_config:
626-
if "locale" in mkdocs_config.theme:
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:
627640
locale = mkdocs_config.theme.locale
628641
logger.debug(
629642
"[rss plugin] Locale detected in theme "
@@ -634,12 +647,11 @@ def guess_locale(mkdocs_config: Config) -> str or None:
634647
if locale.territory
635648
else f"{locale.language}"
636649
)
637-
elif "language" in mkdocs_config.theme:
650+
else:
638651
logger.debug(
639-
"[rss plugin] Language detected in theme "
640-
f"('{mkdocs_config.theme.name}') settings: {mkdocs_config.theme.language}"
652+
"[rss plugin] Nor locale or language detected in theme settings "
653+
f"('{mkdocs_config.theme.name}')."
641654
)
642-
return mkdocs_config.theme.language
643655

644656
return None
645657

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.
File renamed without changes.

tests/test_build.py

Lines changed: 30 additions & 4 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,12 +356,38 @@ 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_lang_without_territory(self):
359+
def test_simple_build_locale_without_territory(self):
360360
with tempfile.TemporaryDirectory() as tmpdirname:
361361
cli_result = self.build_docs_setup(
362362
testproject_path="docs",
363363
mkdocs_yml_filepath=Path(
364-
"tests/fixtures/mkdocs_lang_without_territory.yml"
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"
365391
),
366392
output_path=tmpdirname,
367393
strict=True,

0 commit comments

Comments
 (0)