From f8b7a1a1647756e11bbd0c2ad6c2196ae7f2943d Mon Sep 17 00:00:00 2001 From: Max Value Date: Thu, 5 Mar 2026 13:27:14 +0000 Subject: [PATCH] Added atom file generator --- .gitignore | 3 +- atom.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 ++- templates/atom.xml | 28 +++++++++++++++++ 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 atom.py create mode 100644 templates/atom.xml diff --git a/.gitignore b/.gitignore index bc95d3e..7fa217d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ build/* recipes/* -/index.html +index* +atom-* *.pdf diff --git a/atom.py b/atom.py new file mode 100644 index 0000000..7fc4fcc --- /dev/null +++ b/atom.py @@ -0,0 +1,78 @@ +from jinja2 import Environment, FileSystemLoader +import xml.etree.ElementTree as et +from datetime import datetime +from tomllib import load +from git import Repo +import requests + +""" +Builds the atom file based on the git repo for multiple protocols: https and gem +""" + +def multi(parent, children): + for tag in children: + child = et.SubElement(parent, tag) + child.text = children[tag] + +def page_content(filename): + with open(filename, "rb") as f: + page = load(f) + + return page["information"]["title"], page["information"]["description"] + +# get the repo and the head commit + +repo = Repo(".") +head = repo.heads.main.commit +updated = datetime.fromtimestamp(head.committed_date).strftime("%Y-%m-%dT%H:%M:%SZ") + +# run through pages + +entries = [] +for page in head.tree["src"]: + commits = list(head.iter_items(repo, head, page.path)) + + # run through commit summaries and format for the atom + + summaries = [] + for (i, commit) in enumerate(commits): + created = datetime.fromtimestamp( + commit.committed_date + ).date().isoformat() + + if i+1 == len(commits): + summaries.append(f"(Created {created}) {commit.summary}.") + else: + summaries.append(f"(Updated {created}) {commit.summary}, ") + + # get the commit time and title, summary + + created = datetime.fromtimestamp(commits[-1].committed_date).strftime("%Y-%m-%dT%H:%M:%SZ") + title, summary = page_content(page.path) + + entries.append({ + "title": title, + "summary": summary, + "filename": page.path[4:-5], + "created": created, + "summaries": summaries + }) + +# get the enviroment and template + +enviroment = Environment(loader = FileSystemLoader("templates")) +template = enviroment.get_template("atom.xml") + +# render the atoms for different protocols + +with open("atom-https.xml", "w") as f: + f.write(template.render( + protocol="https", filetype="html", + entries=entries, updated=updated + )) + +with open("atom-gemini.xml", "w") as f: + f.write(template.render( + protocol="gemini", filetype="gmi", + entries=entries, updated=updated + )) diff --git a/requirements.txt b/requirements.txt index 1b2b710..922f1aa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ +gitpython +requests +feedgen jinja2 tqdm -feedgen diff --git a/templates/atom.xml b/templates/atom.xml new file mode 100644 index 0000000..1e32cc8 --- /dev/null +++ b/templates/atom.xml @@ -0,0 +1,28 @@ + + The Bread Machine + Changes to the bread site + Will Greenwood + {{ protocol }}://bread.ozva.co.uk/ + {{ updated }} + /favicon.ico + + + + + {% for entry in entries %} + + {{ protocol }}://bread.ozva.co.uk/recipes/{{ entry.filename }}.{{ filetype }} + + + {{ entry.title }} + {{ entry.created }} + {{ entry.summary }} + + History: + {% for summary in entry.summaries -%} + {{ summary }} + {% endfor -%} + + + {% endfor %} + -- 2.39.2