1
1
from __future__ import annotations
2
2
3
- import functools
4
3
import itertools
5
4
import re
6
5
from abc import ABC , abstractmethod
@@ -224,7 +223,7 @@ def __init__(self) -> None:
224
223
("*" , self ._format_bold ),
225
224
("_" , self ._format_italic ),
226
225
("``" , self ._format_code ),
227
- ("" , functools . partial ( LinkFormatter ().format_link ) ),
226
+ ("" , LinkFormatter ().format_link ),
228
227
]
229
228
230
229
def format (self , line : str ) -> str :
@@ -249,9 +248,10 @@ def _format_code(self, line: str) -> str:
249
248
return self ._code .sub ("\\ 1`\\ 3`" , line )
250
249
251
250
252
- class PreformattedFormatter ( Formatter ):
253
- _format_line = functools . partial ( LineFormatter (). format )
251
+ _line_formatter = LineFormatter ()
252
+
254
253
254
+ class PreformattedFormatter (Formatter ):
255
255
def _handles (self , line : str ) -> bool :
256
256
return line .startswith ("| " ) or line == "|"
257
257
@@ -261,8 +261,6 @@ def format(self, lines: List[str]) -> str:
261
261
262
262
263
263
class ParagraphFormatter (Formatter ):
264
- _format_line = functools .partial (LineFormatter ().format )
265
-
266
264
def __init__ (self , other_formatters : List [Formatter ]) -> None :
267
265
super ().__init__ ()
268
266
self ._other_formatters = other_formatters
@@ -271,18 +269,17 @@ def _handles(self, line: str) -> bool:
271
269
return not any (other .handles (line ) for other in self ._other_formatters )
272
270
273
271
def format (self , lines : List [str ]) -> str :
274
- return self . _format_line (" " .join (lines )) + "\n \n "
272
+ return _line_formatter . format (" " .join (lines )) + "\n \n "
275
273
276
274
277
275
class ListFormatter (Formatter ):
278
276
_strip_lines = False
279
- _format_item = functools .partial (LineFormatter ().format )
280
277
281
278
def _handles (self , line : str ) -> bool :
282
279
return bool (line .strip ().startswith ("- " ) or line .startswith (" " ) and self ._lines )
283
280
284
281
def format (self , lines : List [str ]) -> str :
285
- items = ["- %s" % self . _format_item (line ) for line in self ._combine_lines (lines )]
282
+ items = ["- %s" % _line_formatter . format (line ) for line in self ._combine_lines (lines )]
286
283
return "\n " .join (items ) + "\n \n "
287
284
288
285
def _combine_lines (self , lines : List [str ]) -> Iterator [str ]:
@@ -311,7 +308,7 @@ def format_line(self, line: str) -> str:
311
308
class TableFormatter (Formatter ):
312
309
_table_line = re .compile (r"^\| (.* |)\|$" )
313
310
_line_splitter = re .compile (r" \|(?= )" )
314
- _format_cell_content = functools . partial ( LineFormatter (). format )
311
+ _format_cell_content = _line_formatter . format
315
312
316
313
def _handles (self , line : str ) -> bool :
317
314
return self ._table_line .match (line ) is not None
@@ -351,4 +348,4 @@ def _format_cell(self, content: str) -> str:
351
348
if content .startswith ("=" ) and content .endswith ("=" ):
352
349
content = content [1 :- 1 ]
353
350
354
- return f" { self . _format_cell_content (content ).strip ()} "
351
+ return f" { _line_formatter . format (content ).strip ()} "
0 commit comments