From f0932c37b61a3f3c2a4adf9435d78e2d17fc9567 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Wed, 20 Nov 2024 22:57:12 -0800 Subject: [PATCH 01/15] rough draft --- sphinx_sitemap/__init__.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/sphinx_sitemap/__init__.py b/sphinx_sitemap/__init__.py index 7779093..c31fe93 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -13,6 +13,7 @@ import os import queue +from datetime import datetime, timezone from multiprocessing import Manager from pathlib import Path from typing import Any, Dict, List, Optional @@ -49,6 +50,14 @@ def setup(app: Sphinx) -> Dict[str, Any]: except BaseException: pass + # TODO cleanup + # TODO make sphinx-last-updated-by-git an optional install [git] + try: + app.setup_extension("sphinx_last_updated_by_git") + except BaseException: + print("failed to add extension") + pass + app.connect("builder-inited", record_builder_type) app.connect("html-page-context", add_html_link) app.connect("build-finished", create_sitemap) @@ -133,6 +142,15 @@ def add_html_link(app: Sphinx, pagename: str, templatename, context, doctree): else: file_suffix = app.builder.config.html_file_suffix + # TODO handle pages that don't have a last_updated + last_updated = None + if pagename in env.git_last_updated: + # TODO what is show_sourcelink + timestamp, show_sourcelink = env.git_last_updated[pagename] + utc_date = datetime.fromtimestamp(int(timestamp), timezone.utc) + # TODO verify dates + last_updated = utc_date.strftime("%Y-%m-%dT%H:%M:%SZ") + # Support DirectoryHTMLBuilder path structure # where generated links between pages omit the index.html if env.is_directory_builder: # type: ignore @@ -146,7 +164,7 @@ def add_html_link(app: Sphinx, pagename: str, templatename, context, doctree): sitemap_link = pagename + file_suffix if sitemap_link not in app.builder.config.sitemap_excludes: - env.app.sitemap_links.put(sitemap_link) # type: ignore + env.app.sitemap_links.put((sitemap_link, last_updated)) # type: ignore def create_sitemap(app: Sphinx, exception): @@ -189,7 +207,7 @@ def create_sitemap(app: Sphinx, exception): while True: try: - link = app.env.app.sitemap_links.get_nowait() # type: ignore + link, last_updated = app.env.app.sitemap_links.get_nowait() # type: ignore except queue.Empty: break @@ -204,6 +222,10 @@ def create_sitemap(app: Sphinx, exception): lang=lang, version=version, link=link ) + # TODO clean up lastmod + if last_updated: + ElementTree.SubElement(url, "lastmod").text = last_updated + for lang in locales: lang = lang + "/" ElementTree.SubElement( From 870c4cebcc59d8f0e99b07f30b1991b916e5f16a Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Wed, 20 Nov 2024 22:59:28 -0800 Subject: [PATCH 02/15] Revert "remove layer" This reverts commit 5789946a4e8f830c5db91da2fa6d39554e29e019. --- docs/source/_static/sitemap-icon.svg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/_static/sitemap-icon.svg b/docs/source/_static/sitemap-icon.svg index 6a70dd0..5f7b32c 100644 --- a/docs/source/_static/sitemap-icon.svg +++ b/docs/source/_static/sitemap-icon.svg @@ -1,5 +1,5 @@ - + From 10548ab5a468b2d641ba33304f768a1f3b4d8113 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Wed, 20 Nov 2024 22:59:42 -0800 Subject: [PATCH 03/15] Revert "Update filename" This reverts commit 93880a2aef7496a84b75804f1ae0682acfae7a73. --- docs/source/contributing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/contributing.rst b/docs/source/contributing.rst index 60ae55e..b0dea11 100644 --- a/docs/source/contributing.rst +++ b/docs/source/contributing.rst @@ -20,7 +20,7 @@ Local development .. code-block:: console - pip3 install -r requirements_dev.txt + pip3 install -r dev-requirements.txt #. Install pre-commit Git hook scripts: From c3549f4b5e6e2ecbab2577d909c4dfe2c228ba58 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Wed, 20 Nov 2024 23:05:42 -0800 Subject: [PATCH 04/15] add sitemap_show_lastmod --- sphinx_sitemap/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sphinx_sitemap/__init__.py b/sphinx_sitemap/__init__.py index c31fe93..78e23a3 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -45,6 +45,8 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value("sitemap_excludes", default=[], rebuild="") + app.add_config_value("sitemap_show_lastmod", default=False, rebuild="") + try: app.add_config_value("html_baseurl", default=None, rebuild="") except BaseException: @@ -52,6 +54,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: # TODO cleanup # TODO make sphinx-last-updated-by-git an optional install [git] + # TODO set sitemap_show_lastmod to True try: app.setup_extension("sphinx_last_updated_by_git") except BaseException: @@ -144,7 +147,7 @@ def add_html_link(app: Sphinx, pagename: str, templatename, context, doctree): # TODO handle pages that don't have a last_updated last_updated = None - if pagename in env.git_last_updated: + if app.builder.config.sitemap_show_lastmod and pagename in env.git_last_updated: # TODO what is show_sourcelink timestamp, show_sourcelink = env.git_last_updated[pagename] utc_date = datetime.fromtimestamp(int(timestamp), timezone.utc) From d65539c140e22982210319a97a06a60be47b9efc Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Thu, 21 Nov 2024 10:52:15 -0800 Subject: [PATCH 05/15] Turn show_lastmod true --- sphinx_sitemap/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx_sitemap/__init__.py b/sphinx_sitemap/__init__.py index 78e23a3..a15b8d7 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -54,9 +54,9 @@ def setup(app: Sphinx) -> Dict[str, Any]: # TODO cleanup # TODO make sphinx-last-updated-by-git an optional install [git] - # TODO set sitemap_show_lastmod to True try: app.setup_extension("sphinx_last_updated_by_git") + app.config.sitemap_show_lastmod = True except BaseException: print("failed to add extension") pass From a8535c62d2f2440265cae255e21c42a675d1f4cf Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Thu, 21 Nov 2024 12:49:54 -0800 Subject: [PATCH 06/15] Add [lastmod] --- pyproject.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ac5034e..3f19b16 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,17 +28,17 @@ readme = "README.rst" dynamic = [ "version", "dependencies", - "optional-dependencies", ] +[project.optional-dependencies] +lastmod = ["sphinx-last-updated-by-git"] +dev = [] + [project.urls] documentation = "https://sphinx-sitemap.readthedocs.io/en/latest/index.html" download = "https://pypi.org/project/sphinx-sitemap/" source = "https://github.com/jdillard/sphinx-sitemap" changelog = "https://github.com/jdillard/sphinx-sitemap/blob/master/CHANGELOG.rst" -[tool.setuptools.dynamic] -optional-dependencies = {dev = { file = ["requirements_dev.txt"] }} - [tool.isort] profile = "black" From b4d9fae7044668a98570c2f069dc60f081164954 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Thu, 21 Nov 2024 12:50:56 -0800 Subject: [PATCH 07/15] general cleanup --- sphinx_sitemap/__init__.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/sphinx_sitemap/__init__.py b/sphinx_sitemap/__init__.py index a15b8d7..ffb8e9f 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -20,6 +20,7 @@ from xml.etree import ElementTree from sphinx.application import Sphinx +from sphinx.errors import ExtensionError from sphinx.util.logging import getLogger __version__ = "2.6.0" @@ -52,14 +53,25 @@ def setup(app: Sphinx) -> Dict[str, Any]: except BaseException: pass - # TODO cleanup - # TODO make sphinx-last-updated-by-git an optional install [git] + # install sphinx_last_updated_by_git extension if it exists try: app.setup_extension("sphinx_last_updated_by_git") app.config.sitemap_show_lastmod = True - except BaseException: - print("failed to add extension") - pass + except ExtensionError as e: + # only throw warning if manually configured to show lastmod date + if app.config.sitemap_show_lastmod: + logger.warning( + f"{e}", + type="sitemap", + subtype="configuration", + ) + app.config.sitemap_show_lastmod = False + else: + logger.info( + f"sphinx-sitemap: {e}", + type="sitemap", + subtype="configuration", + ) app.connect("builder-inited", record_builder_type) app.connect("html-page-context", add_html_link) @@ -145,14 +157,14 @@ def add_html_link(app: Sphinx, pagename: str, templatename, context, doctree): else: file_suffix = app.builder.config.html_file_suffix - # TODO handle pages that don't have a last_updated last_updated = None if app.builder.config.sitemap_show_lastmod and pagename in env.git_last_updated: - # TODO what is show_sourcelink timestamp, show_sourcelink = env.git_last_updated[pagename] - utc_date = datetime.fromtimestamp(int(timestamp), timezone.utc) # TODO verify dates - last_updated = utc_date.strftime("%Y-%m-%dT%H:%M:%SZ") + # TODO handle untracked pages (option to use current timestamp?) + if timestamp: + utc_date = datetime.fromtimestamp(int(timestamp), timezone.utc) + last_updated = utc_date.strftime("%Y-%m-%dT%H:%M:%SZ") # Support DirectoryHTMLBuilder path structure # where generated links between pages omit the index.html @@ -225,7 +237,6 @@ def create_sitemap(app: Sphinx, exception): lang=lang, version=version, link=link ) - # TODO clean up lastmod if last_updated: ElementTree.SubElement(url, "lastmod").text = last_updated From 4fac5c6a8c765fc4b7299c69f39b883038cc1378 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Sat, 23 Nov 2024 14:23:29 -0800 Subject: [PATCH 08/15] Reapply "remove layer" This reverts commit 870c4cebcc59d8f0e99b07f30b1991b916e5f16a. --- docs/source/_static/sitemap-icon.svg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/_static/sitemap-icon.svg b/docs/source/_static/sitemap-icon.svg index 5f7b32c..6a70dd0 100644 --- a/docs/source/_static/sitemap-icon.svg +++ b/docs/source/_static/sitemap-icon.svg @@ -1,5 +1,5 @@ - + From aea76b16b02bd18e0fb0828c2e3b1e59c751da30 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Sat, 23 Nov 2024 14:23:54 -0800 Subject: [PATCH 09/15] Reapply "Update filename" This reverts commit 10548ab5a468b2d641ba33304f768a1f3b4d8113. --- docs/source/contributing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/contributing.rst b/docs/source/contributing.rst index b0dea11..60ae55e 100644 --- a/docs/source/contributing.rst +++ b/docs/source/contributing.rst @@ -20,7 +20,7 @@ Local development .. code-block:: console - pip3 install -r dev-requirements.txt + pip3 install -r requirements_dev.txt #. Install pre-commit Git hook scripts: From fad4c4167c4eed9180fa0c92b1d41655d2a02500 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Mon, 25 Nov 2024 19:39:00 -0800 Subject: [PATCH 10/15] sort classiffiers --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 3f19b16..ef2cc8f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,13 +15,13 @@ maintainers = [ {name = "Jared Dillard", email = "jared.dillard@gmail.com"}, ] classifiers = [ + "Framework :: Sphinx :: Extension", "License :: OSI Approved :: MIT License", - "Topic :: Documentation :: Sphinx", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", - "Framework :: Sphinx :: Extension", + "Topic :: Documentation :: Sphinx", ] license = {text = "MIT"} readme = "README.rst" From 1507660c18f4f1e7a13bb0c050304c11b5819dba Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Mon, 25 Nov 2024 19:40:47 -0800 Subject: [PATCH 11/15] clean up deps --- pyproject.toml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ef2cc8f..20af6d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,20 +25,24 @@ classifiers = [ ] license = {text = "MIT"} readme = "README.rst" +dependencies = [ + "requests>=2.28.1", + "flask>=2.0.0", + "sphinx-last-updated-by-git", +] dynamic = [ "version", - "dependencies", + "optional-dependencies", ] -[project.optional-dependencies] -lastmod = ["sphinx-last-updated-by-git"] -dev = [] - [project.urls] documentation = "https://sphinx-sitemap.readthedocs.io/en/latest/index.html" download = "https://pypi.org/project/sphinx-sitemap/" source = "https://github.com/jdillard/sphinx-sitemap" changelog = "https://github.com/jdillard/sphinx-sitemap/blob/master/CHANGELOG.rst" +[tool.setuptools.dynamic] +optional-dependencies = {dev = { file = ["requirements_dev.txt"] }} + [tool.isort] profile = "black" From 3cc65ce66c4d7fc39d4c65ccce0416cfccd97bc2 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Mon, 25 Nov 2024 19:41:48 -0800 Subject: [PATCH 12/15] Make show_lastmod default True --- sphinx_sitemap/__init__.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/sphinx_sitemap/__init__.py b/sphinx_sitemap/__init__.py index ffb8e9f..527f937 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -46,7 +46,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value("sitemap_excludes", default=[], rebuild="") - app.add_config_value("sitemap_show_lastmod", default=False, rebuild="") + app.add_config_value("sitemap_show_lastmod", default=True, rebuild="") try: app.add_config_value("html_baseurl", default=None, rebuild="") @@ -54,24 +54,16 @@ def setup(app: Sphinx) -> Dict[str, Any]: pass # install sphinx_last_updated_by_git extension if it exists - try: - app.setup_extension("sphinx_last_updated_by_git") - app.config.sitemap_show_lastmod = True - except ExtensionError as e: - # only throw warning if manually configured to show lastmod date - if app.config.sitemap_show_lastmod: + if app.config.sitemap_show_lastmod: + try: + app.setup_extension("sphinx_last_updated_by_git") + except ExtensionError as e: logger.warning( f"{e}", type="sitemap", subtype="configuration", ) app.config.sitemap_show_lastmod = False - else: - logger.info( - f"sphinx-sitemap: {e}", - type="sitemap", - subtype="configuration", - ) app.connect("builder-inited", record_builder_type) app.connect("html-page-context", add_html_link) From d9c2b35e9e2dea27f3908104b5fce567f8e1b264 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Mon, 25 Nov 2024 19:42:07 -0800 Subject: [PATCH 13/15] Clean up comments --- sphinx_sitemap/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sphinx_sitemap/__init__.py b/sphinx_sitemap/__init__.py index 527f937..7e781b8 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -153,7 +153,7 @@ def add_html_link(app: Sphinx, pagename: str, templatename, context, doctree): if app.builder.config.sitemap_show_lastmod and pagename in env.git_last_updated: timestamp, show_sourcelink = env.git_last_updated[pagename] # TODO verify dates - # TODO handle untracked pages (option to use current timestamp?) + # TODO handle untracked pages (add option to use current timestamp?) if timestamp: utc_date = datetime.fromtimestamp(int(timestamp), timezone.utc) last_updated = utc_date.strftime("%Y-%m-%dT%H:%M:%SZ") @@ -225,13 +225,16 @@ def create_sitemap(app: Sphinx, exception): else: lang = "" + # add page url ElementTree.SubElement(url, "loc").text = site_url + scheme.format( lang=lang, version=version, link=link ) + # add page lastmode date if it exists if last_updated: ElementTree.SubElement(url, "lastmod").text = last_updated + # add alternate language page urls for lang in locales: lang = lang + "/" ElementTree.SubElement( From 15f1b9c8cd709aa2d9ebe68318cfe626a1d4835b Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Mon, 25 Nov 2024 19:48:04 -0800 Subject: [PATCH 14/15] Bump version --- CHANGELOG.rst | 8 ++++++++ sphinx_sitemap/__init__.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b5c4f19..5635a20 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,14 @@ Changelog ========= +2.7.0 +----- + +*Release date: TBD* + +* |:sparkles:| NEW: Add support for ``lastmod`` using **sphinx-last-updated-by-git** + `#95 `_ + 2.6.0 ----- diff --git a/sphinx_sitemap/__init__.py b/sphinx_sitemap/__init__.py index 7e781b8..a256717 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -23,7 +23,7 @@ from sphinx.errors import ExtensionError from sphinx.util.logging import getLogger -__version__ = "2.6.0" +__version__ = "2.7.0" logger = getLogger(__name__) From 7faa647723ef3f0e2399f8e20c2589ae70bc9b67 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Mon, 25 Nov 2024 19:50:21 -0800 Subject: [PATCH 15/15] Add config value docs --- docs/source/configuration-values.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/source/configuration-values.rst b/docs/source/configuration-values.rst index 3cf4be7..a34aba9 100644 --- a/docs/source/configuration-values.rst +++ b/docs/source/configuration-values.rst @@ -34,3 +34,9 @@ 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_show_lastmod + + Add ```` to sitemap based on last updated time according to Git for each page. + + .. versionadded:: 2.7.0