]> Lady’s Gitweb - x_status_git/blob - post-receive
Add sample Caddyfile
[x_status_git] / post-receive
1 #!/usr/bin/env python3
2 from glob import iglob
3 from itertools import starmap
4 import json
5 from os import mkdir
6 from os.path import exists
7 from pathlib import Path
8 import re
9 from shutil import copy2, rmtree
10 from subprocess import run
11 from sys import stdin
12 from warnings import warn
13 from xml.dom import XHTML_NAMESPACE
14 from xml.dom.minidom import getDOMImplementation
15
16 GIT_DIRECTORY = "/home/USERNAME/Status.git"
17 BUILD_DIRECTORY = "/home/USERNAME/status.site.example/.build"
18 PUBLIC_DIRECTORY = "/home/USERNAME/status.site.example/public"
19 PUBLIC_URL = "https://status.site.example"
20 LANG = "en"
21 LIVE_BRANCH = "live"
22
23 if stdin.read().split()[-1] == f"refs/heads/{LIVE_BRANCH}":
24
25 print(f"This is an update to the '{LIVE_BRANCH}' branch; regenerating site…")
26
27 # Set up the build directory.
28 if exists(BUILD_DIRECTORY):
29 rmtree(BUILD_DIRECTORY)
30 run(["git", "clone", "--local", "--branch", "live", GIT_DIRECTORY, BUILD_DIRECTORY], capture_output=True, encoding="utf-8")
31
32 # Set up various containers.
33 months = {}
34 topics = {}
35
36 # Create an XML representation of the provided status text.
37 def statusxml (text, version="1.0"):
38 doc = getDOMImplementation().createDocument(None, "article", None)
39 articleElt = doc.documentElement
40 articleElt.setAttribute("xmlns", XHTML_NAMESPACE)
41 articleElt.setAttribute("lang", LANG)
42 for para in text.split("\n\n"):
43 paraElt = articleElt.appendChild(doc.createElement("p"))
44 for component in re.findall(r'<[a-z]+:[^\s]*>(?:="[^\n"]+")?|\n|[^<\n]+|<(?![a-z]+:[^\s]*>)', para):
45 if component == "\n":
46 paraElt.appendChild(doc.createElement("br"))
47 elif re.fullmatch(r'<[a-z]+:[^\s]*>(?:="[^\n"]+")?', component):
48 href = component.split(">", maxsplit=1)[0][1:]
49 anchorElt = paraElt.appendChild(doc.createElement("a"))
50 anchorElt.setAttribute("href", href)
51 anchorElt.setAttribute("rel", "noreferrer")
52 anchorElt.appendChild(doc.createTextNode(component if len(href) == len(component) - 2 else component[len(href)+4:-1]))
53 else:
54 paraElt.appendChild(doc.createTextNode(component))
55 return articleElt.toxml()
56
57 # Map status paths to status objects, or None if there is an error.
58 #
59 # The provided path must be to a `text` object.
60 def statusmap (topic, path):
61 status = { "@type": "MicroblogPost" }
62 version_path = next(path.parent.glob("0=*"))
63 if version_path and version_path.name != "0=x_status_git_1.0":
64 warn(f"Unrecognized version for {path}; skipping.")
65 return None
66 if topic:
67 status["subject"] = topic
68 author_path = next(path.parent.glob("1=*"))
69 if author_path:
70 status["author"] = { "name": author_path.name[2:] }
71 with author_path.open("r", encoding="utf-8") as text:
72 status["author"]["@id"] = text.read().strip()
73 date_path = next(path.parent.glob("3=*"))
74 datetime = ""
75 if date_path:
76 with date_path.open("r", encoding="utf-8") as text:
77 datetime = text.read().strip()
78 status["created"] = datetime
79 else:
80 warn(f"Missing date for {path}; skipping.")
81 return None
82 identifier_path = next(path.parent.glob("4=*"))
83 identifier = ""
84 if identifier_path:
85 identifier = identifier_path.name[2:]
86 status["@id"] = f"{PUBLIC_URL}/topics/{topic}/{identifier}" if topic else f"{PUBLIC_URL}/{datetime[0:7]}/{identifier}"
87 with identifier_path.open("r", encoding="utf-8") as text:
88 status["identifier"] = text.read().strip()
89 else:
90 warn(f"Missing identifier for {path}; skipping.")
91 return None
92 with path.open("r", encoding="utf-8") as text:
93 status["content"] = statusxml(text.read().strip())
94 return (datetime, identifier, status)
95
96 # Get status paths.
97 status_paths = []
98 for yearpath in Path(f"{BUILD_DIRECTORY}/").glob("[0-9][0-9][0-9][0-9]"):
99 for monthpath in yearpath.glob("[0-9][0-9]"):
100 for daypath in monthpath.glob("[0-9][0-9]"):
101 for statuspath in daypath.glob("*/text"):
102 status_paths.append((None, statuspath))
103 for topicpath in Path(f"{BUILD_DIRECTORY}/").glob("topic/*"):
104 for hash0path in topicpath.glob("[0-9a-f]"):
105 for hash1path in hash0path.glob("[0-9a-f]"):
106 for hash2path in hash1path.glob("[0-9a-f]"):
107 for hash3path in hash2path.glob("[0-9a-f]"):
108 for statuspath in hash3path.glob("*/text"):
109 status_paths.append((topicpath.name, statuspath))
110
111 # Build status objects and listings.
112 for (datetime, identifier, status) in sorted(filter(None, starmap(statusmap, status_paths))):
113 if "subject" in status:
114 topic = status["subject"]
115 if topic not in topics:
116 topics[topic] = { "@context": { "@language": LANG, "activity": "https://www.w3.org/ns/activitystreams#", "dct": "http://purl.org/dc/terms/", "foaf": "http://xmlns.com/foaf/0.1/", "sioc": "http://rdfs.org/sioc/ns#", "sioct": "http://rdfs.org/sioc/types#", "OrderedCollection": "activity:OrderedCollection", "Thread": "sioc:Thread", "MicroblogPost": "sioct:MicroblogPost", "items": { "@id": "activity:items", "@type": "@id", "@container": "@list" }, "created": { "@id": "dct:created", "@type": "http://www.w3.org/2001/XMLSchema#dateTime" }, "creator": { "@id": "dct:creator", "@type": "@id" }, "identifier": { "@id": "dct:identifier", "@type": "http://www.w3.org/2001/XMLSchema#anyURI" }, "subject": "dct:subject", "name": "foaf:name", "content": { "@id": "sioc:content", "@type": "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral" } }, "@id": f"{PUBLIC_URL}/topics/{topic}", "@type": ["OrderedCollection", "Thread"], "items": [], "subject": topic }
117 topics[topic]["items"].append(status)
118 else:
119 yyyy_mm = datetime[0:7]
120 if yyyy_mm not in months:
121 months[yyyy_mm] = { "@context": { "@language": LANG, "activity": "https://www.w3.org/ns/activitystreams#", "dct": "http://purl.org/dc/terms/", "foaf": "http://xmlns.com/foaf/0.1/", "sioc": "http://rdfs.org/sioc/ns#", "sioct": "http://rdfs.org/sioc/types#", "OrderedCollectionPage": "activity:OrderedCollectionPage", "Thread": "sioc:Thread", "MicroblogPost": "sioct:MicroblogPost", "current": { "@id": "activity:current", "@type": "@id" }, "first": { "@id": "activity:first", "@type": "@id" }, "items": { "@id": "activity:items", "@type": "@id", "@container": "@list" }, "partOf": { "@id": "activity:partOf", "@type": "@id" }, "prev": { "@id": "activity:prev", "@type": "@id" }, "next": { "@id": "activity:next", "@type": "@id" }, "created": { "@id": "dct:created", "@type": "http://www.w3.org/2001/XMLSchema#dateTime" }, "creator": { "@id": "dct:creator", "@type": "@id" }, "identifier": { "@id": "dct:identifier", "@type": "http://www.w3.org/2001/XMLSchema#anyURI" }, "name": "foaf:name", "content": { "@id": "sioc:content", "@type": "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral" } }, "@id": f"{PUBLIC_URL}/{yyyy_mm}", "@type": ["OrderedCollectionPage", "Thread"], "items": [], "partOf": f"{PUBLIC_URL}/statuses" }
122 months[yyyy_mm]["items"].append(status)
123
124 # Set up the public directory.
125 if exists(PUBLIC_DIRECTORY):
126 rmtree(PUBLIC_DIRECTORY)
127 mkdir(PUBLIC_DIRECTORY)
128
129 # Copy H·T·M·L files to their expected locations.
130 copy2(f"{BUILD_DIRECTORY}/index.html", f"{PUBLIC_DIRECTORY}/index.html")
131 copy2(f"{BUILD_DIRECTORY}/about.html", f"{PUBLIC_DIRECTORY}/.about.html")
132 copy2(f"{BUILD_DIRECTORY}/status.html", f"{PUBLIC_DIRECTORY}/.status.html")
133 copy2(f"{BUILD_DIRECTORY}/statuses.html", f"{PUBLIC_DIRECTORY}/.statuses.html")
134 copy2(f"{BUILD_DIRECTORY}/topic.html", f"{PUBLIC_DIRECTORY}/.topic.html")
135 copy2(f"{BUILD_DIRECTORY}/topics.html", f"{PUBLIC_DIRECTORY}/.topics.html")
136
137 # Output “about” metadata
138 if not exists(f"{PUBLIC_DIRECTORY}/about"):
139 mkdir(f"{PUBLIC_DIRECTORY}/about")
140 with open(f"{PUBLIC_DIRECTORY}/about/index.jsonld", "w", encoding="utf-8") as f:
141 json.dump({ "@context": { "@language": LANG, "activity": "https://www.w3.org/ns/activitystreams#", "sioc": "http://rdfs.org/sioc/ns#", "sioct": "http://rdfs.org/sioc/types#", "Forum": "sioc:Forum", "Thread": "sioc:Thread", "Microblog": "sioct:Microblog", "streams": { "@id": "activity:streams", "@type": "@id" } }, "@id": f"{PUBLIC_URL}", "@type": "Microblog", "streams": [{ "@id": f"{PUBLIC_URL}/statuses", "@type": "Thread" }, { "@id": f"{PUBLIC_URL}/topics", "@type": "Forum" }] }, f, ensure_ascii=False, allow_nan=False)
142
143 # Output month‐based listings and the non‐topic index
144 statuspairs = list(enumerate(months.items()))
145 for (index, (yyyy_mm, ld)) in statuspairs:
146 if not exists(f"{PUBLIC_DIRECTORY}/{yyyy_mm}"):
147 mkdir(f"{PUBLIC_DIRECTORY}/{yyyy_mm}")
148 ld["first"] = f"{PUBLIC_URL}/{statuspairs[0][1][0]}"
149 ld["current"] = f"{PUBLIC_URL}/{statuspairs[-1][1][0]}"
150 if index > 0:
151 ld["prev"] = f"{PUBLIC_URL}/{statuspairs[index - 1][1][0]}"
152 if index < len(statuspairs) - 1:
153 ld["next"] = f"{PUBLIC_URL}/{statuspairs[index + 1][1][0]}"
154 with open(f"{PUBLIC_DIRECTORY}/{yyyy_mm}/index.jsonld", "w", encoding="utf-8") as f:
155 json.dump(ld, f, ensure_ascii=False, allow_nan=False)
156 if not exists(f"{PUBLIC_DIRECTORY}/statuses"):
157 mkdir(f"{PUBLIC_DIRECTORY}/statuses")
158 with open(f"{PUBLIC_DIRECTORY}/statuses/index.jsonld", "w", encoding="utf-8") as f:
159 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}/{statuspairs[0][1][0]}", "current": f"{PUBLIC_URL}/{statuspairs[-1][1][0]}", "has_parent": f"{PUBLIC_URL}" }, f, ensure_ascii=False, allow_nan=False)
160
161 # Output topic‐based listings and the topic index
162 if not exists(f"{PUBLIC_DIRECTORY}/topics"):
163 mkdir(f"{PUBLIC_DIRECTORY}/topics")
164 for (topic, ld) in topics.items():
165 if not exists(f"{PUBLIC_DIRECTORY}/topics/{topic}"):
166 mkdir(f"{PUBLIC_DIRECTORY}/topics/{topic}")
167 with open(f"{PUBLIC_DIRECTORY}/topics/{topic}/index.jsonld", "w", encoding="utf-8") as f:
168 json.dump(ld, f, ensure_ascii=False, allow_nan=False)
169 with open(f"{PUBLIC_DIRECTORY}/topics/index.jsonld", "w", encoding="utf-8") as f:
170 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)
171
172 # Remove the build directory.
173 rmtree(BUILD_DIRECTORY)
This page took 0.087883 seconds and 5 git commands to generate.