1
1
from contextlib import contextmanager
2
- from typing import Any , Callable , Dict , List , Optional , Union
2
+ from typing import Any , Callable , Dict , Iterable , List , Mapping , Optional , Union
3
3
4
4
from . import helpers , presets # noqa F401
5
5
from .common import utils # noqa F401
28
28
29
29
class MarkdownIt :
30
30
def __init__ (
31
- self , config : Union [str , AttrDict ] = "commonmark" , renderer_cls = RendererHTML
31
+ self , config : Union [str , Mapping ] = "commonmark" , renderer_cls = RendererHTML
32
32
):
33
33
"""Main parser class
34
34
@@ -43,7 +43,7 @@ def __init__(
43
43
44
44
self .utils = utils
45
45
self .helpers = helpers
46
- self .options = {}
46
+ self .options : Dict [ str , Any ] = {}
47
47
self .configure (config )
48
48
49
49
self .linkify = linkify_it .LinkifyIt () if linkify_it else None
@@ -69,7 +69,7 @@ def set(self, options):
69
69
"""
70
70
self .options = options
71
71
72
- def configure (self , presets : Union [str , AttrDict ]):
72
+ def configure (self , presets : Union [str , Mapping ]):
73
73
"""Batch load of all options and component settings.
74
74
This is an internal method, and you probably will not need it.
75
75
But if you will - see available presets and data structure
@@ -87,13 +87,13 @@ def configure(self, presets: Union[str, AttrDict]):
87
87
)
88
88
if not presets :
89
89
raise ValueError ("Wrong `markdown-it` preset, can't be empty" )
90
- presets = AttrDict (presets )
90
+ config = AttrDict (presets )
91
91
92
- if "options" in presets :
93
- self .set (presets .options )
92
+ if "options" in config :
93
+ self .set (config .options )
94
94
95
- if "components" in presets :
96
- for name , component in presets .components .items ():
95
+ if "components" in config :
96
+ for name , component in config .components .items ():
97
97
rules = component .get ("rules" , None )
98
98
if rules :
99
99
self [name ].ruler .enableOnly (rules )
@@ -122,7 +122,7 @@ def get_active_rules(self) -> Dict[str, List[str]]:
122
122
return rules
123
123
124
124
def enable (
125
- self , names : Union [str , List [str ]], ignoreInvalid : bool = False
125
+ self , names : Union [str , Iterable [str ]], ignoreInvalid : bool = False
126
126
) -> "MarkdownIt" :
127
127
"""Enable list or rules. (chainable)
128
128
@@ -155,7 +155,7 @@ def enable(
155
155
return self
156
156
157
157
def disable (
158
- self , names : Union [str , List [str ]], ignoreInvalid : bool = False
158
+ self , names : Union [str , Iterable [str ]], ignoreInvalid : bool = False
159
159
) -> "MarkdownIt" :
160
160
"""The same as [[MarkdownIt.enable]], but turn specified rules off. (chainable)
161
161
@@ -193,7 +193,7 @@ def add_render_rule(self, name: str, function: Callable, fmt="html"):
193
193
Only applied when ``renderer.__output__ == fmt``
194
194
"""
195
195
if self .renderer .__output__ == fmt :
196
- self .renderer .rules [name ] = function .__get__ (self .renderer )
196
+ self .renderer .rules [name ] = function .__get__ (self .renderer ) # type: ignore
197
197
198
198
def use (self , plugin : Callable , * params , ** options ) -> "MarkdownIt" :
199
199
"""Load specified plugin with given params into current parser instance. (chainable)
@@ -225,7 +225,7 @@ def parse(self, src: str, env: Optional[AttrDict] = None) -> List[Token]:
225
225
and then pass updated object to renderer.
226
226
"""
227
227
env = AttrDict () if env is None else env
228
- if not isinstance (env , AttrDict ):
228
+ if not isinstance (env , AttrDict ): # type: ignore
229
229
raise TypeError (f"Input data should be an AttrDict, not { type (env )} " )
230
230
if not isinstance (src , str ):
231
231
raise TypeError (f"Input data should be a string, not { type (src )} " )
@@ -259,7 +259,7 @@ def parseInline(self, src: str, env: Optional[AttrDict] = None) -> List[Token]:
259
259
tokens in `children` property. Also updates `env` object.
260
260
"""
261
261
env = AttrDict () if env is None else env
262
- if not isinstance (env , AttrDict ):
262
+ if not isinstance (env , AttrDict ): # type: ignore
263
263
raise TypeError (f"Input data should be an AttrDict, not { type (env )} " )
264
264
if not isinstance (src , str ):
265
265
raise TypeError (f"Input data should be a string, not { type (src )} " )
0 commit comments