#!/usr/bin/env python3
"""DMark: cari di mana warna oranye (ast-global-color-0) disimpan untuk angka 02-05."""
import json, re

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
raw = open(f"{WS}/drafts/dmark_beranda_backup2.txt").read()


def blok_json(teks, mulai):
    if teks[mulai] != "{":
        return None, mulai
    dalam, lepas, level, i = False, False, 0, mulai
    while i < len(teks):
        ch = teks[i]
        if dalam:
            if lepas:
                lepas = False
            elif ch == "\\":
                lepas = True
            elif ch == '"':
                dalam = False
        else:
            if ch == '"':
                dalam = True
            elif ch == "{":
                level += 1
            elif ch == "}":
                level -= 1
                if level == 0:
                    return teks[mulai:i + 1], i + 1
        i += 1
    return None, i


print("=== semua blok yang memuat 'ast-global-color-0' ===")
pos, n = 0, 0
while True:
    k = raw.find("<!-- wp:spectra/", pos)
    if k == -1:
        break
    awal = raw.find("{", k)
    potongan, akhir = blok_json(raw, awal)
    pos = akhir
    if not potongan:
        continue
    if "ast-global-color-0" not in potongan:
        continue
    n += 1
    jenis = re.search(r"wp:spectra/([a-z\-]+)", raw[k:k + 40])
    try:
        attr = json.loads(potongan)
    except Exception:
        attr = {}
    teks = re.sub(r"\s+", " ", re.sub("<[^>]+>", " ", str(attr.get("text") or "")))[:36]
    print(f"\n   {n}. blok {jenis.group(1) if jenis else '?'} | teks: «{teks}» | tagName: {attr.get('tagName')}")
    # tampilkan bagian yang memuat oranye
    for m in re.finditer(r".{0,90}ast-global-color-0.{0,60}", potongan):
        print(f"        …{m.group(0)}…")
print(f"\ntotal blok memuat oranye: {n}")
