Skip to content

Commit

Permalink
fix parent triples in vocab; broken links in HTML
Browse files Browse the repository at this point in the history
- fixed parent triples in vocab files e.g. X is superclass of Y where X
  is from an external namespace and Y is in current namespace. Now only
  terms from current namespace are provided as subjects.
- broken links in HTML fixed by creating a new Jinja macro called
  `anchor` which takes a prefixed term and expands it to a HTML anchor
  link. The earlier method of using Respec's [[--]] notation does not
  always work reliably (e.g. external standard vocab has the same term)
  • Loading branch information
coolharsh55 committed Feb 3, 2024
1 parent 6423f48 commit 36d1867
Show file tree
Hide file tree
Showing 706 changed files with 220,612 additions and 326,866 deletions.
4 changes: 2 additions & 2 deletions code/200_serialise_RDF.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,13 @@ def serialize_graph(triples:list, filepath:str, vocab:str, hook:str=None) -> Non
# for classes, replace `skos:broader` with `rdfs:subClassOf`
# and `skos:narrower` with `rdfs:superClassOf` (made up relation)
graph.update("""
INSERT { ?s rdfs:subClassOf ?o . ?o rdfs:superClassOf ?s }
INSERT { ?s rdfs:subClassOf ?o . }
WHERE { ?s a rdfs:Class . ?s skos:broader ?o }
""")
# for properties, replace `skos:broader` with `rdfs:subPropertyOf`
# and `skos:narrower` with `rdfs:superPropertyOf` (made up relation)
graph.update("""
INSERT { ?s rdfs:subPropertyOf ?o . ?o rdfs:superPropertyOf ?s }
INSERT { ?s rdfs:subPropertyOf ?o . }
WHERE { ?s a rdf:Property . ?s skos:broader ?o }
""")
# Delete any hanging `skos:narrower` and `skos:broader`
Expand Down
15 changes: 14 additions & 1 deletion code/300_generate_HTML.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,18 @@ def translation_message(concept:dict, field:str, lang:str) -> str:
return concept[f'{field}-{lang}']


def make_anchor_link(concept:str|dict) -> str:
"""
Generates an HTML anchor link using the given concept.
Can be passed a string (prefixed term) or a dict
"""
if type(concept) == str:
concept = DATA.concepts[concept]
iri = concept['iri']
term = concept['term']
return f"<a href='{iri}'><code>{term}</code></a>"


# == HTML Export ==

# === Jinja setup ===
Expand Down Expand Up @@ -617,6 +629,7 @@ def translation_message(concept:dict, field:str, lang:str) -> str:
'retrieve_example': retrieve_example,
'retrieve_example_for_concept': retrieve_example_for_concept,
'translation_message': translation_message,
'make_anchor_link': make_anchor_link,
}
template_env.filters.update(JINJA2_FILTERS)

Expand Down Expand Up @@ -717,7 +730,7 @@ def _write_template(
# translations should exist and the paths to save the data
with open(TRANSLATIONS_MISSING_FILE, 'r') as fd:
data = json.load(fd)
if ':' in list(data.keys())[0]: # hack to detect repeated script call
if data and ':' in list(data.keys())[0]: # hack to detect repeated script call
missing = {lang:{} for lang in IMPORT_TRANSLATIONS}
# For each concept declared in the missing translations file,
# collect the label, definition, and (if exists) scope note
Expand Down
81 changes: 69 additions & 12 deletions code/jinja2_resources/macro_term_table.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -57,57 +57,110 @@
</tr>

{# term parents #}
{% if concept['skos:broader'] %}
{% for parentlist in concept|get_parent_hierarchy %}<tr class="technical-detail">
{% if concept['skos:broader'] %}{% for parentlist in concept|get_parent_hierarchy %}
<tr class="technical-detail">
<th>Broader/Parent types</th>
<td>{% for parent in parentlist|resolve_concepts %}
<a href="{% if parent['prefix'] == vocab_name %}#{{parent['term']}}{% else %}{{parent['iri']}}{% endif %}">{{ parent['prefixed'] }}</a>{{ " → " if not loop.last }}
{% endfor %}</td>
{% endfor %}</tr>{% endif %}
{% if parent['prefixed'] in vocab and parent['vocab'] == vocab_name %}
<a href="#{{parent['term']}}">{{ parent['prefixed'] }}</a>
{% else %}
<a href="{{parent['iri']}}">{{ parent['prefixed'] }}</a>
{% endif %}
{{ " → " if not loop.last }}{% endfor %}
</td>{% endfor %}
</tr>{% endif %}

{# term children #}
{% if concept['skos:narrower'] %}
<tr class="technical-detail">
<th>Narrower/Specialised types</th>
<td>{% for child in concept['skos:narrower']|ensure_list|sort %}<a href="{{child}}">{{ child|prefix_this }}</a>{{", " if not loop.last}}{% endfor %}</td>
<td>{% for child in concept['skos:narrower']|ensure_list|sort %}
{% set child_prefixed = child|prefix_this %}
{% if child_prefixed in vocab and vocab[child_prefixed]['vocab'] == vocab_name %}
<a href="#{{vocab[child_prefixed]['term']}}">{{ child_prefixed }}</a>
{% else %}
<a href="{{child}}">{{ child_prefixed }}</a>
{% endif %}
{{ ", " if not loop.last }}{% endfor %}
</td>
</tr>{% endif %}

{# properties where term is in domain #}
{% if concept['_type'] == 'class' %}
{% for prop in concept|get_prop_with_term_domain(vocab)|sort(attribute='iri') %}{% if loop.first %}<tr>
<th>Subject of relation</th>
<td>{% endif %}<a href="{{prop['iri']}}">{{ prop['prefixed'] }}{{ ", " if not loop.last }}{% if loop.last %}</td>
<td>{% endif %}
{% if prop['prefixed'] in vocab and prop['vocab'] == vocab_name and prop['_dpvterm'] %}
<a href="#{{prop['term']}}">{{ prop['prefixed'] }}</a>{{ ", " if not loop.last }}
{% else %}
<a href="{{prop['iri']}}">{{ prop['prefixed'] }}</a>{{ ", " if not loop.last }}
{% endif %}
{% if loop.last %}</td>
</tr>{% endif %}{% endfor %}

{# properties where term is in range #}
{% for prop in concept|get_prop_with_term_range(vocab)|sort(attribute='iri') %}{% if loop.first %}<tr>
<th>Object of relation</th>
<td>{% endif %}<a href="{{prop['iri']}}">{{ prop['prefixed'] }}</a>{{ ", " if not loop.last }}{% if loop.last %}</td>
<td>{% endif %}
{% if prop['prefixed'] in vocab and prop['vocab'] == vocab_name and prop['_dpvterm'] %}
<a href="#{{prop['term']}}">{{ prop['prefixed'] }}</a>{{ ", " if not loop.last }}
{% else %}
<a href="{{prop['iri']}}">{{ prop['prefixed'] }}</a>{{ ", " if not loop.last }}
{% endif %}
{% if loop.last %}</td>
</tr>{% endif %}{% endfor %}
{% endif %}

{# sub-properties #}
{% if concept['rdfs:subPropertyOf'] %}<tr class="technical-detail">
<th>Sub-property of</th>
<td><i>{% for parent in concept['rdfs:subPropertyOf']|get_concept_list %}<a href="{{parent['iri']}}">{{parent['prefixed']}}</a>{{", " if not loop.last }}{% endfor %}</i></td>
<td>{% for parent in concept['rdfs:subPropertyOf']|get_concept_list %}
{% if parent['prefixed'] in vocab and parent['vocab'] == vocab_name %}
<a href="#{{parent['term']}}">{{ parent['prefixed'] }}</a>
{% else %}
<a href="{{parent['iri']}}">{{ parent['prefixed'] }}</a>
{% endif %}
{{ " → " if not loop.last }}{% endfor %}
</td>
</tr>{% endif %}

{# super-properties #}
{% if concept['rdfs:superPropertyOf'] %}<tr class="technical-detail">
<th>Super-property of</th>
<td><i>{% for parent in concept['rdfs:superPropertyOf']|get_concept_list %}<a href="{{parent['iri']}}">{{parent['prefixed']}}</a>{{", " if not loop.last }}{% endfor %}</i></td>
<td>{% for parent in concept['rdfs:superPropertyOf']|get_concept_list %}
{% if parent['prefixed'] in vocab and parent['vocab'] == vocab_name %}
<a href="#{{parent['term']}}">{{ parent['prefixed'] }}</a>
{% else %}
<a href="{{parent['iri']}}">{{ parent['prefixed'] }}</a>
{% endif %}
{{ " → " if not loop.last }}{% endfor %}
</td>
</tr>{% endif %}

{# property domain includes #}
{% if concept['dcam:domainIncludes'] %}<tr class="technical-detail">
<th>Domain includes</th>
<td><i>{% for parent in concept['dcam:domainIncludes']|get_concept_list %}<a href="{{parent['iri']}}">{{parent['prefixed']}}</a>{{", " if not loop.last }}{% endfor %}</i></td>
<td>{% for parent in concept['dcam:domainIncludes']|get_concept_list %}
{% if parent['prefixed'] in vocab and parent['vocab'] == vocab_name %}
<a href="#{{parent['term']}}">{{ parent['prefixed'] }}</a>
{% else %}
<a href="{{parent['iri']}}">{{ parent['prefixed'] }}</a>
{% endif %}
{{ ", " if not loop.last }}{% endfor %}
</td>
</tr>{% endif %}

{# property range includes #}
{% if concept['dcam:rangeIncludes'] %}<tr class="technical-detail">
<th>Range includes</th>
<td><i>{% for parent in concept['dcam:rangeIncludes']|get_concept_list %}<a href="{{parent['iri']}}">{{parent['prefixed']}}</a>{{", " if not loop.last }}{% endfor %}</i></td>
<td>{% for parent in concept['dcam:rangeIncludes']|get_concept_list %}
{% if parent['prefixed'] in vocab and parent['vocab'] == vocab_name %}
<a href="#{{parent['term']}}">{{ parent['prefixed'] }}</a>
{% else %}
<a href="{{parent['iri']}}">{{ parent['prefixed'] }}</a>
{% endif %}
{{ ", " if not loop.last }}{% endfor %}
</td>
</tr>{% endif %}

<tr class="table-separator"></tr>
Expand Down Expand Up @@ -216,4 +269,8 @@ concept. If top is None, then use all concepts. `lang` is used to retrieve the l
{% endfor %}
</ul>{% endif %}
{% if first_iteration %}</div>{% endif %}
{% endmacro %}

{% macro anchor(term) %}
{{ term|make_anchor_link|safe }}
{% endmacro %}
Loading

0 comments on commit 36d1867

Please sign in to comment.