-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_web.py
More file actions
119 lines (96 loc) · 3.44 KB
/
update_web.py
File metadata and controls
119 lines (96 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import json
import os
import shutil
from typing import Self
class IndexItem:
name: str
path: str
def __init__(self, name, path):
self.name = name
self.path = path
def to_dict(self) -> dict:
return {
"type": "item",
"name": self.name,
"path": self.path,
}
class IndexGroup:
name: str
children: list[IndexItem | Self]
def __init__(self, name, children):
self.name = name
self.children = children
def to_dict(self) -> dict:
return {
"type": "group",
"name": self.name,
"children": list(map(lambda c: c.to_dict(), self.children)),
}
def index_registry() -> IndexGroup:
children = find_children("./registry")
return IndexGroup("registry", children)
def find_children(registry, path="") -> list[IndexItem | IndexGroup]:
children = []
with os.scandir(os.path.join(registry, path)) as it:
for entry in it:
if entry.is_file() and entry.name.endswith(".ttl"):
name = os.path.splitext(entry.name)[0]
children.append(IndexItem(name, path + name))
elif entry.is_dir():
entry_children = find_children(registry, path + entry.name + "/")
children.append(IndexGroup(entry.name, entry_children))
return children
def create_registry_files(base: str, registry: list[IndexItem | IndexGroup]):
for item in registry:
if isinstance(item, IndexItem):
create_registry_item_files(base, item)
else:
print(f"group {item.name}")
os.makedirs(os.path.join(base, item.name))
create_registry_files(os.path.join(base, item.name), item.children)
def create_registry_item_files(path, item: IndexItem):
srcpath = os.path.join("registry", item.path) + ".ttl"
dstpath = os.path.join(path, item.name) + ".ttl"
dstdir = path # os.path.dirname(dstpath)
# copy turtle file
shutil.copyfile(srcpath, dstpath)
# create web page
querypath = os.path.relpath(os.path.splitext(dstpath)[0], "./web")
webname = item.name + ".md"
with open(os.path.join(dstdir, webname), "w") as f:
f.write(
"---\n"
"layout: page\n"
"css:\n"
" - query.css\n"
"js:\n"
" - rdflib.min.js\n"
" - turtle.js\n"
" - query.js\n"
"preload:\n"
f" - {item.name}.ttl\n"
f"permalink: {querypath}.html\n"
"---\n"
)
f.write(f'{{% include query.html query="{item.path}" %}}\n')
def filter_turtle(base, items: list[str]) -> list[str]:
def isdir(item):
return os.path.isdir(os.path.join(base, item))
def isturtle(item):
filepath = os.path.join(base, item)
return os.path.isfile(filepath) and item.endswith(".ttl")
return list(filter(lambda item: not (isdir(item) or isturtle(item)), items))
if __name__ == "__main__":
# copy docs
shutil.copytree('./docs', './web/docs')
# generate registry index
index = index_registry()
os.makedirs("./web/_data", exist_ok=True)
with open("./web/_data/registry.json", "w") as f:
f.write(json.dumps([index.to_dict()]))
# clean up the registry directory in the website, in case it already exists
try:
shutil.rmtree("./web/registry")
except:
pass
create_registry_files("./web", [index])