summaryrefslogtreecommitdiff
path: root/util/doc-check.py
blob: 1f4351b6974f43ea16246815257744109b63134a (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
#!/usr/bin/env -S PYTHONDONTWRITEBYTECODE=1 python

# file    : util/doc-check.py
# project : Salis-VM
# author  : Paul Oliver <contact@pauloliver.dev>
#
# Simple utility to check file sections match indices.
# Enforced indexing style is inspired by ImGui.

import pathlib
import re

def check_pattern(pattern, content, path):
    if not re.findall(pattern, content, re.MULTILINE):
        raise RuntimeError(f"Could not find pattern: '{pattern}' in '{path}'")

def check_path(path, doc_prefix):
    with open(path, "r") as f:
        content = f.read()

    check_pattern(fr"^{doc_prefix} file    : {path}$", content, path)
    check_pattern(fr"^{doc_prefix} project : Salis-VM$", content, path)
    check_pattern(fr"^{doc_prefix} author  : Paul Oliver <contact@pauloliver.dev>$", content, path)

    entries = [s.strip() for s in re.findall(fr"^\s*{doc_prefix} \[section\] .*$", content, re.MULTILINE)]
    entries_index = entries[len(entries) // 2:]
    entries_content = entries[:len(entries) // 2]

    if entries_index != entries_content:
        raise RuntimeError(f"Index/section mismatch in: '{path}': {entries_index} != {entries_content}")

prefix_map = {
    "#": ("py", ),
    "//": ("c", "cpp", "h"),
    ";": ("asm", ),
}

[
    check_path(path, f"\\s*{prefix}")
    for prefix, extensions in prefix_map.items()
    for extension in extensions
    for path in pathlib.Path(".").rglob(f"*.{extension}")
]