# -*- coding: utf-8 -*-
# 扫描 ONEBOT 手册 PDF: 定位每个系列的章节页范围 + 参数/尺寸关键词页
# 用法: python tools\scan-manual-pages.py
import fitz, os, re, sys

pdf = r"OnebotCatalog\ONEBOT - 直线气缸 - 中文.pdf"
doc = fitz.open(pdf)
DIGITS = {0xF6B1 + i: str(i) for i in range(10)}

def dec(text):
    return "".join(DIGITS.get(ord(ch), ch) for ch in text)

# 系列代码 → 显示名 (来自 build-onebot-catalog.ps1)
SERIES = ["KC", "KCB", "KSB", "KCS", "KS", "KSU", "M", "MS", "MSC", "MAL", "MALC"]
KEYWORDS = ["订购", "规格", "参数", "外形尺寸", "符号", "特性", "理论出力", "重量", "结构"]

print("总页数:", doc.page_count)
pages = []
for i in range(doc.page_count):
    t = dec(doc[i].get_text())
    pages.append(t)

# 1) 每页出现的系列码标题 (页首大字号? 简化: 整页文本匹配)
hits = {}
for i, t in enumerate(pages):
    found = set()
    for s in SERIES:
        # 系列标题一般单独成行, 如 "KC 系列" / "KC系列"
        if re.search(r"(?m)^\s*" + s + r"\s*系列", t) or re.search(r"(?m)^\s*" + s + r"系列", t):
            found.add(s)
    if found:
        hits[i + 1] = sorted(found)
        kw = [k for k in KEYWORDS if k in t]
        print(f"p{i+1:3d} 系列: {found}  关键词: {kw[:4]}")

print("\n--- 每页系列线索 (含页码的系列码出现) ---")
for i, t in enumerate(pages):
    for s in SERIES:
        if re.search(r"(?m)^\s*" + s + r"\s*$", t):  # 整页只有系列码的目录页可能
            print(f"p{i+1:3d} 独立行: {s}")
