1
1
# ` create_parser_hook `
2
2
3
- This hook wraps the standard function that Misago uses to create a markup parser instance.
3
+ This hook wraps the standard function that Misago uses to create a configured MarkdownIt instance.
4
4
5
5
6
6
## Location
@@ -17,15 +17,14 @@ from misago.parser.hooks import create_parser_hook
17
17
``` python
18
18
def custom_create_parser_filter (
19
19
action : CreateParserHookAction,
20
- context : ' ParserContext' ,
21
20
* ,
22
- block_patterns : list[Pattern] ,
23
- inline_patterns : list[Pattern] ,
24
- pre_processors : list[Callable[[Parser, str ],
25
- str ]] ,
26
- post_processors : list [Callable[[Parser, list[ dict ] ],
27
- list[ dict ]]] ,
28
- ) -> Parser :
21
+ config : str | PresetType ,
22
+ options_update : Mapping[ str , Any] | None = None ,
23
+ enable : str | Iterable[ str ] | None = None ,
24
+ disable : str | Iterable[ str ] | None = None ,
25
+ plugins : Iterable [Callable[[MarkdownIt ],
26
+ None ]] | None = None ,
27
+ ) -> MarkdownIt :
29
28
...
30
29
```
31
30
@@ -36,146 +35,120 @@ A function implemented by a plugin that can be registered in this hook.
36
35
37
36
#### ` action: CreateParserHookAction `
38
37
39
- A standard Misago function used to create a markup parser instance or the next filter function from another plugin.
38
+ A standard Misago function used to create a configured MarkdownIt instance or the next filter function from another plugin.
40
39
41
40
See the [ action] ( #action ) section for details.
42
41
43
42
44
- #### ` context: ParserContext `
45
-
46
- An instance of the ` ParserContext ` data class that contains dependencies used during parsing.
47
-
43
+ #### ` config: str | PresetType `
48
44
49
- #### ` block_patterns: list[Pattern] `
45
+ A ` str ` with the name of the preset to use, or a ` PresetType ` instance.
50
46
51
- A list of ` Pattern ` instances of block patterns to be used by the parser.
52
47
48
+ #### ` options_update: Mapping[str, Any] | None = None `
53
49
54
- #### ` inline_patterns: list[Pattern] `
50
+ A ` Mapping ` with preset's options overrides.
55
51
56
- A list of ` Pattern ` instances of inline patterns to be used by the parser.
57
52
53
+ #### ` enable: str | Iterable[str] | None = None `
58
54
59
- #### ` pre_processors: list[Callable[[Parser, str], str]] `
55
+ Argument to call the ` MarkdownIt.enable(...) ` method with. If empty or ` None ` , ` enable() ` will not be called.
60
56
61
- A list of pre-processor functions called by the parser to prepare markup for parsing.
62
57
63
- A pre-processor function should have the following signature:
58
+ #### ` disable: str | Iterable[str] | None = None `
64
59
65
- ``` python
66
- def custom_postprocessor (parser : Parser, markup : str ) -> str :
67
- # Do something with the 'markup'...
68
- return markup
69
- ```
60
+ Argument to call the ` MarkdownIt.disable(...) ` method with. If empty or ` None ` , ` disable() ` will not be called.
70
61
71
62
72
- #### ` post_processors: list [Callable[[Parser, list[dict]], list[dict]]] `
63
+ #### ` plugins: Iterable [Callable[[MarkdownIt], None]] | None = None `
73
64
74
- A list of post-processor functions called by the parser to finalize the AST.
75
-
76
- A post-processor function should have the following signature:
77
-
78
- ``` python
79
- def custom_postprocessor (parser : Parser, ast : list[dict ]) -> list[dict ]:
80
- # Do something with the 'ast'...
81
- return ast
82
- ```
65
+ A list of ` MarkdownIt ` plugins. Each plugin must be a callable that accepts a single argument: the ` MarkdownIt ` instance.
83
66
84
67
85
68
### Return value
86
69
87
- An instance of the ` Parser ` class.
70
+ An instance of the ` MarkdownIt ` class.
88
71
89
72
90
73
## Action
91
74
92
75
``` python
93
76
def create_parser_action (
94
- context : ' ParserContext' ,
95
77
* ,
96
- block_patterns : list[Pattern] ,
97
- inline_patterns : list[Pattern] ,
98
- pre_processors : list[Callable[[Parser, str ],
99
- str ]] ,
100
- post_processors : list [Callable[[Parser, list[ dict ] ],
101
- list[ dict ]]] ,
102
- ) -> Parser :
78
+ config : str | PresetType ,
79
+ options_update : Mapping[ str , Any] | None = None ,
80
+ enable : str | Iterable[ str ] | None = None ,
81
+ disable : str | Iterable[ str ] | None = None ,
82
+ plugins : Iterable [Callable[[MarkdownIt ],
83
+ None ]] | None = None ,
84
+ ) -> MarkdownIt :
103
85
...
104
86
```
105
87
106
- A standard Misago function used to create a markup parser instance or the next filter function from another plugin.
88
+ A standard Misago function used to create a configured MarkdownIt instance or the next filter function from another plugin.
107
89
108
90
109
91
### Arguments
110
92
111
- #### ` context: ParserContext `
93
+ #### ` config: str | PresetType `
112
94
113
- An instance of the ` ParserContext ` data class that contains dependencies used during parsing .
95
+ A ` str ` with the name of the preset to use, or a ` PresetType ` instance .
114
96
115
97
116
- #### ` block_patterns: list[Pattern] `
98
+ #### ` options_update: Mapping[str, Any] | None = None `
117
99
118
- A list of ` Pattern ` instances of block patterns to be used by the parser .
100
+ A ` Mapping ` with preset's options overrides .
119
101
120
102
121
- #### ` inline_patterns: list[Pattern] `
103
+ #### ` enable: str | Iterable[str] | None = None `
122
104
123
- A list of ` Pattern ` instances of inline patterns to be used by the parser .
105
+ Argument to call the ` MarkdownIt.enable(...) ` method with. If empty or ` None ` , ` enable() ` will not be called .
124
106
125
107
126
- #### ` pre_processors: list[Callable[[Parser, str], str]] `
108
+ #### ` disable: str | Iterable[ str] | None = None `
127
109
128
- A list of pre-processor functions called by the parser to prepare markup for parsing .
110
+ Argument to call the ` MarkdownIt.disable(...) ` method with. If empty or ` None ` , ` disable() ` will not be called .
129
111
130
- A pre-processor function should have the following signature:
131
112
132
- ``` python
133
- def custom_postprocessor (parser : Parser, markup : str ) -> str :
134
- # Do something with the 'markup'...
135
- return markup
136
- ```
137
-
138
-
139
- #### ` post_processors: list[Callable[[Parser, list[dict]], list[dict]]] `
140
-
141
- A list of post-processor functions called by the parser to finalize the AST.
142
-
143
- A post-processor function should have the following signature:
113
+ #### ` plugins: Iterable[Callable[[MarkdownIt], None]] | None = None `
144
114
145
- ``` python
146
- def custom_postprocessor (parser : Parser, ast : list[dict ]) -> list[dict ]:
147
- # Do something with the 'ast'...
148
- return ast
149
- ```
115
+ A list of ` MarkdownIt ` plugins. Each plugin must be a callable that accepts a single argument: the ` MarkdownIt ` instance.
150
116
151
117
152
118
### Return value
153
119
154
- An instance of the ` Parser ` class.
120
+ An instance of the ` MarkdownIt ` class.
155
121
156
122
157
123
## Example
158
124
159
- The code below implements a custom filter function that adds new block pattern to the parser :
125
+ The code below implements a custom filter function that disables automatic linkification in parsed messages :
160
126
161
127
``` python
162
- from misago.parser.context import ParserContext
163
- from misago.parser.hooks import create_parser_hook
164
- from misago.parser.parser import Parser
128
+ from typing import Any, Callable, Iterable, Mapping
165
129
166
- from .patterns import PluginPattern
130
+ from markdown_it import MarkdownIt
131
+ from markdown_it.utils import PresetType
132
+ from misago.parser.hooks import create_parser_hook
167
133
168
134
169
135
@create_parser_hook.append_filter
170
- def create_parser_with_custom_pattern (
136
+ def create_customized_parser (
171
137
action ,
172
- context : ParserContext,
173
138
* ,
174
- block_patterns ,
175
- ** kwargs ,
139
+ config : str | PresetType,
140
+ options_update : Mapping[str , Any] | None = None ,
141
+ enable : str | Iterable[str ] | None = None ,
142
+ disable : str | Iterable[str ] | None = None ,
143
+ plugins : Iterable[Callable[[MarkdownIt], None ]] | None = None ,
176
144
) -> Parser:
177
- block_patterns.append(PluginPattern)
178
-
179
- # Call the next function in chain
180
- return action(context, block_patterns = block_patterns, ** kwargs)
145
+ options_update[" linkify" ] = False
146
+
147
+ return action(
148
+ config = config,
149
+ options_update = options_update,
150
+ enable = enable,
151
+ disable = disable,
152
+ plugins = plugins,
153
+ )
181
154
```
0 commit comments