# -*- coding: cp1252 -*-
#{'b': ['4.0', '12.0'], 'font size': {'10': ['1.1'], '12': ['2.0', '7.0'], '8': ['6.0']}, 'i': ['16.39'], 'i0': ['18.0'], 'ul': [], 'ulnone': [], 'b0': ['6.0', '13.0']}
"""
<html>
<head>
<title>BlahBlah</title>
</head>
<body>
jo
<size 10>
Hi
</font>
jk
<center>
asdfsa
</center>
asd
</body>
</html>
"""
def html2widget(text):
text = text.split('<body>')[-1].split('</body>')[0]
widget = {'b':[], 'b0':[],'i':[], 'i0':[], 'font size':{}, 'ul':[], 'ulnone':[]}
loc = [1,0]
temp = ''
type = ''
new_text = ''
for i in text:
if temp == '<':
if i == '>':
print type
type = type.split(' ')
if type[0] == 'size':
if type[1] in widget['font size'].keys():
widget['font size'][type[1]].append('%s.%s' % (loc[0],loc[1]))
else:
widget['font size'][type[1]] = ['%s.%s' % (loc[0],loc[1])]
else:
widget[type[0]].append('%s.%s' % (loc[0],loc[1]))
temp = ''
type = ''
else:
type += i
elif i == '<':
temp = i
elif i == '\n':
loc[0] += 1
loc[1] = 0
new_text += i
else:
loc[1] += 1
new_text += i
return new_text, widget
def display(text, widget):
import Tkinter
import threading
import time
face = 'Courier '
start_size = '12'
win = Tkinter.Tk()
ed = Tkinter.Text(win)
ed.grid(row=1,column=1)
threading.Thread(None, win.mainloop, None).start()
ed.insert(0.0, text)
time.sleep(1)
ed.tag_add(face+start_size, '0.0', 'end')
ed.tag_configure(face+start_size, font=face+start_size)
time.sleep(1)
#Size
print 'Size'
locs = {}
for size in widget['font size']:
for i in widget['font size'][size]:
locs[i] = size
klocs = locs.keys()
klocs.sort()
for loc in klocs:
ed.tag_add('sel', loc, 'end')
ed.focus_set()
time.sleep(.5)
ed.tag_add(face+locs[loc], loc, 'end')
ed.tag_configure(face+locs[loc], font=face+locs[loc])
#Bold
print 'Bold'
index = 0
for start in widget['b']:
end = widget['b0'][index]
utag = ed.tag_names(start)[-1]
cur_tag = utag+' bold'
print cur_tag
start_loc = start.split('.')
start_loc[0] = int(start_loc[0])
start_loc[1] = int(start_loc[1])
loc = [0,0]
for i in text:
if loc[0] == start_loc[0] and loc[1] == start_loc[1]:
start_loc = [-1,-1]
if start_loc == [-1,-1]:
print loc[0], loc[1]
print ed.tag_names('%s.%s' % (loc[0], loc[1]))
if ed.tag_names('%s.%s' % (loc[0], loc[1]))[-1] != utag:
ed.tag_add('sel', start, '%s.%s' % (loc[0], loc[1]))
ed.focus_set()
time.sleep(.5)
ed.tag_add(cur_tag, start, '%s.%s' % (loc[0], loc[1]))
ed.tag_configure(cur_tag, font=cur_tag)
start = '%s.%s' % (loc[0], loc[1])
elif '%s.%s' % (loc[0], loc[1]) == end:
ed.tag_add('sel', start, '%s.%s' % (loc[0], loc[1]))
ed.focus_set()
time.sleep(.5)
ed.tag_add(cur_tag, start, '%s.%s' % (loc[0], loc[1]))
ed.tag_configure(cur_tag, font=cur_tag)
break
if i == '\n':
loc[0] += 1
loc[1] = 0
else:
loc[1] += 1
index += 1
##
##
##
## if i in locs.keys():
## locs[i][1] += 'bold'
## else:
## locs[i] = ['0',' bold']
##
## klocs = locs.keys()
## klocs.sort()
##
## temp = ''
## for item in klocs:
## if item == i:
## locs[i][0] = locs[temp][0]
## break
## temp = i
##
## i = widget['b0']
## if i in locs.keys():
## locs[i][1] += 'bold'
## else:
## locs[i] = ['0',' bold']
##
## klocs = locs.keys()
## klocs.sort()
##
## temp = ''
## for item in klocs:
## if item == i:
## locs[i][0] = locs[temp][0]
## break
## temp = i
##
## klocs = locs.keys()
## klocs.sort()
##
## print klocs
## print
## print locs
##
## for loc in klocs:
## ed.tag_add(face+''.join(locs[loc]), loc, 'end')
## ed.tag_configure(face+''.join(locs[loc]), font=face+''.join(locs[loc]))
print 'Done'
a ="""HI
<b>sdfs<b0>sd
ds<i>sdf<i0>
ad<b>fs<b0>d"""
s,d = html2widget(a)
display(s,d)