Unverified Commit e41b555c authored by Maxime Lefrançois's avatar Maxime Lefrançois
Browse files

replace fields in html

parent b8446a3e
Loading
Loading
Loading
Loading
+29 −1
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ from functools import cache
import os
from pathlib import Path
import logging
from re import Match
from dominate.tags import *
from dominate.util import text, raw
from markdown import markdown
@@ -640,6 +641,7 @@ class WebsiteGenerator:
            ).exists()
        ):
            value = path.read_text()
            value = self.replace_fields(document, value)
        elif (
            file_name
            and (
@@ -648,7 +650,9 @@ class WebsiteGenerator:
                )
            ).exists()
        ):
            value = markdown(path.read_text(), extensions=["extra", "admonition", "codehilite"])
            md = path.read_text()
            md = self.replace_fields(document, md)
            value = markdown(md, extensions=["extra", "admonition", "codehilite"])
        elif property:
            value = next(
                filter(
@@ -1605,3 +1609,27 @@ class WebsiteGenerator:

        else:  # fallback
            raw("[?]<sup title='object property' class='type-op'>op</sup>")

    def replace_fields(self, document: SAREFGraphDocument, html:str) -> str:
        def replace_field(match:Match[str]) -> str:
            field = match.group(1)
            func_name = f"replace_{field}"
            if hasattr(self, func_name) and callable(getattr(self, func_name)):
                func = getattr(self, func_name)
                return func(document)
            else:
                logger.error(f"Field {field} not implemented")
                return ""
        return re.sub(r"\{\{(.*?)\}\}", replace_field, html)

    def replace_table_1(self, document: SAREFGraphDocument):
        with table() as el:
            with tr():
                th("Prefix")
                th("Namespace")
            for prefix, ns in sorted(document.namespaces, key=lambda x: x[0]):
                with tr():
                    td(prefix)
                    td(ns)
            caption("Table 1: Namespace Declarations", id="Table_1")
        return el.render(indent='    ')