Skip to content
Draft
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
1 change: 1 addition & 0 deletions .github/workflows/deploy-pr-preview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Full commit history is needed for the 'Last updated / Created on' feature to display correctly on pages.
submodules: true

- name: Pull latest from submodules
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy-production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Full commit history is needed for the 'Last updated / Created on' feature to display correctly on pages.
submodules: true

- name: Pull latest from submodules
Expand Down
66 changes: 66 additions & 0 deletions docs/_extensions/page_version_history_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Adds metadata to pages based on the Git version history. It sources information from the 'last updated' commit and the 'created at' commit for each page. Metadata includes the date of the commit and the name of its author. This metadata can be used in layouts and templates, for example: {{ page_version_history_metadata.last_updated_date }}

import os
import subprocess
import traceback

def run_git_log(command):
result = subprocess.run(
command,
check=True,
encoding="UTF-8",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)

return result.stdout.strip().split('|')

def handle_exception(error):
print(error)
traceback.print_exc()
pass

def page_version_history_metadata(app, pagename, templatename, context, doctree):
page_filepath_absolute_with_extension_glob = os.path.join(app.srcdir, pagename + ".md")

context["page_version_history_metadata"] = {
"last_updated_hash": None,
"last_updated_date": None,
"last_updated_author_name": None,
"last_updated_subject": None,
}

try:
git_log_results = run_git_log(
[
"git",
"log",
"-1",
"--date=format:%-d %b %Y",
"--pretty=format:%H|%ad|%an|%s",
"--",
# page_filepath_absolute_with_extension_glob,
"/home/runner/work/dea-knowledge-hub/dea-knowledge-hub/", # TODO This is a test
]
)

print(page_filepath_absolute_with_extension_glob) # TODO Remove this after testing
print(git_log_results) # TODO Remove this after testing

context["page_version_history_metadata"]["last_updated_hash"] = git_log_results[0]

context["page_version_history_metadata"]["last_updated_date"] = git_log_results[1]

context["page_version_history_metadata"]["last_updated_author_name"] = git_log_results[2]

context["page_version_history_metadata"]["last_updated_subject"] = git_log_results[3]

except (ValueError, Exception) as e:
handle_exception(e)

def setup(app):
app.connect('html-page-context', page_version_history_metadata)

return {
"version": "1"
}
12 changes: 12 additions & 0 deletions docs/_layouts/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@
{{ super() }}
{% endblock %}

{% block body %}
{{ super() }}

{% if page_version_history_metadata.last_updated_hash %}
<div class="page-version-history-metadata">
<div>
{% if page_version_history_metadata.last_updated_hash %}<div>Last updated on {{ page_version_history_metadata.last_updated_date }}</div>{% endif %}
</div>
</div>
{% endif %}
{% endblock %}

1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"sphinx_sitemap",
"notfound.extension",
"sphinx_copybutton",
"page_version_history_metadata",
]

myst_enable_extensions = [
Expand Down