|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +import ark |
| 5 | +from rdflib import Graph, URIRef, Literal |
| 6 | +from rdflib.namespace import RDF, SDO |
| 7 | + |
| 8 | + |
| 9 | +SITE_GRAPH = Graph() |
| 10 | + |
| 11 | + |
| 12 | +def add_via_site_graph(g: Graph, page_data: dict, data: str, a: URIRef) -> None: |
| 13 | + if not SITE_GRAPH.value(predicate=RDF.type, object=a, any=True): |
| 14 | + SITE_GRAPH.parse(data=data, format="turtle") |
| 15 | + for s, p, o in SITE_GRAPH: |
| 16 | + g.add((s, p, o)) |
| 17 | + |
| 18 | + |
| 19 | +def add_webpage_to_graph(g: Graph, page_data: dict) -> None: |
| 20 | + node = page_data["node"] |
| 21 | + site = g.value(predicate=RDF.type, object=SDO.WebSite, any=False) |
| 22 | + path = ark.site.out("") |
| 23 | + node_path = ark.utils.rewrite_urls(f"'{node.url}'", path).strip("'") |
| 24 | + page = URIRef(os.path.join(str(site), node_path)) |
| 25 | + g.add((page, RDF.type, SDO.WebPage)) |
| 26 | + if node_path.startswith("blog"): |
| 27 | + g.add((page, RDF.type, SDO.BlogPosting)) |
| 28 | + g.add((page, SDO.isPartOf, site)) |
| 29 | + author = g.value(subject=site, predicate=SDO.author, any=False) |
| 30 | + g.add((page, SDO.author, author)) |
| 31 | + g.add((page, SDO.copyrightHolder, author)) |
| 32 | + if node.get("title"): |
| 33 | + g.add((page, SDO.headline, Literal(node.get("title")))) |
| 34 | + g.add((page, SDO.license, Literal(ark.site.config.get("license", "")))) |
| 35 | + g.add((page, SDO.inLanguage, Literal("en"))) |
| 36 | + if node.get("word_count"): |
| 37 | + g.add((page, SDO.wordCount, Literal(node.get("word_count", 0)))) |
| 38 | + url = Literal(os.path.join(str(site), node_path)) |
| 39 | + g.add((page, SDO.url, url)) |
| 40 | + if node.get("date"): |
| 41 | + g.add((page, SDO.dateCreated, Literal(node.get("date")))) |
| 42 | + |
| 43 | + |
| 44 | +# JSON-LD generation event |
| 45 | +@ark.events.register(ark.events.Event.RENDER_PAGE) |
| 46 | +def create_json_ld(page_data: dict) -> str: |
| 47 | + g = Graph() |
| 48 | + add_via_site_graph(g, page_data, data=ark.site.config["site_ttl"], a=SDO.WebSite) |
| 49 | + add_via_site_graph(g, page_data, data=ark.site.config["author_ttl"], a=SDO.Person) |
| 50 | + add_webpage_to_graph(g, page_data) |
| 51 | + json_ld_g = g.serialize( |
| 52 | + format="json-ld", |
| 53 | + context={"schema": SDO._NS}, |
| 54 | + auto_compact=True, |
| 55 | + ) |
| 56 | + json_ld_g = json.dumps(json.loads(json_ld_g), separators=(",", ":")) |
| 57 | + page_data["json_ld"] = json_ld_g |
| 58 | + return page_data |
0 commit comments