Skip to content

Commit c3d724e

Browse files
harded docs page building logic
1 parent 2d553e1 commit c3d724e

File tree

3 files changed

+123
-89
lines changed

3 files changed

+123
-89
lines changed

software/SchemaTerms/example-code/jinjaTemplating/templates/simple/DataTypePage.tpl

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,35 @@
88
<link rel="stylesheet" type="text/css" href="util/schemaorg.css" />
99
<link rel="stylesheet" type="text/css" href="util/prettify.css" />
1010
<script type="text/javascript" src="util/prettify.js"></script>
11-
11+
1212
</head>
1313
<body>
1414
{% set TERMTYPE = "DataType" %}
1515
{% include 'PageHeader.tpl' with context %}
1616
<div id="mainContent">
17-
{% include 'InfoBlock.tpl' with context %}
17+
{% include 'InfoBlock.tpl' with context %}
18+
19+
<div><h2>Specific Properties</h2>
20+
{% for prop in term.properties %}<a href="{{href_prefix}}{{prop}}.html">{{ prop }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
21+
</div>
22+
23+
<div>
24+
{% for type in term.expectedTypeFor %}
25+
{% if loop.first %}<h2>Expected Type For</h2>{% endif %}<a href="{{href_prefix}}{{type}}">{{ type }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
26+
</div>
1827

19-
<div><h2>Specific Properties</h2>
20-
{% for prop in term.properties %}<a href="{{href_prefix}}{{prop}}.html">{{ prop }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
21-
</div>
22-
23-
<div>
24-
{% for type in term.expectedTypeFor %}
25-
{% if loop.first %}<h2>Expected Type For</h2>{% endif %}<a href="{{href_prefix}}{{type}}">{{ type }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
26-
</div>
28+
<div>
29+
{% for sub in term.subs %}
30+
{% if loop.first %}<h2>More Specific Data Types</h2>{% endif %}
31+
<a href="{% if sub.startswith('http') %}{{ sub }}{% else %}{{ href_prefix }}{{ sub }}.html{% endif %}">
32+
{{ sub }}
33+
</a>{% if not loop.last %}, {% endif %}
34+
{% endfor %}
35+
</div>
2736

28-
<div>
29-
{% for sub in term.subs %}
30-
{% if loop.first %}<h2>More Specific Data Types</h2>{% endif %}<a href="{{href_prefix}}{{sub}}.html">{{ sub }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
31-
</div>
37+
<!-- list source references and acknowledgements -->
38+
{% include 'Ackblock.tpl' with context %}
3239

33-
<!-- list source references and acknowledgements -->
34-
{% include 'Ackblock.tpl' with context %}
35-
3640
</div> <!-- mainContent -->
3741
</body>
3842
</html>

software/SchemaTerms/example-code/jinjaTemplating/templates/simple/TypePage.tpl

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,40 @@
88
<link rel="stylesheet" type="text/css" href="util/schemaorg.css" />
99
<link rel="stylesheet" type="text/css" href="util/prettify.css" />
1010
<script type="text/javascript" src="util/prettify.js"></script>
11-
11+
1212
</head>
1313
<body>
1414
{% set TERMTYPE = "Type" %}
1515
{% include 'PageHeader.tpl' with context %}
1616
<div id="mainContent">
17-
{% include 'InfoBlock.tpl' with context %}
18-
19-
<div><h2>Properties Specific to this Type</h2>
20-
{% for prop in term.properties %}<a href="{{href_prefix}}{{prop}}.html">{{ prop }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
21-
</div>
22-
23-
<div><h2>All Available Properties</h2>
24-
{% for prop in term.allproperties %}<a href="{{href_prefix}}{{prop}}.html">{{ prop }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
25-
</div>
26-
27-
28-
<div>
29-
{% for type in term.expectedTypeFor %}
30-
{% if loop.first %}<h2>Expected Type For</h2>{% endif %}<a href="{{href_prefix}}{{type}}">{{ type }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
31-
</div>
32-
33-
<div>
34-
{% for sub in term.subs %}
35-
{% if loop.first %}<h2>More Specific Types</h2>{% endif %}<a href="{{href_prefix}}{{sub}}.html">{{ sub }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
36-
</div>
37-
38-
<!-- list source references and acknowledgements -->
39-
{% include 'Ackblock.tpl' with context %}
40-
17+
{% include 'InfoBlock.tpl' with context %}
18+
19+
<div><h2>Properties Specific to this Type</h2>
20+
{% for prop in term.properties %}<a href="{{href_prefix}}{{prop}}.html">{{ prop }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
21+
</div>
22+
23+
<div><h2>All Available Properties</h2>
24+
{% for prop in term.allproperties %}<a href="{{href_prefix}}{{prop}}.html">{{ prop }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
25+
</div>
26+
27+
28+
<div>
29+
{% for type in term.expectedTypeFor %}
30+
{% if loop.first %}<h2>Expected Type For</h2>{% endif %}<a href="{{href_prefix}}{{type}}">{{ type }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
31+
</div>
32+
33+
<div>
34+
{% for sub in term.subs %}
35+
{% if loop.first %}<h2>More Specific Data Types</h2>{% endif %}
36+
<a href="{% if sub.startswith('http') %}{{ sub }}{% else %}{{ href_prefix }}{{ sub }}.html{% endif %}">
37+
{{ sub }}
38+
</a>{% if not loop.last %}, {% endif %}
39+
{% endfor %}
40+
</div>
41+
42+
<!-- list source references and acknowledgements -->
43+
{% include 'Ackblock.tpl' with context %}
44+
4145
</div> <!-- mainContent -->
4246
</body>
4347
</html>

software/util/buildocspages.py

Lines changed: 73 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import sys
1010

1111
# Import schema.org libraries
12-
if not os.getcwd() in sys.path:
12+
if os.getcwd() not in sys.path:
1313
sys.path.insert(1, os.getcwd())
1414

1515
import software.scripts.buildtermlist as buildtermlist
@@ -134,15 +134,16 @@ def buildTermCatList(terms, checkCat=False):
134134
return termcat, termcount
135135

136136

137-
VISITLIST = []
137+
class UnknownTermError(LookupError):
138+
"""Raised when there is no definition for a given term."""
138139

139140

140141
class listingNode:
141-
def __init__(self, term, depth=0, title="", parent=None):
142-
global VISITLIST
142+
def __init__(self, term, depth=0, title="", parent=None, visit_set=None):
143143
termdesc = sdotermsource.SdoTermSource.getTerm(term)
144-
if parent is None:
145-
VISITLIST = []
144+
if not termdesc:
145+
raise UnknownTermError(f"No description for term {term}")
146+
visit_set = visit_set or set()
146147
self.repeat = False
147148
self.subs = []
148149
self.parent = parent
@@ -152,22 +153,36 @@ def __init__(self, term, depth=0, title="", parent=None):
152153
self.depth = depth
153154
self.retired = termdesc.retired
154155
self.pending = termdesc.pending
155-
if self.id not in VISITLIST:
156-
VISITLIST.append(self.id)
156+
if self.id not in visit_set:
157+
visit_set.add(self.id)
157158
if termdesc.termType == sdoterm.SdoTermType.ENUMERATION:
158159
for enum in sorted(termdesc.enumerationMembers.ids):
159-
self.subs.append(listingNode(enum, depth=depth + 1, parent=self))
160+
try:
161+
self.subs.append(
162+
listingNode(
163+
enum, depth=depth + 1, parent=self, visit_set=visit_set
164+
)
165+
)
166+
except UnknownTermError as e:
167+
log.warning(
168+
f"Error while building enumeration node {enum} for {term}: {e}"
169+
)
160170
for sub in sorted(termdesc.subs.ids):
161-
self.subs.append(listingNode(sub, depth=depth + 1, parent=self))
162-
171+
try:
172+
self.subs.append(
173+
listingNode(
174+
sub, depth=depth + 1, parent=self, visit_set=visit_set
175+
)
176+
)
177+
except UnknownTermError as e:
178+
log.warning(
179+
f"Error while building child node {sub} for {term}: {e}"
180+
)
163181
else: # Visited this node before so don't parse children
164182
self.repeat = True
165183

166184

167-
def jsonldtree(page):
168-
global VISITLIST
169-
VISITLIST = []
170-
185+
def jsonldtree(page) -> str:
171186
term = {}
172187
context = {}
173188
context["rdfs"] = "http://www.w3.org/2000/01/rdf-schema#"
@@ -176,38 +191,49 @@ def jsonldtree(page):
176191
context["description"] = "rdfs:comment"
177192
context["children"] = {"@reverse": "rdfs:subClassOf"}
178193
term["@context"] = context
179-
data = _jsonldtree("Thing", term)
180-
return json.dumps(data, indent=3)
181-
182-
183-
def _jsonldtree(tid: str, term=None):
184-
termdesc = sdotermsource.SdoTermSource.getTerm(tid)
185-
if not term:
186-
term = {}
187-
term["@type"] = "rdfs:Class"
188-
term["@id"] = "schema:" + termdesc.id
189-
term["name"] = termdesc.label
190-
if termdesc.supers:
191-
sups = ["schema:" + sup for sup in termdesc.supers.ids]
192-
if len(sups) == 1:
193-
term["rdfs:subClassOf"] = sups[0]
194-
else:
195-
term["rdfs:subClassOf"] = sups
196-
term["description"] = textutils.ShortenOnSentence(
197-
textutils.StripHtmlTags(termdesc.comment)
198-
)
199-
if termdesc.pending:
200-
term["pending"] = True
201-
if termdesc.retired:
202-
term["attic"] = True
203-
if tid not in VISITLIST:
204-
VISITLIST.append(tid)
205-
if termdesc.subs:
206-
subs = []
207-
for sub in termdesc.subs.ids:
208-
subs.append(_jsonldtree(tid=sub))
209-
term["children"] = subs
210-
return term
194+
data = _jsonldtree("Thing", visitset=set(), term=term)
195+
return json.dumps(data, indent=2)
196+
197+
198+
def _jsonldtree(tid: str, visitset: set, term: dict | None = None) -> dict:
199+
try:
200+
termdesc = sdotermsource.SdoTermSource.getTerm(tid)
201+
if not termdesc:
202+
raise UnknownTermError(f"No description for term {term}")
203+
if not term:
204+
term = {}
205+
term["@type"] = "rdfs:Class"
206+
term["@id"] = "schema:" + termdesc.id
207+
term["name"] = termdesc.label
208+
if termdesc.supers:
209+
sups = ["schema:" + sup for sup in termdesc.supers.ids]
210+
if len(sups) == 1:
211+
term["rdfs:subClassOf"] = sups[0]
212+
else:
213+
term["rdfs:subClassOf"] = sups
214+
term["description"] = textutils.ShortenOnSentence(
215+
textutils.StripHtmlTags(termdesc.comment)
216+
)
217+
if termdesc.pending:
218+
term["pending"] = True
219+
if termdesc.retired:
220+
term["attic"] = True
221+
if tid not in visitset:
222+
visitset.add(tid)
223+
if termdesc.subs:
224+
subs = []
225+
for sub in termdesc.subs.ids:
226+
try:
227+
subs.append(_jsonldtree(tid=sub, visitset=visitset))
228+
except UnknownTermError as e:
229+
log.warning(
230+
f"Error while building listing node for {term}: {e}"
231+
)
232+
term["children"] = subs
233+
return term
234+
except Exception as e:
235+
e.add_note(f"while building JSON tree for id:{tid}-{term}")
236+
raise
211237

212238

213239
listings = None

0 commit comments

Comments
 (0)