-
Notifications
You must be signed in to change notification settings - Fork 815
/
_markdown.py
1254 lines (1027 loc) · 38.1 KB
/
_markdown.py
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import asyncio
import re
from functools import partial
from pathlib import Path, PurePath
from typing import Callable, Iterable, Optional
from urllib.parse import unquote
from markdown_it import MarkdownIt
from markdown_it.token import Token
from rich import box
from rich.style import Style
from rich.syntax import Syntax
from rich.table import Table
from rich.text import Text
from typing_extensions import TypeAlias
from textual._slug import TrackedSlugs
from textual.app import ComposeResult
from textual.await_complete import AwaitComplete
from textual.containers import Horizontal, Vertical, VerticalScroll
from textual.events import Mount
from textual.message import Message
from textual.reactive import reactive, var
from textual.widget import Widget
from textual.widgets import Static, Tree
TableOfContentsType: TypeAlias = "list[tuple[int, str, str | None]]"
"""Information about the table of contents of a markdown document.
The triples encode the level, the label, and the optional block id of each heading.
"""
class Navigator:
"""Manages a stack of paths like a browser."""
def __init__(self) -> None:
self.stack: list[Path] = []
self.index = 0
@property
def location(self) -> Path:
"""The current location.
Returns:
A path for the current document.
"""
if not self.stack:
return Path(".")
return self.stack[self.index]
@property
def start(self) -> bool:
"""Is the current location at the start of the stack?"""
return self.index == 0
@property
def end(self) -> bool:
"""Is the current location at the end of the stack?"""
return self.index >= len(self.stack) - 1
def go(self, path: str | PurePath) -> Path:
"""Go to a new document.
Args:
path: Path to new document.
Returns:
New location.
"""
location, anchor = Markdown.sanitize_location(str(path))
if location == Path(".") and anchor:
current_file, _ = Markdown.sanitize_location(str(self.location))
path = f"{current_file}#{anchor}"
new_path = self.location.parent / Path(path)
self.stack = self.stack[: self.index + 1]
new_path = new_path.absolute()
self.stack.append(new_path)
self.index = len(self.stack) - 1
return new_path
def back(self) -> bool:
"""Go back in the stack.
Returns:
True if the location changed, otherwise False.
"""
if self.index:
self.index -= 1
return True
return False
def forward(self) -> bool:
"""Go forward in the stack.
Returns:
True if the location changed, otherwise False.
"""
if self.index < len(self.stack) - 1:
self.index += 1
return True
return False
class MarkdownBlock(Static):
"""The base class for a Markdown Element."""
DEFAULT_CSS = """
MarkdownBlock {
height: auto;
}
"""
def __init__(self, markdown: Markdown, *args, **kwargs) -> None:
self._markdown: Markdown = markdown
"""A reference to the Markdown document that contains this block."""
self._text = Text()
self._token: Token | None = None
self._blocks: list[MarkdownBlock] = []
super().__init__(*args, **kwargs)
def compose(self) -> ComposeResult:
yield from self._blocks
self._blocks.clear()
def set_content(self, text: Text) -> None:
self._text = text
self.update(text)
async def action_link(self, href: str) -> None:
"""Called on link click."""
self.post_message(Markdown.LinkClicked(self._markdown, href))
def notify_style_update(self) -> None:
"""If CSS was reloaded, try to rebuild this block from its token."""
super().notify_style_update()
self.rebuild()
def rebuild(self) -> None:
"""Rebuild the content of the block if we have a source token."""
if self._token is not None:
self.build_from_token(self._token)
def build_from_token(self, token: Token) -> None:
"""Build the block content from its source token.
This method allows the block to be rebuilt on demand, which is useful
when the styles assigned to the
[Markdown.COMPONENT_CLASSES][textual.widgets.Markdown.COMPONENT_CLASSES]
change.
See https://github.com/Textualize/textual/issues/3464 for more information.
Args:
token: The token from which this block is built.
"""
self._token = token
style_stack: list[Style] = [Style()]
content = Text()
if token.children:
for child in token.children:
if child.type == "text":
content.append(
# Ensure repeating spaces and/or tabs get squashed
# down to a single space.
re.sub(r"\s+", " ", child.content),
style_stack[-1],
)
if child.type == "hardbreak":
content.append("\n")
if child.type == "softbreak":
content.append(" ", style_stack[-1])
elif child.type == "code_inline":
content.append(
child.content,
style_stack[-1]
+ self._markdown.get_component_rich_style(
"code_inline", partial=True
),
)
elif child.type == "em_open":
style_stack.append(
style_stack[-1]
+ self._markdown.get_component_rich_style("em", partial=True)
)
elif child.type == "strong_open":
style_stack.append(
style_stack[-1]
+ self._markdown.get_component_rich_style(
"strong", partial=True
)
)
elif child.type == "s_open":
style_stack.append(
style_stack[-1]
+ self._markdown.get_component_rich_style("s", partial=True)
)
elif child.type == "link_open":
href = child.attrs.get("href", "")
action = f"link({href!r})"
style_stack.append(
style_stack[-1] + Style.from_meta({"@click": action})
)
elif child.type == "image":
href = child.attrs.get("src", "")
alt = child.attrs.get("alt", "")
action = f"link({href!r})"
style_stack.append(
style_stack[-1] + Style.from_meta({"@click": action})
)
content.append("🖼 ", style_stack[-1])
if alt:
content.append(f"({alt})", style_stack[-1])
if child.children is not None:
for grandchild in child.children:
content.append(grandchild.content, style_stack[-1])
style_stack.pop()
elif child.type.endswith("_close"):
style_stack.pop()
self.set_content(content)
class MarkdownHeader(MarkdownBlock):
"""Base class for a Markdown header."""
DEFAULT_CSS = """
MarkdownHeader {
color: $text;
margin: 2 0 1 0;
}
"""
class MarkdownH1(MarkdownHeader):
"""An H1 Markdown header."""
DEFAULT_CSS = """
MarkdownH1 {
content-align: center middle;
color: $markdown-h1-color;
background: $markdown-h1-background;
text-style: $markdown-h1-text-style;
}
"""
class MarkdownH2(MarkdownHeader):
"""An H2 Markdown header."""
DEFAULT_CSS = """
MarkdownH2 {
color: $markdown-h2-color;
background: $markdown-h2-background;
text-style: $markdown-h2-text-style;
}
"""
class MarkdownH3(MarkdownHeader):
"""An H3 Markdown header."""
DEFAULT_CSS = """
MarkdownH3 {
color: $markdown-h3-color;
background: $markdown-h3-background;
text-style: $markdown-h3-text-style;
margin: 1 0;
width: auto;
}
"""
class MarkdownH4(MarkdownHeader):
"""An H4 Markdown header."""
DEFAULT_CSS = """
MarkdownH4 {
color: $markdown-h4-color;
background: $markdown-h4-background;
text-style: $markdown-h4-text-style;
margin: 1 0;
}
"""
class MarkdownH5(MarkdownHeader):
"""An H5 Markdown header."""
DEFAULT_CSS = """
MarkdownH5 {
color: $markdown-h5-color;
background: $markdown-h5-background;
text-style: $markdown-h5-text-style;
margin: 1 0;
}
"""
class MarkdownH6(MarkdownHeader):
"""An H6 Markdown header."""
DEFAULT_CSS = """
MarkdownH6 {
color: $markdown-h6-color;
background: $markdown-h6-background;
text-style: $markdown-h6-text-style;
margin: 1 0;
}
"""
class MarkdownHorizontalRule(MarkdownBlock):
"""A horizontal rule."""
DEFAULT_CSS = """
MarkdownHorizontalRule {
border-bottom: heavy $secondary;
height: 1;
padding-top: 1;
margin-bottom: 1;
}
"""
class MarkdownParagraph(MarkdownBlock):
"""A paragraph Markdown block."""
SCOPED_CSS = False
DEFAULT_CSS = """
Markdown > MarkdownParagraph {
margin: 0 0 1 0;
}
"""
class MarkdownBlockQuote(MarkdownBlock):
"""A block quote Markdown block."""
DEFAULT_CSS = """
MarkdownBlockQuote {
background: $boost;
border-left: outer $success-darken-2;
margin: 1 0;
padding: 0 1;
}
MarkdownBlockQuote:light {
border-left: outer $secondary;
}
MarkdownBlockQuote > BlockQuote {
margin-left: 2;
margin-top: 1;
}
"""
class MarkdownList(MarkdownBlock):
DEFAULT_CSS = """
MarkdownList {
width: 1fr;
}
MarkdownList MarkdownList {
margin: 0;
padding-top: 0;
}
"""
class MarkdownBulletList(MarkdownList):
"""A Bullet list Markdown block."""
DEFAULT_CSS = """
MarkdownBulletList {
margin: 0 0 1 0;
padding: 0 0;
}
MarkdownBulletList Horizontal {
height: auto;
width: 1fr;
}
MarkdownBulletList Vertical {
height: auto;
width: 1fr;
}
"""
def compose(self) -> ComposeResult:
for block in self._blocks:
if isinstance(block, MarkdownListItem):
bullet = MarkdownBullet()
bullet.symbol = block.bullet
yield Horizontal(bullet, Vertical(*block._blocks))
self._blocks.clear()
class MarkdownOrderedList(MarkdownList):
"""An ordered list Markdown block."""
DEFAULT_CSS = """
MarkdownOrderedList {
margin: 0 0 1 0;
padding: 0 0;
}
MarkdownOrderedList Horizontal {
height: auto;
width: 1fr;
}
MarkdownOrderedList Vertical {
height: auto;
width: 1fr;
}
"""
def compose(self) -> ComposeResult:
suffix = ". "
start = 1
if self._blocks and isinstance(self._blocks[0], MarkdownOrderedListItem):
try:
start = int(self._blocks[0].bullet)
except ValueError:
pass
symbol_size = max(
len(f"{number}{suffix}")
for number, block in enumerate(self._blocks, start)
if isinstance(block, MarkdownListItem)
)
for number, block in enumerate(self._blocks, start):
if isinstance(block, MarkdownListItem):
bullet = MarkdownBullet()
bullet.symbol = f"{number}{suffix}".rjust(symbol_size + 1)
yield Horizontal(bullet, Vertical(*block._blocks))
self._blocks.clear()
class MarkdownTableContent(Widget):
"""Renders a Markdown table."""
DEFAULT_CSS = """
MarkdownTableContent {
width: 100%;
height: auto;
}
MarkdownTableContent > .markdown-table--header {
text-style: bold;
}
"""
COMPONENT_CLASSES = {"markdown-table--header", "markdown-table--lines"}
def __init__(self, headers: list[Text], rows: list[list[Text]]):
self.headers = headers
"""List of header text."""
self.rows = rows
"""The row contents."""
super().__init__()
self.shrink = True
def render(self) -> Table:
table = Table(
expand=True,
box=box.SIMPLE_HEAVY,
style=self.rich_style,
header_style=self.get_component_rich_style("markdown-table--header"),
border_style=self.get_component_rich_style("markdown-table--lines"),
collapse_padding=True,
padding=0,
)
for header in self.headers:
table.add_column(header)
for row in self.rows:
if row:
table.add_row(*row)
return table
async def action_link(self, href: str) -> None:
"""Pass a link action on to the MarkdownTable parent."""
if isinstance(self.parent, MarkdownTable):
await self.parent.action_link(href)
class MarkdownTable(MarkdownBlock):
"""A Table markdown Block."""
DEFAULT_CSS = """
MarkdownTable {
width: 100%;
background: $surface;
}
"""
def compose(self) -> ComposeResult:
def flatten(block: MarkdownBlock) -> Iterable[MarkdownBlock]:
for block in block._blocks:
if block._blocks:
yield from flatten(block)
yield block
headers: list[Text] = []
rows: list[list[Text]] = []
for block in flatten(self):
if isinstance(block, MarkdownTH):
headers.append(block._text)
elif isinstance(block, MarkdownTR):
rows.append([])
elif isinstance(block, MarkdownTD):
rows[-1].append(block._text)
yield MarkdownTableContent(headers, rows)
self._blocks.clear()
class MarkdownTBody(MarkdownBlock):
"""A table body Markdown block."""
class MarkdownTHead(MarkdownBlock):
"""A table head Markdown block."""
class MarkdownTR(MarkdownBlock):
"""A table row Markdown block."""
class MarkdownTH(MarkdownBlock):
"""A table header Markdown block."""
class MarkdownTD(MarkdownBlock):
"""A table data Markdown block."""
class MarkdownBullet(Widget):
"""A bullet widget."""
DEFAULT_CSS = """
MarkdownBullet {
width: auto;
color: $success;
text-style: bold;
&:light {
color: $secondary;
}
}
"""
symbol = reactive("\u25cf")
"""The symbol for the bullet."""
def render(self) -> Text:
return Text(self.symbol)
class MarkdownListItem(MarkdownBlock):
"""A list item Markdown block."""
DEFAULT_CSS = """
MarkdownListItem {
layout: horizontal;
margin-right: 1;
height: auto;
}
MarkdownListItem > Vertical {
width: 1fr;
height: auto;
}
"""
def __init__(self, markdown: Markdown, bullet: str) -> None:
self.bullet = bullet
super().__init__(markdown)
class MarkdownOrderedListItem(MarkdownListItem):
pass
class MarkdownUnorderedListItem(MarkdownListItem):
pass
class MarkdownFence(MarkdownBlock):
"""A fence Markdown block."""
DEFAULT_CSS = """
MarkdownFence {
margin: 1 0;
overflow: auto;
width: 100%;
height: auto;
max-height: 20;
color: rgb(210,210,210);
}
MarkdownFence > * {
width: auto;
}
"""
def __init__(self, markdown: Markdown, code: str, lexer: str) -> None:
super().__init__(markdown)
self.code = code
self.lexer = lexer
self.theme = (
self._markdown.code_dark_theme
if self.app.current_theme.dark
else self._markdown.code_light_theme
)
def _block(self) -> Syntax:
return Syntax(
self.code,
lexer=self.lexer,
word_wrap=False,
indent_guides=True,
padding=(1, 2),
theme=self.theme,
)
def _on_mount(self, _: Mount) -> None:
"""Watch app theme switching."""
self.watch(self.app, "theme", self._retheme)
def _retheme(self) -> None:
"""Rerender when the theme changes."""
self.theme = (
self._markdown.code_dark_theme
if self.app.current_theme.dark
else self._markdown.code_light_theme
)
self.get_child_by_type(Static).update(self._block())
def compose(self) -> ComposeResult:
yield Static(
self._block(),
expand=True,
shrink=False,
)
HEADINGS = {
"h1": MarkdownH1,
"h2": MarkdownH2,
"h3": MarkdownH3,
"h4": MarkdownH4,
"h5": MarkdownH5,
"h6": MarkdownH6,
}
NUMERALS = " ⅠⅡⅢⅣⅤⅥ"
class Markdown(Widget):
DEFAULT_CSS = """
Markdown {
height: auto;
padding: 0 2 1 2;
layout: vertical;
color: $foreground;
background: $surface;
overflow-y: auto;
&:focus {
background-tint: $foreground 5%;
}
}
.em {
text-style: italic;
}
.strong {
text-style: bold;
}
.s {
text-style: strike;
}
.code_inline {
text-style: bold dim;
}
"""
COMPONENT_CLASSES = {"em", "strong", "s", "code_inline"}
"""
These component classes target standard inline markdown styles.
Changing these will potentially break the standard markdown formatting.
| Class | Description |
| :- | :- |
| `code_inline` | Target text that is styled as inline code. |
| `em` | Target text that is emphasized inline. |
| `s` | Target text that is styled inline with strikethrough. |
| `strong` | Target text that is styled inline with strong. |
"""
BULLETS = ["\u25cf ", "▪ ", "‣ ", "• ", "⭑ "]
code_dark_theme: reactive[str] = reactive("material")
"""The theme to use for code blocks when the App theme is dark."""
code_light_theme: reactive[str] = reactive("material-light")
"""The theme to use for code blocks when the App theme is light."""
def __init__(
self,
markdown: str | None = None,
*,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
parser_factory: Callable[[], MarkdownIt] | None = None,
open_links: bool = True,
):
"""A Markdown widget.
Args:
markdown: String containing Markdown or None to leave blank for now.
name: The name of the widget.
id: The ID of the widget in the DOM.
classes: The CSS classes of the widget.
parser_factory: A factory function to return a configured MarkdownIt instance. If `None`, a "gfm-like" parser is used.
open_links: Open links automatically. If you set this to `False`, you can handle the [`LinkClicked`][textual.widgets.markdown.Markdown.LinkClicked] events.
"""
super().__init__(name=name, id=id, classes=classes)
self._markdown = markdown
self._parser_factory = parser_factory
self._table_of_contents: TableOfContentsType | None = None
self._open_links = open_links
class TableOfContentsUpdated(Message):
"""The table of contents was updated."""
def __init__(
self, markdown: Markdown, table_of_contents: TableOfContentsType
) -> None:
super().__init__()
self.markdown: Markdown = markdown
"""The `Markdown` widget associated with the table of contents."""
self.table_of_contents: TableOfContentsType = table_of_contents
"""Table of contents."""
@property
def control(self) -> Markdown:
"""The `Markdown` widget associated with the table of contents.
This is an alias for [`TableOfContentsUpdated.markdown`][textual.widgets.Markdown.TableOfContentsSelected.markdown]
and is used by the [`on`][textual.on] decorator.
"""
return self.markdown
class TableOfContentsSelected(Message):
"""An item in the TOC was selected."""
def __init__(self, markdown: Markdown, block_id: str) -> None:
super().__init__()
self.markdown: Markdown = markdown
"""The `Markdown` widget where the selected item is."""
self.block_id: str = block_id
"""ID of the block that was selected."""
@property
def control(self) -> Markdown:
"""The `Markdown` widget where the selected item is.
This is an alias for [`TableOfContentsSelected.markdown`][textual.widgets.Markdown.TableOfContentsSelected.markdown]
and is used by the [`on`][textual.on] decorator.
"""
return self.markdown
class LinkClicked(Message):
"""A link in the document was clicked."""
def __init__(self, markdown: Markdown, href: str) -> None:
super().__init__()
self.markdown: Markdown = markdown
"""The `Markdown` widget containing the link clicked."""
self.href: str = unquote(href)
"""The link that was selected."""
@property
def control(self) -> Markdown:
"""The `Markdown` widget containing the link clicked.
This is an alias for [`LinkClicked.markdown`][textual.widgets.Markdown.LinkClicked.markdown]
and is used by the [`on`][textual.on] decorator.
"""
return self.markdown
async def _on_mount(self, _: Mount) -> None:
if self._markdown is not None:
await self.update(self._markdown)
def on_markdown_link_clicked(self, event: LinkClicked) -> None:
if self._open_links:
self.app.open_url(event.href)
def _watch_code_dark_theme(self) -> None:
"""React to the dark theme being changed."""
if self.app.current_theme.dark:
for block in self.query(MarkdownFence):
block._retheme()
def _watch_code_light_theme(self) -> None:
"""React to the light theme being changed."""
if not self.app.current_theme.dark:
for block in self.query(MarkdownFence):
block._retheme()
@staticmethod
def sanitize_location(location: str) -> tuple[Path, str]:
"""Given a location, break out the path and any anchor.
Args:
location: The location to sanitize.
Returns:
A tuple of the path to the location cleaned of any anchor, plus
the anchor (or an empty string if none was found).
"""
location, _, anchor = location.partition("#")
return Path(location), anchor
def goto_anchor(self, anchor: str) -> bool:
"""Try and find the given anchor in the current document.
Args:
anchor: The anchor to try and find.
Note:
The anchor is found by looking at all of the headings in the
document and finding the first one whose slug matches the
anchor.
Note that the slugging method used is similar to that found on
GitHub.
Returns:
True when the anchor was found in the current document, False otherwise.
"""
if not self._table_of_contents or not isinstance(self.parent, Widget):
return False
unique = TrackedSlugs()
for _, title, header_id in self._table_of_contents:
if unique.slug(title) == anchor:
self.query_one(f"#{header_id}").scroll_visible(top=True)
return True
return False
async def load(self, path: Path) -> None:
"""Load a new Markdown document.
Args:
path: Path to the document.
Raises:
OSError: If there was some form of error loading the document.
Note:
The exceptions that can be raised by this method are all of
those that can be raised by calling [`Path.read_text`][pathlib.Path.read_text].
"""
path, anchor = self.sanitize_location(str(path))
data = await asyncio.get_running_loop().run_in_executor(
None, partial(path.read_text, encoding="utf-8")
)
await self.update(data)
if anchor:
self.goto_anchor(anchor)
def unhandled_token(self, token: Token) -> MarkdownBlock | None:
"""Process an unhandled token.
Args:
token: The MarkdownIt token to handle.
Returns:
Either a widget to be added to the output, or `None`.
"""
return None
def update(self, markdown: str) -> AwaitComplete:
"""Update the document with new Markdown.
Args:
markdown: A string containing Markdown.
Returns:
An optionally awaitable object. Await this to ensure that all children have been mounted.
"""
parser = (
MarkdownIt("gfm-like")
if self._parser_factory is None
else self._parser_factory()
)
table_of_contents = []
def parse_markdown(tokens) -> Iterable[MarkdownBlock]:
"""Create a stream of MarkdownBlock widgets from markdown.
Args:
tokens: List of tokens
Yields:
Widgets for mounting.
"""
stack: list[MarkdownBlock] = []
stack_append = stack.append
block_id: int = 0
for token in tokens:
token_type = token.type
if token_type == "heading_open":
block_id += 1
stack_append(HEADINGS[token.tag](self, id=f"block{block_id}"))
elif token_type == "hr":
yield MarkdownHorizontalRule(self)
elif token_type == "paragraph_open":
stack_append(MarkdownParagraph(self))
elif token_type == "blockquote_open":
stack_append(MarkdownBlockQuote(self))
elif token_type == "bullet_list_open":
stack_append(MarkdownBulletList(self))
elif token_type == "ordered_list_open":
stack_append(MarkdownOrderedList(self))
elif token_type == "list_item_open":
if token.info:
stack_append(MarkdownOrderedListItem(self, token.info))
else:
item_count = sum(
1
for block in stack
if isinstance(block, MarkdownUnorderedListItem)
)
stack_append(
MarkdownUnorderedListItem(
self,
self.BULLETS[item_count % len(self.BULLETS)],
)
)
elif token_type == "table_open":
stack_append(MarkdownTable(self))
elif token_type == "tbody_open":
stack_append(MarkdownTBody(self))
elif token_type == "thead_open":
stack_append(MarkdownTHead(self))
elif token_type == "tr_open":
stack_append(MarkdownTR(self))
elif token_type == "th_open":
stack_append(MarkdownTH(self))
elif token_type == "td_open":
stack_append(MarkdownTD(self))
elif token_type.endswith("_close"):
block = stack.pop()
if token.type == "heading_close":
heading = block._text.plain
level = int(token.tag[1:])
table_of_contents.append((level, heading, block.id))
if stack:
stack[-1]._blocks.append(block)
else:
yield block
elif token_type == "inline":
stack[-1].build_from_token(token)
elif token_type in ("fence", "code_block"):
fence = MarkdownFence(self, token.content.rstrip(), token.info)
if stack:
stack[-1]._blocks.append(fence)
else:
yield fence
else:
external = self.unhandled_token(token)
if external is not None:
if stack:
stack[-1]._blocks.append(external)
else:
yield external
markdown_block = self.query("MarkdownBlock")
async def await_update() -> None:
"""Update in batches."""
BATCH_SIZE = 200
batch: list[MarkdownBlock] = []
tokens = await asyncio.get_running_loop().run_in_executor(