Repository navigation
Expand file tree
/
Copy pathdocgen.py
More file actions
149 lines (124 loc) · 4.78 KB
/
Copy pathdocgen.py
File metadata and controls
149 lines (124 loc) · 4.78 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
"""
DevFlow Fullstack - Documentation Generator
Usage:
python docgen.py --file foo.py --mode inline|function|api|readme
python docgen.py --dir src/ --mode all
"""
import argparse, os, re
from pathlib import Path
from datetime import datetime
def generate_inline_doc(filepath):
"""Add inline comments for complex logic"""
content = Path(filepath).read_text(encoding="utf-8", errors="ignore")
lines = content.split("\n")
new_lines = []
for i, line in enumerate(lines):
new_lines.append(line)
# Add comment before complex conditions
if re.search(r"if\s+.*\band\b.*\bor\b|if\s+.*>.*<|if\s+.*!\(", line) and "//" not in line and "#" not in line:
new_lines.insert(len(new_lines)-1, " // 复合条件判断 -> 考虑提取为有意义的布尔变量")
# Add comment on regex
if re.search(r"re\.(match|search|findall|sub)\(", line) and "//" not in line:
new_lines.insert(len(new_lines)-1, f" // 正则匹配: 考虑添加注释解释匹配规则")
return "\n".join(new_lines)
def generate_function_doc(filepath):
"""Generate function-level documentation"""
content = Path(filepath).read_text(encoding="utf-8", errors="ignore")
lines = content.split("\n")
ext = Path(filepath).suffix
result = []
in_func = False
func_name = ""
params = []
for i, line in enumerate(lines):
# Detect function/method definitions
m = re.match(r"(?:def|function|func|public|private|protected)\s+(\w+)\s*\(([^)]*)\)", line.strip())
if m:
func_name = m.group(1)
params = [p.strip() for p in m.group(2).split(",") if p.strip()]
result.append(f"\n### `{func_name}()`")
result.append("")
if params:
result.append("| 参数 | 说明 |")
result.append("|------|------|")
for p in params:
result.append(f"| `{p}` | TODO |")
result.append("")
result.append("**功能**: TODO")
result.append("")
result.append("**返回值**: TODO")
result.append("")
return "\n".join(result)
def generate_readme(dirpath):
"""Generate README snippet"""
name = Path(dirpath).resolve().name
now = datetime.now().strftime("%Y-%m-%d")
files = list(Path(dirpath).rglob("*"))
exts = set(f.suffix for f in files if f.suffix)
lang_map = {".py":"Python", ".js":"JavaScript", ".ts":"TypeScript", ".tsx":"React+TS",
".jsx":"React", ".java":"Java", ".go":"Go", ".rs":"Rust", ".c":"C", ".cpp":"C++"}
langs = [lang_map.get(e, e) for e in exts if e in lang_map]
return f"""# {name}
> 最后更新: {now}
## 速览
- **语言**: {', '.join(langs) if langs else 'N/A'}
- **类型**: TODO (CLI / Web / Library / Mobile)
## 安装
```bash
# TODO: 安装步骤
```
## 使用
```bash
# TODO: 使用示例
```
## 结构
```
{name}/
├── src/ # 源代码
├── tests/ # 测试
└── README.md # 本文件
```
## 贡献
TODO: 贡献指南
"""
def main():
parser = argparse.ArgumentParser(description="DevFlow Documentation Generator")
parser.add_argument("--file")
parser.add_argument("--dir")
parser.add_argument("--mode", default="inline",
choices=["inline","function","readme","all"])
parser.add_argument("--output", help="输出文件路径")
args = parser.parse_args()
target = args.file or args.dir
if not target:
print("请指定 --file 或 --dir")
results = []
if args.mode in ("inline", "all"):
results.append("/* === 行内注释建议 === */")
if args.file:
results.append(f"文件: {args.file}")
results.append("(对复杂逻辑添加解释性注释)")
else:
results.append(f"目录: {args.dir}")
if args.mode in ("function", "all"):
results.append("\n/* === 函数文档 === */")
if args.file:
results.append(generate_function_doc(args.file))
else:
for root, dirs, files in os.walk(target):
dirs[:] = [d for d in dirs if d not in {"node_modules",".git","dist","build"}]
for f in files[:5]:
if f.endswith((".py",".js",".ts",".tsx",".java")):
results.append(generate_function_doc(os.path.join(root, f)))
if args.mode in ("readme", "all"):
results.append("\n/* === README 模板 === */")
results.append(generate_readme(target if not args.file else Path(args.file).parent))
output = "\n".join(results)
if args.output:
Path(args.output).write_text(output, encoding="utf-8")
print(f"已输出到 {args.output}")
else:
print(output)
if __name__ == "__main__":
main()