-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·200 lines (160 loc) · 7.1 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
#!/usr/bin/env python3
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
def copy_ontologies():
"""Copy ontologies to web path."""
latest = {}
# publish ontologies
for source in sorted(glob("ontology/*/*/*", recursive=True)):
if not source.endswith(".ttl"):
continue
print(f"Copy ontology {source}")
parts = re.match("ontology/([^/]*)/([^/]*)", source)
name = parts.group(1)
version = float(parts.group(2))
base = f"docs/ontology/{name}/{version}/{name}"
os.makedirs(f"docs/ontology/{name}/{version}/", exist_ok=True)
g = Graph()
g.parse(source)
g.serialize(destination=f"{base}.ttl", format="turtle")
g.serialize(destination=f"{base}.rdf", format="xml")
g.serialize(destination=f"{base}.owl", format="xml")
g.serialize(destination=f"{base}.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/ontology/{name}/latest/", exist_ok=True)
os.system(f"cp docs/ontology/{name}/{version}/* docs/ontology/{name}/latest/")
def copy_website():
"""Copy website to web path."""
os.system(f"cp -r page/* docs/")
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 = "http://vowl.visualdataweb.org/downloads/owl2vowl_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)
for source in sorted(glob("ontology/*/*/*", recursive=True)):
if not source.endswith(".ttl"):
continue
print(f"Generating PDF docs for {source}")
parts = re.match("ontology/([^/]*)/([^/]*)", source)
name = parts.group(1)
version = float(parts.group(2))
html_file = f"docs/ontology/{name}/{version}/index.html"
pdf_file = f"docs/ontology/{name}/{version}/{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 (mot 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."""
for source in sorted(glob("ontology/*/*/*", recursive=True)):
if not source.endswith(".ttl"):
continue
print(f"Generating VOWL for {source}")
parts = re.match("ontology/([^/]*)/([^/]*)", source)
name = parts.group(1)
version = float(parts.group(2))
os.makedirs(f"docs/webvowl/data/ontology/{name}/{version}/", exist_ok=True)
# 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 docs/webvowl/data/ontology/{name}/{version}/{name}.json > /dev/null
""")
def create_documentation():
"""Generate LODE documentation and instert VOWL visualization."""
for source in sorted(glob("ontology/*/*/*", recursive=True)):
if not source.endswith(".ttl"):
continue
print(f"Generating docs for {source}")
parts = re.match("ontology/([^/]*)/([^/]*)", source)
name = parts.group(1)
version = float(parts.group(2))
os.makedirs(f"docs/ontology/{name}/{version}/", exist_ok=True)
html_file = f"docs/ontology/{name}/{version}/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#ontology/{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"""
<div id="overview" class="section">
<h2>Overview</h2>
<div class="figure">
<iframe id="iframe-overview" width="100%" height ="600px" src="{path_to_webvowl}"></iframe>
<div class="caption"><strong>Figure 1:</strong> Ontology overview</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 main():
download_owl2vowl()
generate_vowl()
create_documentation()
build_pdf()
copy_ontologies()
copy_website()
if __name__ == "__main__":
main()