# -*- coding: utf-8 -*- import json from pathlib import Path s = Path("apps/miniprogram/weapp/src/data/nomadCodeBook.ts").read_text(encoding="utf-8") key = "chapters: " i = s.index(key) j = s.index("[", i) depth = 0 end = -1 in_str = False esc = False quote = "" for k in range(j, len(s)): c = s[k] if in_str: if esc: esc = False elif c == "\\": esc = True elif c == quote: in_str = False continue if c in "\"'": in_str = True quote = c continue if c == "[": depth += 1 elif c == "]": depth -= 1 if depth == 0: end = k break try: arr = json.loads(s[j : end + 1]) print("chapters", len(arr), "first", arr[0]["title"], "bodyLen", len(arr[0].get("content") or "")) print("last", arr[-1]["title"]) except Exception as e: print("FAIL", e) print(s[j : j + 120])