]>
Lady’s Gitweb - x_status_git/blob - post-receive
3 from itertools
import starmap
6 from os
.path
import exists
7 from pathlib
import Path
9 from shutil
import copy2
, rmtree
10 from subprocess
import run
12 from warnings
import warn
13 from xml
.dom
import XHTML_NAMESPACE
14 from xml
.dom
.minidom
import getDOMImplementation
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"
23 if stdin
.read().split()[-1] == f
"refs/heads/{LIVE_BRANCH}":
25 print(f
"This is an update to the '{LIVE_BRANCH}' branch; regenerating site…")
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")
32 # Set up various containers.
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
):
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]))
54 paraElt
.appendChild(doc
.createTextNode(component
))
55 return articleElt
.toxml()
57 # Map status paths to status objects, or None if there is an error.
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.")
67 status
["subject"] = topic
68 author_path
= next(path
.parent
.glob("1=*"))
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 title_path
= next(path
.parent
.glob("2=*"))
75 with title_path
.open("r", encoding
="utf-8") as text
:
76 title
= text
.read().strip()
77 status
["title"] = title
78 date_path
= next(path
.parent
.glob("3=*"))
81 with date_path
.open("r", encoding
="utf-8") as text
:
82 datetime
= text
.read().strip()
83 status
["created"] = datetime
85 warn(f
"Missing date for {path}; skipping.")
87 identifier_path
= next(path
.parent
.glob("4=*"))
90 identifier
= identifier_path
.name
[2:]
91 status
["@id"] = f
"{PUBLIC_URL}/topics/{topic}/{identifier}" if topic
else f
"{PUBLIC_URL}/{datetime[0:7]}/{identifier}"
92 with identifier_path
.open("r", encoding
="utf-8") as text
:
93 status
["identifier"] = text
.read().strip()
95 warn(f
"Missing identifier for {path}; skipping.")
97 with path
.open("r", encoding
="utf-8") as text
:
98 status
["content"] = statusxml(text
.read().strip())
99 return (datetime
, identifier
, status
)
103 for yearpath
in Path(f
"{BUILD_DIRECTORY}/").glob("[0-9][0-9][0-9][0-9]"):
104 for monthpath
in yearpath
.glob("[0-9][0-9]"):
105 for daypath
in monthpath
.glob("[0-9][0-9]"):
106 for statuspath
in daypath
.glob("*/text"):
107 status_paths
.append((None, statuspath
))
108 for topicpath
in Path(f
"{BUILD_DIRECTORY}/").glob("topic/*"):
109 for hash0path
in topicpath
.glob("[0-9a-f]"):
110 for hash1path
in hash0path
.glob("[0-9a-f]"):
111 for hash2path
in hash1path
.glob("[0-9a-f]"):
112 for hash3path
in hash2path
.glob("[0-9a-f]"):
113 for statuspath
in hash3path
.glob("*/text"):
114 status_paths
.append((topicpath
.name
, statuspath
))
116 # Build status objects and listings.
117 for (datetime
, identifier
, status
) in sorted(filter(None, starmap(statusmap
, status_paths
))):
118 if "subject" in status
:
119 topic
= status
["subject"]
120 if topic
not in topics
:
121 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
}
122 topics
[topic
]["items"].append(status
)
124 yyyy_mm
= datetime
[0:7]
125 if yyyy_mm
not in months
:
126 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" }
127 months
[yyyy_mm
]["items"].append(status
)
129 # Set up the public directory.
130 if exists(PUBLIC_DIRECTORY
):
131 rmtree(PUBLIC_DIRECTORY
)
132 mkdir(PUBLIC_DIRECTORY
)
134 # Copy H·T·M·L files to their expected locations.
135 copy2(f
"{BUILD_DIRECTORY}/index.html", f
"{PUBLIC_DIRECTORY}/index.html")
136 copy2(f
"{BUILD_DIRECTORY}/about.html", f
"{PUBLIC_DIRECTORY}/.about.html")
137 copy2(f
"{BUILD_DIRECTORY}/status.html", f
"{PUBLIC_DIRECTORY}/.status.html")
138 copy2(f
"{BUILD_DIRECTORY}/statuses.html", f
"{PUBLIC_DIRECTORY}/.statuses.html")
139 copy2(f
"{BUILD_DIRECTORY}/topic.html", f
"{PUBLIC_DIRECTORY}/.topic.html")
140 copy2(f
"{BUILD_DIRECTORY}/topics.html", f
"{PUBLIC_DIRECTORY}/.topics.html")
142 # Output “about” metadata
143 if not exists(f
"{PUBLIC_DIRECTORY}/about"):
144 mkdir(f
"{PUBLIC_DIRECTORY}/about")
145 with
open(f
"{PUBLIC_DIRECTORY}/about/index.jsonld", "w", encoding
="utf-8") as f
:
146 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)
148 # Output month‐based listings and the non‐topic index
149 statuspairs
= list(enumerate(months
.items()))
150 for (index
, (yyyy_mm
, ld
)) in statuspairs
:
151 if not exists(f
"{PUBLIC_DIRECTORY}/{yyyy_mm}"):
152 mkdir(f
"{PUBLIC_DIRECTORY}/{yyyy_mm}")
153 ld
["first"] = f
"{PUBLIC_URL}/{statuspairs[0][1][0]}"
154 ld
["current"] = f
"{PUBLIC_URL}/{statuspairs[-1][1][0]}"
156 ld
["prev"] = f
"{PUBLIC_URL}/{statuspairs[index - 1][1][0]}"
157 if index
< len(statuspairs
) - 1:
158 ld
["next"] = f
"{PUBLIC_URL}/{statuspairs[index + 1][1][0]}"
159 with
open(f
"{PUBLIC_DIRECTORY}/{yyyy_mm}/index.jsonld", "w", encoding
="utf-8") as f
:
160 json
.dump(ld
, f
, ensure_ascii
=False, allow_nan
=False)
161 if not exists(f
"{PUBLIC_DIRECTORY}/statuses"):
162 mkdir(f
"{PUBLIC_DIRECTORY}/statuses")
163 with
open(f
"{PUBLIC_DIRECTORY}/statuses/index.jsonld", "w", encoding
="utf-8") as f
:
164 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)
166 # Output topic‐based listings and the topic index
167 if not exists(f
"{PUBLIC_DIRECTORY}/topics"):
168 mkdir(f
"{PUBLIC_DIRECTORY}/topics")
169 for (topic
, ld
) in topics
.items():
170 if not exists(f
"{PUBLIC_DIRECTORY}/topics/{topic}"):
171 mkdir(f
"{PUBLIC_DIRECTORY}/topics/{topic}")
172 with
open(f
"{PUBLIC_DIRECTORY}/topics/{topic}/index.jsonld", "w", encoding
="utf-8") as f
:
173 json
.dump(ld
, f
, ensure_ascii
=False, allow_nan
=False)
174 with
open(f
"{PUBLIC_DIRECTORY}/topics/index.jsonld", "w", encoding
="utf-8") as f
:
175 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)
177 # Remove the build directory.
178 rmtree(BUILD_DIRECTORY
)
This page took 0.079481 seconds and 5 git commands to generate.