forked from thonny/thonny
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrst_utils.py
More file actions
496 lines (383 loc) · 17.4 KB
/
Copy pathrst_utils.py
File metadata and controls
496 lines (383 loc) · 17.4 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import tkinter as tk
import traceback
from logging import getLogger
from pystart import get_workbench, ui_utils
from pystart.codeview import get_syntax_options_for_tag
from pystart.tktextext import TweakableText
from pystart.ui_utils import get_hyperlink_cursor, lookup_style_option
logger = getLogger(__name__)
class RstText(TweakableText):
def __init__(self, master=None, cnf={}, read_only=False, **kw):
super().__init__(
master=master,
cnf=cnf,
read_only=read_only,
**{
"font": "TkDefaultFont",
# "cursor" : "",
**kw,
},
)
self.configure_tags()
self._visitor = None
def configure_tags(self):
main_font = tk.font.nametofont("TkDefaultFont")
bold_font = main_font.copy()
bold_font.configure(weight="bold", size=main_font.cget("size"))
italic_font = main_font.copy()
italic_font.configure(slant="italic", size=main_font.cget("size"))
h1_font = main_font.copy()
h1_font.configure(size=main_font.cget("size") * 2, weight="bold")
h2_font = main_font.copy()
h2_font.configure(size=round(main_font.cget("size") * 1.5), weight="bold")
h3_font = main_font.copy()
h3_font.configure(size=main_font.cget("size"), weight="bold")
small_font = main_font.copy()
small_font.configure(size=round(main_font.cget("size") * 0.8))
small_italic_font = italic_font.copy()
small_italic_font.configure(size=round(main_font.cget("size") * 0.8))
# Underline on font looks better than underline on tag
underline_font = main_font.copy()
underline_font.configure(underline=True)
self.tag_configure("h1", font=h1_font, spacing3=5)
self.tag_configure("h2", font=h2_font, spacing3=5)
self.tag_configure("h3", font=h3_font, spacing3=5)
self.tag_configure("p", spacing1=0, spacing3=10, spacing2=0)
self.tag_configure("line_block", spacing1=0, spacing3=10, spacing2=0)
self.tag_configure("em", font=italic_font)
self.tag_configure("strong", font=bold_font)
# TODO: hyperlink syntax options may require different background as well
hyperlink_opts = get_syntax_options_for_tag("hyperlink")
hyperlink_opts["underline"] = False
hyperlink_opts["font"] = underline_font
self.tag_configure("a", **hyperlink_opts)
self.tag_configure("small", font=small_font)
self.tag_configure("light", foreground="gray")
self.tag_configure("remark", font=small_italic_font)
self.tag_bind("a", "<Enter>", self._hyperlink_enter)
self.tag_bind("a", "<Leave>", self._hyperlink_leave)
self.tag_configure("topic_title", lmargin2=16, font=bold_font)
self.tag_configure("topic_body", lmargin1=16, lmargin2=16)
self.tag_configure(
"code",
font="TkFixedFont",
# wrap="none", # TODO: needs automatic hor-scrollbar and better padding mgmt
# background="#eeeeee"
)
# if ui_utils.get_tk_version_info() >= (8,6,6):
# self.tag_configure("code", lmargincolor=self["background"])
for i in range(1, 6):
self.tag_configure("list%d" % i, lmargin1=i * 10, lmargin2=i * 10 + 10)
toti_code_font = bold_font.copy()
toti_code_font.configure(
family=tk.font.nametofont("TkFixedFont").cget("family"), size=bold_font.cget("size")
)
self.tag_configure("topic_title_code", font=toti_code_font)
self.tag_raise("topic_title_code", "code")
self.tag_raise("topic_title_code", "topic_title")
self.tag_raise("a", "topic_title")
# TODO: topic_title + em
self.tag_raise("em", "topic_title")
self.tag_raise("a", "em")
self.tag_raise("a", "topic_body")
self.tag_raise("a", "topic_title")
if ui_utils.get_tk_version_info() >= (8, 6, 6):
self.tag_configure("sel", lmargincolor=self["background"])
self.tag_raise("sel")
def clear(self):
self.direct_delete("1.0", "end")
def load_rst(self, rst_source, global_tags=()):
self.clear()
self.append_rst(rst_source, global_tags)
def append_rst(self, rst_source, global_tags=()):
try:
import docutils.core
doc = docutils.core.publish_doctree(rst_source)
doc.walkabout(self.create_visitor(doc, global_tags))
except Exception:
self.direct_insert("end", "RST SOURCE:\n" + rst_source + "\n\n")
self.direct_insert("end", traceback.format_exc())
# For debugging:
# self.direct_insert("end", doc.pformat())
# self.direct_insert("end", rst_source)
def create_visitor(self, doc, global_tags=()):
# Pass unique tag count from previous visitor
# to keep uniqueness
if self._visitor is None:
unique_tag_count = 0
else:
unique_tag_count = self._visitor.unique_tag_count
import docutils.nodes
class TkTextRenderingVisitor(docutils.nodes.GenericNodeVisitor):
def __init__(self, document, text, global_tags=(), unique_tag_count=0):
super().__init__(document)
self._context_tags = list(global_tags)
self.text = text
self.section_level = 0
self.in_topic = False
self.in_paragraph = False
self.in_title = False
self.active_lists = []
self.unique_tag_count = unique_tag_count
def visit_document(self, node):
pass
def visit_Text(self, node):
self._append_text(self._node_to_text(node))
def visit_section(self, node):
self.section_level += 1
def depart_section(self, node):
self.section_level -= 1
def _get_title_tag(self):
if self.in_topic:
return "topic_title"
else:
return "h%d" % (self.section_level + 1)
def visit_title(self, node):
self.in_title = True
self._add_tag(self._get_title_tag())
def depart_title(self, node):
self.in_title = False
self._append_text("\n")
self._pop_tag(self._get_title_tag())
def visit_paragraph(self, node):
self.in_paragraph = True
if not self.active_lists:
self._add_tag("p")
def depart_paragraph(self, node):
self.in_paragraph = False
self._append_text("\n")
if not self.active_lists:
self._pop_tag("p")
def visit_line_block(self, node):
self._add_tag("line_block")
def depart_line_block(self, node):
self._pop_tag("line_block")
def visit_line(self, node):
pass
def depart_line(self, node):
self._append_text("\n")
def visit_topic(self, node):
self.in_topic = True
if "toggle" in node.attributes["classes"]:
return self._visit_toggle_topic(node)
elif "empty" in node.attributes["classes"]:
return self._visit_empty_topic(node)
else:
return self.default_visit(node)
def _visit_toggle_topic(self, node):
tag = self._create_unique_tag()
title_id_tag = tag + "_title"
body_id_tag = tag + "_body"
def get_toggler_image_name(kind):
if get_workbench().uses_dark_ui_theme():
return kind + "_light"
else:
return kind
if "open" in node.attributes["classes"]:
initial_image = get_toggler_image_name("boxminus")
initial_elide = False
else:
initial_image = get_toggler_image_name("boxplus")
initial_elide = True
label = tk.Label(
self.text,
image=get_workbench().get_image(initial_image),
borderwidth=0,
background=self.text["background"],
cursor="arrow",
)
def toggle_body(event=None):
elide = self.text.tag_cget(body_id_tag, "elide")
if elide == "1":
elide = True
elif elide == "0":
elide = False
else:
elide = bool(elide)
elide = not elide
self.text.tag_configure(body_id_tag, elide=elide)
if self.text.has_selection():
self.text.tag_remove("sel", "1.0", "end")
if elide:
label.configure(
image=get_workbench().get_image(get_toggler_image_name("boxplus"))
)
else:
label.configure(
image=get_workbench().get_image(get_toggler_image_name("boxminus"))
)
assert isinstance(node.children[0], docutils.nodes.title)
# self.text.tag_bind(title_id_tag, "<1>", toggle_body, True)
self._add_tag(title_id_tag)
self._append_window(label)
label.bind("<1>", toggle_body, True)
node.children[0].walkabout(self)
self._pop_tag(title_id_tag)
self.text.tag_configure(body_id_tag, elide=initial_elide)
self._add_tag(body_id_tag)
self._add_tag("topic_body")
for child in list(node.children)[1:]:
child.walkabout(self)
self._pop_tag("topic_body")
self._pop_tag(body_id_tag)
if "tight" not in node.attributes["classes"]:
self._append_text("\n")
raise docutils.nodes.SkipNode()
def _visit_empty_topic(self, node):
img = get_workbench().get_image(
"boxdot_light" if get_workbench().uses_dark_ui_theme() else "boxdot"
)
label = tk.Label(
self.text,
image=img,
borderwidth=0,
background=self.text["background"],
cursor="arrow",
)
self._append_window(label)
assert isinstance(node.children[0], docutils.nodes.title)
node.children[0].walkabout(self)
if "tight" not in node.attributes["classes"]:
self._append_text("\n")
raise docutils.nodes.SkipNode()
def depart_topic(self, node):
# only for non-toggle topics
self.in_topic = False
self._append_text("\n")
def visit_image(self, node):
self._append_image(node.attributes["uri"])
if not self.in_paragraph and not self.in_title:
self._append_text("\n")
def visit_reference(self, node):
tag = self._create_unique_tag()
node.unique_tag = tag
self._add_tag("a")
self._add_tag(tag)
def handle_click(event):
get_workbench().open_url(node.attributes["refuri"])
self.text.tag_bind(tag, "<ButtonRelease-1>", handle_click)
def depart_reference(self, node):
self._pop_tag("a")
self._pop_tag(node.unique_tag)
def visit_literal(self, node):
self._add_tag("code")
def depart_literal(self, node):
self._pop_tag("code")
def visit_inline(self, node):
for cls in node.attributes["classes"]:
self._add_tag(cls)
def depart_inline(self, node):
for cls in node.attributes["classes"]:
self._pop_tag(cls)
def visit_literal_block(self, node):
self._add_tag("code")
def depart_literal_block(self, node):
self._pop_tag("code")
self._append_text("\n\n")
def visit_bullet_list(self, node):
self.active_lists.append(node.attributes["bullet"])
def depart_bullet_list(self, node):
self._append_text("\n")
self.active_lists.pop()
def visit_enumerated_list(self, node):
self.active_lists.append(node.attributes["enumtype"])
def depart_enumerated_list(self, node):
self._append_text("\n")
self.active_lists.pop()
def visit_list_item(self, node):
if self.active_lists[-1] == "*":
self._append_text("• ")
elif self.active_lists[-1] == "arabic":
for i, sib in enumerate(node.parent.children):
if sib is node:
self._append_text("%d. " % (i + 1))
break
def visit_note(self, node):
self._add_tag("em")
def depart_note(self, node):
self._pop_tag("em")
def visit_target(self, node):
pass
def visit_substitution_definition(self, node):
raise docutils.nodes.SkipNode()
def visit_system_message(self, node):
logger.warning("docutils message: '%s'. Context: %s" % (node.astext(), node.parent))
raise docutils.nodes.SkipNode
def visit_emphasis(self, node):
self._add_tag("em")
def depart_emphasis(self, node):
self._pop_tag("em")
def visit_strong(self, node):
self._add_tag("strong")
def depart_strong(self, node):
self._pop_tag("strong")
def visit_block_quote(self, node):
self._add_tag("code")
def depart_block_quote(self, node):
self._pop_tag("code")
def default_visit(self, node):
self._append_text(self._node_to_text(node))
print("skipping children", type(node), node)
raise docutils.nodes.SkipChildren()
def default_departure(self, node):
# Pass all other nodes through.
pass
def _create_unique_tag(self):
self.unique_tag_count += 1
return "_UT_%s" % self.unique_tag_count
def _node_to_text(self, node):
if node.parent.attributes.get("xml:space") == "preserve":
return node.astext()
else:
return node.astext().replace("\r", "").replace("\n", " ")
def _add_tag(self, tag):
self._context_tags.append(tag)
def _pop_tag(self, tag):
self._context_tags.remove(tag)
def _append_text(self, chars, extra_tags=()):
# print("APPP", chars, tags)
self.text.direct_insert("end", chars, self._get_effective_tags(extra_tags))
def _append_image(self, name, extra_tags=()):
index = self.text.index("end-1c")
self.text.image_create(index, image=get_workbench().get_image(name))
for tag in self._get_effective_tags(extra_tags):
self.text.tag_add(tag, index)
def _append_window(self, window, extra_tags=()):
index = self.text.index("end-1c")
self.text.window_create(index, window=window)
for tag in self._get_effective_tags(extra_tags):
self.text.tag_add(tag, index)
def _get_effective_tags(self, extra_tags):
tags = set(extra_tags) | set(self._context_tags)
if self.active_lists:
tags.add("list%d" % min(len(self.active_lists), 5))
# combine tags
if "code" in tags and "topic_title" in tags:
tags.remove("code")
tags.remove("topic_title")
tags.add("topic_title_code")
return tuple(sorted(tags))
self._visitor = TkTextRenderingVisitor(doc, self, global_tags, unique_tag_count)
return self._visitor
def _hyperlink_enter(self, event):
self.config(cursor=get_hyperlink_cursor())
def _hyperlink_leave(self, event):
self.config(cursor="")
def on_theme_changed(self):
self.configure_tags()
text_bg = lookup_style_option("Text", "background")
for name in self.window_names():
w = self.nametowidget(name)
if isinstance(w, tk.Label):
w.configure(background=text_bg)
def escape(s):
return (
s.replace("\\", "\\\\")
.replace("*", "\\*")
.replace("`", "\\`")
.replace("_", "\\_")
.replace("..", "\\..")
)
def create_title(text, line_symbol="="):
text = text.replace("\r\n", "\n").replace("\n", " ").strip()
return text + "\n" + line_symbol * len(text) + "\n"