Skip to content

Commit

Permalink
add atom feed
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Jun 23, 2024
1 parent 576c5f1 commit 965002c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ watch:
make build
echo "Started watcher..."
while true; do \
inotifywait -qr -e modify -e create -e delete -e move --include 'src/.*$$' .; \
inotifywait -qr -e modify -e create -e delete -e move --include 'src/.*$$' --include 'scripts/.*$$' .; \
make build; \
done

Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
};
py = pkgs.python311.withPackages (ps: with ps; [
jinja2
pytz
]);
shellHook = ''
find-up () {
Expand Down
65 changes: 56 additions & 9 deletions scripts/build.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,66 @@
#!/usr/bin/env python3

from __future__ import annotations

import json
import os
import shutil
import subprocess
import tempfile
import dataclasses

from datetime import datetime
from pathlib import Path
import dataclasses
from typing import Any
from xml.etree.ElementTree import Element, SubElement, ElementTree

import jinja2 # type: ignore
import pytz # type: ignore


def formatdate(x: str) -> str:
"""Convert 2024/06/08 to June 8, 2024"""
dt = datetime.strptime(x, "%Y-%m-%d")
return dt.strftime("%B %-d, %Y")
je = jinja2.Environment()


je = jinja2.Environment()
je.filters["formatdate"] = formatdate
# DATA


@dataclasses.dataclass
class PostMeta:
title: str
date: str
timestamp: datetime
public: bool

@classmethod
def parse(cls, d: dict[str, Any]) -> PostMeta:
return cls(
title=d["title"],
date=d["date"],
timestamp=datetime.fromisoformat(d["timestamp"]),
public=d["public"],
)


PostIndex = dict[str, PostMeta]


# UTILS


def site_updated_at() -> datetime:
r = subprocess.run(["git", "log", "-1", "--format=%cI"], capture_output=True, text=True)
text = r.stdout.strip()
return datetime.fromisoformat(text).astimezone(pytz.utc)


def article_updated_at(slug: str) -> datetime:
p = f"src/posts/tex/{slug}.tex"
r = subprocess.run(["git", "log", "-1", "--format=%cI", p], capture_output=True, text=True)
text = r.stdout.strip()
return datetime.fromisoformat(text).astimezone(pytz.utc)


# BUILD STEPS


def empty_dist() -> None:
# Removing and recreating the directory messes with the `make watch+serve` workflow. So we
# instead empty the directory each time if it already exists.
Expand Down Expand Up @@ -118,6 +137,33 @@ def compile_posts(posts: PostIndex):
fp.write(post)


def compile_feed(posts: PostIndex):
# fmt: off
feed = Element("feed", xmlns="http://www.w3.org/2005/Atom")
SubElement(feed, "title").text = "sunsetglow"
SubElement(feed, "link", href="https://sunsetglow.net/atom.xml", rel="self", type="application/atom+xml")
SubElement(feed, "link", href="https://sunsetglow.net/", rel="alternate", type="text/html")
SubElement(feed, "updated").text = site_updated_at().isoformat()
SubElement(feed, "id").text = "tag:sunsetglow.net,2024:site"

for slug, meta in posts.items():
post = SubElement(feed, "entry")
SubElement(post, "id").text = f"tag:sunsetglow.net,{meta.timestamp.strftime('%Y-%m-%d')}:{slug}"
SubElement(post, "link", href=f"https://sunsetglow.net/posts/{slug}.html", type="text/html")
SubElement(post, "title").text = meta.title
SubElement(post, "published").text = meta.timestamp.isoformat()
SubElement(post, "updated").text = article_updated_at(slug).isoformat()

author = SubElement(post, "author")
SubElement(author, "name").text = "blissful"
SubElement(author, "email").text = "[email protected]"
# fmt: on

tree = ElementTree(feed)
with open("dist/atom.xml", "wb") as fh:
tree.write(fh, encoding="utf-8", xml_declaration=True)


def main():
os.chdir(os.environ["PROJECT_ROOT"])

Expand All @@ -128,6 +174,7 @@ def main():
shutil.copytree("src/assets", "dist/assets")
compile_index(posts)
compile_posts(posts)
compile_feed(posts)


if __name__ == "__main__":
Expand Down
8 changes: 6 additions & 2 deletions src/index.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="/assets/css/global.css" />
<link rel="stylesheet" type="text/css" href="/assets/css/index.css" />
<link href="https://sunsetglow.net/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed" />
<title>sunset glow</title>
</head>
<body>
<div class="flex flex-col gap-2 p-4">
<div>sunset glow</div>
<div>sunset glow <a href="/atom.xml">(feed)</a></div>
<div class="flex flex-col gap-1">
<div class="bold">About:</div>
<ul>
Expand All @@ -30,7 +31,10 @@
<div class="bold">Writing:</div>
<ul>
{% for slug, post in posts.items() %}
<li><span class="tabular-nums">{{ post.date }}</span> | <a href="/posts/{{ slug }}.html">{{ post.title }}</a></li>
<li>
<span class="tabular-nums">{{ post.timestamp.strftime("%Y-%m-%d") }}</span>
| <a href="/posts/{{ slug }}.html">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/posts/index.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"frontend-build-systems": {
"title": "Exposition of Frontend Build Systems",
"date": "2024-06-08",
"timestamp": "2024-06-08T03:46:00Z",
"public": true
}
}
2 changes: 1 addition & 1 deletion src/posts/template.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="py-16" />
<div class="flex flex-col gap-4">
<div class="display text-xxl medium">{{ meta.title }}</div>
<div class="display text-sm fg-secondary italic">{{ meta.date | formatdate }}</div>
<div class="display text-sm fg-secondary italic">{{ meta.timestamp.strftime("%B %-d, %Y") }}</div>
</div>
<div class="pb-8 w-full"></div>
<div class="pandoc">
Expand Down

0 comments on commit 965002c

Please sign in to comment.