Skip to content

Commit

Permalink
generate docs using pyLODE
Browse files Browse the repository at this point in the history
  • Loading branch information
keski committed Sep 17, 2024
1 parent a79014b commit 1c00767
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 195 deletions.
105 changes: 12 additions & 93 deletions docs-scripts/build.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
#!/usr/bin/env python3
import os
import re
from jinja2 import Environment, FileSystemLoader
from rdflib import DC, RDF, RDFS, Graph
from rdflib import Graph
import logging
import shutil
import hashlib
from pylode.profiles.vocpub import VocPub

# Configure root logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logging.basicConfig(format='%(levelname)-8s [%(filename)s:%(lineno)d] %(message)s', level=logging.DEBUG)

prefixes = {}


def hash_file(filename):
"""Returns the SHA-256 hash of the file passed into it"""
Expand Down Expand Up @@ -58,94 +55,16 @@ def create_documentation(name, version):
versioned_target = f"docs/vocab/{name}/{version}/index.html"
latest_target = f"docs/vocab/{name}/latest/index.html"

# Load RDF data into a graph
g = Graph(bind_namespaces="none")
g.parse(file=open(f"hefquin-vocabs/{name}.ttl"), format="turtle")

# Set up Jinja2 environment and load the template
env = Environment(loader=FileSystemLoader('./docs-scripts/'))
env.filters['prefix'] = prefix
env.filters['prefix_list'] = prefix_list
env.filters['fragment'] = fragment
html_template = env.get_template('template.j2')

# Collect prefixes
prefixes = {}
for p, namespace in g.namespaces():
prefixes[p] = namespace

# Get the identifier of the vocabulary
for s, p, _ in g.triples((None, DC.identifier, None)):
vocab_uri = s

# Extract general information about the vocabulary
data = {
"creator": g.value(vocab_uri, DC.creator),
"date": g.value(vocab_uri, DC.date),
"format": g.value(vocab_uri, DC.format),
"identifier": g.value(vocab_uri, DC.identifier),
"title": g.value(vocab_uri, DC.title),
"label": g.value(vocab_uri, RDFS.label),
"description": g.value(vocab_uri, DC.description),
"comment": g.value(vocab_uri, RDFS.comment),
"prefixes": prefixes,
"classes": [],
"properties": [],
"instances": []
}

# Extract classes
for s in g.subjects(RDF.type, RDFS.Class):
data["classes"].append({
"uri": s,
"label": g.value(s, RDFS.label, default="N/A"),
"comment": g.value(s, RDFS.comment, default="N/A"),
"subclasses": [o for o in g.objects(s, RDFS.subClassOf)]
})

# Extract properties
for s in g.subjects(RDF.type, RDF.Property):
data["properties"].append({
"uri": s,
"label": g.value(s, RDFS.label, default="N/A"),
"comment": g.value(s, RDFS.comment, default="N/A"),
"domain": g.value(s, RDFS.domain, default="N/A"),
"range": g.value(s, RDFS.range, default="N/A")
})

# Extract instances
for s in g.subjects(RDF.type, None):
if s not in [cls['uri'] for cls in data['classes']] and s not in [prop['uri'] for prop in data['properties']]:
data["instances"].append({
"uri": s,
"type": [o for o in g.objects(s, RDF.type)]
})

## Save the HTML to a file
with open(versioned_target, "w") as file:
data["location"] = f"{vocab_uri}/{version}/{name}"
html_content = html_template.render(data)
file.write(html_content)

with open(latest_target, "w") as file:
data["location"] = f"{vocab_uri}/latest/{name}"
html_content = html_template.render(data)
file.write(html_content)


def prefix_list(l):
return [prefix(i) for i in l]


def prefix(resource):
for prefix, uri in prefixes.items():
if resource.startswith(uri):
return f"{prefix}:{resource.replace(uri, '')}"
return resource


def fragment(resource):
return re.split(r"[/#]", resource)[-1]
source = f"hefquin-vocabs/{name}.ttl"

od = VocPub(ontology=source)
html = od.make_html()

with open(versioned_target, "w") as f:
f.write(html)

with open(latest_target, "w") as f:
f.write(html)


def main():
Expand Down
2 changes: 1 addition & 1 deletion docs-scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rdflib
jinja2
git+https://github.com/LiUSemWeb/pyLODE.git
101 changes: 0 additions & 101 deletions docs-scripts/template.j2

This file was deleted.

0 comments on commit 1c00767

Please sign in to comment.