/
rdftoweb.py
370 lines (268 loc) · 9.77 KB
/
rdftoweb.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
""" rdftoweb.py
author: Bryce Mecum (mecum@nceas.ucsb.edu)
Cnverts an RDF graph (in Turtle format) into a set of static HTML pags.
"""
import sys
import os
import RDF
import shutil # for copying the styles.css file
from urllib.parse import quote_plus, unquote_plus
# Global variable to hold namespaces
NS = {}
def getConcept(uri):
"""
Return the concept a URI is for, based upon string matching.
"""
if uri.startswith("http://lod.dataone.org/dataset"):
return 'dataset'
elif uri.startswith("http://lod.dataone.org/person"):
return 'person'
elif uri.startswith("http://lod.dataone.org/organization"):
return 'organization'
else:
return 'blank'
def isInternal(pages, uri):
"""
Checks if a URI is internal or external.
A URI is internal if that URI is the subject
of any triples in the graph.
Otherwise it is external.
"""
for concept in pages:
if uri in pages[concept]:
return True
return False
def getLinkFor(uri):
"""
Create a link for a given URI.
This helps us create links to files that need to be escaped.
The filename will be singly-quoted and the link will need to be
doubly-quoted.
"""
concept = getConcept(uri)
filename = getFilename(uri)
link = "/%s/%s.html" % (concept, quote_plus(filename))
return link
def getFilename(uri):
"""
Get the name on disk for a URI.
"""
filename = None
if uri.startswith("https://cn.dataone.org/cn/v1/resolve/"):
filename = uri.replace("https://cn.dataone.org/cn/v1/resolve/", "")
elif uri.startswith("https://dataone.org/person/"):
filename = uri.replace("https://dataone.org/person/", "")
elif uri.startswith("https://dataone.org/organization"):
filename = uri.replace("https://dataone.org/organization/", "")
else:
filename = uri
filename = quote_plus(filename)
return filename
def pageHTML(content='', concepts=None):
"""
Creates HTML for a web page and inserts the HTML from content into it.
"""
html_string = """<html>
<head>
<link rel="stylesheet" href="/styles.css">
</head>
<body>
<h1>RDF to Web Output</h1>
"""
if concepts is not None:
html_string += """<h2>Concepts</h2>
<ul class='nav'>"""
for concept in concepts:
html_string += """
<li><a href='/%s'>%s</a></li>""" % (concept, concept.capitalize())
html_string += """
</ul>"""
html_string += content
html_string += """
</body>
</html>"""
return html_string
def createIndex(base_dir, pages):
"""
Create an index HTML for all concepts. The index.html file will located in
the base directory.
"""
page_html = pageHTML(concepts=pages.keys())
with open("%s/index.html" % base_dir, 'w') as f:
f.write(page_html)
def createConceptIndex(base_dir, pages, concept):
"""
Create an index HTML page for a concept.
"""
unique_uris = []
for page in pages[concept]:
if page not in unique_uris:
unique_uris.append(page)
html_string = """
<h2>%s</h2>""" % concept.capitalize()
html_string += """
<ul>"""
if not os.path.exists(base_dir + '/' + concept):
os.mkdir(base_dir + '/' + concept)
for uri in unique_uris:
html_string += """
<li><a href='%s'>%s</a></li>""" % (getLinkFor(uri), uri)
html_string += """
</ul>"""
page_html = pageHTML(html_string, concepts=pages.keys())
with open("%s/%s/index.html" % (base_dir, concept), "w") as f:
f.write(page_html)
def createPages(base_dir, pages):
"""
Create HTML pages for a set of pages.
"""
for concept in pages:
concept_folder_path = base_dir + '/' + concept
if not os.path.exists(concept_folder_path):
os.mkdir(concept_folder_path)
# Create indicies
createIndex(base_dir, pages)
createConceptIndex(base_dir, pages, concept)
# Create pages
for page in pages[concept]:
content = contentHTML(pages, concept, page)
page_html = pageHTML(content, concepts=pages.keys())
concept = getConcept(page)
filename = getFilename(page)
with open(concept_folder_path + '/' + filename + ".html", "w") as f:
f.write(page_html)
def contentHTML(pages, concept, page):
"""
Makes the content HTML (what changes across pages) for a page..
"""
title_html = """<h2><a href='%s'>%s</a></h2>
""" % (getLinkFor(page), substitutePrefix(unquote_plus(page)))
html_string = """%s
<table>
<thead>
<tr>
<th>Predicate</th>
<th>Object</th>
</tr>
</thead>
<tbody>""" % title_html
for statement in pages[concept][page]:
# Do something different depending on what the object is
# If blank: Inline the node
# If URI:
# - If it links to an internal resource, show that
# - If it links to an exteranl resource, show that
# If literal: Just show it
object_string = statement['o']
# Check for blank first
if 'blank' in pages and object_string in pages['blank']:
object_content = blankNodeHTML(object_string, pages)
# Internal
elif isInternal(pages, object_string):
object_content = "<a class='internal' href='%s'>%s</a>" % (getLinkFor(object_string), substitutePrefix(object_string))
# External or literal
else:
if object_string.startswith("http"):
object_uri = unquote_plus(object_string)
object_content = "<a class='external' href='%s'>%s</a>" % (object_uri, substitutePrefix(object_uri))
else:
object_content = object_string
predicate_ele = substitutePrefix(statement['p'])
html_string += """
<tr>
<td>%s</td>
<td>%s</td>
</tr>""" % (predicate_ele, object_content)
html_string += """
</tbody>
</table>"""
return html_string
def blankNodeHTML(blank_node, pages):
"""Generates an HTML table for a blank node for inlining inside a result
table for another resource."""
if 'blank' not in pages:
raise Exception("blankNodeHTML was called but no blank nodes were found.")
if blank_node not in pages['blank']:
raise Exception("blankNodeHTML was called for a blank node that doesn't exist.")
blank_node_content = pages['blank'][blank_node]
if len(blank_node_content) <= 0:
raise Exception("Blank node was found but had on statements associated with it.")
html_string = """<table>
<thead>
<tr>
<th>Predicate</th>
<th>Object</th>
</tr>
</thead>
<tbody>"""
for statement in blank_node_content:
predicate_content = statement['p']
# Prefix the predicate if we can
predicate_content = substitutePrefix(predicate_content)
object_string = statement['o']
# Blank
if object_string in pages['blank']:
object_content = blankNodeHTML(object_string, pages)
# Internal
elif isInternal(pages, object_string):
object_content = "<a class='internal' href='%s'>%s</a>" % (getLinkFor(object_string), substitutePrefix(object_string))
# External or literal
else:
if object_string.startswith("http"):
object_uri = unquote_plus(object_string)
object_content = "<a class='external' href='%s'>%s</a>" % (object_uri, substitutePrefix(object_uri))
else:
object_content = object_string
html_string += """<tr>
<td>
%s
</td>
<td>
%s
</td>
</tr>""" % (predicate_content, object_content)
html_string += """</tbody>
</table>"""
return html_string
def substitutePrefix(term):
"""Attempt to prefix the term with its namespace string instead of the
fully-specified URI.
Returns either the prefix'd term string (foo:Bar) or the original string if
the term did not start with a URI in an known namespace."""
for prefix in NS:
if term.startswith(NS[prefix]):
term = term.replace(NS[prefix], prefix + ":")
return term
def main():
base_vocab = "http://lod.dataone.org/"
base_dir = "output"
pages = { }
parser = RDF.TurtleParser()
model = RDF.Model()
# Figure out filename
if len(sys.argv) != 2:
print("You must specify a filename and nothing else to rdftoweb.py.")
print(" e.g. python rdftwoweb.py mydataset.ttl")
parser.parse_into_model(model, "file:" + sys.argv[1])
namespaces = parser.namespaces_seen()
for namespace in namespaces:
NS[namespace] = str(namespaces[namespace])
for statement in model:
page = None
subject = unquote_plus(str(statement.subject))
concept = getConcept(subject)
if concept not in pages:
pages[concept] = {}
if subject not in pages[concept]:
pages[concept][subject] = []
predicate_string = str(statement.predicate)
predicate_string = predicate_string.replace(base_vocab, 'glbase:')
pages[concept][subject].append({'p': predicate_string, 'o': str(statement.object) })
# Create base dir
if not os.path.exists(base_dir):
os.mkdir(base_dir)
createPages(base_dir, pages)
# Copy in the base stylehseet
shutil.copyfile('styles.css', 'output/styles.css')
if __name__ == "__main__":
main()