/[volute]/trunk/projects/vocabularies/src/code/rdf2html.py
ViewVC logotype

Contents of /trunk/projects/vocabularies/src/code/rdf2html.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 670 - (show annotations)
Thu Jul 3 14:11:12 2008 UTC (12 years, 5 months ago) by alasdair.gray
File MIME type: text/x-python
File size: 6641 byte(s)
Updated rdf2html to cover all skos constructs used in the vocabularies

Added rdf2html into the build system for UCD and removed the place holder file
1 #! /usr/bin/python
2
3 # Given a SKOS .rdf file as argument, this generates a fairly basic HTML
4 # page documenting the vocabulary.
5 #
6 # Usage:
7 #
8 # python rdf2html.py foo.rdf >foo.html
9 #
10 # Requires the redland-bindings for python, available from librdf.org
11
12 import sys
13 import RDF
14 from xml.sax.saxutils import escape
15
16 rdfNS = RDF.NS("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
17 rdfsNS = RDF.NS("http://www.w3.org/2000/01/rdf-schema#")
18 skosNS = RDF.NS("http://www.w3.org/2008/05/skos#")
19 dcNS = RDF.NS("http://purl.org/dc/elements/1.1/")
20
21 if len(sys.argv) == 2:
22 rdfFile = sys.argv[1]
23 else:
24 sys.stderr.write("Usage: "+sys.argv[0]+" foo.rdf\n")
25 sys.exit(1)
26
27 out = sys.stdout
28
29 store = RDF.MemoryStorage()
30 model = RDF.Model(store)
31 modelUri = RDF.Uri(string="file:" + rdfFile)
32 parser = RDF.Parser()
33
34 parser.parse_into_model(model, modelUri)
35
36 schemename = model.get_source(rdfNS.type, skosNS.ConceptScheme)
37 schemenamelen = len(schemename._get_uri().__str__())
38 title = escape(model.get_target(schemename, dcNS.title).__str__())
39
40 out.write("""<html xmlns='http://www.w3.org/1999/xhtml'>
41 <head>
42 <title>%s</title>
43 <style type='text/css'>
44 dt { font: bold; }
45 div.c0 { background: #FDD; }
46 div.c1 { background: #DDF; }
47 </style>
48 </head>
49
50 <body>
51 <h1>%s</h1>
52 <p><strong>Namespace: %s</strong></p>
53
54 <h2>Contents</h2>
55 <ul>
56 <li><a href='#_topconcepts'>Top concepts</a></li>
57 <li><a href='#_concepts'>Concepts</a></li>
58 <li><a href='#_collections'>Collections</a></li>
59 </ul>
60
61 """ % (title, title, schemename._get_uri()))
62
63 # the list of top-level concepts
64 out.write("<h2><a name='_topconcepts'>Top concepts</a></h2>\n<ul>\n")
65 for n in model.get_targets(schemename, skosNS.hasTopConcept):
66 conceptURI = n._get_uri().__str__()[schemenamelen+1:]
67 conceptName = model.get_target(n, skosNS.prefLabel)
68 if conceptName == None:
69 conceptName = conceptURI
70 out.write("<li><a href='#%s'>%s</a></li>\n" % (conceptURI,conceptName))
71 out.write("</ul>\n")
72
73 # The list of concepts
74 out.write("<h2><a name='_concepts'>Concepts</a></h2>\n")
75 evenodd=0
76 for subj in model.get_sources(rdfNS.type, skosNS.Concept):
77 # the schemenamelen+1 means that we omit the #
78 # (needs to be marginally more general to cope with slash namespaces)
79
80 # Display the preferred label as the concept heading if it exists
81 # otherwise display the URI
82 conceptURI = subj._get_uri().__str__()[schemenamelen+1:]
83 prefLabel = model.get_target(subj, skosNS.prefLabel)
84 if prefLabel == None:
85 conceptName = model.get_target(subj, skosNS.prefLabel)
86 else:
87 conceptName = prefLabel
88 out.write("<div class='c%d'><h3><a name='%s'>Concept: %s</a></h3>\n<dl>\n"
89 % (evenodd, conceptURI, conceptName))
90
91 # Set the background colour for the next concept
92 evenodd = (evenodd+1)%2
93
94 # Display preferred label if it exists
95 if prefLabel != None:
96 out.write("<dt>preferred label:</dt>\n<dd>%s</dd>\n" % prefLabel)
97
98 # Display taxonomical number if it exists
99 notation = model.get_target(subj, skosNS.notation)
100 if notation != None:
101 out.write("<dt>notation:</dt>\n<dd>%s</dd>\n" % notation)
102
103 # display the alternative labels if any exist
104 labelled = 0
105 altLabels = model.get_targets(subj, skosNS.altLabel)
106 for n in altLabels:
107 if not labelled:
108 out.write("<dt>alternative labels:</dt>\n")
109 labelled = 1
110 out.write("<dd>%s</dd> \n" % (n))
111
112 # Display definition if it exists
113 definition = model.get_target(subj, skosNS.definition)
114 if definition != None:
115 out.write("<dt>definition:</dt>\n<dd>%s</dd>\n" % definition)
116
117 # Display scope note if it exists
118 scopeNote = model.get_target(subj, skosNS.scopeNote)
119 if scopeNote != None:
120 out.write("<dt>scope note:</dt>\n<dd>%s</dd>\n" % scopeNote)
121
122 # Display broader terms
123 labelled = 0
124 broaderConcepts = model.get_targets(subj, skosNS.broader)
125 for n in broaderConcepts:
126 if not labelled:
127 out.write("<dt>broader terms:</dt>\n")
128 labelled = 1
129 nstr = n._get_uri().__str__()[schemenamelen+1:]
130 nLabel = model.get_target(n, skosNS.prefLabel)
131 if nLabel == None:
132 nLabel = nstr
133 out.write("<dd><a href='#%s'>%s</a></dd>\n" % (nstr,nLabel))
134
135 # Display narrower terms
136 labelled = 0
137 narrowerConcepts = model.get_targets(subj, skosNS.narrower)
138 for n in narrowerConcepts:
139 if not labelled:
140 out.write("<dt>narrower terms:</dt>\n")
141 labelled = 1
142 nstr = n._get_uri().__str__()[schemenamelen+1:]
143 nLabel = model.get_target(n, skosNS.prefLabel)
144 if nLabel == None:
145 nLabel = nstr
146 out.write("<dd><a href='#%s'>%s</a></dd>\n" % (nstr,nLabel))
147
148 # Display related terms
149 labelled = 0
150 relatedConcepts = model.get_targets(subj, skosNS.related)
151 for n in relatedConcepts:
152 if not labelled:
153 out.write("<dt>related terms:</dt>\n")
154 labelled = 1
155 nstr = n._get_uri().__str__()[schemenamelen+1:]
156 nLabel = model.get_target(n, skosNS.prefLabel)
157 if nLabel == None:
158 nLabel = nstr
159 out.write("<dd><a href='#%s'>%s</a></dd>\n" % (nstr,nLabel))
160
161 # Reached the end of the SKOS concept
162 out.write("</dl>\n</div>\n\n")
163
164 # The list of collections
165 out.write("<h2><a name='_collections'>Collections</a></h2>\n")
166 for subj in model.get_sources(rdfNS.type, skosNS.Collection):
167 if subj.is_resource():
168 collectionURI = subj._get_uri().__str__()[schemenamelen+1:]
169 collectionName = model.get_target(subj, rdfsNS.label)
170 if collectionName == None:
171 collectionName = collectionURI
172 out.write("<div class='c%d'><h3><a name='%s'>Collection: %s</a></h3>\n<dl>\n"
173 % (evenodd, collectionURI, collectionName))
174 else:
175 out.write("<div class='c%d'><h3>Unnamed collection</h3>\n<dl>\n" % evenodd)
176 evenodd = (evenodd+1)%2
177 out.write("<dt>label:</dt><dd>")
178 # Collections seem to have rdfs:label, not skos:prefLabel, for some reason
179 for label in model.get_targets(subj, rdfsNS.label):
180 out.write("%s\n" % label)
181 out.write("</dd>\n")
182 out.write("<dt>members:</dt>\n")
183 for member in model.get_targets(subj, skosNS.member):
184 if member.is_resource():
185 mstr = member._get_uri().__str__()[schemenamelen+1:]
186 else:
187 mstr = 'unnamed'
188 out.write("<dd><a href='#%s'>%s</a></dd>\n" % (mstr, model.get_target(member, skosNS.prefLabel)))
189 # Reached the end of the collection
190 out.write("</dl>\n</div>\n")
191
192 out.write("</body>\n</html>\n")
193

Properties

Name Value
svn:executable *

msdemlei@ari.uni-heidelberg.de
ViewVC Help
Powered by ViewVC 1.1.26