-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_index.py
More file actions
70 lines (49 loc) · 1.72 KB
/
generate_index.py
File metadata and controls
70 lines (49 loc) · 1.72 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
import os
import re
NAME_RE = re.compile(r"^\s*//\s*@name\s+(.*)\s*$")
DESC_RE = re.compile(r"^\s*//\s*@description\s+(.*)\s*$")
def parse_userscript_meta(filepath: str):
"""
从 Userscript 头部提取 @name 和 @description
"""
name = None
desc = None
in_block = False
with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
for line in f:
if "// ==UserScript==" in line:
in_block = True
continue
if "// ==/UserScript==" in line:
break
if not in_block:
continue
m1 = NAME_RE.match(line)
if m1 and not name:
name = m1.group(1).strip()
m2 = DESC_RE.match(line)
if m2 and not desc:
desc = m2.group(1).strip()
if name and desc:
break
return name, desc
def generate_markdown_list(root_dir="."):
items = []
for dirpath, _, filenames in os.walk(root_dir):
for filename in filenames:
if filename.endswith("user.js"):
full_path = os.path.join(dirpath, filename)
rel_path = os.path.relpath(full_path, root_dir).replace("\\", "/")
name, desc = parse_userscript_meta(full_path)
if not name:
name = os.path.splitext(filename)[0]
if not desc:
desc = ""
text = f"{name} - {desc}".strip(" -")
items.append(f"- [{text}]({rel_path})")
return "\n".join(sorted(items))
f = open("README.md", "w", encoding="utf-8")
f.write("# eWloYW8's Userscript Collection\n\n")
f.write(generate_markdown_list())
f.write("\n")
f.close()