forked from ChinaGodMan/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_shields.py
More file actions
58 lines (50 loc) · 2.03 KB
/
Copy pathupdate_shields.py
File metadata and controls
58 lines (50 loc) · 2.03 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
import os
from content_snippet import get_file_description
from helper import (
extract_lang_code,
format_str,
get_md_files,
get_repo_name,
read_json
)
from writer import process_file
NEW_CONTENT_PATH = 'utils/templates/SHIELDS.md'
def get_new_content(script_directory, lang_code=""):
with open(NEW_CONTENT_PATH, 'r', encoding='utf-8') as file:
content = file.read()
new_content = format_str(content, lang_code)
# 判断目录下的passed.json
if os.path.exists(script_directory + "/docs/passed.json"):
URL = f"https://github.com/{get_repo_name()}/raw/main/{script_directory}/docs/passed.json"
else:
URL = f"https://github.com/{get_repo_name()}/raw/main/docs/passed.json"
new_content = new_content.format(
PASSED_URL=URL
)
return new_content
def main():
json_path = 'docs/ScriptsPath.json'
data = read_json(json_path)
scripts = data.get('scripts', [])
start_tag = "<!--SHIELDS-->"
end_tag = "<!--SHIELDS-END-->"
for script in scripts:
script_directory = script.get('directory', '')
cnfile_path = os.path.join(script_directory, "README.md")
olddescriptions = get_file_description(cnfile_path, start_tag, end_tag)
# 构建中文徽章信息比对olddescriptions
new_content = get_new_content(script_directory)
if olddescriptions + "\n" == new_content: # 换行符添加上,就这样了能用就行
continue
else:
print(f"----\033[94m[{script.get('name', '')}]\033[0m\033[92m 内容变化,执行替换\033[0m")
md_files = get_md_files(script_directory)
for md_file in md_files:
lang = extract_lang_code(md_file)
if lang is None:
lang = ""
file_path = os.path.join(script_directory, md_file)
new_content = get_new_content(script_directory, lang)
process_file(file_path, new_content, start_tag, end_tag, "head")
if __name__ == '__main__':
main()