Repository navigation
Expand file tree
/
Copy pathhtmlrichtextdisplayer.py
More file actions
156 lines (133 loc) · 4.41 KB
/
Copy pathhtmlrichtextdisplayer.py
File metadata and controls
156 lines (133 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python
import wx
import wx.richtext as rt
from basicdisplayer import BasicDisplayer
class DocEntryElement:
data = ""
lastPosition = 0
begin = False
titleType = ""
class HTMLRichTextDisplayer(BasicDisplayer):
"""
This displayer works a little like render, the different is the render works while
the html is parsing. So here only a little html and css will be recognized and utilized
in this displayer.
"""
def __init__(self, rc, css):
if rc:
self.rc = rc
self.attrs = {}
self.tag = False
self.css = css
self.urlContinure = 0
# docEntryList record all headers in html.
self.docEntryList = []
self.docEntryElement = DocEntryElement()
self.docEntryElement.data = ""
self.docEntryElement.lastPosition = 0
self.docEntryElement.begin = False
self.docEntryElement.titleType = ""
# dictContent record all self or outer links.
self.dictContent = {}
def displaybegin(self, tag, attrs):
if tag == 'link' or tag == 'meta' or tag == 'style' or tag == 'script':
self.tag = True
self.attrs = {}
else:
self.tag = tag
self.attrs[tag] = attrs
def displayend(self, tag):
if tag == 'a' and 'a' in self.attrs and 'href' in self.attrs['a']:
# Add handler for an url
self.rc.Bind(wx.EVT_TEXT_URL, self.OnURL)
self.urlContinure = 0
if tag == 'span' and self.docEntryElement.begin == True:
# Record catalog and reset catalog flags.
self.docEntryList.append({self.docEntryElement.titleType:[self.docEntryElement.lastPosition, self.docEntryElement.data]})
self.docEntryElement.data = ""
self.docEntryElement.lastPosition = 0
self.docEntryElement.begin = False
self.docEntryElement.titleType = ""
del self.attrs[tag]
self.tag = False
def displaydata(self, data, type):
selfLinkUrl = ""
size = ""
weight = ""
family = ""
style = rt.RichTextAttr()
if self.tag == True or type != 0:
# Do nothing for these tags.
return
if 'a' in self.attrs and 'href' in self.attrs['a'] and self.urlContinure == 0:
# Add to dict for content search.
selfLinkUrl = self.attrs['a']['href']
self.urlContinure = 1
if 'span' in self.attrs and 'class' in self.attrs['span'] and self.attrs['span']['class'][0] == 'h':
# Select headers from html and record them.
self.docEntryElement.lastPosition = self.rc.GetLastPosition()
self.docEntryElement.begin = True
self.docEntryElement.titleType = self.attrs['span']['class']
self.rc.SetInsertionPoint(self.rc.GetLastPosition())
if 'span' in self.attrs and 'class' in self.attrs['span']:
import re
l = re.split(' ', self.attrs['span']['class'])
for selector in l:
if selector in self.css:
for decls in self.css[selector]:
if 'font-size' in decls:
size = decls['font-size']
if 'font-weight' in decls:
weight = decls['font-weight']
if 'font-family' in decls:
family = decls['font-family']
if self.docEntryElement.begin:
self.docEntryElement.data += data + " "
self.dictContent[selfLinkUrl] = self.rc.GetLastPosition()
if family != "":
# TODO: parse family string.
style.SetFontFaceName(family)
if size != "":
# TODO: parse font size.
if size == '1em':
pass#style.SetFontSize(wx.FONTSIZE_MEDIUM)
if weight != "":
# TODO: parse font weight.
if weight == 'bold':
style.SetFontWeight(wx.FONTWEIGHT_BOLD)
elif weight == 'light':
style.SetFontWeight(wx.FONTWEIGHT_LIGHT)
else:
# Reset font weight back to normal.
style.SetFontWeight(wx.FONTWEIGHT_NORMAL)
if selfLinkUrl != "":
# Set url style
style.SetFontUnderlined(True)
style.SetTextColour(wx.BLUE)
else:
# Reset url style.
style.SetFontUnderlined(False)
style.SetTextColour(wx.BLACK)
self.rc.BeginStyle(style)
if selfLinkUrl != "":
self.rc.BeginURL(selfLinkUrl)
self.rc.WriteText(data)
if selfLinkUrl != "":
self.rc.EndURL()
self.rc.EndStyle()
def OnURL(self, event):
"""
Seek the url if it is in current html.
Parameter event contains the url name, which
is needed by searching process.
"""
key = event.GetString()
if key in self.dictContent:
# In order to keep found header showing at top
# here first seek to the end of file then seek
# back to the header
self.rc.ShowPosition(self.rc.GetLastPosition())
self.rc.ShowPosition(self.dictContent.get(key))
def SetPosition(self, pos):
self.rc.ShowPosition(self.rc.GetLastPosition())
self.rc.ShowPosition(pos)