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
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions docs/source/advanced-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 ------------------------------------------------

Expand Down
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
8 changes: 7 additions & 1 deletion sphinx_sitemap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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:
Expand Down Expand Up @@ -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 + "/"
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": "XX",
},
)
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,
}
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down