]> Lady’s Gitweb - x_status_git/commitdiff
Add atom feeds
authorLady <redacted>
Fri, 15 Dec 2023 03:46:02 +0000 (22:46 -0500)
committerLady <redacted>
Sat, 23 Dec 2023 20:17:52 +0000 (15:17 -0500)
Caddyfile
README.markdown
post-receive

index 03420b830e05f4eb0b4ee6cf5f21947c5067a1b9..5b8aca551673ee1445a0b5358b7538106fad93fa 100644 (file)
--- a/Caddyfile
+++ b/Caddyfile
@@ -9,9 +9,14 @@ status.site.example {
                path_regexp jsonld \.jsonld$
        }
 
+       @atom {
+               path_regexp atom \.atom$
+       }
+
        @bare {
                not path_regexp /$
                not path_regexp \.jsonld$
+               not path_regexp \.atom$
        }
 
        @empty {
@@ -21,11 +26,13 @@ status.site.example {
        handle / {
                rewrite * /index.html
                header Link </about.jsonld>;rel=meta;type="application/ld+json"
+               header Link </statuses.atom>;rel=alternate;type="application/atom+xml"
        }
 
        handle /about {
                rewrite * /.about.html
                header Link </about.jsonld>;rel=meta;type="application/ld+json"
+               header Link </statuses.atom>;rel=alternate;type="application/atom+xml"
        }
 
        redir /about/ /about
@@ -35,12 +42,15 @@ status.site.example {
        handle /statuses {
                rewrite * /.statuses.html
                header Link </statuses.jsonld>;rel=meta;type="application/ld+json"
+               header Link </statuses.atom>;rel=alternate;type="application/atom+xml"
        }
 
        redir /statuses/ /statuses
 
        rewrite /statuses.jsonld /statuses/index.jsonld
 
+       rewrite /statuses.atom /statuses/index.atom
+
        @dated {
                path_regexp matcher ^/statuses/(?P<ym>\d{4}-\d{2})(?P<suffix>/[^/.]+)?(?:\..*|/)?$
                not path_regexp ^/statuses/index[/.]?
@@ -51,6 +61,7 @@ status.site.example {
                        handle @bare {
                                rewrite * /.topic.html
                                header Link </statuses/{re.matcher.ym}.jsonld>;rel=meta;type="application/ld+json"
+                               header Link </statuses.atom>;rel=alternate;type="application/atom+xml"
                        }
 
                        handle @slash {
@@ -60,12 +71,17 @@ status.site.example {
                        handle @jsonld {
                                rewrite * /statuses/{re.matcher.ym}/index.jsonld
                        }
+
+                       handle @atom {
+                               rewrite * /statuses/{re.matcher.ym}/index.atom
+                       }
                }
 
                handle {
                        handle @bare {
                                rewrite * /.status.html
                                header Link </statuses/{re.matcher.ym}.jsonld>;rel=meta;type="application/ld+json"
+                               header Link </statuses.atom>;rel=alternate;type="application/atom+xml"
                        }
 
                        handle @slash {
@@ -93,6 +109,7 @@ status.site.example {
                        handle @bare {
                                rewrite * /.topic.html
                                header Link </topics/{re.matcher.topic}.jsonld>;rel=meta;type="application/ld+json"
+                               header Link </topics/{re.matcher.topic}.atom>;rel=alternate;type="application/atom+xml"
                        }
 
                        handle @slash {
@@ -102,12 +119,17 @@ status.site.example {
                        handle @jsonld {
                                rewrite * /topics/{re.matcher.topic}/index.jsonld
                        }
+
+                       handle @atom {
+                               rewrite * /topics/{re.matcher.topic}/index.atom
+                       }
                }
 
                handle {
                        handle @bare {
                                rewrite * /.status.html
                                header Link </topics/{re.matcher.topic}.jsonld>;rel=meta;type="application/ld+json"
+                               header Link </topics/{re.matcher.topic}.atom>;rel=alternate;type="application/atom+xml"
                        }
 
                        handle @slash {
index 8a29e731f8f9ac86281707d7f2deeb451e529814..4195de1a79e2b8583dd581b48d01f62defa3b7b7 100644 (file)
@@ -154,6 +154,24 @@ In all cases, for `/$PATH.jsonld`, this just serves the file at
       `[0-9A-Za-z_-]+`):
     Serve the file at `/topics/$TOPIC/index.jsonld`.
 
+### Atom responses
+
+These responses **should** be served with a `Content-Type` of
+  `application/atom+xml`.
+In all cases, for `/$PATH.atom`, this just serves the file at
+  `/$PATH/index.atom`.
+
+ +  **`GET /statuses.atom`**:
+    Serve the file at `/statuses/index.atom`.
+
+ +  **`GET /statuses/$YYYY-MM.atom`** (where `$YYYY-MM` is an
+      `xsd:gYearMonth`):
+    Serve the file at `/$YYYY-MM/index.atom`.
+
+ +  **`GET /topics/$TOPIC.atom`** (where `$TOPIC` matches
+      `[0-9A-Za-z_-]+`):
+    Serve the file at `/topics/$TOPIC/index.atom`.
+
 ### Other Headers
 
 All responses **should** have a `Access-Control-Allow-Origin` header
index 17a8d227d298970e66e597183367cd5e0d4ba0e5..f6289dabf8303c50e5aa0b5fe0705f42e6bed47b 100755 (executable)
@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+from datetime import datetime as dt, timezone
 from glob import iglob
 from itertools import starmap
 import json
@@ -11,7 +12,7 @@ from subprocess import run
 from sys import stdin
 from warnings import warn
 from xml.dom import XHTML_NAMESPACE
-from xml.dom.minidom import getDOMImplementation
+from xml.dom.minidom import getDOMImplementation, parseString
 
 GIT_DIRECTORY = "/home/USERNAME/Status.git"
 BUILD_DIRECTORY = "/home/USERNAME/status.site.example/.build"
@@ -20,6 +21,10 @@ PUBLIC_URL = "https://status.site.example"
 LANG = "en"
 LIVE_BRANCH = "live"
 
+UTC = timezone.utc
+CURRENT_DATETIME = f"{dt.now(UTC).replace(tzinfo=None).isoformat(timespec='seconds')}Z"
+ATOM_NAMESPACE = "http://www.w3.org/2005/Atom"
+
 if stdin.read().split()[-1] == f"refs/heads/{LIVE_BRANCH}":
 
        print(f"This is an update to the '{LIVE_BRANCH}' branch; regenerating site…")
@@ -101,6 +106,69 @@ if stdin.read().split()[-1] == f"refs/heads/{LIVE_BRANCH}":
                        status["content"] = statusxml(text.read().strip())
                return (datetime, identifier, status)
 
+       def atomForLD (ld):
+               doc = getDOMImplementation().createDocument(None, "feed", None)
+               atomElt = doc.documentElement
+               atomElt.setAttribute("xmlns", ATOM_NAMESPACE)
+               atomElt.setAttribute("xml:lang", LANG)
+               subject = ld["subject"] if "subject" in ld else "Statuses"
+               titleElt = atomElt.appendChild(doc.createElement("title"))
+               titleElt.appendChild(doc.createTextNode(f"{subject} @ {PUBLIC_URL}"))
+               updatedElt = atomElt.appendChild(doc.createElement("updated"))
+               updatedElt.appendChild(doc.createTextNode(CURRENT_DATETIME))
+               generatorElt = atomElt.appendChild(doc.createElement("generator"))
+               generatorElt.appendChild(doc.createTextNode("x_status_git"))
+               generatorElt.setAttribute("uri", "https://git.ladys.computer/x_status_git")
+               atomLinks = {}
+               if "OrderedCollectionPage" in ld["@type"]:
+                       idElt = atomElt.appendChild(doc.createElement("id"))
+                       idElt.appendChild(doc.createTextNode(f"{PUBLIC_URL}/statuses"))
+                       atomLinks["alternate"] = f"{PUBLIC_URL}/statuses"
+                       atomLinks["current"] = f"{PUBLIC_URL}/statuses.atom"
+                       atomLinks["self"] = atomLinks["current"] if ld["@id"] == ld["current"] else f"{ld['@id']}.atom"
+                       if "prev" in ld:
+                               atomLinks["prev-archive"] = f"{ld['prev']}.atom"
+                       if "next" in ld and ld["next"] != ld["current"]:
+                               atomLinks["next-archive"] = f"{ld['next']}.atom"
+               else:
+                       idElt = atomElt.appendChild(doc.createElement("id"))
+                       idElt.appendChild(doc.createTextNode(ld["@id"]))
+                       atomLinks["alternate"] = ld["@id"]
+                       atomLinks["self"] = f"{ld['@id']}.atom"
+               for (rel, href) in atomLinks.items():
+                       linkElt = atomElt.appendChild(doc.createElement("link"))
+                       linkElt.setAttribute("rel", rel)
+                       linkElt.setAttribute("href", href)
+               for item in ld["items"]:
+                       entryElt = atomElt.appendChild(doc.createElement("entry"))
+                       title = item["title"] if "title" in item else item["content"][0:27] + "…"
+                       titleElt = entryElt.appendChild(doc.createElement("title"))
+                       titleElt.appendChild(doc.createTextNode(title))
+                       idElt = entryElt.appendChild(doc.createElement("id"))
+                       idElt.appendChild(doc.createTextNode(item["@id"]))
+                       updatedElt = entryElt.appendChild(doc.createElement("updated"))
+                       updatedElt.appendChild(doc.createTextNode(CURRENT_DATETIME))
+                       if "created" in item:
+                               publishedElt = entryElt.appendChild(doc.createElement("published"))
+                               publishedElt.appendChild(doc.createTextNode(item["created"]))
+                       authorElt = entryElt.appendChild(doc.createElement("author"))
+                       if "author" in item:
+                               nameElt = authorElt.appendChild(doc.createElement("name"))
+                               nameElt.appendChild(doc.createTextNode(item["author"]["name"]))
+                               uriElt = authorElt.appendChild(doc.createElement("uri"))
+                               uriElt.appendChild(doc.createTextNode(item["author"]["@id"]))
+                       else:
+                               nameElt = authorElt.appendChild(doc.createElement("name"))
+                               nameElt.appendChild(doc.createTextNode("Anonymous"))
+                       contentElt = entryElt.appendChild(doc.createElement("content"))
+                       contentElt.setAttribute("type", "xhtml")
+                       contentDiv = contentElt.appendChild(doc.createElement("div"))
+                       contentDiv.setAttribute("xmlns", XHTML_NAMESPACE)
+                       contentDiv.setAttribute("lang", LANG)
+                       for child in list(parseString(item["content"]).documentElement.childNodes):
+                               contentDiv.appendChild(child)
+               return (atomLinks["self"], atomElt.toxml())
+
        # Get status paths.
        status_paths = []
        for yearpath in Path(f"{BUILD_DIRECTORY}/").glob("[0-9][0-9][0-9][0-9]"):
@@ -163,6 +231,9 @@ if stdin.read().split()[-1] == f"refs/heads/{LIVE_BRANCH}":
                        ld["next"] = f"{PUBLIC_URL}/statuses/{statuspairs[index + 1][1][0]}"
                with open(f"{PUBLIC_DIRECTORY}/statuses/{yyyy_mm}/index.jsonld", "w", encoding="utf-8") as f:
                        json.dump(ld, f, ensure_ascii=False, allow_nan=False)
+               atomlink, atomxml = atomForLD(ld)
+               with open(f"{PUBLIC_DIRECTORY}/{atomlink[len(PUBLIC_URL):-5]}/index.atom", "w", encoding="utf-8") as f:
+                       f.write(atomxml)
        with open(f"{PUBLIC_DIRECTORY}/statuses/index.jsonld", "w", encoding="utf-8") as f:
                json.dump({ "@context": { "@language": LANG, "activity": "https://www.w3.org/ns/activitystreams#", "sioc": "http://rdfs.org/sioc/ns#", "OrderedCollection": "activity:OrderedCollection", "Thread": "sioc:Thread", "current": { "@id": "activity:current", "@type": "@id" }, "first": { "@id": "activity:first", "@type": "@id" }, "has_parent": { "@id": "sioc:has_parent", "@type": "id" } }, "@id": f"{PUBLIC_URL}/statuses", "@type": ["OrderedCollection", "Thread"], "first": f"{PUBLIC_URL}/statuses/{statuspairs[0][1][0]}", "current": f"{PUBLIC_URL}/statuses/{statuspairs[-1][1][0]}", "has_parent": f"{PUBLIC_URL}" }, f, ensure_ascii=False, allow_nan=False)
 
@@ -174,6 +245,9 @@ if stdin.read().split()[-1] == f"refs/heads/{LIVE_BRANCH}":
                        mkdir(f"{PUBLIC_DIRECTORY}/topics/{topic}")
                with open(f"{PUBLIC_DIRECTORY}/topics/{topic}/index.jsonld", "w", encoding="utf-8") as f:
                        json.dump(ld, f, ensure_ascii=False, allow_nan=False)
+               atomlink, atomxml = atomForLD(ld)
+               with open(f"{PUBLIC_DIRECTORY}/{atomlink[len(PUBLIC_URL):-5]}/index.atom", "w", encoding="utf-8") as f:
+                       f.write(atomxml)
        with open(f"{PUBLIC_DIRECTORY}/topics/index.jsonld", "w", encoding="utf-8") as f:
                json.dump({ "@context": { "@language": LANG, "activity": "https://www.w3.org/ns/activitystreams#", "dct": "http://purl.org/dc/terms/", "sioc": "http://rdfs.org/sioc/ns#", "Collection": "activity:Collection", "Forum": "sioc:Forum", "items": { "@id": "activity:items", "@type": "@id" }, "has_parent": { "@id": "sioc:has_parent", "@type": "id" }, "subject": "dct:subject" }, "@id": f"{PUBLIC_URL}/topics", "@type": ["Collection", "Forum"], "items": list(map(lambda a: { "@id": a["@id"], "subject": a["subject"] }, topics.values())), "has_parent": f"{PUBLIC_URL}" }, f, ensure_ascii=False, allow_nan=False)
 
This page took 0.031702 seconds and 4 git commands to generate.