2
2
3
3
from collections .abc import Callable , Generator , Iterable , Mapping , MutableMapping
4
4
from contextlib import contextmanager
5
- from typing import Any
5
+ from typing import Any , Literal , overload
6
6
7
7
from . import helpers , presets # noqa F401
8
8
from .common import normalize_url , utils # noqa F401
12
12
from .renderer import RendererHTML , RendererProtocol
13
13
from .rules_core .state_core import StateCore
14
14
from .token import Token
15
- from .utils import OptionsDict
15
+ from .utils import EnvType , OptionsDict , OptionsType , PresetType
16
16
17
17
try :
18
18
import linkify_it
19
19
except ModuleNotFoundError :
20
20
linkify_it = None
21
21
22
22
23
- _PRESETS = {
23
+ _PRESETS : dict [ str , PresetType ] = {
24
24
"default" : presets .default .make (),
25
25
"js-default" : presets .js_default .make (),
26
26
"zero" : presets .zero .make (),
32
32
class MarkdownIt :
33
33
def __init__ (
34
34
self ,
35
- config : str | Mapping = "commonmark" ,
36
- options_update : Mapping | None = None ,
35
+ config : str | PresetType = "commonmark" ,
36
+ options_update : Mapping [ str , Any ] | None = None ,
37
37
* ,
38
38
renderer_cls : Callable [[MarkdownIt ], RendererProtocol ] = RendererHTML ,
39
39
):
@@ -67,6 +67,26 @@ def __init__(
67
67
def __repr__ (self ) -> str :
68
68
return f"{ self .__class__ .__module__ } .{ self .__class__ .__name__ } ()"
69
69
70
+ @overload
71
+ def __getitem__ (self , name : Literal ["inline" ]) -> ParserInline :
72
+ ...
73
+
74
+ @overload
75
+ def __getitem__ (self , name : Literal ["block" ]) -> ParserBlock :
76
+ ...
77
+
78
+ @overload
79
+ def __getitem__ (self , name : Literal ["core" ]) -> ParserCore :
80
+ ...
81
+
82
+ @overload
83
+ def __getitem__ (self , name : Literal ["renderer" ]) -> RendererProtocol :
84
+ ...
85
+
86
+ @overload
87
+ def __getitem__ (self , name : str ) -> Any :
88
+ ...
89
+
70
90
def __getitem__ (self , name : str ) -> Any :
71
91
return {
72
92
"inline" : self .inline ,
@@ -75,7 +95,7 @@ def __getitem__(self, name: str) -> Any:
75
95
"renderer" : self .renderer ,
76
96
}[name ]
77
97
78
- def set (self , options : MutableMapping ) -> None :
98
+ def set (self , options : OptionsType ) -> None :
79
99
"""Set parser options (in the same format as in constructor).
80
100
Probably, you will never need it, but you can change options after constructor call.
81
101
@@ -86,7 +106,7 @@ def set(self, options: MutableMapping) -> None:
86
106
self .options = OptionsDict (options )
87
107
88
108
def configure (
89
- self , presets : str | Mapping , options_update : Mapping | None = None
109
+ self , presets : str | PresetType , options_update : Mapping [ str , Any ] | None = None
90
110
) -> MarkdownIt :
91
111
"""Batch load of all options and component settings.
92
112
This is an internal method, and you probably will not need it.
@@ -108,9 +128,9 @@ def configure(
108
128
109
129
options = config .get ("options" , {}) or {}
110
130
if options_update :
111
- options = {** options , ** options_update }
131
+ options = {** options , ** options_update } # type: ignore
112
132
113
- self .set (options )
133
+ self .set (options ) # type: ignore
114
134
115
135
if "components" in config :
116
136
for name , component in config ["components" ].items ():
@@ -206,15 +226,19 @@ def reset_rules(self) -> Generator[None, None, None]:
206
226
self [chain ].ruler .enableOnly (rules )
207
227
self .inline .ruler2 .enableOnly (chain_rules ["inline2" ])
208
228
209
- def add_render_rule (self , name : str , function : Callable , fmt : str = "html" ) -> None :
229
+ def add_render_rule (
230
+ self , name : str , function : Callable [..., Any ], fmt : str = "html"
231
+ ) -> None :
210
232
"""Add a rule for rendering a particular Token type.
211
233
212
234
Only applied when ``renderer.__output__ == fmt``
213
235
"""
214
236
if self .renderer .__output__ == fmt :
215
237
self .renderer .rules [name ] = function .__get__ (self .renderer ) # type: ignore
216
238
217
- def use (self , plugin : Callable , * params , ** options ) -> MarkdownIt :
239
+ def use (
240
+ self , plugin : Callable [..., None ], * params : Any , ** options : Any
241
+ ) -> MarkdownIt :
218
242
"""Load specified plugin with given params into current parser instance. (chainable)
219
243
220
244
It's just a sugar to call `plugin(md, params)` with curring.
@@ -229,7 +253,7 @@ def func(tokens, idx):
229
253
plugin (self , * params , ** options )
230
254
return self
231
255
232
- def parse (self , src : str , env : MutableMapping | None = None ) -> list [Token ]:
256
+ def parse (self , src : str , env : EnvType | None = None ) -> list [Token ]:
233
257
"""Parse the source string to a token stream
234
258
235
259
:param src: source string
@@ -252,7 +276,7 @@ def parse(self, src: str, env: MutableMapping | None = None) -> list[Token]:
252
276
self .core .process (state )
253
277
return state .tokens
254
278
255
- def render (self , src : str , env : MutableMapping | None = None ) -> Any :
279
+ def render (self , src : str , env : EnvType | None = None ) -> Any :
256
280
"""Render markdown string into html. It does all magic for you :).
257
281
258
282
:param src: source string
@@ -266,7 +290,7 @@ def render(self, src: str, env: MutableMapping | None = None) -> Any:
266
290
env = {} if env is None else env
267
291
return self .renderer .render (self .parse (src , env ), self .options , env )
268
292
269
- def parseInline (self , src : str , env : MutableMapping | None = None ) -> list [Token ]:
293
+ def parseInline (self , src : str , env : EnvType | None = None ) -> list [Token ]:
270
294
"""The same as [[MarkdownIt.parse]] but skip all block rules.
271
295
272
296
:param src: source string
@@ -286,7 +310,7 @@ def parseInline(self, src: str, env: MutableMapping | None = None) -> list[Token
286
310
self .core .process (state )
287
311
return state .tokens
288
312
289
- def renderInline (self , src : str , env : MutableMapping | None = None ) -> Any :
313
+ def renderInline (self , src : str , env : EnvType | None = None ) -> Any :
290
314
"""Similar to [[MarkdownIt.render]] but for single paragraph content.
291
315
292
316
:param src: source string
0 commit comments