tkhtmlview-gfm 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tkhtmlview/__init__.py +134 -0
- tkhtmlview/html_parser.py +829 -0
- tkhtmlview/utils.py +19 -0
- tkhtmlview_gfm-0.1.0.dist-info/METADATA +164 -0
- tkhtmlview_gfm-0.1.0.dist-info/RECORD +8 -0
- tkhtmlview_gfm-0.1.0.dist-info/WHEEL +5 -0
- tkhtmlview_gfm-0.1.0.dist-info/licenses/LICENSE +23 -0
- tkhtmlview_gfm-0.1.0.dist-info/top_level.txt +1 -0
tkhtmlview/__init__.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""
|
|
2
|
+
tkinter HTML text widgets
|
|
3
|
+
"""
|
|
4
|
+
import sys
|
|
5
|
+
import tkinter as tk
|
|
6
|
+
from tkhtmlview import html_parser
|
|
7
|
+
from tkhtmlview.utils import RenderHTML
|
|
8
|
+
|
|
9
|
+
VERSION = "0.1.0"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class _ScrolledText(tk.Text):
|
|
13
|
+
def __init__(self, master=None, **kw):
|
|
14
|
+
self.frame = tk.Frame(master)
|
|
15
|
+
|
|
16
|
+
self.vbar = tk.Scrollbar(self.frame)
|
|
17
|
+
self.xscroll = tk.Scrollbar(self.frame, orient="horizontal")
|
|
18
|
+
|
|
19
|
+
if "xscroll" in kw:
|
|
20
|
+
# self.vbar.orient = "vertical"
|
|
21
|
+
# print("vs")
|
|
22
|
+
kw["xscrollcommand"] = self.xscroll.set
|
|
23
|
+
self.xscroll.pack(side=tk.BOTTOM, fill="x")
|
|
24
|
+
self.xscroll["command"] = self.xview
|
|
25
|
+
del kw["xscroll"]
|
|
26
|
+
|
|
27
|
+
kw["yscrollcommand"] = self.vbar.set
|
|
28
|
+
self.vbar.pack(side=tk.RIGHT, fill=tk.Y)
|
|
29
|
+
self.vbar["command"] = self.yview
|
|
30
|
+
|
|
31
|
+
tk.Text.__init__(self, self.frame, **kw)
|
|
32
|
+
self.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
|
|
33
|
+
|
|
34
|
+
text_meths = vars(tk.Text).keys()
|
|
35
|
+
methods = vars(tk.Pack).keys() | vars(tk.Grid).keys() | vars(tk.Place).keys()
|
|
36
|
+
methods = methods.difference(text_meths)
|
|
37
|
+
|
|
38
|
+
for m in methods:
|
|
39
|
+
if m[0] != "_" and m != "config" and m != "configure":
|
|
40
|
+
setattr(self, m, getattr(self.frame, m))
|
|
41
|
+
|
|
42
|
+
def __str__(self):
|
|
43
|
+
return str(self.frame)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class HTMLScrolledText(_ScrolledText):
|
|
47
|
+
|
|
48
|
+
"""
|
|
49
|
+
HTML scrolled text widget
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
def __init__(self, *args, html=None, **kwargs):
|
|
53
|
+
super().__init__(*args, **kwargs)
|
|
54
|
+
self._w_init(kwargs)
|
|
55
|
+
self.html_parser = html_parser.HTMLTextParser()
|
|
56
|
+
if isinstance(html, str):
|
|
57
|
+
self.set_html(html)
|
|
58
|
+
elif isinstance(html, RenderHTML):
|
|
59
|
+
self.set_html(html.get_html())
|
|
60
|
+
|
|
61
|
+
def _w_init(self, kwargs):
|
|
62
|
+
if "wrap" not in kwargs.keys():
|
|
63
|
+
self.config(wrap="word")
|
|
64
|
+
if "background" not in kwargs.keys():
|
|
65
|
+
if sys.platform.startswith("win"):
|
|
66
|
+
self.config(background="SystemWindow")
|
|
67
|
+
else:
|
|
68
|
+
self.config(background="white")
|
|
69
|
+
|
|
70
|
+
def fit_height(self):
|
|
71
|
+
"""
|
|
72
|
+
Fit widget height to wrapped lines
|
|
73
|
+
"""
|
|
74
|
+
for h in range(1, 4):
|
|
75
|
+
self.config(height=h)
|
|
76
|
+
self.master.update()
|
|
77
|
+
if self.yview()[1] >= 1:
|
|
78
|
+
break
|
|
79
|
+
else:
|
|
80
|
+
self.config(height=0.5 + 3 / self.yview()[1])
|
|
81
|
+
|
|
82
|
+
def set_html(self, html, strip=True):
|
|
83
|
+
"""
|
|
84
|
+
Set HTML widget text. If strip is enabled (default) it ignores spaces and new lines.
|
|
85
|
+
"""
|
|
86
|
+
prev_state = self.cget("state")
|
|
87
|
+
self.config(state=tk.NORMAL)
|
|
88
|
+
self.delete("1.0", tk.END)
|
|
89
|
+
for tag in self.tag_names():
|
|
90
|
+
self.tag_delete(tag)
|
|
91
|
+
|
|
92
|
+
self.html_parser.w_set_html(self, html, strip=strip)
|
|
93
|
+
self.config(state=prev_state)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class HTMLText(HTMLScrolledText):
|
|
97
|
+
|
|
98
|
+
"""
|
|
99
|
+
HTML text widget
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
def _w_init(self, kwargs):
|
|
103
|
+
super()._w_init(kwargs)
|
|
104
|
+
self.vbar.pack_forget()
|
|
105
|
+
|
|
106
|
+
def fit_height(self):
|
|
107
|
+
super().fit_height()
|
|
108
|
+
# self.master.update()
|
|
109
|
+
self.vbar.pack_forget()
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class HTMLLabel(HTMLText):
|
|
113
|
+
|
|
114
|
+
"""
|
|
115
|
+
HTML label widget
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
def _w_init(self, kwargs):
|
|
119
|
+
super()._w_init(kwargs)
|
|
120
|
+
if "background" not in kwargs.keys():
|
|
121
|
+
if sys.platform.startswith("win"):
|
|
122
|
+
self.config(background="SystemButtonFace")
|
|
123
|
+
else:
|
|
124
|
+
self.config(background="#d9d9d9")
|
|
125
|
+
|
|
126
|
+
if "borderwidth" not in kwargs.keys():
|
|
127
|
+
self.config(borderwidth=0)
|
|
128
|
+
|
|
129
|
+
if "padx" not in kwargs.keys():
|
|
130
|
+
self.config(padx=3)
|
|
131
|
+
|
|
132
|
+
def set_html(self, *args, **kwargs):
|
|
133
|
+
super().set_html(*args, **kwargs)
|
|
134
|
+
self.config(state=tk.DISABLED)
|
|
@@ -0,0 +1,829 @@
|
|
|
1
|
+
"""
|
|
2
|
+
HTML parser
|
|
3
|
+
"""
|
|
4
|
+
import os
|
|
5
|
+
import webbrowser
|
|
6
|
+
import tkinter as tk
|
|
7
|
+
from tkinter import font
|
|
8
|
+
from copy import deepcopy
|
|
9
|
+
from PIL import Image, ImageTk
|
|
10
|
+
from html.parser import HTMLParser
|
|
11
|
+
from collections import OrderedDict
|
|
12
|
+
import requests
|
|
13
|
+
from io import BytesIO
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# __________________________________________________________________________________________________
|
|
17
|
+
class Defs:
|
|
18
|
+
DEFAULT_TEXT_FONT_FAMILY = ("Segoe ui", "Calibri", "Helvetica", "TkTextFont")
|
|
19
|
+
FONT_SIZE = 14
|
|
20
|
+
PREFORMATTED_FONT_FAMILY = ("Courier", "DejaVu Sans Mono", "TkFixedFont")
|
|
21
|
+
HEADINGS_FONT_SIZE = {
|
|
22
|
+
"h1": 32,
|
|
23
|
+
"h2": 24,
|
|
24
|
+
"h3": 18,
|
|
25
|
+
"h4": 16,
|
|
26
|
+
"h5": 13,
|
|
27
|
+
"h6": 10,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class HTML:
|
|
32
|
+
# ----------------------------------------------------------------------------------------------
|
|
33
|
+
"""
|
|
34
|
+
List of supported HTML tags and attrs
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
class Tag:
|
|
38
|
+
BR = "br"
|
|
39
|
+
UL = "ul"
|
|
40
|
+
OL = "ol"
|
|
41
|
+
LI = "li"
|
|
42
|
+
IMG = "img"
|
|
43
|
+
A = "a"
|
|
44
|
+
B = "b"
|
|
45
|
+
STRONG = "strong"
|
|
46
|
+
I = "i"
|
|
47
|
+
EM = "em"
|
|
48
|
+
U = "u"
|
|
49
|
+
MARK = "mark"
|
|
50
|
+
SPAN = "span"
|
|
51
|
+
DIV = "div"
|
|
52
|
+
P = "p"
|
|
53
|
+
PRE = "pre"
|
|
54
|
+
CODE = "code"
|
|
55
|
+
H1 = "h1"
|
|
56
|
+
H2 = "h2"
|
|
57
|
+
H3 = "h3"
|
|
58
|
+
H4 = "h4"
|
|
59
|
+
H5 = "h5"
|
|
60
|
+
H6 = "h6"
|
|
61
|
+
TABLE = "table"
|
|
62
|
+
TR = "tr"
|
|
63
|
+
TH = "th"
|
|
64
|
+
TD = "td"
|
|
65
|
+
DEL = "del" # GFM: del
|
|
66
|
+
S = "s" # GFM: s
|
|
67
|
+
BLOCKQUOTE = "blockquote" # GFM: blockquote
|
|
68
|
+
INPUT = "input" # GFM: input
|
|
69
|
+
HR = "hr" # GFM: hr
|
|
70
|
+
|
|
71
|
+
class Attrs:
|
|
72
|
+
STYLE = "style"
|
|
73
|
+
HREF = "href"
|
|
74
|
+
SRC = "src"
|
|
75
|
+
WIDTH = "width"
|
|
76
|
+
HEIGHT = "height"
|
|
77
|
+
TYPE = "type"
|
|
78
|
+
CHECKED = "checked" # GFM: checked
|
|
79
|
+
DISABLED = "disabled" # GFM: disabled
|
|
80
|
+
ALIGN = "align" # GFM: align
|
|
81
|
+
|
|
82
|
+
class TypeOrderedList:
|
|
83
|
+
_1 = "1"
|
|
84
|
+
a = "a"
|
|
85
|
+
A = "A"
|
|
86
|
+
|
|
87
|
+
class Style:
|
|
88
|
+
COLOR = "color"
|
|
89
|
+
BACKGROUND_COLOR = "background-color"
|
|
90
|
+
FONT_FAMILY = "font-family"
|
|
91
|
+
FONT_SIZE = "font-size"
|
|
92
|
+
TEXT_ALIGN = "text-align"
|
|
93
|
+
TEXT_DECORATION = "text-decoration"
|
|
94
|
+
FONT_WEIGHT = "font-weight" # ← "bold" → Fnt.WEIGHT
|
|
95
|
+
FONT_STYLE = "font-style" # ← "italic" → Fnt.SLANT
|
|
96
|
+
|
|
97
|
+
class StyleTextDecoration:
|
|
98
|
+
UNDERLINE = "underline"
|
|
99
|
+
LINE_THROUGH = "line-through"
|
|
100
|
+
|
|
101
|
+
HEADING_TAGS = (
|
|
102
|
+
Tag.H1,
|
|
103
|
+
Tag.H2,
|
|
104
|
+
Tag.H3,
|
|
105
|
+
Tag.H4,
|
|
106
|
+
Tag.H5,
|
|
107
|
+
Tag.H6,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
TEXT_ALIGN_TAGS = HEADING_TAGS + (
|
|
111
|
+
Tag.UL,
|
|
112
|
+
Tag.OL,
|
|
113
|
+
Tag.LI,
|
|
114
|
+
Tag.DIV,
|
|
115
|
+
Tag.P,
|
|
116
|
+
Tag.PRE,
|
|
117
|
+
Tag.CODE,
|
|
118
|
+
Tag.TD,
|
|
119
|
+
Tag.TH,
|
|
120
|
+
Tag.BLOCKQUOTE # GFM: blockquote
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
NEW_LINE_TAGS = HEADING_TAGS + (
|
|
124
|
+
Tag.UL,
|
|
125
|
+
Tag.OL,
|
|
126
|
+
Tag.DIV,
|
|
127
|
+
Tag.P,
|
|
128
|
+
Tag.PRE,
|
|
129
|
+
#Tag.CODE,
|
|
130
|
+
Tag.TABLE,
|
|
131
|
+
Tag.TR,
|
|
132
|
+
Tag.BLOCKQUOTE # GFM: blockquote
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
STYLE_TAGS = TEXT_ALIGN_TAGS + (
|
|
136
|
+
Tag.A,
|
|
137
|
+
Tag.B,
|
|
138
|
+
Tag.STRONG,
|
|
139
|
+
Tag.I,
|
|
140
|
+
Tag.EM,
|
|
141
|
+
Tag.U,
|
|
142
|
+
Tag.MARK,
|
|
143
|
+
Tag.SPAN,
|
|
144
|
+
Tag.TD,
|
|
145
|
+
Tag.TH,
|
|
146
|
+
Tag.DEL, # GFM: DEL
|
|
147
|
+
Tag.S, # GFM: S
|
|
148
|
+
Tag.INPUT # GFM: INPUT
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
# --------------------------------------------------------------------------------------------------
|
|
153
|
+
# Text widget defs
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class WCfg:
|
|
157
|
+
KEY = "config"
|
|
158
|
+
BACKGROUND = "background"
|
|
159
|
+
FOREGROUND = "foreground"
|
|
160
|
+
JUSTIFY = "justify"
|
|
161
|
+
TABS = "tabs"
|
|
162
|
+
LMARGIN1 = "lmargin1" # ← ADD
|
|
163
|
+
LMARGIN2 = "lmargin2" # ← ADD
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
class Fnt:
|
|
167
|
+
KEY = "font"
|
|
168
|
+
FAMILY = "family"
|
|
169
|
+
SIZE = "size"
|
|
170
|
+
WEIGHT = "weight"
|
|
171
|
+
SLANT = "slant"
|
|
172
|
+
UNDERLINE = "underline"
|
|
173
|
+
OVERSTRIKE = "overstrike"
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
class Bind:
|
|
177
|
+
KEY = "bind"
|
|
178
|
+
LINK = "link"
|
|
179
|
+
IMAGE = "image"
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
class WTag:
|
|
183
|
+
START_INDEX = "start_index"
|
|
184
|
+
END_INDEX = "end_index"
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
DEFAULT_STACK = {
|
|
188
|
+
WCfg.KEY: {
|
|
189
|
+
WCfg.BACKGROUND: [],
|
|
190
|
+
WCfg.FOREGROUND: [("__DEFAULT__", "black")],
|
|
191
|
+
WCfg.JUSTIFY: [("__DEFAULT__", "left")],
|
|
192
|
+
WCfg.TABS: [("__DEFAULT__", ())],
|
|
193
|
+
WCfg.LMARGIN1: [("__DEFAULT__", 0)], # ← ADD
|
|
194
|
+
WCfg.LMARGIN2: [("__DEFAULT__", 0)],
|
|
195
|
+
},
|
|
196
|
+
Fnt.KEY: {
|
|
197
|
+
Fnt.FAMILY: [],
|
|
198
|
+
Fnt.SIZE: [("__DEFAULT__", Defs.FONT_SIZE)],
|
|
199
|
+
Fnt.WEIGHT: [("__DEFAULT__", "normal")],
|
|
200
|
+
Fnt.SLANT: [("__DEFAULT__", "roman")],
|
|
201
|
+
Fnt.UNDERLINE: [("__DEFAULT__", False)],
|
|
202
|
+
Fnt.OVERSTRIKE: [("__DEFAULT__", False)],
|
|
203
|
+
},
|
|
204
|
+
Bind.KEY: {
|
|
205
|
+
Bind.LINK: [("__DEFAULT__", None)],
|
|
206
|
+
},
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
# __________________________________________________________________________________________________
|
|
211
|
+
# functions
|
|
212
|
+
def get_existing_font(font_families):
|
|
213
|
+
# ------------------------------------------------------------------------------------------
|
|
214
|
+
try:
|
|
215
|
+
return next(
|
|
216
|
+
filter(
|
|
217
|
+
lambda f: f.lower() in (f.lower() for f in font.families()),
|
|
218
|
+
font_families,
|
|
219
|
+
)
|
|
220
|
+
)
|
|
221
|
+
except Exception:
|
|
222
|
+
return "TkTextFont"
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
# __________________________________________________________________________________________________
|
|
226
|
+
# classes
|
|
227
|
+
class HLinkSlot:
|
|
228
|
+
# ----------------------------------------------------------------------------------------------
|
|
229
|
+
|
|
230
|
+
def __init__(self, w, tag_name, url):
|
|
231
|
+
# ------------------------------------------------------------------------------------------
|
|
232
|
+
self._w = w
|
|
233
|
+
self.tag_name = tag_name
|
|
234
|
+
self.URL = url
|
|
235
|
+
|
|
236
|
+
def call(self, event):
|
|
237
|
+
# ------------------------------------------------------------------------------------------
|
|
238
|
+
webbrowser.open(self.URL)
|
|
239
|
+
self._w.tag_config(self.tag_name, foreground="purple")
|
|
240
|
+
|
|
241
|
+
def enter(self, event):
|
|
242
|
+
# ------------------------------------------------------------------------------------------
|
|
243
|
+
self._w.config(cursor="hand2")
|
|
244
|
+
|
|
245
|
+
def leave(self, event):
|
|
246
|
+
# ------------------------------------------------------------------------------------------
|
|
247
|
+
self._w.config(cursor="")
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
class ListTag:
|
|
251
|
+
# ----------------------------------------------------------------------------------------------
|
|
252
|
+
def __init__(self, ordered: bool, list_type=None):
|
|
253
|
+
# ------------------------------------------------------------------------------------------
|
|
254
|
+
self.ordered = ordered
|
|
255
|
+
self.type = list_type
|
|
256
|
+
self.index = 0
|
|
257
|
+
|
|
258
|
+
def add(self):
|
|
259
|
+
# ------------------------------------------------------------------------------------------
|
|
260
|
+
if self.ordered:
|
|
261
|
+
self.index += 1
|
|
262
|
+
|
|
263
|
+
def line_index(self):
|
|
264
|
+
if not self.ordered:
|
|
265
|
+
return chr(8226)
|
|
266
|
+
if self.type == HTML.TypeOrderedList._1:
|
|
267
|
+
return str(self.index)
|
|
268
|
+
elif self.type == HTML.TypeOrderedList.a:
|
|
269
|
+
return self._index_to_str(self.index).lower()
|
|
270
|
+
elif self.type == HTML.TypeOrderedList.A:
|
|
271
|
+
return self._index_to_str(self.index).upper()
|
|
272
|
+
|
|
273
|
+
def _index_to_str(self, index):
|
|
274
|
+
# ------------------------------------------------------------------------------------------
|
|
275
|
+
prefix = ""
|
|
276
|
+
if index > 26:
|
|
277
|
+
prefix = self._index_to_str(index // 26)
|
|
278
|
+
index = index % 26
|
|
279
|
+
|
|
280
|
+
return prefix + chr(0x60 + index)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
class HTMLTextParser(HTMLParser):
|
|
284
|
+
# ----------------------------------------------------------------------------------------------
|
|
285
|
+
|
|
286
|
+
def __init__(self):
|
|
287
|
+
# ------------------------------------------------------------------------------------------
|
|
288
|
+
super().__init__()
|
|
289
|
+
# set list tabs
|
|
290
|
+
self.cached_images = {}
|
|
291
|
+
|
|
292
|
+
self.DEFAULT_TEXT_FONT_FAMILY = get_existing_font(Defs.DEFAULT_TEXT_FONT_FAMILY)
|
|
293
|
+
self.PREFORMATTED_FONT_FAMILY = get_existing_font(Defs.PREFORMATTED_FONT_FAMILY)
|
|
294
|
+
|
|
295
|
+
def _parse_attrs(self, attrs):
|
|
296
|
+
# ------------------------------------------------------------------------------------------
|
|
297
|
+
attrs_dict = {
|
|
298
|
+
HTML.Attrs.STYLE: {},
|
|
299
|
+
HTML.Attrs.HREF: None,
|
|
300
|
+
HTML.Attrs.SRC: None,
|
|
301
|
+
HTML.Attrs.WIDTH: None,
|
|
302
|
+
HTML.Attrs.HEIGHT: None,
|
|
303
|
+
HTML.Attrs.TYPE: None,
|
|
304
|
+
HTML.Attrs.ALIGN: None, # ← ADD
|
|
305
|
+
HTML.Attrs.CHECKED: None, # ← ADD
|
|
306
|
+
HTML.Attrs.DISABLED: None, # ← ADD
|
|
307
|
+
}
|
|
308
|
+
for k, v in attrs:
|
|
309
|
+
k = k.lower()
|
|
310
|
+
if k == HTML.Attrs.STYLE:
|
|
311
|
+
for p in v.split(";"):
|
|
312
|
+
try:
|
|
313
|
+
p_key = p.split(":")[0].strip().lower()
|
|
314
|
+
p_value = p.split(":")[1].strip().lower()
|
|
315
|
+
attrs_dict[HTML.Attrs.STYLE][p_key] = p_value
|
|
316
|
+
except:
|
|
317
|
+
pass
|
|
318
|
+
elif k in (
|
|
319
|
+
HTML.Attrs.HREF,
|
|
320
|
+
HTML.Attrs.SRC,
|
|
321
|
+
HTML.Attrs.WIDTH,
|
|
322
|
+
HTML.Attrs.HEIGHT,
|
|
323
|
+
HTML.Attrs.TYPE,
|
|
324
|
+
HTML.Attrs.ALIGN, # ← ADD
|
|
325
|
+
HTML.Attrs.CHECKED, # ← ADD
|
|
326
|
+
HTML.Attrs.DISABLED, # ← ADD
|
|
327
|
+
):
|
|
328
|
+
attrs_dict[k] = v
|
|
329
|
+
return attrs_dict
|
|
330
|
+
|
|
331
|
+
def _w_tags_add(self):
|
|
332
|
+
# ------------------------------------------------------------------------------------------
|
|
333
|
+
tag = {WCfg.KEY: {}, Fnt.KEY: {}, Bind.KEY: {}}
|
|
334
|
+
|
|
335
|
+
for k1 in (WCfg.KEY, Fnt.KEY, Bind.KEY):
|
|
336
|
+
for k2 in DEFAULT_STACK[k1]:
|
|
337
|
+
tag[k1][k2] = self.stack[k1][k2][-1][1]
|
|
338
|
+
|
|
339
|
+
self._w_tags[self._w.index("end-1c")] = tag
|
|
340
|
+
|
|
341
|
+
def _stack_get_main_key(self, key):
|
|
342
|
+
# ------------------------------------------------------------------------------------------
|
|
343
|
+
if key in WCfg.__dict__.values():
|
|
344
|
+
main_key = WCfg.KEY
|
|
345
|
+
elif key in Fnt.__dict__.values():
|
|
346
|
+
main_key = Fnt.KEY
|
|
347
|
+
elif key in Bind.__dict__.values():
|
|
348
|
+
main_key = Bind.KEY
|
|
349
|
+
else:
|
|
350
|
+
raise ValueError(f"key {key} doesn't exists")
|
|
351
|
+
|
|
352
|
+
return main_key
|
|
353
|
+
|
|
354
|
+
def _stack_add(self, tag, key, value=None):
|
|
355
|
+
# ------------------------------------------------------------------------------------------
|
|
356
|
+
main_key = self._stack_get_main_key(key)
|
|
357
|
+
|
|
358
|
+
if value is None:
|
|
359
|
+
# if value is none, add the previous value
|
|
360
|
+
value = self.stack[main_key][key][-1][1]
|
|
361
|
+
|
|
362
|
+
self.stack[main_key][key].append((tag, value))
|
|
363
|
+
|
|
364
|
+
def _stack_index(self, tag, key):
|
|
365
|
+
# ------------------------------------------------------------------------------------------
|
|
366
|
+
main_key = self._stack_get_main_key(key)
|
|
367
|
+
index = None
|
|
368
|
+
for i, v in enumerate(self.stack[main_key][key]):
|
|
369
|
+
if v[0] == tag:
|
|
370
|
+
index = i
|
|
371
|
+
|
|
372
|
+
return index
|
|
373
|
+
|
|
374
|
+
def _stack_pop(self, tag, key):
|
|
375
|
+
# ------------------------------------------------------------------------------------------
|
|
376
|
+
main_key = self._stack_get_main_key(key)
|
|
377
|
+
|
|
378
|
+
index = None
|
|
379
|
+
if len(self.stack[main_key][key]) > 1:
|
|
380
|
+
index = self._stack_index(tag, key)
|
|
381
|
+
|
|
382
|
+
if index is not None:
|
|
383
|
+
return self.stack[main_key][key].pop(index)[1]
|
|
384
|
+
|
|
385
|
+
def _parse_styles(self, tag, attrs):
|
|
386
|
+
# ------------------------------------------------------------------------------------------
|
|
387
|
+
# -------------------------------------------------------------------------------- [ COLOR ]
|
|
388
|
+
if HTML.Style.COLOR in attrs[HTML.Attrs.STYLE].keys():
|
|
389
|
+
self._stack_add(
|
|
390
|
+
tag, WCfg.FOREGROUND, attrs[HTML.Attrs.STYLE][HTML.Style.COLOR]
|
|
391
|
+
)
|
|
392
|
+
elif tag == HTML.Tag.A and attrs[HTML.Attrs.HREF]:
|
|
393
|
+
self._stack_add(tag, WCfg.FOREGROUND, "blue")
|
|
394
|
+
else:
|
|
395
|
+
self._stack_add(tag, WCfg.FOREGROUND)
|
|
396
|
+
|
|
397
|
+
# ---------------------------------------------------------------------- [ BACKGROUND_COLOR ]
|
|
398
|
+
if HTML.Style.BACKGROUND_COLOR in attrs[HTML.Attrs.STYLE].keys():
|
|
399
|
+
self._stack_add(
|
|
400
|
+
tag,
|
|
401
|
+
WCfg.BACKGROUND,
|
|
402
|
+
attrs[HTML.Attrs.STYLE][HTML.Style.BACKGROUND_COLOR],
|
|
403
|
+
)
|
|
404
|
+
elif tag == HTML.Tag.MARK:
|
|
405
|
+
self._stack_add(tag, WCfg.BACKGROUND, "yellow")
|
|
406
|
+
else:
|
|
407
|
+
self._stack_add(tag, WCfg.BACKGROUND)
|
|
408
|
+
|
|
409
|
+
# -------------------------------------------------------------------------- [ FONT_FAMILY ]
|
|
410
|
+
# font family
|
|
411
|
+
if HTML.Style.FONT_FAMILY in attrs[HTML.Attrs.STYLE].keys():
|
|
412
|
+
font_family = Defs.DEFAULT_TEXT_FONT_FAMILY
|
|
413
|
+
for f in attrs[HTML.Attrs.STYLE][HTML.Style.FONT_FAMILY].split(","):
|
|
414
|
+
f = f.strip()
|
|
415
|
+
if f in map(lambda f: f.lower(), font.families()):
|
|
416
|
+
font_family = f
|
|
417
|
+
break
|
|
418
|
+
self._stack_add(tag, Fnt.FAMILY, font_family)
|
|
419
|
+
elif tag in (HTML.Tag.PRE, HTML.Tag.CODE):
|
|
420
|
+
self._stack_add(tag, Fnt.FAMILY, self.PREFORMATTED_FONT_FAMILY)
|
|
421
|
+
else:
|
|
422
|
+
self._stack_add(tag, Fnt.FAMILY)
|
|
423
|
+
|
|
424
|
+
# ---------------------------------------------------------------------------- [ FONT_SIZE ]
|
|
425
|
+
if HTML.Style.FONT_SIZE in attrs[HTML.Attrs.STYLE].keys():
|
|
426
|
+
size = Defs.FONT_SIZE
|
|
427
|
+
if attrs[HTML.Attrs.STYLE][HTML.Style.FONT_SIZE].endswith("px"):
|
|
428
|
+
if attrs[HTML.Attrs.STYLE][HTML.Style.FONT_SIZE][:-2].isdigit():
|
|
429
|
+
size = int(attrs[HTML.Attrs.STYLE][HTML.Style.FONT_SIZE][:-2])
|
|
430
|
+
elif attrs[HTML.Attrs.STYLE][HTML.Style.FONT_SIZE].endswith(r"%"):
|
|
431
|
+
if attrs[HTML.Attrs.STYLE][HTML.Style.FONT_SIZE][:-1].isdigit():
|
|
432
|
+
size = int(
|
|
433
|
+
(
|
|
434
|
+
int(attrs[HTML.Attrs.STYLE][HTML.Style.FONT_SIZE][:-1])
|
|
435
|
+
* Defs.FONT_SIZE
|
|
436
|
+
)
|
|
437
|
+
/ 100
|
|
438
|
+
)
|
|
439
|
+
self._stack_add(tag, Fnt.SIZE, size)
|
|
440
|
+
elif tag.startswith("h") and len(tag) == 2:
|
|
441
|
+
self._stack_add(tag, Fnt.SIZE, Defs.HEADINGS_FONT_SIZE[tag])
|
|
442
|
+
else:
|
|
443
|
+
self._stack_add(tag, Fnt.SIZE)
|
|
444
|
+
|
|
445
|
+
# --------------------------------------------------------------------------- [ TEXT_ALIGN ]
|
|
446
|
+
if (
|
|
447
|
+
HTML.Style.TEXT_ALIGN in attrs[HTML.Attrs.STYLE].keys()
|
|
448
|
+
and tag in HTML.TEXT_ALIGN_TAGS
|
|
449
|
+
):
|
|
450
|
+
self._stack_add(
|
|
451
|
+
tag, WCfg.JUSTIFY, attrs[HTML.Attrs.STYLE][HTML.Style.TEXT_ALIGN]
|
|
452
|
+
)
|
|
453
|
+
else:
|
|
454
|
+
self._stack_add(tag, WCfg.JUSTIFY)
|
|
455
|
+
|
|
456
|
+
# ---------------------------------------------------------------------- [ TEXT_DECORATION ]
|
|
457
|
+
if HTML.Style.TEXT_DECORATION in attrs[HTML.Attrs.STYLE].keys():
|
|
458
|
+
if tag == HTML.Tag.STRONG:
|
|
459
|
+
self._stack_add(tag, Fnt.UNDERLINE, False)
|
|
460
|
+
self._stack_add(tag, Fnt.OVERSTRIKE, False)
|
|
461
|
+
elif (
|
|
462
|
+
HTML.StyleTextDecoration.UNDERLINE
|
|
463
|
+
in attrs[HTML.Attrs.STYLE][HTML.Style.TEXT_DECORATION]
|
|
464
|
+
):
|
|
465
|
+
self._stack_add(tag, Fnt.UNDERLINE, True)
|
|
466
|
+
self._stack_add(tag, Fnt.OVERSTRIKE, False)
|
|
467
|
+
elif (
|
|
468
|
+
HTML.StyleTextDecoration.LINE_THROUGH
|
|
469
|
+
in attrs[HTML.Attrs.STYLE][HTML.Style.TEXT_DECORATION]
|
|
470
|
+
):
|
|
471
|
+
self._stack_add(tag, Fnt.UNDERLINE, False)
|
|
472
|
+
self._stack_add(tag, Fnt.OVERSTRIKE, True)
|
|
473
|
+
else:
|
|
474
|
+
self._stack_add(tag, Fnt.UNDERLINE)
|
|
475
|
+
self._stack_add(tag, Fnt.OVERSTRIKE)
|
|
476
|
+
elif tag == HTML.Tag.A and attrs[HTML.Attrs.HREF]:
|
|
477
|
+
self._stack_add(tag, Fnt.UNDERLINE, True)
|
|
478
|
+
self._stack_add(tag, Fnt.OVERSTRIKE, False)
|
|
479
|
+
elif tag == HTML.Tag.U:
|
|
480
|
+
self._stack_add(tag, Fnt.UNDERLINE, True)
|
|
481
|
+
self._stack_add(tag, Fnt.OVERSTRIKE, False)
|
|
482
|
+
else:
|
|
483
|
+
self._stack_add(tag, Fnt.UNDERLINE)
|
|
484
|
+
self._stack_add(tag, Fnt.OVERSTRIKE)
|
|
485
|
+
|
|
486
|
+
def handle_starttag(self, tag, attrs):
|
|
487
|
+
# ------------------------------------------------------------------------------------------
|
|
488
|
+
tag = tag.lower()
|
|
489
|
+
attrs = self._parse_attrs(attrs)
|
|
490
|
+
|
|
491
|
+
if tag in HTML.STYLE_TAGS:
|
|
492
|
+
# ---------------------------------------------------------------------- [ STYLED_TAGS ]
|
|
493
|
+
self._parse_styles(tag, attrs)
|
|
494
|
+
|
|
495
|
+
if tag in (HTML.Tag.B, HTML.Tag.STRONG) or tag in HTML.HEADING_TAGS:
|
|
496
|
+
self._stack_add(tag, Fnt.WEIGHT, "bold")
|
|
497
|
+
|
|
498
|
+
elif tag in (HTML.Tag.I, HTML.Tag.EM):
|
|
499
|
+
self._stack_add(tag, Fnt.SLANT, "italic")
|
|
500
|
+
|
|
501
|
+
elif tag in (HTML.Tag.DEL, HTML.Tag.S): # GFM: strikethrough
|
|
502
|
+
# GFM: strikethrough
|
|
503
|
+
self._stack_add(tag, Fnt.OVERSTRIKE, True)
|
|
504
|
+
|
|
505
|
+
elif tag == HTML.Tag.A:
|
|
506
|
+
self._stack_add(tag, Bind.LINK, attrs[HTML.Attrs.HREF])
|
|
507
|
+
|
|
508
|
+
elif tag == HTML.Tag.OL:
|
|
509
|
+
# ---------------------------------------------------------------- [ ORDERED_LISTS ]
|
|
510
|
+
if (
|
|
511
|
+
attrs[HTML.Attrs.TYPE]
|
|
512
|
+
and attrs[HTML.Attrs.TYPE] in HTML.TypeOrderedList.__dict__.values()
|
|
513
|
+
):
|
|
514
|
+
list_type = attrs[HTML.Attrs.TYPE]
|
|
515
|
+
else:
|
|
516
|
+
list_type = HTML.TypeOrderedList._1
|
|
517
|
+
self.list_tags.append(ListTag(ordered=True, list_type=list_type))
|
|
518
|
+
|
|
519
|
+
tabs = []
|
|
520
|
+
for i in range(len(self.list_tags)):
|
|
521
|
+
offset = 30 * (i + 1)
|
|
522
|
+
tabs += [offset, tk.RIGHT, offset + 5, tk.LEFT]
|
|
523
|
+
self._stack_add(tag, WCfg.TABS, tabs)
|
|
524
|
+
|
|
525
|
+
elif tag == HTML.Tag.UL:
|
|
526
|
+
# -------------------------------------------------------------- [ UNORDERED_LISTS ]
|
|
527
|
+
self.list_tags.append(ListTag(ordered=False))
|
|
528
|
+
|
|
529
|
+
tabs = []
|
|
530
|
+
for i in range(len(self.list_tags)):
|
|
531
|
+
offset = 30 * (i + 1)
|
|
532
|
+
tabs += [offset, tk.RIGHT, offset + 5, tk.LEFT]
|
|
533
|
+
self._stack_add(tag, WCfg.TABS, tabs)
|
|
534
|
+
|
|
535
|
+
elif tag == HTML.Tag.LI:
|
|
536
|
+
level = len(self.list_tags)
|
|
537
|
+
if level:
|
|
538
|
+
self.list_tags[-1].add()
|
|
539
|
+
|
|
540
|
+
if self.strip:
|
|
541
|
+
self._insert_new_line()
|
|
542
|
+
|
|
543
|
+
line_index = self.list_tags[-1].line_index()
|
|
544
|
+
if self.list_tags[-1].ordered:
|
|
545
|
+
line_index = "\t" + "\t\t" * (level - 1) + line_index + ".\t"
|
|
546
|
+
else:
|
|
547
|
+
line_index = "\t" + "\t\t" * (level - 1) + line_index + "\t"
|
|
548
|
+
|
|
549
|
+
self._stack_add(tag, Fnt.UNDERLINE, False)
|
|
550
|
+
self._stack_add(tag, Fnt.OVERSTRIKE, False)
|
|
551
|
+
self._w_tags_add()
|
|
552
|
+
self._w.insert(tk.INSERT, line_index)
|
|
553
|
+
self._stack_pop(tag, Fnt.UNDERLINE)
|
|
554
|
+
self._stack_pop(tag, Fnt.OVERSTRIKE)
|
|
555
|
+
|
|
556
|
+
elif tag in (HTML.Tag.TH, HTML.Tag.TD):
|
|
557
|
+
self._w.insert(tk.INSERT, "\t")
|
|
558
|
+
|
|
559
|
+
elif tag == HTML.Tag.BLOCKQUOTE: # GFM: blockquote
|
|
560
|
+
self._stack_add(tag, WCfg.LMARGIN1, 30) # ← was WCfg.TABS
|
|
561
|
+
self._stack_add(tag, WCfg.LMARGIN2, 30)
|
|
562
|
+
|
|
563
|
+
#if tag in HTML.STYLE_TAGS:
|
|
564
|
+
#print(f"DEBUG: {tag} entered STYLE_TAGS block")
|
|
565
|
+
|
|
566
|
+
elif tag == HTML.Tag.IMG and attrs[HTML.Attrs.SRC]:
|
|
567
|
+
# -------------------------------------------------------------------- [ UNSTYLED_TAGS ]
|
|
568
|
+
image = None
|
|
569
|
+
# print(attrs[HTML.Attrs.SRC] , self.cached_images)
|
|
570
|
+
if attrs[HTML.Attrs.SRC].startswith(("https://", "ftp://", "http://")):
|
|
571
|
+
if attrs[HTML.Attrs.SRC] in self.cached_images.keys():
|
|
572
|
+
image = deepcopy(self.cached_images[attrs[HTML.Attrs.SRC]])
|
|
573
|
+
else:
|
|
574
|
+
try:
|
|
575
|
+
image = Image.open(
|
|
576
|
+
BytesIO(requests.get(attrs[HTML.Attrs.SRC]).content)
|
|
577
|
+
)
|
|
578
|
+
self.cached_images[attrs[HTML.Attrs.SRC]] = deepcopy(image)
|
|
579
|
+
except:
|
|
580
|
+
pass
|
|
581
|
+
|
|
582
|
+
if attrs[HTML.Attrs.SRC] in self.cached_images.keys():
|
|
583
|
+
image = deepcopy(self.cached_images[attrs[HTML.Attrs.SRC]])
|
|
584
|
+
elif os.path.exists(attrs[HTML.Attrs.SRC]):
|
|
585
|
+
image = Image.open(attrs[HTML.Attrs.SRC])
|
|
586
|
+
self.cached_images[attrs[HTML.Attrs.SRC]] = deepcopy(image)
|
|
587
|
+
if image:
|
|
588
|
+
width = image.size[0]
|
|
589
|
+
height = image.size[1]
|
|
590
|
+
resize = False
|
|
591
|
+
if str(attrs[HTML.Attrs.WIDTH]).isdigit():
|
|
592
|
+
width = int(attrs[HTML.Attrs.WIDTH])
|
|
593
|
+
resize = True
|
|
594
|
+
if str(attrs[HTML.Attrs.HEIGHT]).isdigit():
|
|
595
|
+
height = int(attrs[HTML.Attrs.HEIGHT])
|
|
596
|
+
resize = True
|
|
597
|
+
if resize:
|
|
598
|
+
image = image.resize((width, height), Image.Resampling.LANCZOS)
|
|
599
|
+
self.images.append(ImageTk.PhotoImage(image))
|
|
600
|
+
self._w.image_create(tk.INSERT, image=self.images[-1])
|
|
601
|
+
|
|
602
|
+
elif tag == HTML.Tag.INPUT and attrs.get(HTML.Attrs.TYPE) == "checkbox":
|
|
603
|
+
# -------------------------------------------------------------- [ TASK_LISTS ]
|
|
604
|
+
if attrs.get(HTML.Attrs.CHECKED):
|
|
605
|
+
self._w.insert(tk.INSERT, "☑ ")
|
|
606
|
+
else:
|
|
607
|
+
self._w.insert(tk.INSERT, "☐ ")
|
|
608
|
+
|
|
609
|
+
#elif tag == HTML.Tag.BLOCKQUOTE:
|
|
610
|
+
# ---------------------------------------------------------------- [ BLOCKQUOTE ]
|
|
611
|
+
# self._stack_add(tag, WCfg.TABS, [30, tk.LEFT, 35, tk.LEFT]) # ← ADD
|
|
612
|
+
|
|
613
|
+
elif tag == HTML.Tag.HR:
|
|
614
|
+
# ---------------------------------------------------------------[ HORIZONTAL RULE ]
|
|
615
|
+
#self._insert_new_line()
|
|
616
|
+
#width = self._w.winfo_width()
|
|
617
|
+
#chars = max(8, width // 8) # rough: ~16px per char at default font
|
|
618
|
+
#self._w.insert(tk.INSERT, "─" * chars)
|
|
619
|
+
#self._w.insert(tk.INSERT, "─" * 80)
|
|
620
|
+
#self._insert_new_line()
|
|
621
|
+
self._insert_new_line()
|
|
622
|
+
width = self._w.winfo_width()
|
|
623
|
+
if width < 2:
|
|
624
|
+
width = 550 # fallback for unmapped widget
|
|
625
|
+
chars = max(1, width // 8)
|
|
626
|
+
self._w.insert(tk.INSERT, "─" * chars)
|
|
627
|
+
self._insert_new_line()
|
|
628
|
+
|
|
629
|
+
# ← STANDALONE IF (not elif) — runs for ANY tag, including those in STYLE_TAGS
|
|
630
|
+
if tag in HTML.TEXT_ALIGN_TAGS:
|
|
631
|
+
align = attrs.get(HTML.Attrs.ALIGN)
|
|
632
|
+
if align:
|
|
633
|
+
self._stack_add(tag, WCfg.JUSTIFY,
|
|
634
|
+
{"center": "center", "right": "right"}.get(align, "left"))
|
|
635
|
+
|
|
636
|
+
elif tag == HTML.Tag.TABLE:
|
|
637
|
+
tabs = []
|
|
638
|
+
for i in range(30): # HF was len(self.list_tags)):
|
|
639
|
+
offset = 40 * (i + 1)
|
|
640
|
+
tabs += [offset, tk.LEFT ]
|
|
641
|
+
self._stack_add(tag, WCfg.TABS, tabs)
|
|
642
|
+
|
|
643
|
+
if self.strip:
|
|
644
|
+
if tag == HTML.Tag.BR:
|
|
645
|
+
self._insert_new_line()
|
|
646
|
+
else:
|
|
647
|
+
self.html_tags.append(tag)
|
|
648
|
+
|
|
649
|
+
if (
|
|
650
|
+
tag in HTML.NEW_LINE_TAGS
|
|
651
|
+
and self.strip
|
|
652
|
+
and self._w.index("end-1c") != "1.0"
|
|
653
|
+
):
|
|
654
|
+
if tag in (HTML.Tag.DIV,):
|
|
655
|
+
self._insert_new_line()
|
|
656
|
+
elif tag in (HTML.Tag.UL, HTML.Tag.OL):
|
|
657
|
+
if len(self.list_tags) == 1:
|
|
658
|
+
self._insert_new_line(double=True)
|
|
659
|
+
else:
|
|
660
|
+
self._insert_new_line(double=False)
|
|
661
|
+
else:
|
|
662
|
+
self._insert_new_line(double=True)
|
|
663
|
+
|
|
664
|
+
self._w_tags_add()
|
|
665
|
+
|
|
666
|
+
#if self._w_tags:
|
|
667
|
+
# for key, tag in self._w_tags.items():
|
|
668
|
+
# print(f"DEBUG: tag='{key}', config={tag[WCfg.KEY]}")
|
|
669
|
+
|
|
670
|
+
def handle_charref(self, data):
|
|
671
|
+
# ------------------------------------------------------------------------------------------
|
|
672
|
+
try:
|
|
673
|
+
char = chr(int(data))
|
|
674
|
+
self._w.insert(tk.INSERT, char)
|
|
675
|
+
except:
|
|
676
|
+
pass
|
|
677
|
+
|
|
678
|
+
def _insert_new_line(self, double=False):
|
|
679
|
+
# ------------------------------------------------------------------------------------------
|
|
680
|
+
self._remove_last_space()
|
|
681
|
+
if self._w.get("end-3c", "end-1c") == "\n\n":
|
|
682
|
+
pass
|
|
683
|
+
elif self._w.get("end-2c", "end-1c") == "\n":
|
|
684
|
+
if double:
|
|
685
|
+
self._w.insert(tk.INSERT, "\n")
|
|
686
|
+
elif double:
|
|
687
|
+
self._w.insert(tk.INSERT, "\n\n")
|
|
688
|
+
else:
|
|
689
|
+
self._w.insert(tk.INSERT, "\n")
|
|
690
|
+
|
|
691
|
+
def _text_rstrip(self):
|
|
692
|
+
# ------------------------------------------------------------------------------------------
|
|
693
|
+
for _ in range(3):
|
|
694
|
+
if self._w.get("end-2c", "end-1c") in (" ", "\n"):
|
|
695
|
+
self._w.delete("end-2c", "end-1c")
|
|
696
|
+
|
|
697
|
+
def _remove_last_space(self):
|
|
698
|
+
# ------------------------------------------------------------------------------------------
|
|
699
|
+
if self._w.get("end-2c", "end-1c") == " ":
|
|
700
|
+
self._w.delete("end-2c", "end-1c")
|
|
701
|
+
|
|
702
|
+
def _remove_multi_spaces(self, data):
|
|
703
|
+
# ------------------------------------------------------------------------------------------
|
|
704
|
+
data = data.replace(" ", " ")
|
|
705
|
+
if " " in data:
|
|
706
|
+
data = self._remove_multi_spaces(data)
|
|
707
|
+
return data
|
|
708
|
+
|
|
709
|
+
def handle_data(self, data):
|
|
710
|
+
if HTML.Tag.PRE in self.html_tags:
|
|
711
|
+
pass
|
|
712
|
+
elif not data.strip():
|
|
713
|
+
if self.strip:
|
|
714
|
+
data = ""
|
|
715
|
+
elif self.strip:
|
|
716
|
+
# left strip
|
|
717
|
+
if self._w.index("end-1c").endswith(".0"):
|
|
718
|
+
data = data.lstrip()
|
|
719
|
+
elif self._w.get("end-2c", "end-1c") == " ":
|
|
720
|
+
data = data.lstrip()
|
|
721
|
+
|
|
722
|
+
data = data.replace("\n", " ").replace("\t", " ")
|
|
723
|
+
data = f"{data}" # FIXME: attaching a space in blind is wrong - SPACE REMOVED
|
|
724
|
+
data = self._remove_multi_spaces(data)
|
|
725
|
+
if len(self.html_tags) and self.html_tags[-1] in (
|
|
726
|
+
HTML.Tag.UL,
|
|
727
|
+
HTML.Tag.OL,
|
|
728
|
+
):
|
|
729
|
+
self._w.insert(tk.INSERT, "\t" * 2 * len(self.list_tags))
|
|
730
|
+
|
|
731
|
+
self._w.insert(tk.INSERT, data)
|
|
732
|
+
|
|
733
|
+
def handle_endtag(self, tag):
|
|
734
|
+
# ------------------------------------------------------------------------------------------
|
|
735
|
+
tag = tag.lower()
|
|
736
|
+
|
|
737
|
+
try:
|
|
738
|
+
index = len(self.html_tags) - self.html_tags[::-1].index(tag) - 1
|
|
739
|
+
self.html_tags.pop(index)
|
|
740
|
+
except:
|
|
741
|
+
pass
|
|
742
|
+
|
|
743
|
+
if tag in HTML.STYLE_TAGS:
|
|
744
|
+
self._stack_pop(tag, WCfg.FOREGROUND)
|
|
745
|
+
self._stack_pop(tag, WCfg.BACKGROUND)
|
|
746
|
+
self._stack_pop(tag, WCfg.JUSTIFY)
|
|
747
|
+
self._stack_pop(tag, Fnt.FAMILY)
|
|
748
|
+
self._stack_pop(tag, Fnt.SIZE)
|
|
749
|
+
self._stack_pop(tag, Fnt.UNDERLINE)
|
|
750
|
+
self._stack_pop(tag, Fnt.OVERSTRIKE)
|
|
751
|
+
self._stack_pop(tag, WCfg.LMARGIN1) # ← GFM
|
|
752
|
+
self._stack_pop(tag, WCfg.LMARGIN2) # ← GFM
|
|
753
|
+
|
|
754
|
+
if tag == HTML.Tag.B or tag == HTML.Tag.STRONG or tag in HTML.HEADING_TAGS:
|
|
755
|
+
self._stack_pop(tag, Fnt.WEIGHT)
|
|
756
|
+
|
|
757
|
+
elif tag in (HTML.Tag.I, HTML.Tag.EM):
|
|
758
|
+
self._stack_pop(tag, Fnt.SLANT)
|
|
759
|
+
|
|
760
|
+
elif tag == HTML.Tag.A:
|
|
761
|
+
self._stack_pop(tag, Bind.LINK)
|
|
762
|
+
|
|
763
|
+
elif tag in (HTML.Tag.OL, HTML.Tag.UL):
|
|
764
|
+
if len(self.list_tags):
|
|
765
|
+
self.list_tags = self.list_tags[:-1]
|
|
766
|
+
|
|
767
|
+
self._stack_pop(tag, WCfg.TABS)
|
|
768
|
+
|
|
769
|
+
elif tag == HTML.Tag.BLOCKQUOTE:
|
|
770
|
+
self._stack_pop(tag, WCfg.TABS)
|
|
771
|
+
|
|
772
|
+
if tag in HTML.NEW_LINE_TAGS and self.strip:
|
|
773
|
+
self._insert_new_line()
|
|
774
|
+
|
|
775
|
+
self._w_tags_add()
|
|
776
|
+
|
|
777
|
+
if tag in HTML.NEW_LINE_TAGS and self.strip:
|
|
778
|
+
if tag in (HTML.Tag.DIV, HTML.Tag.UL, HTML.Tag.OL):
|
|
779
|
+
if not len(self.list_tags):
|
|
780
|
+
self._insert_new_line(double=True)
|
|
781
|
+
else:
|
|
782
|
+
self._insert_new_line(double=True)
|
|
783
|
+
|
|
784
|
+
def _w_tags_apply_all(self):
|
|
785
|
+
# ------------------------------------------------------------------------------------------
|
|
786
|
+
# update indexes
|
|
787
|
+
if self.strip:
|
|
788
|
+
self._text_rstrip()
|
|
789
|
+
end_index = tk.END
|
|
790
|
+
for key, tag in reversed(tuple(self._w_tags.items())):
|
|
791
|
+
tag[WTag.START_INDEX] = key
|
|
792
|
+
tag[WTag.END_INDEX] = end_index
|
|
793
|
+
end_index = key
|
|
794
|
+
|
|
795
|
+
# add tags
|
|
796
|
+
self.hlink_slots = []
|
|
797
|
+
for key, tag in self._w_tags.items():
|
|
798
|
+
if "config" in tag: # HF change justify to left for tkinter (only supports left, right, center)
|
|
799
|
+
if tag["config"].get("justify") == "justify":
|
|
800
|
+
tag["config"]["justify"] = "left"
|
|
801
|
+
self._w.tag_add(key, tag[WTag.START_INDEX], tag[WTag.END_INDEX])
|
|
802
|
+
self._w.tag_config(key, font=font.Font(**tag[Fnt.KEY]), **tag[WCfg.KEY])
|
|
803
|
+
if tag[Bind.KEY][Bind.LINK]:
|
|
804
|
+
self.hlink_slots.append(
|
|
805
|
+
HLinkSlot(self._w, key, tag[Bind.KEY][Bind.LINK])
|
|
806
|
+
)
|
|
807
|
+
self._w.tag_bind(key, "<Button-1>", self.hlink_slots[-1].call)
|
|
808
|
+
self._w.tag_bind(key, "<Leave>", self.hlink_slots[-1].leave)
|
|
809
|
+
self._w.tag_bind(key, "<Enter>", self.hlink_slots[-1].enter)
|
|
810
|
+
|
|
811
|
+
def w_set_html(self, w, html, strip):
|
|
812
|
+
# ------------------------------------------------------------------------------------------
|
|
813
|
+
self._w = w
|
|
814
|
+
self.stack = deepcopy(DEFAULT_STACK)
|
|
815
|
+
self.stack[WCfg.KEY][WCfg.BACKGROUND].append(
|
|
816
|
+
("__DEFAULT__", self._w.cget("background"))
|
|
817
|
+
)
|
|
818
|
+
self.stack[Fnt.KEY][Fnt.FAMILY].append(
|
|
819
|
+
("__DEFAULT__", self.DEFAULT_TEXT_FONT_FAMILY)
|
|
820
|
+
)
|
|
821
|
+
self._w_tags = OrderedDict()
|
|
822
|
+
self.html_tags = []
|
|
823
|
+
self.images = []
|
|
824
|
+
self.list_tags = []
|
|
825
|
+
self.strip = strip
|
|
826
|
+
self._w_tags_add()
|
|
827
|
+
self.feed(html)
|
|
828
|
+
self._w_tags_apply_all()
|
|
829
|
+
del self._w
|
tkhtmlview/utils.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class RenderHTML:
|
|
5
|
+
def __init__(self, file, encoding = "utf-8"):
|
|
6
|
+
self._file = file
|
|
7
|
+
if not os.path.exists(self._file):
|
|
8
|
+
raise FileNotFoundError(f"No such HTML file: {self._file}")
|
|
9
|
+
|
|
10
|
+
with open(file, "r", encoding=encoding) as f:
|
|
11
|
+
self._html = f.read()
|
|
12
|
+
|
|
13
|
+
def __repr__(self):
|
|
14
|
+
return f"<{self.__class__.__name__}: {self._file}>"
|
|
15
|
+
|
|
16
|
+
def get_html(self):
|
|
17
|
+
return str(self._html)
|
|
18
|
+
|
|
19
|
+
__str__ = get_html
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tkhtmlview-gfm
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Display HTML with Tkinter + GitHub Flavored Markdown support
|
|
5
|
+
Author-email: Palash Bauri <palashbauri1@gmail.com>, Lesley Smith <lesley_nicole@outlook.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Lesley Smith
|
|
9
|
+
Copyright (c) 2020 Palash Bauri
|
|
10
|
+
Copyright (c) 2018 paolo-gurisatti
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
|
|
30
|
+
Project-URL: Homepage, https://github.com/LesleySmithDigital/tkhtmlview-gfm
|
|
31
|
+
Project-URL: Repository, https://github.com/LesleySmithDigital/tkhtmlview-gfm
|
|
32
|
+
Project-URL: Issues, https://github.com/LesleySmithDigital/tkhtmlview-gfm/issues
|
|
33
|
+
Project-URL: Upstream Project, https://github.com/rdbende/tkhtmlview
|
|
34
|
+
Project-URL: Release, https://github.com/LesleySmithDigital/tkhtmlview-gfm/releases/tag/v0.1.0
|
|
35
|
+
Keywords: tkinter,html,markdown,gfm,gui
|
|
36
|
+
Classifier: Development Status :: 4 - Beta
|
|
37
|
+
Classifier: Intended Audience :: Developers
|
|
38
|
+
Classifier: Operating System :: OS Independent
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
42
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
43
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
44
|
+
Classifier: Topic :: Text Processing :: Markup :: HTML
|
|
45
|
+
Classifier: Topic :: Text Processing :: Markup :: Markdown
|
|
46
|
+
Requires-Python: <4,>=3.11
|
|
47
|
+
Description-Content-Type: text/markdown
|
|
48
|
+
License-File: LICENSE
|
|
49
|
+
Requires-Dist: requests<3,>=2.32.4
|
|
50
|
+
Requires-Dist: Pillow<14,>=11
|
|
51
|
+
Dynamic: license-file
|
|
52
|
+
|
|
53
|
+
<p align="center"><img src="assets/tkquill.png" width="25%" alt="quill"></p>
|
|
54
|
+
|
|
55
|
+
<h1 align="center">tkhtmlview-gfm</h1>
|
|
56
|
+
|
|
57
|
+
<p align="center">Display HTML with Tkinter, + GitHub Flavored Markdown support.</p>
|
|
58
|
+
|
|
59
|
+
<p align="center">Forked from
|
|
60
|
+
<a href="https://github.com/rdbende/tkhtmlview">tkhtmlview
|
|
61
|
+
</a> by Palash Bauri.
|
|
62
|
+
</p>
|
|
63
|
+
|
|
64
|
+
[](https://github.com/LesleySmithDigital/LICENSE)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
## What's New
|
|
68
|
+
|
|
69
|
+
Adds rendering support for GFM features not available in upstream:
|
|
70
|
+
|
|
71
|
+
| Feature | HTML | Status |
|
|
72
|
+
|---------|------|--------|
|
|
73
|
+
| Strikethrough | `<del>` / `<s>` | ✓ |
|
|
74
|
+
| Blockquote | `<blockquote>` | ✓ |
|
|
75
|
+
| Horizontal rule | `<hr>` | ✓ |
|
|
76
|
+
| Task lists | `<input type="checkbox">` | ✓ (visual only) |
|
|
77
|
+
| Tables | `<table>` | ✓ |
|
|
78
|
+
| Text alignment | `align="center"` / `text-align` | ✓ |
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
<p align="center">
|
|
82
|
+
<img src="assets/gfm_test.png" width="50%" alt="new features">
|
|
83
|
+
</p>
|
|
84
|
+
|
|
85
|
+
## Installation
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pip install tkhtmlview-gfm
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Usage
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
import tkinter as tk
|
|
95
|
+
from tkhtmlview import HTMLScrolledText
|
|
96
|
+
|
|
97
|
+
root = tk.Tk()
|
|
98
|
+
w = HTMLScrolledText(root)
|
|
99
|
+
w.pack(fill="both", expand=True)
|
|
100
|
+
root.update_idletasks() # ← forces the widget to compute its real size
|
|
101
|
+
w.set_html("""
|
|
102
|
+
<p>Normal <del>struck</del> text</p>
|
|
103
|
+
<blockquote><p>Quoted</p></blockquote>
|
|
104
|
+
<hr>
|
|
105
|
+
<ul><li>☐ Task</li><li>☑ Done</li></ul>
|
|
106
|
+
<h1 align="center">Centered</h1>
|
|
107
|
+
""")
|
|
108
|
+
|
|
109
|
+
root.mainloop()
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Handle Live Resize
|
|
113
|
+
|
|
114
|
+
Bind <Configure> to re-render (debounced so it doesn't fire 50 times per drag):
|
|
115
|
+
|
|
116
|
+
**Example:**
|
|
117
|
+
```python
|
|
118
|
+
_configure_job = None
|
|
119
|
+
|
|
120
|
+
def on_configure(event):
|
|
121
|
+
global _configure_job
|
|
122
|
+
if _configure_job is not None:
|
|
123
|
+
preview.after_cancel(_configure_job)
|
|
124
|
+
_configure_job = preview.after(200, render_preview)
|
|
125
|
+
|
|
126
|
+
preview.bind("<Configure>", on_configure)
|
|
127
|
+
|
|
128
|
+
# Now when you drag the window edge, the HR (and any other width-dependent rendering) updates ~200 ms after you stop dragging.
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
TL;DR: **root.update_idletasks()** before **set_html** fixes the initial render. The <Configure> binding handles resize. The fallback in the parser is a safety net, which gives you a reasonable line.
|
|
132
|
+
|
|
133
|
+
## Known Limitations
|
|
134
|
+
|
|
135
|
+
- Task list checkboxes are visual only (not interactive)
|
|
136
|
+
- " hr " is rendered as a row of ─ characters (not a true line)
|
|
137
|
+
- text-align: justify falls back to left (Tkinter limitation)
|
|
138
|
+
- No CSS margin/padding/border support
|
|
139
|
+
|
|
140
|
+
## Development
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
git clone https://github.com/LesleySmithDigital/tkhtmlview-gfm.git
|
|
144
|
+
cd tkhtmlview-gfm
|
|
145
|
+
pip install -e .
|
|
146
|
+
pytest tests/test_gfm.py
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Testing
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
pip install pytest
|
|
153
|
+
pytest tests/test_gfm.py
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## License
|
|
157
|
+
MIT
|
|
158
|
+
|
|
159
|
+
## Credits
|
|
160
|
+
Based on [tkhtmlview](https://github.com/rdbende/tkhtmlview) by Palash Bauri(MIT License).
|
|
161
|
+
|
|
162
|
+
## Author
|
|
163
|
+
|
|
164
|
+
- [@LesleySmithDigital](https://www.github.com/LesleySmithDigital)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
tkhtmlview/__init__.py,sha256=65glcUR0MgJOuNB1C0mdmgi79NxSh4IFSxXSfzRsZVY,3869
|
|
2
|
+
tkhtmlview/html_parser.py,sha256=qLOLgTys9ZPu2a2BO1vTRg_JzbpOKCLoPNauCy53n7g,31139
|
|
3
|
+
tkhtmlview/utils.py,sha256=Xt0qI6GX9adBkze1MlksO-nhSPQoWHmZPtN-SxDooj4,494
|
|
4
|
+
tkhtmlview_gfm-0.1.0.dist-info/licenses/LICENSE,sha256=uGVAC6r-AJA2UEcQdNY96p59oVmK3d-x_9rDsmY14nI,1159
|
|
5
|
+
tkhtmlview_gfm-0.1.0.dist-info/METADATA,sha256=LHAuTHOCA1_6hqjkcG845BisaCwuO9Zx8Ttaw86cElE,5650
|
|
6
|
+
tkhtmlview_gfm-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
7
|
+
tkhtmlview_gfm-0.1.0.dist-info/top_level.txt,sha256=ArYyP_LpOBDgirE46Y_D70iWUrAbHjBjRb0ZSqFi_1Q,11
|
|
8
|
+
tkhtmlview_gfm-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lesley Smith
|
|
4
|
+
Copyright (c) 2020 Palash Bauri
|
|
5
|
+
Copyright (c) 2018 paolo-gurisatti
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
tkhtmlview
|