"""Module to modify the HTML files generated by Sphinx."""

from importlib import import_module
from pathlib import Path

from bs4 import BeautifulSoup

from ansys.fluent.core.solver.flobject import Base


def modify_html(soup: BeautifulSoup) -> None:
    """Modify the HTML files generated by Sphinx to write original python class
    names."""
    module_name = soup.find(class_="sig-prename descclassname").text.removesuffix(".")
    module = import_module(module_name)
    class_desc = soup.find(class_="sig-name descname")
    class_name = class_desc.text
    cls = getattr(module, class_name)
    if issubclass(cls, Base):
        if cls.__name__ != cls._python_name:
            class_desc.string = cls._python_name


if __name__ == "__main__":
    html_dir = (
        Path(__file__).parent
        / "_build"
        / "html"
        / "api"
        / "solver"
        / "_autosummary"
        / "settings"
    )
    for html_file in html_dir.glob("*.html"):
        with open(html_file, "r", encoding="utf-8") as f:
            soup = BeautifulSoup(f, "html.parser", from_encoding="utf-8")

        modify_html(soup)

        with open(html_file, "w", encoding="utf-8") as f:
            f.write(str(soup))
