Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration option sitemap_lastmod #94

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add configuration option sitemap_lastmod
This option adds the "lastmod" entries to the "sitemap.xml" "url" entries.
berhoel committed Aug 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 26c99f724d5043c19b01bfa634cfc9d8bd4b5075
13 changes: 13 additions & 0 deletions docs/source/advanced-configuration.rst
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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 + "/"
28 changes: 27 additions & 1 deletion tests/test_simple.py
Original file line number Diff line number Diff line change
@@ -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,
}