from writer import process_file import json import sys sys.dont_write_bytecode = True # 读取 JSON 文件 def read_json(file_path): with open(file_path, 'r', encoding='utf-8') as file: return json.load(file) # 根据 relatedscripts 的 ID 找到对应的脚本 def find_script_by_greasyfork_id(scripts, greasyfork_id): for script in scripts: if str(script.get('GreasyFork')) == str(greasyfork_id): return script return None # 生成描述信息,仅针对当前脚本的 relatedscripts def generate_description(all_scripts, single_group=False): related_scripts_map = {} # single_group为True时,脚本归入“所有脚本”组,不进行分类 if single_group: related_scripts_map['所有脚本'] = all_scripts else: for script in all_scripts: relatedscript = script.get('relatedscripts') if relatedscript not in related_scripts_map: related_scripts_map[relatedscript] = [] related_scripts_map[relatedscript].append(script) return related_scripts_map # 生成 HTML 表格 def generate_html_table(scripts): html_table = '''''' for script in scripts: full_path = script.get("backuppath") + "/" + script.get("path") placeholder_image = "https://github.com/ChinaGodMan/UserScriptsHistory/raw/main/20241026_03380458.png" img_tag = f'' if script.get("img") else f'' html_table += f'''

{script.get("name")} 

{script.get("description")}
{img_tag}
Greasemonkey / 自述文件 / 讨论
''' return html_table # 生成分组后的HTML def generate_grouped_html(related_scripts_map, use_details=True, center=False): html_output = "" center_o = '' center_c = '' # 居中显示 if center: center_o = '
' center_c = '
' for related_id, scripts in related_scripts_map.items(): # 分组下的内容收缩 if use_details: html_output += f'{center_o}
{related_id}' else: html_output += f"

{related_id}

" html_output += generate_html_table(scripts) if use_details: print(center_c) html_output += f"{center_c}
" return html_output json_file_path = 'docs/ScriptsPath.json' data = read_json(json_file_path) # 按 relatedscripts 分类脚本 related_scripts_map = generate_description(data.get('scripts', [])) html_output = generate_grouped_html(related_scripts_map, False) # 读取 README.md 文件并替换表格 readme_path = 'docs/README.md' process_file(readme_path, html_output, "", "", "head")