from pathlib import Path

HARDWARE_INDEX = Path(__file__).parent.parent / "hardware/index.md"
HARDWARE_DIR = Path(__file__).parent.parent / "hardware"

def generate_hardware_index():
    boards = []
    for board_dir in HARDWARE_DIR.iterdir():
        if board_dir.is_dir() and (board_dir / "index.md").exists():
            # 关键修改：使用标准Sphinx路径格式
            boards.append(f"{board_dir.name}/index")  # 移除了./和空格
    
    toctree_content = (
        "# 硬件文档中心\n\n"
        "```{toctree}\n"
        ":maxdepth: 2\n"
        ":caption: 板卡列表\n"
        ":glob:\n\n" +  # 添加glob指令增强容错性
        "\n".join(sorted(boards)) + "\n```")
    
    HARDWARE_INDEX.write_text(toctree_content, encoding="utf-8")

if __name__ == "__main__":
    generate_hardware_index()