Skip to content

Commit

Permalink
fix: deprecation of Theme._vars by using config attributes (#212)
Browse files Browse the repository at this point in the history
Supersedes #206 
Closes #205
  • Loading branch information
Guts authored Dec 7, 2023
2 parents e31831b + d0903cf commit 560d706
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 10 deletions.
48 changes: 40 additions & 8 deletions mkdocs_rss_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,16 +612,48 @@ def guess_locale(mkdocs_config: Config) -> str or None:
# MkDocs locale settings - might be added in future mkdocs versions
# see: https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/24
if mkdocs_config.get("locale"):
logger.warning(
DeprecationWarning(
"[rss-plugin] Mkdocs does not support locale option at the "
"configuration root but under theme sub-configuration. It won't be "
"supported anymore by the plugin in the next version."
)
)
return mkdocs_config.get("locale")

# Some themes implement a locale or a language setting
if "theme" in mkdocs_config and "locale" in mkdocs_config.get("theme"):
locale = mkdocs_config.get("theme")._vars.get("locale")
return f"{locale.language}-{locale.territory}"
elif "theme" in mkdocs_config and "language" in mkdocs_config.get("theme"):
return mkdocs_config.get("theme")._vars.get("language")
else:
return None
# Some themes implement a locale or a language settings
if "theme" in mkdocs_config:
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 "
f"('{mkdocs_config.theme.name}') settings: {locale=}"
)
return (
f"{locale.language}-{locale.territory}"
if locale.territory
else f"{locale.language}"
)
else:
logger.debug(
"[rss plugin] Nor locale or language detected in theme settings "
f"('{mkdocs_config.theme.name}')."
)

return None

@staticmethod
def filter_pages(pages: list, attribute: str, length: int) -> list:
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
12 changes: 12 additions & 0 deletions tests/fixtures/mkdocs_locale_without_territory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
site_name: Test RSS Plugin
site_description: Test a language code with territory
site_url: https://guts.github.io/mkdocs-rss-plugin

use_directory_urls: true

plugins:
- rss

theme:
name: mkdocs
locale: fr
56 changes: 54 additions & 2 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,6 +356,58 @@ 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_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_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,
)

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_pretty_print_enabled(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
Expand Down

0 comments on commit 560d706

Please sign in to comment.