pocketpy/scripts/gen_docs.py
ykiko 2773b9993e
Implement dynamic dlib (#309)
* lazy cpp_function and capsule.

* remove retv.

* remove type_map.

* remove object pool from initialize.

* support dynamic library.

* remove vector_bool.

* remove unused header.

* fix export name.

* fix test name.

* some fix

* some fix

* ...

---------

Co-authored-by: blueloveTH <blueloveth@foxmail.com>
2024-09-22 21:48:10 +08:00

68 lines
1.7 KiB
Python

import re
from dataclasses import dataclass
NEWLINE = '\n'
@dataclass
class Function:
name: str
args: str
ret: str
comment: str
is_py_raise: bool
is_py_return: bool
def signature(self):
tmp = f"PK_API {self.ret} {self.name}{self.args}"
return tmp + ';'
def badges(self):
res = []
if self.is_py_raise:
res.append('[!badge text="raise" variant="danger"](../introduction/#py_raise-macro)')
if self.is_py_return:
res.append('[!badge text="return"](../introduction/#py_return-macro)')
return ' '.join(res)
def markdown(self):
lines = [
f"### {self.name}" + f" {self.badges()}",
f"```c",
self.comment,
f"{self.signature()}",
f"```",
]
return '\n'.join(lines)
with open('include/pocketpy/pocketpy.h') as f:
header = f.read()
matches = re.finditer(r"((?:/// [^\n]+[\n])*?)PK_API\s+(\w+\*?)\s+(\w+)(\(.*?\))\s*(PY_RAISE)?\s*(PY_RETURN)?\s*;", header, re.DOTALL)
# ^1 comment ^2 ret ^3 n ^4 args ^5 py_raise? ^6 py_return?
functions: list[Function] = []
for match in matches:
functions.append(Function(
name=match[3],
args=match[4],
ret=match[2],
comment=match[1].strip(),
is_py_raise=bool(match[5]),
is_py_return=bool(match[6])
))
# print(functions[-1])
# generate markdown
with open('docs/C-API/functions.md', 'w', newline='\n') as f:
f.write('\n'.join([
'---',
'title: Functions',
'icon: dot',
'order: 0',
'---',
'\n\n',
]))
for function in functions:
f.write(function.markdown())
f.write('\n\n')