diff --git a/docs/source/advanced-configuration.rst b/docs/source/advanced-configuration.rst index a3cd8c1..b62cd84 100644 --- a/docs/source/advanced-configuration.rst +++ b/docs/source/advanced-configuration.rst @@ -139,6 +139,20 @@ 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`` a string +representing the last modification date. + +.. code-block:: python + + 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/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..116d5bb 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -21,7 +21,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 +44,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]) + try: app.add_config_value("html_baseurl", default=None, rebuild="") except BaseException: @@ -203,6 +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 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 a27f4d2..4848fab 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": "XX", + }, ) 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, + } 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