Skip to content

Commit

Permalink
Simplified sitemap_lastmod option
Browse files Browse the repository at this point in the history
  • Loading branch information
berhoel committed Aug 20, 2024
1 parent dd58ec8 commit 3284efc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
9 changes: 5 additions & 4 deletions docs/source/advanced-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
16 changes: 5 additions & 11 deletions sphinx_sitemap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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="")
Expand Down Expand Up @@ -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
Expand All @@ -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 + "/"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 3284efc

Please sign in to comment.