Skip to content

Commit

Permalink
add: handle and test special case of Material theme
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Dec 7, 2023
1 parent 13595c8 commit d0903cf
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
24 changes: 18 additions & 6 deletions mkdocs_rss_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,22 @@ def guess_locale(mkdocs_config: Config) -> str or None:
)
return mkdocs_config.get("locale")

# Some themes implement a locale or a language setting
# Some themes implement a locale or a language settings
if "theme" in mkdocs_config:
if "locale" in mkdocs_config.theme:
if (
mkdocs_config.theme.name == "material"
and "language" in mkdocs_config.theme
):
# TODO: remove custom behavior when Material theme switches to locale
# see: https://github.com/squidfunk/mkdocs-material/discussions/6453
logger.debug(
"[rss plugin] Language detected in Material theme "
f"('{mkdocs_config.theme.name}') settings: "
f"{mkdocs_config.theme.get('language')}"
)
return mkdocs_config.theme.get("language")

elif "locale" in mkdocs_config.theme:
locale = mkdocs_config.theme.locale
logger.debug(
"[rss plugin] Locale detected in theme "
Expand All @@ -634,12 +647,11 @@ def guess_locale(mkdocs_config: Config) -> str or None:
if locale.territory
else f"{locale.language}"
)
elif "language" in mkdocs_config.theme:
else:
logger.debug(
"[rss plugin] Language detected in theme "
f"('{mkdocs_config.theme.name}') settings: {mkdocs_config.theme.language}"
"[rss plugin] Nor locale or language detected in theme settings "
f"('{mkdocs_config.theme.name}')."
)
return mkdocs_config.theme.language

return None

Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/mkdocs_language_specific_material.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
site_name: Test RSS Plugin
site_description: Test a language code set in with territory
site_url: https://guts.github.io/mkdocs-rss-plugin

plugins:
- rss

theme:
name: material
locale: en_US
language: fr # custom setting for historical reason - see: https://github.com/squidfunk/mkdocs-material/discussions/6453
34 changes: 30 additions & 4 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,12 @@ def test_simple_build_item_length_unlimited(self):
len(feed_item.description), 150, feed_item.title
)

def test_simple_build_lang_with_territory(self):
def test_simple_build_locale_with_territory(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path(
"tests/fixtures/mkdocs_lang_with_territory.yml"
"tests/fixtures/mkdocs_locale_with_territory.yml"
),
output_path=tmpdirname,
strict=True,
Expand All @@ -356,12 +356,38 @@ def test_simple_build_lang_with_territory(self):
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
self.assertEqual(feed_parsed.feed.get("language"), "en-US")

def test_simple_build_lang_without_territory(self):
def test_simple_build_locale_without_territory(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path(
"tests/fixtures/mkdocs_lang_without_territory.yml"
"tests/fixtures/mkdocs_locale_without_territory.yml"
),
output_path=tmpdirname,
strict=True,
)

if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))

self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
self.assertEqual(feed_parsed.feed.get("language"), "fr")

# updated items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
self.assertEqual(feed_parsed.feed.get("language"), "fr")

def test_simple_build_language_specific_material(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path(
"tests/fixtures/mkdocs_language_specific_material.yml"
),
output_path=tmpdirname,
strict=True,
Expand Down

0 comments on commit d0903cf

Please sign in to comment.