summaryrefslogtreecommitdiff
path: root/scripts/apidocs/gen_apidocs.py
blob: d01e44160c3bda625b617a3180c5a9a30891b564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Generates a static webpage for the public C-API of `celeritas-core`
#
# TODO:
#   - remove prefixes like 'static' and 'inline'
#   - parse docstrings from source

import re
import os
from pathlib import Path

# --- HTML Fragments
page_start = """
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <link rel="stylesheet" href="doc_styles.css">
    <!-- <link rel="stylesheet" href="prism.css"> -->
    <!-- <script src="prism.js"></script> -->

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
    <!-- and it's easy to individually load additional languages -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/go.min.js"></script>
    <script>hljs.highlightAll();</script>

    <title>Celeritas core API</title>
</head>
<body>
<main>
"""

page_header = """
<header>
    <h1>CELERITAS CORE API DOCS</h1>
</header>
"""

page_footer = """
<footer>
</footer>
"""

page_end = """
</main>
</body>
</html>
"""

def emit_function_sig(signature: str) -> str:
    return f"""
    <li class="signature">
    <pre><code class="language-c">{signature}</code></pre>
    </li>
    """

categories = {
    "Core": "src/core",
    "Render": "src/new_render",
    "Maths": "src/maths",
    "RAL": "src/ral",
    "Systems": "src/systems",
}

def find_pub_functions_in_folder(folder_path):
    functions = []
    for filename in os.listdir(folder_path):
        filepath = os.path.join(folder_path, filename)
        if os.path.isfile(filepath):
            file_funcs = find_pub_functions_in_file(filepath)
            functions.extend(file_funcs)

    return functions

def find_pub_functions_in_file(file_path):
    pattern = r'PUB\s+(\w+\s+)*(\w+)\s+(\w+)\s*\((.*?)\)'

    with open(file_path, 'r') as file:
        content = file.read()

    matches = re.finditer(pattern, content, re.MULTILINE)

    # Collect all the functions into an array
    functions = []
    for match in matches:
        signature = match.group(0)
        if signature.startswith("PUB "):
            signature = signature[4:]
        if signature.startswith("c_static_inline "):
            signature = signature[16:]

        print(signature)
        functions.append(signature)

    return functions

def generate_html():
    html_filepath = "index.html"

    script_dir = Path(__file__).resolve().parent
    grandparent_dir = script_dir.parents[1]

    with open(html_filepath, 'w') as export_file:
        export_file.write(page_start)
        export_file.write(page_header)
        # TODO: make the actual content
        for category in categories.keys():
            folder = os.path.join(grandparent_dir, categories[category])
            category_funcs = find_pub_functions_in_folder(folder)
            export_file.write(f"<h3>{category}</h3>")
            export_file.write("<ul class=\"category-list\">")
            for func in category_funcs:
                export_file.write(emit_function_sig(func))
            export_file.write("</ul>")
        export_file.write(page_end)

if __name__ == "__main__":
    generate_html()