LastModified#
- class documenteer.ext.lastmodified.LastModified#
Bases:
objectComputes per-page “last modified” timestamps from Git commit history.
A single instance is created in
setup, which connects twohtml-page-contexthandlers:add_time_context(early, before pydata-sphinx-theme’supdate_and_remove_templates) exposes the UTC<time>context variables. pydata empty-checks thelast-updatedfooter component and drops it unless these variables already exist when it runs, so they must be set first.add_last_modified(late, after sphinx-last-updated-by-git’s own handler) sets the human-readablelast_updatedvalue and appends the machine-readable<head>metadata.
Both handlers share a single per-page Git computation via
get_last_modified_datetime, which is memoized in_date_cache.The instance lazily constructs a
GitRepositoryon first use and remembers when timestamps can’t be produced, so that those builds are a clean no-op. Timestamps are disabled when the source directory isn’t a Git repository (including the pytest temporary source directory) and when the repository is a shallow clone (whose truncated history would yield misleading dates – a warning is emitted in that case).Methods Summary
add_last_modified(app, pagename, ...)Populate the
last_updatedvalue and<head>metadata.add_time_context(app, pagename, ...)Expose the UTC
<time>context variables for the footer.get_last_modified_datetime(app, pagename)Compute the most recent Git commit datetime for a page.
Methods Documentation
- add_last_modified(app, pagename, templatename, context, doctree)#
Populate the
last_updatedvalue and<head>metadata.This
html-page-contexthandler runs late – after sphinx-last-updated-by-git’s own handler (which the user-guide preset auto-loads through sphinx-sitemap and which resetslast_updatedtoNonewhenhtml_last_updated_fmtis unset) and after pydata-sphinx-theme’s_fix_canonical_url(so the JSON-LD metadata sees the correctedpageurl). It sets thelast_updatedcontext value – formatted in the commit’s own timezone offset and emitted by pydata-sphinx-theme as thedocbuild:last-updatemeta tag – and appends machine-readable last-modified metadata to the page<head>.The
<time>footer context variables are set separately and earlier byadd_time_context.- Parameters:
app (
Sphinx) – The Sphinx application.pagename (
str) – The docname of the page being rendered.templatename (
str) – The template used to render the page.context (
dict) – The template context, modified in place.doctree (
object|None) – The doctree for the page, orNonefor pages without a source document (such pages are skipped).
- Return type:
- add_time_context(app, pagename, templatename, context, doctree)#
Expose the UTC
<time>context variables for the footer.This
html-page-contexthandler runs early – before pydata-sphinx-theme’supdate_and_remove_templates(priority 500), which empty-checks thelast-updatedfooter component and drops it unless its context is already populated. Documenteer’s override of that component renders a<time>element gated ondocumenteer_last_modified_iso, so this handler must set it first:documenteer_last_modified_iso– the canonical UTC ISO 8601 timestamp, used in the<time datetime="...">attribute (whichrubin-last-modified.jslocalizes to the reader’s timezone).documenteer_last_modified_date– the UTC date asYYYY-MM-DD, the no-JavaScript visible fallback text.
- Parameters:
app (
Sphinx) – The Sphinx application.pagename (
str) – The docname of the page being rendered.templatename (
str) – The template used to render the page.context (
dict) – The template context, modified in place.doctree (
object|None) – The doctree for the page, orNonefor pages without a source document (such pages are skipped).
- Return type:
- get_last_modified_datetime(app, pagename)#
Compute the most recent Git commit datetime for a page.
- Parameters:
- Returns:
The timezone-aware datetime of the most recent commit touching the page’s own source file or any of its
include/literalincludedependencies, orNoneif there’s no Git repository or none of those files are tracked.- Return type:
datetime.datetime or None