1
1
from __future__ import annotations
2
2
3
3
import sys
4
- from typing import ClassVar , Dict , Iterable , List , Optional , Type , Union
4
+ from typing import ClassVar , Iterable
5
5
6
6
from markdown_it import MarkdownIt
7
7
from markdown_it .token import Token
@@ -31,7 +31,7 @@ class MarkdownElement:
31
31
new_line : ClassVar [bool ] = True
32
32
33
33
@classmethod
34
- def create (cls , markdown : " Markdown" , token : Token ) -> " MarkdownElement" :
34
+ def create (cls , markdown : Markdown , token : Token ) -> MarkdownElement :
35
35
"""Factory to create markdown element,
36
36
37
37
Args:
@@ -43,30 +43,28 @@ def create(cls, markdown: "Markdown", token: Token) -> "MarkdownElement":
43
43
"""
44
44
return cls ()
45
45
46
- def on_enter (self , context : " MarkdownContext" ) -> None :
46
+ def on_enter (self , context : MarkdownContext ) -> None :
47
47
"""Called when the node is entered.
48
48
49
49
Args:
50
50
context (MarkdownContext): The markdown context.
51
51
"""
52
52
53
- def on_text (self , context : " MarkdownContext" , text : TextType ) -> None :
53
+ def on_text (self , context : MarkdownContext , text : TextType ) -> None :
54
54
"""Called when text is parsed.
55
55
56
56
Args:
57
57
context (MarkdownContext): The markdown context.
58
58
"""
59
59
60
- def on_leave (self , context : " MarkdownContext" ) -> None :
60
+ def on_leave (self , context : MarkdownContext ) -> None :
61
61
"""Called when the parser leaves the element.
62
62
63
63
Args:
64
64
context (MarkdownContext): [description]
65
65
"""
66
66
67
- def on_child_close (
68
- self , context : "MarkdownContext" , child : "MarkdownElement"
69
- ) -> bool :
67
+ def on_child_close (self , context : MarkdownContext , child : MarkdownElement ) -> bool :
70
68
"""Called when a child element is closed.
71
69
72
70
This method allows a parent element to take over rendering of its children.
@@ -81,8 +79,8 @@ def on_child_close(
81
79
return True
82
80
83
81
def __rich_console__ (
84
- self , console : " Console" , options : " ConsoleOptions"
85
- ) -> " RenderResult" :
82
+ self , console : Console , options : ConsoleOptions
83
+ ) -> RenderResult :
86
84
return ()
87
85
88
86
@@ -100,14 +98,14 @@ class TextElement(MarkdownElement):
100
98
101
99
style_name = "none"
102
100
103
- def on_enter (self , context : " MarkdownContext" ) -> None :
101
+ def on_enter (self , context : MarkdownContext ) -> None :
104
102
self .style = context .enter_style (self .style_name )
105
103
self .text = Text (justify = "left" )
106
104
107
- def on_text (self , context : " MarkdownContext" , text : TextType ) -> None :
105
+ def on_text (self , context : MarkdownContext , text : TextType ) -> None :
108
106
self .text .append (text , context .current_style if isinstance (text , str ) else None )
109
107
110
- def on_leave (self , context : " MarkdownContext" ) -> None :
108
+ def on_leave (self , context : MarkdownContext ) -> None :
111
109
context .leave_style ()
112
110
113
111
@@ -118,7 +116,7 @@ class Paragraph(TextElement):
118
116
justify : JustifyMethod
119
117
120
118
@classmethod
121
- def create (cls , markdown : " Markdown" , token : Token ) -> " Paragraph" :
119
+ def create (cls , markdown : Markdown , token : Token ) -> Paragraph :
122
120
return cls (justify = markdown .justify or "left" )
123
121
124
122
def __init__ (self , justify : JustifyMethod ) -> None :
@@ -135,10 +133,10 @@ class Heading(TextElement):
135
133
"""A heading."""
136
134
137
135
@classmethod
138
- def create (cls , markdown : " Markdown" , token : Token ) -> " Heading" :
136
+ def create (cls , markdown : Markdown , token : Token ) -> Heading :
139
137
return cls (token .tag )
140
138
141
- def on_enter (self , context : " MarkdownContext" ) -> None :
139
+ def on_enter (self , context : MarkdownContext ) -> None :
142
140
self .text = Text ()
143
141
context .enter_style (self .style_name )
144
142
@@ -172,7 +170,7 @@ class CodeBlock(TextElement):
172
170
style_name = "markdown.code_block"
173
171
174
172
@classmethod
175
- def create (cls , markdown : " Markdown" , token : Token ) -> " CodeBlock" :
173
+ def create (cls , markdown : Markdown , token : Token ) -> CodeBlock :
176
174
node_info = token .info or ""
177
175
lexer_name = node_info .partition (" " )[0 ]
178
176
return cls (lexer_name or "text" , markdown .code_theme )
@@ -199,9 +197,7 @@ class BlockQuote(TextElement):
199
197
def __init__ (self ) -> None :
200
198
self .elements : Renderables = Renderables ()
201
199
202
- def on_child_close (
203
- self , context : "MarkdownContext" , child : "MarkdownElement"
204
- ) -> bool :
200
+ def on_child_close (self , context : MarkdownContext , child : MarkdownElement ) -> bool :
205
201
self .elements .append (child )
206
202
return False
207
203
@@ -238,9 +234,7 @@ def __init__(self) -> None:
238
234
self .header : TableHeaderElement | None = None
239
235
self .body : TableBodyElement | None = None
240
236
241
- def on_child_close (
242
- self , context : "MarkdownContext" , child : "MarkdownElement"
243
- ) -> bool :
237
+ def on_child_close (self , context : MarkdownContext , child : MarkdownElement ) -> bool :
244
238
if isinstance (child , TableHeaderElement ):
245
239
self .header = child
246
240
elif isinstance (child , TableBodyElement ):
@@ -272,9 +266,7 @@ class TableHeaderElement(MarkdownElement):
272
266
def __init__ (self ) -> None :
273
267
self .row : TableRowElement | None = None
274
268
275
- def on_child_close (
276
- self , context : "MarkdownContext" , child : "MarkdownElement"
277
- ) -> bool :
269
+ def on_child_close (self , context : MarkdownContext , child : MarkdownElement ) -> bool :
278
270
assert isinstance (child , TableRowElement )
279
271
self .row = child
280
272
return False
@@ -286,9 +278,7 @@ class TableBodyElement(MarkdownElement):
286
278
def __init__ (self ) -> None :
287
279
self .rows : list [TableRowElement ] = []
288
280
289
- def on_child_close (
290
- self , context : "MarkdownContext" , child : "MarkdownElement"
291
- ) -> bool :
281
+ def on_child_close (self , context : MarkdownContext , child : MarkdownElement ) -> bool :
292
282
assert isinstance (child , TableRowElement )
293
283
self .rows .append (child )
294
284
return False
@@ -298,11 +288,9 @@ class TableRowElement(MarkdownElement):
298
288
"""MarkdownElement corresponding to `tr_open` and `tr_close`."""
299
289
300
290
def __init__ (self ) -> None :
301
- self .cells : List [TableDataElement ] = []
291
+ self .cells : list [TableDataElement ] = []
302
292
303
- def on_child_close (
304
- self , context : "MarkdownContext" , child : "MarkdownElement"
305
- ) -> bool :
293
+ def on_child_close (self , context : MarkdownContext , child : MarkdownElement ) -> bool :
306
294
assert isinstance (child , TableDataElement )
307
295
self .cells .append (child )
308
296
return False
@@ -313,7 +301,7 @@ class TableDataElement(MarkdownElement):
313
301
and `th_open` and `th_close`."""
314
302
315
303
@classmethod
316
- def create (cls , markdown : " Markdown" , token : Token ) -> " MarkdownElement" :
304
+ def create (cls , markdown : Markdown , token : Token ) -> MarkdownElement :
317
305
style = str (token .attrs .get ("style" )) or ""
318
306
319
307
justify : JustifyMethod
@@ -333,7 +321,7 @@ def __init__(self, justify: JustifyMethod) -> None:
333
321
self .content : Text = Text ("" , justify = justify )
334
322
self .justify = justify
335
323
336
- def on_text (self , context : " MarkdownContext" , text : TextType ) -> None :
324
+ def on_text (self , context : MarkdownContext , text : TextType ) -> None :
337
325
text = Text (text ) if isinstance (text , str ) else text
338
326
text .stylize (context .current_style )
339
327
self .content .append_text (text )
@@ -343,17 +331,15 @@ class ListElement(MarkdownElement):
343
331
"""A list element."""
344
332
345
333
@classmethod
346
- def create (cls , markdown : " Markdown" , token : Token ) -> " ListElement" :
334
+ def create (cls , markdown : Markdown , token : Token ) -> ListElement :
347
335
return cls (token .type , int (token .attrs .get ("start" , 1 )))
348
336
349
337
def __init__ (self , list_type : str , list_start : int | None ) -> None :
350
- self .items : List [ListItem ] = []
338
+ self .items : list [ListItem ] = []
351
339
self .list_type = list_type
352
340
self .list_start = list_start
353
341
354
- def on_child_close (
355
- self , context : "MarkdownContext" , child : "MarkdownElement"
356
- ) -> bool :
342
+ def on_child_close (self , context : MarkdownContext , child : MarkdownElement ) -> bool :
357
343
assert isinstance (child , ListItem )
358
344
self .items .append (child )
359
345
return False
@@ -381,9 +367,7 @@ class ListItem(TextElement):
381
367
def __init__ (self ) -> None :
382
368
self .elements : Renderables = Renderables ()
383
369
384
- def on_child_close (
385
- self , context : "MarkdownContext" , child : "MarkdownElement"
386
- ) -> bool :
370
+ def on_child_close (self , context : MarkdownContext , child : MarkdownElement ) -> bool :
387
371
self .elements .append (child )
388
372
return False
389
373
@@ -419,7 +403,7 @@ def render_number(
419
403
420
404
class Link (TextElement ):
421
405
@classmethod
422
- def create (cls , markdown : " Markdown" , token : Token ) -> " MarkdownElement" :
406
+ def create (cls , markdown : Markdown , token : Token ) -> MarkdownElement :
423
407
url = token .attrs .get ("href" , "#" )
424
408
return cls (token .content , str (url ))
425
409
@@ -434,7 +418,7 @@ class ImageItem(TextElement):
434
418
new_line = False
435
419
436
420
@classmethod
437
- def create (cls , markdown : " Markdown" , token : Token ) -> " MarkdownElement" :
421
+ def create (cls , markdown : Markdown , token : Token ) -> MarkdownElement :
438
422
"""Factory to create markdown element,
439
423
440
424
Args:
@@ -449,10 +433,10 @@ def create(cls, markdown: "Markdown", token: Token) -> "MarkdownElement":
449
433
def __init__ (self , destination : str , hyperlinks : bool ) -> None :
450
434
self .destination = destination
451
435
self .hyperlinks = hyperlinks
452
- self .link : Optional [ str ] = None
436
+ self .link : str | None = None
453
437
super ().__init__ ()
454
438
455
- def on_enter (self , context : " MarkdownContext" ) -> None :
439
+ def on_enter (self , context : MarkdownContext ) -> None :
456
440
self .link = context .current_style .link
457
441
self .text = Text (justify = "left" )
458
442
super ().on_enter (context )
@@ -476,15 +460,15 @@ def __init__(
476
460
console : Console ,
477
461
options : ConsoleOptions ,
478
462
style : Style ,
479
- inline_code_lexer : Optional [ str ] = None ,
463
+ inline_code_lexer : str | None = None ,
480
464
inline_code_theme : str = "monokai" ,
481
465
) -> None :
482
466
self .console = console
483
467
self .options = options
484
468
self .style_stack : StyleStack = StyleStack (style )
485
469
self .stack : Stack [MarkdownElement ] = Stack ()
486
470
487
- self ._syntax : Optional [ Syntax ] = None
471
+ self ._syntax : Syntax | None = None
488
472
if inline_code_lexer is not None :
489
473
self ._syntax = Syntax ("" , inline_code_lexer , theme = inline_code_theme )
490
474
@@ -504,7 +488,7 @@ def on_text(self, text: str, node_type: str) -> None:
504
488
else :
505
489
self .stack .top .on_text (self , text )
506
490
507
- def enter_style (self , style_name : Union [ str , Style ] ) -> Style :
491
+ def enter_style (self , style_name : str | Style ) -> Style :
508
492
"""Enter a style context."""
509
493
style = self .console .get_style (style_name , default = "none" )
510
494
self .style_stack .push (style )
@@ -531,7 +515,7 @@ class Markdown(JupyterMixin):
531
515
highlighting, or None for no highlighting. Defaults to None.
532
516
"""
533
517
534
- elements : ClassVar [Dict [str , Type [MarkdownElement ]]] = {
518
+ elements : ClassVar [dict [str , type [MarkdownElement ]]] = {
535
519
"paragraph_open" : Paragraph ,
536
520
"heading_open" : Heading ,
537
521
"fence" : CodeBlock ,
@@ -556,17 +540,17 @@ def __init__(
556
540
self ,
557
541
markup : str ,
558
542
code_theme : str = "monokai" ,
559
- justify : Optional [ JustifyMethod ] = None ,
560
- style : Union [ str , Style ] = "none" ,
543
+ justify : JustifyMethod | None = None ,
544
+ style : str | Style = "none" ,
561
545
hyperlinks : bool = True ,
562
- inline_code_lexer : Optional [ str ] = None ,
563
- inline_code_theme : Optional [ str ] = None ,
546
+ inline_code_lexer : str | None = None ,
547
+ inline_code_theme : str | None = None ,
564
548
) -> None :
565
549
parser = MarkdownIt ().enable ("strikethrough" ).enable ("table" )
566
550
self .markup = markup
567
551
self .parsed = parser .parse (markup )
568
552
self .code_theme = code_theme
569
- self .justify : Optional [ JustifyMethod ] = justify
553
+ self .justify : JustifyMethod | None = justify
570
554
self .style = style
571
555
self .hyperlinks = hyperlinks
572
556
self .inline_code_lexer = inline_code_lexer
@@ -772,7 +756,7 @@ def __rich_console__(
772
756
if args .path == "-" :
773
757
markdown_body = sys .stdin .read ()
774
758
else :
775
- with open (args .path , "rt" , encoding = "utf-8" ) as markdown_file :
759
+ with open (args .path , encoding = "utf-8" ) as markdown_file :
776
760
markdown_body = markdown_file .read ()
777
761
778
762
markdown = Markdown (
0 commit comments