-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
239 lines (212 loc) · 6.67 KB
/
Copy pathindex.py
File metadata and controls
239 lines (212 loc) · 6.67 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python3
"""
生成报告索引页面
用法:
python index.py # 扫描 archive/ 生成 index.html
python index.py -o out.html # 指定输出文件名
"""
import sys
import os
from pathlib import Path
from datetime import datetime
def scan_archive(archive_dir: Path) -> list:
"""扫描 archive 目录,返回报告列表"""
reports = []
if not archive_dir.exists():
return reports
for html_file in sorted(archive_dir.glob('*.html'), key=os.path.getmtime, reverse=True):
# 从文件名提取信息
name = html_file.stem
mtime = datetime.fromtimestamp(html_file.stat().st_mtime)
size = html_file.stat().st_size
# 尝试提取标题(读取 HTML 文件的第一行)
title = name
try:
content = html_file.read_text(encoding='utf-8', errors='ignore')
# 提取 <title> 标签
import re
title_match = re.search(r'<title>(.*?)</title>', content)
if title_match:
title = title_match.group(1)
# 或者提取第一个 <h1>
h1_match = re.search(r'<h1>(.*?)</h1>', content)
if h1_match:
title = h1_match.group(1)
except:
pass
reports.append({
'name': name,
'title': title,
'filename': html_file.name,
'mtime': mtime,
'size': size
})
return reports
def generate_index_html(reports: list) -> str:
"""生成索引页面 HTML"""
# 生成报告列表 HTML
report_items = ''
for r in reports:
date_str = r['mtime'].strftime('%Y-%m-%d %H:%M')
size_str = f"{r['size']:,} 字节"
report_items += f'''
<tr>
<td><a href="archive/{r['filename']}">{r['title']}</a></td>
<td>{date_str}</td>
<td>{size_str}</td>
<td><a href="archive/{r['filename']}" class="btn">打开</a></td>
</tr>
'''
if not report_items:
report_items = '''
<tr>
<td colspan="4" style="text-align: center; color: #999; padding: 40px;">
暂无报告。使用 <code>python render.py input.md</code> 生成报告。
</td>
</tr>
'''
return f'''<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>报告索引</title>
<style>
* {{ box-sizing: border-box; margin: 0; padding: 0; }}
body {{
font-family: -apple-system, "Microsoft YaHei", "PingFang SC", sans-serif;
background: #f5f5f5;
color: #333;
line-height: 1.6;
}}
.container {{
max-width: 1000px;
margin: 0 auto;
padding: 40px 20px;
}}
h1 {{
color: #1a1a1a;
margin-bottom: 8px;
}}
.subtitle {{
color: #666;
margin-bottom: 32px;
}}
.actions {{
margin-bottom: 24px;
display: flex;
gap: 12px;
}}
.btn {{
display: inline-block;
padding: 8px 16px;
background: #4a90d9;
color: white;
text-decoration: none;
border-radius: 4px;
font-size: 14px;
}}
.btn:hover {{ background: #357abd; }}
.btn.secondary {{
background: #f5f5f5;
color: #555;
border: 1px solid #ddd;
}}
.btn.secondary:hover {{ background: #e8e8e8; }}
table {{
width: 100%;
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}}
th, td {{
padding: 12px 16px;
text-align: left;
border-bottom: 1px solid #eee;
}}
th {{
background: #f8f9fa;
font-weight: 600;
color: #555;
}}
tr:hover {{ background: #f8f9fa; }}
a {{ color: #4a90d9; text-decoration: none; }}
a:hover {{ text-decoration: underline; }}
.empty {{
text-align: center;
padding: 60px 20px;
color: #999;
}}
code {{
background: #f0f0f0;
padding: 2px 6px;
border-radius: 3px;
font-size: 13px;
}}
footer {{
margin-top: 40px;
padding-top: 20px;
border-top: 1px solid #eee;
color: #999;
font-size: 13px;
text-align: center;
}}
</style>
</head>
<body>
<div class="container">
<h1>📄 报告索引</h1>
<p class="subtitle">共 {len(reports)} 份报告</p>
<div class="actions">
<a href="interactive.html" class="btn">📝 打开 MD 编辑器</a>
<button class="btn secondary" onclick="location.reload()">🔄 刷新列表</button>
</div>
<table>
<thead>
<tr>
<th>标题</th>
<th>生成时间</th>
<th>大小</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{report_items}
</tbody>
</table>
<footer>
MD → HTML 交互式报告工具 | <a href="https://github.com">GitHub</a>
</footer>
</div>
</body>
</html>'''
def main():
import argparse
parser = argparse.ArgumentParser(description='生成报告索引页面')
parser.add_argument('-o', '--output', default='index.html',
help='输出文件名(默认:index.html)')
args = parser.parse_args()
script_dir = Path(__file__).parent
archive_dir = script_dir / 'archive'
# 扫描报告
print(f'📁 扫描 archive/ 目录...')
reports = scan_archive(archive_dir)
print(f' 找到 {len(reports)} 份报告')
# 生成索引页面
html = generate_index_html(reports)
# 写入文件
output_path = script_dir / args.output
output_path.write_text(html, encoding='utf-8')
print(f'✅ 已生成索引页面: {output_path}')
# 打开浏览器
if sys.platform == 'win32':
os.startfile(str(output_path))
elif sys.platform == 'darwin':
import subprocess
subprocess.run(['open', str(output_path)])
else:
import subprocess
subprocess.run(['xdg-open', str(output_path)])
if __name__ == '__main__':
main()