# -*- coding: utf-8 -*-
import io, re
SRC = 'index_main.html'
txt = io.open(SRC, encoding='utf-8').read()
new = 'details/up-ese-mse.html'
i = txt.find('const VERIFIED_DETAILS=new Set([')
j = txt.find(']);', i)
body = txt[i:j]
items = re.findall(r'"([^"]+)"', body)
if new in items:
    print('already in VD set', len(items))
else:
    pos = len(items)
    for k, it in enumerate(items):
        if it > new:
            pos = k
            break
    lit = '"' + items[pos] + '"' if pos < len(items) else None
    if lit and lit in body:
        idx = body.find(lit)
        body = body[:idx] + '"' + new + '",' + body[idx:]
    else:
        body = body.rstrip().rstrip(',') + ',"' + new + '"'
    txt = txt[:i] + body + txt[j:]
    io.open(SRC, 'w', encoding='utf-8').write(txt)
    print('inserted at', pos, 'before', items[pos] if pos < len(items) else 'END', 'count', len(items) + 1)
