Commit 67ea3f39 authored by Deimos's avatar Deimos
Browse files

Create plugin to set modified based on git history

This creates a new Pelican plugin that will set the modified date on
each article (blog post) and page (docs page) based on the last time
that the source file was modified in git. This makes it so that it's no
longer necessary to remember to update the Modified: line in each file
whenever it's edited.
parent c8ac17ae
Title: Announcing Tildes - a non-profit community site driven by its users' interests
Date: May 2, 2018
Modified: May 31, 2019
Slug: announcing-tildes
Summary: Online communities are in a precarious and unhealthy state right now. We can do better.
......
Title: Tildes is open-source
Date: July 27, 2018
Modified: February 17, 2019
Slug: open-source
Summary: The code behind Tildes is now open-source and available on GitLab.
......
Title: Tildes Code of Conduct
Slug: code-of-conduct
Modified: July 20, 2019
[TOC]
......
Title: Development Setup
Summary: How to initially set up a development version of Tildes
Modified: 2018-07-17
[TOC]
......
Title: Development
Summary: General info for working on a development version of Tildes
Modified: February 17, 2019
[TOC]
......
Title: Frequently Asked Questions
Slug: faq
Modified: March 19, 2019
Summary: Information about random aspects of the site
[TOC]
......
Title: Mechanics (Future)
Slug: mechanics-future
Modified: May 11, 2018
Summary: Information about some future mechanics not present in the alpha
[TOC]
......
Title: Mechanics
Slug: mechanics
Modified: October 1, 2018
Summary: Description of the basic mechanics on Tildes, and some reasoning behind them
[TOC]
......
Title: Overall Goals
Slug: overall-goals
Modified: May 11, 2018
Summary: Some of the overall goals of Tildes
[TOC]
......
Title: Tildes Privacy Policy
Slug: privacy-policy
Modified: May 24, 2018
[TOC]
......
Title: Technical Goals
Slug: technical-goals
Modified: March 19, 2019
Summary: Some of the goals of Tildes from the technical side
[TOC]
......
Title: Tildes Terms of Use
Slug: terms-of-use
Modified: May 25, 2019
[TOC]
......
Title: Text Formatting
Modified: March 6, 2019
Summary: A reference for markdown used on Tildes
[TOC]
......
Title: Topic Tagging
Modified: November 8, 2018
Summary: Information and guidelines for tagging topics
## Basic info/restrictions
......
PLUGIN_PATHS = ["plugins"]
PLUGINS = ["pelican_git_modified_plugin"]
AUTHOR = 'Chad Birch (Deimos)'
SITENAME = 'Tildes'
SITEURL = 'https://blog.tildes.net'
......
from datetime import datetime
import subprocess
from pelican.signals import content_object_init
def set_modified_date_from_git(content):
"""Set the content object's modified date based on git history."""
# get the date (YYYY-MM-DD) of the last commit that affected this file
git_process = subprocess.run(
["git", "log", "-1", "--format=%cd", "--date=format:%Y-%m-%d", content.source_path],
capture_output=True,
text=True,
)
git_output = git_process.stdout.strip()
if git_output:
# git returned a date successfully, get the date from the output
year, month, day = [int(part) for part in git_output.split("-")]
modified_date = datetime(year, month, day)
else:
# git had no output, just use the current date
now = datetime.now()
modified_date = datetime(now.year, now.month, now.day)
content.modified = modified_date
content.locale_modified = modified_date.strftime(content.date_format)
def register():
"""Register the method to run at the end of __init__ for each page/article."""
content_object_init.connect(set_modified_date_from_git)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment