-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.py
executable file
·400 lines (337 loc) · 14.7 KB
/
build.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env python3
from pybars import Compiler
from bs4 import BeautifulSoup, formatter
from glob import glob
import os
import re
import urllib.request
import zipfile
from pylode.profiles.vocpub import VocPub
import pdfkit
from rdflib import Graph
import logging
from playwright.sync_api import sync_playwright
# Configure root logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logging.basicConfig(format='%(levelname)-8s [%(filename)s:%(lineno)d] %(message)s', level=logging.DEBUG)
def copy_ontologies():
"""Copy ontologies to web path."""
map = {
"modules": "ontology",
"demo": "demo"
}
for type in ["modules", "demo"]:
latest = {}
# publish ontologies
for source in sorted(glob(f"ontology/{type}/*/*/*", recursive=True)):
if not source.endswith(".ttl"):
continue
parts = re.match(f"ontology/{type}/([^/]*)/([^/]*)", source)
name = parts.group(1)
version = parts.group(2)
target = f"docs/{map[type]}/{name}/{version}/"
os.makedirs(target, exist_ok=True)
logging.debug(f"Source:\t{source}")
logging.debug(f"Name:\t{name}")
logging.debug(f"Target:\t{target}")
g = Graph()
g.parse(source)
g.serialize(destination=f"{target}{name}.ttl", format="turtle")
g.serialize(destination=f"{target}{name}.rdf", format="xml")
g.serialize(destination=f"{target}{name}.owl", format="xml")
g.serialize(destination=f"{target}{name}.jsonld", format="json-ld")
if latest.get(name, "0") < version:
latest[name] = version
# add latest
for name, version in latest.items():
os.makedirs(f"docs/{map[type]}/{name}/latest/", exist_ok=True)
os.system(f"cp docs/{map[type]}/{name}/{version}/* docs/{map[type]}/{name}/latest/")
def download_owl2vowl():
"""Download and extract OWL2VOWL."""
os.system("mkdir -p temp")
path_to_jar = "temp/owl2vowl.jar"
if not os.path.isfile(path_to_jar):
path_to_zip = "temp/owl2vowl_0.3.7.zip"
if not os.path.isfile(path_to_zip):
url = "https://github.com/VisualDataWeb/OWL2VOWL/archive/refs/tags/0.3.7.zip"
urllib.request.urlretrieve(url, path_to_zip)
with zipfile.ZipFile(path_to_zip, 'r') as zip_ref:
zip_ref.extractall("temp/")
def build_pdf():
"""Convert docs to PDF using wkhtmltopdf."""
try:
html_formatter = formatter.HTMLFormatter(indent=4)
map = {
"modules": "ontology",
"demo": "demo"
}
for type in ["modules", "demo"]:
for source in sorted(glob(f"ontology/{type}/*/*/*", recursive=True)):
if not source.endswith(".ttl"):
continue
logger.info(f"Generating PDF docs for {source}")
parts = re.match(f"ontology/{type}/([^/]*)/([^/]*)", source)
name = parts.group(1)
version = parts.group(2)
target = f"docs/{map[type]}/{name}/{version}/"
html_file = f"{target}/index.html"
pdf_file = f"{target}/{name}.pdf"
# Drop TOC, logo and iframe before generating PDF
with open(html_file, encoding="utf-8") as f:
soup = BeautifulSoup(f, "html.parser")
style = soup.new_tag('style', type='text/css')
style.append("""
/* hack to avoid lonely heading (most of the time at least) */
h2 {
page-break-inside: avoid;
}
.section h2::after {
content: "";
display: block;
height: 300px;
margin-bottom: -300px;
}
.entity {
page-break-inside: avoid;
}
p, dt, dd, ul {
margin-top: 10px;
margin-bottom: 10px;
padding-top: 0;
padding-bottom: 0;
}
""")
soup.head.append(style)
for table in soup.select("table"):
table.insert(0, soup.new_tag("thead"))
table.wrap(soup.new_tag("tbody")).wrap(soup.new_tag("table"))
table.unwrap()
for id in ["toc", "pylode", "overview"]:
tag = soup.find(id=id)
if tag != None:
tag.decompose()
pdfkit.from_string(soup.prettify(formatter=html_formatter), pdf_file, options = { "enable-local-file-access": "" })
except Exception as e:
print(e)
print("PDF conversion cancelled. Is 'wkhtmltopdf' (https://wkhtmltopdf.org/) installed?")
def generate_vowl():
"""Generate VOWL specifications."""
map = {
"modules": "ontology",
"demo": "demo"
}
for type in ["modules", "demo"]:
for source in sorted(glob(f"ontology/{type}/*/*/*", recursive=True)):
if not source.endswith(".ttl"):
continue
logger.info(f"Generating VOWL for: {source}")
parts = re.match(f"ontology/{type}/([^/]*)/([^/]*)", source)
name = parts.group(1)
version = parts.group(2)
target = f"docs/webvowl/data/{map[type]}/{name}/{version}/"
os.makedirs(target, exist_ok=True)
logging.debug(f"Source:\t{source}")
logging.debug(f"Name:\t{name}")
logging.debug(f"Target:\t{target}")
logging.debug(f"Output:\t{target}{name}.json")
# Note: The flag "add-opens" below fixes an issue with
# "InaccessibleObjectException" in later versions of Java
os.system(f"""
java -Dlog4j.configurationFile=log4j2.xml \
--add-opens java.base/java.lang=ALL-UNNAMED \
-jar temp/owl2vowl.jar \
-file {source} \
-output {target}{name}.json > /dev/null
""")
def create_documentation():
"""Generate LODE documentation and instert VOWL visualization."""
map = {
"modules": "ontology",
"demo": "demo"
}
for type in ["modules", "demo"]:
for source in sorted(glob(f"ontology/{type}/*/*/*", recursive=True)):
if not source.endswith(".ttl"):
continue
logger.debug(f"Generating docs for {source}")
parts = re.match(f"ontology/{type}/([^/]*)/([^/]*)", source)
name = parts.group(1)
version = parts.group(2)
target = f"docs/{map[type]}/{name}/{version}/"
os.makedirs(target, exist_ok=True)
html_file = f"{target}/index.html"
od = VocPub(ontology=source)
od.make_html(destination=html_file)
# relative path to webvowl and vowl file
path_to_webvowl = f"../../../webvowl/index.html#{map[type]}/{name}/{version}/{name}"
# # Insert overview section into documentation with WebVOWL in an iframe
with open(html_file, encoding="utf-8") as f:
soup = BeautifulSoup(f, "html.parser")
overview = BeautifulSoup(
f"""
<style>
#iframe-overview {{
width: 100%;
height: 600px;
}}
.caption {{
display: flex;
justify-content: space-between;
}}
.caption a {{
text-decoration: none;
color: black;
font-size: 2em;
}}
</style>
<div id="overview" class="section">
<h2>Overview</h2>
<div class="figure">
<iframe id="iframe-overview" src="{path_to_webvowl}">
</iframe>
<div class="caption">
<div>
<strong>Figure 1:</strong> Ontology overview.
</div>
<div>
<a title="Show fullscreen" href="{path_to_webvowl}">⛶</a>
</div>
</div>
</div>
</section>
""", "html.parser")
tag = soup.find(id='metadata')
tag.insert_after(overview)
html_formatter = formatter.HTMLFormatter(indent=4)
with open(html_file, "w") as f:
f.write(soup.prettify(formatter=html_formatter))
def create_index_file():
print("Generating index file")
compiler = Compiler()
template_file = "index.hbs"
index_file = "docs/index.html"
core = ["actor", "actorODP", "cvn",
"material", "process", "processODP",
"product", "resourceODP", "value",
"energy", "quantity", "statement", "location"]
data = {
"core": [],
"other": [],
"demo": []
}
for type in ["modules", "demo"]:
ontologies = {}
for source in glob(f"ontology/{type}/*/*/*", recursive=True):
if not source.endswith(".ttl"):
continue
parts = re.match(f"ontology/{type}/([^/]*)/([^/]*)", source)
name = parts.group(1)
version = parts.group(2)
if not ontologies.get(name):
ontologies[name] = {
"name": name,
"versions": []
}
ontologies[name]["versions"].append(version)
ontologies[name]["versions"].sort(reverse=True)
# Split into core, other and demo
for name in ontologies:
ontology = {
"name": name,
"versions": ontologies[name]["versions"]
}
if type == "demo":
data["demo"].append(ontology)
else:
if name in core:
data["core"].append(ontology)
else:
data["other"].append(ontology)
for list in data.values():
list.sort(key=lambda x: x["name"])
# sort by name ascending, version descending
with open(template_file, "r") as f:
template = compiler.compile(f.read())
with open(index_file, "w") as f:
f.write(template({"data": data}))
def html_to_pdf_with_playwright(html_file, pdf_file):
"""Convert HTML file to a PDF file using Playwright."""
try:
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
# Load the HTML file
page.goto(f"file://{os.path.abspath(html_file)}")
# Generate PDF
page.pdf(path=pdf_file, format="A4", margin={"top": "10mm", "bottom": "10mm", "left": "10mm", "right": "10mm"}, scale=0.7)
browser.close()
except Exception as e:
logger.error(f"Failed to generate PDF for {html_file}: {e}")
def build_pdf_playwright():
"""Convert docs to PDF using Playwright."""
try:
html_formatter = formatter.HTMLFormatter(indent=4)
map = {
"modules": "ontology",
"demo": "demo"
}
for type in ["modules", "demo"]:
for source in sorted(glob(f"ontology/{type}/*/*/*", recursive=True)):
if not source.endswith(".ttl"):
continue
logger.info(f"Generating PDF docs for {source}")
parts = re.match(f"ontology/{type}/([^/]*)/([^/]*)", source)
name = parts.group(1)
version = parts.group(2)
target = f"docs/{map[type]}/{name}/{version}/"
html_file = f"{target}/index.html"
pdf_file = f"{target}/{name}.pdf"
# Drop TOC, logo, and iframe before generating PDF
with open(html_file, encoding="utf-8") as f:
soup = BeautifulSoup(f, "html.parser")
style = soup.new_tag('style', type='text/css')
style.append("""
/* hack to avoid lonely heading (most of the time at least) */
h2 {
page-break-inside: avoid;
}
.section h2::after {
content: "";
display: block;
height: 300px;
margin-bottom: -300px;
}
.entity {
page-break-inside: avoid;
}
p, dt, dd, ul {
margin-top: 10px;
margin-bottom: 10px;
padding-top: 0;
padding-bottom: 0;
}
""")
soup.head.append(style)
for id in ["toc", "pylode", "overview"]:
tag = soup.find(id=id)
if tag is not None:
tag.decompose()
# Write modified HTML back to the file (if necessary)
with open(html_file, "w", encoding="utf-8") as f:
f.write(soup.prettify(formatter=html_formatter))
# Generate PDF using Playwright
html_to_pdf_with_playwright(html_file, pdf_file)
except Exception as e:
logger.error(e)
logger.error("PDF conversion cancelled. Ensure Playwright is installed and configured properly.")
def main():
download_owl2vowl()
generate_vowl()
create_documentation()
#build_pdf()
build_pdf_playwright()
create_index_file()
copy_ontologies()
if __name__ == "__main__":
main()