]>
Lady’s Gitweb - x_status_git/blob - post-receive
1b20aa89ef699edbe43aa9708e273f9e188c7d11
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
):
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 date_path
= next(path
.parent
.glob("3=*"))
76 with date_path
.open("r", encoding
="utf-8") as text
:
77 datetime
= text
.read().strip()
78 status
["created"] = datetime
80 warn(f
"Missing date for {path}; skipping.")
82 identifier_path
= next(path
.parent
.glob("4=*"))
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()
90 warn(f
"Missing identifier for {path}; skipping.")
92 with path
.open("r", encoding
="utf-8") as text
:
93 status
["content"] = statusxml(text
.read().strip())
94 return (datetime
, identifier
, status
)
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
))
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#", "OrderedCollection": "activity:OrderedCollection", "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", "items": [], "subject": topic
}
117 topics
[topic
]["items"].append(status
)
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#", "OrderedCollectionPage": "activity:OrderedCollectionPage", "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", "items": [], "partOf": f
"{PUBLIC_URL}/statuses" }
122 months
[yyyy_mm
]["items"].append(status
)
124 # Set up the public directory.
125 if exists(PUBLIC_DIRECTORY
):
126 rmtree(PUBLIC_DIRECTORY
)
127 mkdir(PUBLIC_DIRECTORY
)
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}/status.html", f
"{PUBLIC_DIRECTORY}/.status.html")
132 copy2(f
"{BUILD_DIRECTORY}/statuses.html", f
"{PUBLIC_DIRECTORY}/.statuses.html")
133 copy2(f
"{BUILD_DIRECTORY}/topic.html", f
"{PUBLIC_DIRECTORY}/.topic.html")
134 copy2(f
"{BUILD_DIRECTORY}/topics.html", f
"{PUBLIC_DIRECTORY}/.topics.html")
136 # Output month‐based listings and the non‐topic index
137 statuspairs
= list(enumerate(months
.items()))
138 for (index
, (yyyy_mm
, ld
)) in statuspairs
:
139 if not exists(f
"{PUBLIC_DIRECTORY}/{yyyy_mm}"):
140 mkdir(f
"{PUBLIC_DIRECTORY}/{yyyy_mm}")
141 ld
["first"] = f
"{PUBLIC_URL}/{statuspairs[0][1][0]}"
142 ld
["current"] = f
"{PUBLIC_URL}/{statuspairs[-1][1][0]}"
144 ld
["prev"] = f
"{PUBLIC_URL}/{statuspairs[index - 1][1][0]}"
145 if index
< len(statuspairs
) - 1:
146 ld
["next"] = f
"{PUBLIC_URL}/{statuspairs[index + 1][1][0]}"
147 with
open(f
"{PUBLIC_DIRECTORY}/{yyyy_mm}/index.jsonld", "w", encoding
="utf-8") as f
:
148 json
.dump(ld
, f
, ensure_ascii
=False, allow_nan
=False)
149 if not exists(f
"{PUBLIC_DIRECTORY}/statuses"):
150 mkdir(f
"{PUBLIC_DIRECTORY}/statuses")
151 with
open(f
"{PUBLIC_DIRECTORY}/statuses/index.jsonld", "w", encoding
="utf-8") as f
:
152 json
.dump({ "@context": { "@language": LANG
, "activity": "https://www.w3.org/ns/activitystreams#", "OrderedCollection": "activity:OrderedCollection", "current": { "@id": "activity:current", "@type": "@id" }, "first": { "@id": "activity:first", "@type": "@id" } }, "@id": f
"{PUBLIC_URL}/statuses", "@type": "OrderedCollection", "first": f
"{PUBLIC_URL}/{statuspairs[0][1][0]}", "current": f
"{PUBLIC_URL}/{statuspairs[-1][1][0]}" }, f
, ensure_ascii
=False, allow_nan
=False)
154 # Output topic‐based listings and the topic index
155 if not exists(f
"{PUBLIC_DIRECTORY}/topics"):
156 mkdir(f
"{PUBLIC_DIRECTORY}/topics")
157 for (topic
, ld
) in topics
.items():
158 if not exists(f
"{PUBLIC_DIRECTORY}/topics/{topic}"):
159 mkdir(f
"{PUBLIC_DIRECTORY}/topics/{topic}")
160 with
open(f
"{PUBLIC_DIRECTORY}/topics/{topic}/index.jsonld", "w", encoding
="utf-8") as f
:
161 json
.dump(ld
, f
, ensure_ascii
=False, allow_nan
=False)
162 with
open(f
"{PUBLIC_DIRECTORY}/topics/index.jsonld", "w", encoding
="utf-8") as f
:
163 json
.dump({ "@context": { "@language": LANG
, "activity": "https://www.w3.org/ns/activitystreams#", "dct": "http://purl.org/dc/terms/", "Collection": "activity:Collection", "items": { "@id": "activity:items", "@type": "@id" }, "subject": "dct:subject" }, "@id": f
"{PUBLIC_URL}/topics", "@type": "Collection", "items": list(map(lambda a
: { "@id": a
["@id"], "subject": a
["subject"] }, topics
.values())) }, f
, ensure_ascii
=False, allow_nan
=False)
165 # Remove the build directory.
166 rmtree(BUILD_DIRECTORY
)
This page took 0.060472 seconds and 3 git commands to generate.