-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocWriter.py
67 lines (48 loc) · 1.86 KB
/
docWriter.py
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
import os
import re
files_to_read = ["get.ts", "post.ts", "delete.ts", "put.ts"]
current_directory = os.getcwd() # Current directory
def generate_markdown(folder_name: str, function_names: str, new_dir: bool) -> str:
"""
This function will generate the markdown for each function
"""
markdown = f"### {folder_name.capitalize()}\n\n" if new_dir else ""
for function_name in function_names:
markdown += f"#### {function_name}\n\n"
markdown += f"Request {function_name}.\n\n"
markdown += f"Docs: [https://docs.wildduck.email/api/#operation/{function_name}](https://docs.wildduck.email/api/#operation/{function_name})\n\n"
return markdown
def process_file(file_path: str, new_dir: bool) -> str:
"""
Process the selected file and read the markdown inside it
"""
result = ""
with open(file_path, "r") as file:
file_content = file.read()
matches = re.findall(r"export const (\w+) = async", file_content)
if matches:
function_names = matches
folder_name = os.path.basename(os.path.dirname(file_path))
markdown = generate_markdown(folder_name, function_names, new_dir)
result += markdown
return result
def process_directory(directory_path: str) -> str:
"""
Walk through directories, get all the request functions and
generate the markdown documentation
"""
docs = ""
for root, _, files in os.walk(directory_path):
new_dir = True
for file in files:
if file in files_to_read:
file_path = os.path.join(root, file)
docs += process_file(file_path, new_dir)
new_dir = False
return docs
def main():
docs = process_directory(current_directory)
with open("docs.md", "w") as file:
file.write(docs)
if __name__ == "__main__":
main()