I have a page on my Jekyll blog called changes.md. I want a python script (update_changes_link.py) to automatically update two date strings in a footer.html based on the top-level header in changes.md
I want it to be a simple script that I call, not a formal Jekyll plugin.
In the below example, both instances of "2023-05-19" in footer.html should be changed to "2023-05-22".
file structure:
- _includes/
- footer.html
- changes.md
- _plugins/
- update_changes_link.py
footer.html
<hr>
<div class="footer" style="text-align:right;">
<i>
<a href="/changes#2023-05-19">Website last updated: 2023-05-19</a>
<i/>
</div>
excerpt of changes.md
---
layout: page
title: Changes
---
<section id="2023-05-22"></section>
# `2023-05-22`
- Formatting updates to improve performance on mobile (largely shifting some widths to using [Em/em](https://en.wikipedia.org/wiki/Em_(typography)) and adding dynamic changes with `@media screen and (min-width: 500px)` in the CSS). Also learned how to 'Toggle device toolbar' in Chrome DevTools.
<section id="2023-05-19"></section>
# `2023-05-19`
- Updated my `special-box note` (at the top of my homepage) about actively
import re
import os
def find_new_date(changes_md_path):
# read the changes file
with open(changes_md_path, 'r') as changes_file:
changes = changes_file.read()
# find all dates using a regular expression
dates = re.findall(r'# `(\d{4}-\d{2}-\d{2})`', changes)
# return the most recent date
return max(dates)
def update_footer(new_date, footer_html_path):
# read the footer file
with open(footer_html_path, 'r') as footer_file:
footer = footer_file.read()
# find the old date using a regular expression
old_date = re.search(
r'<a href="/changes#(\d{4}-\d{2}-\d{2})">Website last updated: (\d{4}-\d{2}-\d{2})</a>', footer)
if old_date:
# replace all instances of the old date with the new date
updated_footer = footer.replace(old_date.group(
1), new_date).replace(old_date.group(2), new_date)
# write the updated footer back to the file
with open(footer_html_path, 'w') as footer_file:
footer_file.write(updated_footer)
print(f"Updated changes link in the footer from {old_date.group(1)} to {new_date}.")
def main():
# get paths
root_dir = os.path.dirname(os.path.abspath(__file__))
changes_md_path = os.path.join(root_dir, '..', 'changes.md')
footer_html_path = os.path.join(root_dir, '..', '_includes', 'footer.html')
# get the new date
new_date = find_new_date(changes_md_path)
# update the footer
update_footer(new_date, footer_html_path)
if __name__ == "__main__":
main()