Skip to content

Commit

Permalink
Added figures to generated documentation
Browse files Browse the repository at this point in the history
Also, added prefLabel as anchoring points such that one can use the browser
to easily get documentation of a concept one knows the prefLabel of.

For example, entering

    https://w3id.org/emmo#Atom

should bring you to the section in the html doc that documents Atom.
  • Loading branch information
jesper-friis committed Jun 2, 2024
1 parent f744f34 commit f96665b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
48 changes: 38 additions & 10 deletions ontopy/ontodoc_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def __init__(
self.individuals = set()
self.datatypes = set()

# All navigation IDs added by the ontology. Used to warn about
# dublicated IDs
self.navids = set()

if ontology:
self.add_ontology(ontology)

Expand Down Expand Up @@ -137,7 +141,7 @@ def get_refdoc(
subsections: str = "all",
header: bool = True,
) -> str:
# pylint: disable=too-many-branches,too-many-locals
# pylint: disable=too-many-branches,too-many-locals,too-many-statements
"""Return reference documentation of all module entities.
Arguments:
Expand Down Expand Up @@ -186,15 +190,23 @@ def add_header(name):
]
)

def add_keyvalue(key, value, escape=True, htmllink=True):
def add_keyvalue(
key, value, escape=True, htmllink=True, show_figure=True
):
"""Help function for adding a key-value row to table."""
if escape:
value = html.escape(str(value))
if htmllink:
value = re.sub(
r"(https?://[^\s]+)", r'<a href="\1">\1</a>', value
)
value = value.replace("\n", "<br>")
if show_figure and re.match(
r"^(http://|https://)[a-zA-Z.+?@/_-]+\.(png|jpg|jpeg|svg|gif)$",
value,
):
value = f'<img src="{value}">'

Check warning on line 201 in ontopy/ontodoc_rst.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontodoc_rst.py#L201

Added line #L201 was not covered by tests
else:
if escape:
value = html.escape(str(value))
if htmllink:
value = re.sub(
r"(https?://[^\s]+)", r'<a href="\1">\1</a>', value
)
value = value.replace("\n", "<br>")
lines.extend(
[
" <tr>",
Expand Down Expand Up @@ -222,11 +234,27 @@ def add_keyvalue(key, value, escape=True, htmllink=True):
)
for entity in sorted(maps[subsection], key=get_label):
label = get_label(entity)
navid = navid2 = ""
if entity.name in self.navids:
warnings.warn(f"duplicated entity names: {entity.name}")

Check warning on line 239 in ontopy/ontodoc_rst.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontodoc_rst.py#L239

Added line #L239 was not covered by tests
else:
self.navids.add(entity.name)
navid = f' <div id="{entity.name}"></div>'
if hasattr(entity, "prefLabel"):
preflabel = str(entity.prefLabel.first())
if preflabel != entity.name:
if preflabel in self.navids:
warnings.warn(f"duplicated prefLabel: {preflabel}")

Check warning on line 247 in ontopy/ontodoc_rst.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontodoc_rst.py#L247

Added line #L247 was not covered by tests
else:
self.navids.add(preflabel)
navid2 = f' <div id="{preflabel}"></div>'

lines.extend(
[
".. raw:: html",
"",
f' <div id="{entity.name}"></div>',
navid,
navid2,
"",
f"{label}",
"^" * len(label),
Expand Down
5 changes: 4 additions & 1 deletion ontopy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,11 @@ def copy_annotation(onto, src, dst):
"""
if onto.world[src]:
src = onto.world[src]
else:
elif src in onto:
src = onto[src]
else:
warnings.warn(f"skipping copy for missing source annotation: {src}")
return

Check warning on line 895 in ontopy/utils.py

View check run for this annotation

Codecov / codecov/patch

ontopy/utils.py#L894-L895

Added lines #L894 - L895 were not covered by tests

if onto.world[dst]:
dst = onto.world[dst]
Expand Down

0 comments on commit f96665b

Please sign in to comment.