From 26c99f724d5043c19b01bfa634cfc9d8bd4b5075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berthold=20H=C3=B6llmann?= Date: Tue, 13 Aug 2024 21:51:51 +0200 Subject: [PATCH 1/4] Add configuration option `sitemap_lastmod` This option adds the "lastmod" entries to the "sitemap.xml" "url" entries. --- docs/source/advanced-configuration.rst | 13 ++++++++++++ docs/source/configuration-values.rst | 8 ++++++++ sphinx_sitemap/__init__.py | 14 ++++++++++++- tests/test_simple.py | 28 +++++++++++++++++++++++++- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/docs/source/advanced-configuration.rst b/docs/source/advanced-configuration.rst index a3cd8c1..5e09f33 100644 --- a/docs/source/advanced-configuration.rst +++ b/docs/source/advanced-configuration.rst @@ -139,6 +139,19 @@ To exclude a set of pages, add each page's path to ``sitemap_exclude``: "genindex.html", ] +.. _configuration_lastmod: + +Adding Last Modification Date +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +To add the date of the last page modification set ``sitemap_lastmod`` to either ``True``, or a string representating +the last modification date. + +.. code-block:: python + + sitemap_lastmod = True + # or + sitemap_lastmod = "2024-08-13" .. _sitemapindex.xml: https://support.google.com/webmasters/answer/75712?hl=en .. _sitemaps.org: https://www.sitemaps.org/protocol.html diff --git a/docs/source/configuration-values.rst b/docs/source/configuration-values.rst index 3cf4be7..3294cfc 100644 --- a/docs/source/configuration-values.rst +++ b/docs/source/configuration-values.rst @@ -34,3 +34,11 @@ A list of of possible configuration values to configure in **conf.py**: See :ref:`configuration_excluding_pages` for more information. .. versionadded:: 2.6.0 + +.. confval:: sitemap_lastmod + + The entry ``lastmod`` is added to the output file. + + See :ref:`configuration_lastmod` for more information. + + .. versionadded:: 2.7.0 diff --git a/sphinx_sitemap/__init__.py b/sphinx_sitemap/__init__.py index 7779093..2365ed8 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -11,6 +11,7 @@ # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. +import datetime import os import queue from multiprocessing import Manager @@ -21,7 +22,7 @@ from sphinx.application import Sphinx from sphinx.util.logging import getLogger -__version__ = "2.6.0" +__version__ = "2.7.0" logger = getLogger(__name__) @@ -44,6 +45,8 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value("sitemap_excludes", default=[], rebuild="") + app.add_config_value("sitemap_lastmod", default=None, rebuild="", types=[str, bool]) + try: app.add_config_value("html_baseurl", default=None, rebuild="") except BaseException: @@ -187,6 +190,8 @@ def create_sitemap(app: Sphinx, exception): else: version = "" + date = f"{datetime.datetime.now():%Y-%m-%d}" + while True: try: link = app.env.app.sitemap_links.get_nowait() # type: ignore @@ -203,6 +208,13 @@ def create_sitemap(app: Sphinx, exception): ElementTree.SubElement(url, "loc").text = site_url + scheme.format( lang=lang, version=version, link=link ) + if app.builder.config.sitemap_lastmod: + if isinstance(app.builder.config.sitemap_lastmod, str): + ElementTree.SubElement(url, "lastmod").text = ( + app.builder.config.sitemap_lastmod + ) + else: + ElementTree.SubElement(url, "lastmod").text = date for lang in locales: lang = lang + "/" diff --git a/tests/test_simple.py b/tests/test_simple.py index a27f4d2..26b6ccb 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -68,12 +68,21 @@ def test_html_file_suffix(app, status, warning): "search", ] } + lastmod = [ + e.text + for e in doc.findall(".//{http://www.sitemaps.org/schemas/sitemap/0.9}lastmod") + ] + assert not lastmod @pytest.mark.sphinx( "dirhtml", freshenv=True, - confoverrides={"html_baseurl": "https://example.org/docs/", "language": "en"}, + confoverrides={ + "html_baseurl": "https://example.org/docs/", + "language": "en", + "sitemap_lastmod": True, + }, ) def test_simple_dirhtml(app, status, warning): app.warningiserror = True @@ -99,6 +108,11 @@ def test_simple_dirhtml(app, status, warning): "search/", ] } + lastmod = [ + e.text + for e in doc.findall(".//{http://www.sitemaps.org/schemas/sitemap/0.9}lastmod") + ] + assert len(lastmod) == len(urls) @pytest.mark.sphinx( @@ -108,6 +122,7 @@ def test_simple_dirhtml(app, status, warning): "html_baseurl": "https://example.org/docs/", "language": "en", "sitemap_excludes": ["search.html", "genindex.html"], + "sitemap_lastmod": "2024-08-13", }, ) def test_simple_excludes(app, status, warning): @@ -132,3 +147,14 @@ def test_simple_excludes(app, status, warning): "elitr", ] } + + lastmod = [ + e.text + for e in doc.findall(".//{http://www.sitemaps.org/schemas/sitemap/0.9}lastmod") + ] + + assert len(lastmod) == len(urls) + + assert set(lastmod) == { + app.builder.config.sitemap_lastmod, + } From a98cd38e4d41b0d8e5bd8d4ca76d5d113d36c49c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berthold=20H=C3=B6llmann?= Date: Tue, 13 Aug 2024 21:53:47 +0200 Subject: [PATCH 2/4] Add `{posargs}` to the `pytest` call This allows to specify additional command line arguments for pytest with the tox call. --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index ff201c2..e716c34 100644 --- a/tox.ini +++ b/tox.ini @@ -13,7 +13,7 @@ deps = sphinx7: Sphinx[test]~=7.0 sphinxlast: Sphinx[test] commands = - pytest -W ignore::DeprecationWarning + pytest -W ignore::DeprecationWarning {posargs} [flake8] max-line-length = 100 From dd58ec8f297c8af49166a016c781a43bd0a67a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berthold=20H=C3=B6llmann?= Date: Tue, 20 Aug 2024 21:37:22 +0200 Subject: [PATCH 3/4] Fixed typo in documentation. --- docs/source/advanced-configuration.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/advanced-configuration.rst b/docs/source/advanced-configuration.rst index 5e09f33..937e218 100644 --- a/docs/source/advanced-configuration.rst +++ b/docs/source/advanced-configuration.rst @@ -144,7 +144,7 @@ To exclude a set of pages, add each page's path to ``sitemap_exclude``: Adding Last Modification Date ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -To add the date of the last page modification set ``sitemap_lastmod`` to either ``True``, or a string representating +To add the date of the last page modification set ``sitemap_lastmod`` to either ``True``, or a string representing the last modification date. .. code-block:: python From 3284efcebbe996a94cddf3caeaff9b69150ae4c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berthold=20H=C3=B6llmann?= Date: Tue, 20 Aug 2024 22:12:54 +0200 Subject: [PATCH 4/4] Simplified `sitemap_lastmod` option --- docs/source/advanced-configuration.rst | 9 +++++---- docs/source/conf.py | 3 +++ sphinx_sitemap/__init__.py | 16 +++++----------- tests/test_simple.py | 2 +- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/docs/source/advanced-configuration.rst b/docs/source/advanced-configuration.rst index 937e218..b62cd84 100644 --- a/docs/source/advanced-configuration.rst +++ b/docs/source/advanced-configuration.rst @@ -144,14 +144,15 @@ To exclude a set of pages, add each page's path to ``sitemap_exclude``: Adding Last Modification Date ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -To add the date of the last page modification set ``sitemap_lastmod`` to either ``True``, or a string representing -the last modification date. +To add the date of the last page modification set ``sitemap_lastmod`` a string +representing the last modification date. .. code-block:: python - sitemap_lastmod = True - # or sitemap_lastmod = "2024-08-13" + # or + import datetime + sitemap_lastmod = f"{datetime.datetime.now():%Y-%m-%d}" .. _sitemapindex.xml: https://support.google.com/webmasters/answer/75712?hl=en .. _sitemaps.org: https://www.sitemaps.org/protocol.html diff --git a/docs/source/conf.py b/docs/source/conf.py index 29ae58f..993eaea 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -11,6 +11,7 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # +import datetime import re import subprocess @@ -121,6 +122,8 @@ # Output file base name for HTML help builder. htmlhelp_basename = "SphinxSitemapdoc" +# Set last modification date in sitemap +sitemap_lastmod = f"{datetime.datetime.now():%Y-%m-%d}" # -- Options for LaTeX output ------------------------------------------------ diff --git a/sphinx_sitemap/__init__.py b/sphinx_sitemap/__init__.py index 2365ed8..116d5bb 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -11,7 +11,6 @@ # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. -import datetime import os import queue from multiprocessing import Manager @@ -45,7 +44,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value("sitemap_excludes", default=[], rebuild="") - app.add_config_value("sitemap_lastmod", default=None, rebuild="", types=[str, bool]) + app.add_config_value("sitemap_lastmod", default=None, rebuild="", types=[str]) try: app.add_config_value("html_baseurl", default=None, rebuild="") @@ -190,8 +189,6 @@ def create_sitemap(app: Sphinx, exception): else: version = "" - date = f"{datetime.datetime.now():%Y-%m-%d}" - while True: try: link = app.env.app.sitemap_links.get_nowait() # type: ignore @@ -208,13 +205,10 @@ def create_sitemap(app: Sphinx, exception): ElementTree.SubElement(url, "loc").text = site_url + scheme.format( lang=lang, version=version, link=link ) - if app.builder.config.sitemap_lastmod: - if isinstance(app.builder.config.sitemap_lastmod, str): - ElementTree.SubElement(url, "lastmod").text = ( - app.builder.config.sitemap_lastmod - ) - else: - ElementTree.SubElement(url, "lastmod").text = date + if app.builder.config.sitemap_lastmod is not None: + ElementTree.SubElement(url, "lastmod").text = ( + app.builder.config.sitemap_lastmod + ) for lang in locales: lang = lang + "/" diff --git a/tests/test_simple.py b/tests/test_simple.py index 26b6ccb..4848fab 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -81,7 +81,7 @@ def test_html_file_suffix(app, status, warning): confoverrides={ "html_baseurl": "https://example.org/docs/", "language": "en", - "sitemap_lastmod": True, + "sitemap_lastmod": "XX", }, ) def test_simple_dirhtml(app, status, warning):