
import string
from unidecode import unidecode
import re
from bs4 import BeautifulSoup

'''
┳
'''


with open('dc.htm', encoding='iso-8859-1', mode='r') as frd:
    dc = frd.read()

dc = re.sub(r'(?<=\n\n  )(<p><b>(?!The )|<div class="fig)', r'┳<entry>\1', dc)

dc = re.sub(r'(<p><br |<div style="clear: both">|<pre>)', r'┳\1', dc)

dc = re.sub(r'(<entry>[^┳]*)(?=\n\n)', r'\1</entry>┳', dc)

dc = dc.replace('\n', '').replace('┳', '\n')

dc = re.sub(r' {2,}', r' ', dc)


with open('ndc.htm', 'w') as fwn:
    fwn.write(dc)

with open('ndc.htm', 'r') as frn:
    lines = [line.strip() for line in frn if line.strip().startswith('<entry>')]

for line in lines:
    soup = BeautifulSoup(line, 'html5lib')
    se = soup.entry

    hws = list()
    dfl = se.select_one('entry > div.figleft:first-child')

    if dfl:
        hw = dfl.img['alt'].strip()
        if hw in string.ascii_uppercase:
            hws.append(hw)

    pb = se.select_one('entry > p:first-of-type > b:first-child')

    if pb:
        hw = pb.get_text().strip()
        if hw and hw not in hws:
            hws.append(hw)

    dc = str(se)
    CSS = '<link href="ctcd.css" rel="stylesheet">'

    for hw in hws:
        with open('dict.txt', 'a') as fad:
            fad.write(f'{hw}\n{CSS}{dc}\n</>\n')
        print(hw)









