#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""补追加 VD 集合（修复上一轮 VD 追加失败的 bug）"""

import os

WORKDIR = r"D:\work buddy\2026-11-05-14-15-52"
html_path = os.path.join(WORKDIR, "index_main.html")

NEW_VD_URLS = [
    "details/duke-qfe.html",
    "details/up-ee-mse.html",
    "details/jhu-bioinfo-ms.html",
    "details/up-bmi-ms.html",
    "details/jhu-finecon.html",
]

with open(html_path, "r", encoding="utf-8") as f:
    content = f.read()

# 找到 VD 集合
vd_start = content.index("const VERIFIED_DETAILS=new Set([") + len("const VERIFIED_DETAILS=new Set(")
depth = 0
j = vd_start
while j < len(content):
    ch = content[j]
    if ch == "[":
        depth += 1
    elif ch == "]":
        depth -= 1
        if depth == 0:
            break
    j += 1

# 从 ] 往前找最后一个有意义的字符
search_pos = j - 1
while search_pos > vd_start and content[search_pos] in ' \t\n\r':
    search_pos -= 1

last_char = content[search_pos]
print(f"VD 集合最后一个非空白字符: {repr(last_char)} (位置 {search_pos})")

if last_char == '"':
    # 最后是引号，直接在后面加逗号+新条目
    insert_text = "," + ",".join(f'"{u}"' for u in NEW_VD_URLS)
    new_content = content[:search_pos+1] + insert_text + content[search_pos+1:]
    print("模式：最后是引号，直接追加")
elif last_char == ',':
    # 最后是逗号，直接在逗号后面加新条目
    insert_text = ",".join(f'"{u}"' for u in NEW_VD_URLS)
    new_content = content[:search_pos+1] + insert_text + content[search_pos+1:]
    print("模式：最后是逗号，逗号后追加")
else:
    print(f"警告：未知结尾字符 {repr(last_char)}，不追加")
    exit(1)

with open(html_path, "w", encoding="utf-8") as f:
    f.write(new_content)
print(f"[DONE] VD 追加完成，文件大小: {len(new_content)} chars")
