Skip to content

Commit

Permalink
Add configuration option sitemap_lastmod
Browse files Browse the repository at this point in the history
This option adds the "lastmod" entries to the "sitemap.xml" "url" entries.
  • Loading branch information
berhoel committed Aug 13, 2024
1 parent b01c95e commit 26c99f7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
13 changes: 13 additions & 0 deletions docs/source/advanced-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions docs/source/configuration-values.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 13 additions & 1 deletion sphinx_sitemap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__)

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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 + "/"
Expand Down
28 changes: 27 additions & 1 deletion tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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):
Expand All @@ -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,
}

0 comments on commit 26c99f7

Please sign in to comment.