From 6cdc2cf6c6145b19601309b5bbc883f55a7e44eb Mon Sep 17 00:00:00 2001 From: Matt from Documatt Date: Tue, 30 Jun 2020 10:15:58 +0200 Subject: [PATCH] sitemap_locales option --- README.rst | 49 ++++++++++++++++++++++++++++++++++---- sphinx_sitemap/__init__.py | 20 ++++++++++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index cc7d8da..f0294c1 100644 --- a/README.rst +++ b/README.rst @@ -52,9 +52,34 @@ Multilingual Configuration For multilingual sitemaps, you have to generate a sitemap per language/locale and then manually add their locations to a `sitemapindex`_ file. -The extension will look at the `language`_ config value for the current language -being built, and the `locale_dirs`_ value for the directory for alternate -languages, so make sure those are set. +Primary language is `language`_ config value. Alternative languages are either +manually set by ``sitemap_locales`` option or auto-detected by the extension from +the `locale_dirs`_ config value, so make sure one of those is set. + +``sitemap_locales`` configuration is handy you want to list in the sitemap only some +of existing locales, if third-party extension adds locale_dirs to Sphinx for the +languages which you don't support in your docs, or to "exclude" primary language +(`language`_). For example, if primary language is en, sitemap will contain it twice:: + + + + + https://my-site.com/docs/en/index.html + + + + + + https://my-site.com/docs/en/about.html + + + + + + +If you limit sitemap:: + + sitemap_locales = ['es', 'fr'] The end result is something like the following for each language/version build:: @@ -64,13 +89,27 @@ The end result is something like the following for each language/version build:: https://my-site.com/docs/en/index.html - https://my-site.com/docs/en/about.html - + + + +If you set special value ``[None]``:: + + sitemap_locales = [None] + +only primary language is generated:: + + + + + https://my-site.com/docs/en/index.html + + + https://my-site.com/docs/en/about.html diff --git a/sphinx_sitemap/__init__.py b/sphinx_sitemap/__init__.py index b848f1c..2f9601e 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -27,6 +27,11 @@ def setup(app): default="{lang}{version}{link}", rebuild=False ) + app.add_config_value( + 'sitemap_locales', + default=None, + rebuild=False + ) try: app.add_config_value( @@ -50,6 +55,21 @@ def setup(app): def get_locales(app, exception): + # Manually configured list of locales + sitemap_locales = app.builder.config.sitemap_locales + if sitemap_locales: + # special value to add nothing -> use primary language only + if sitemap_locales == [None]: + return + + # otherwise, add each locale + for locale in sitemap_locales: + # skip primary language + if locale != app.builder.config.language: + app.locales.append(locale) + return + + # Or autodetect for locale_dir in app.builder.config.locale_dirs: locale_dir = os.path.join(app.confdir, locale_dir) if os.path.isdir(locale_dir):