|
1 | 1 | from abc import ABC, abstractmethod
|
2 | 2 | from enum import IntEnum, auto
|
3 | 3 | from types import SimpleNamespace
|
4 |
| -from typing import Union, List, Dict |
| 4 | +from typing import Callable, Match, Union, List, Dict |
5 | 5 | import re
|
6 | 6 |
|
7 | 7 |
|
8 | 8 | class Directive:
|
9 | 9 | def __init__(
|
10 |
| - self, pattern: str, replacement: str, |
| 10 | + self, pattern: str, replacement: Union[str, Callable[[Match], str]], |
11 | 11 | name: Union[str, None] = None,
|
12 | 12 | flags: int = 0
|
13 | 13 | ):
|
@@ -249,7 +249,7 @@ def inline_markdown(self):
|
249 | 249 | ),
|
250 | 250 | Directive(
|
251 | 251 | pattern=r'`(?P<label>[^<`]+?)(\n?)<(?P<url>[^>`]+)>`_+',
|
252 |
| - replacement=r'[\g<label>](\g<url>)' |
| 252 | + replacement=lambda m: '[' + m.group('label') + '](' + re.sub(r"\s+", "", m.group('url')) + ')' |
253 | 253 | ),
|
254 | 254 | Directive(
|
255 | 255 | pattern=r':mod:`(?P<label>[^`]+)`',
|
@@ -316,7 +316,7 @@ def inline_markdown(self):
|
316 | 316 |
|
317 | 317 | ESCAPING_RULES: List[Directive] = [
|
318 | 318 | Directive(
|
319 |
| - pattern=r'__(?P<text>\S+)__', |
| 319 | + pattern=r'(?<!`)__(?P<text>\S+)__(?!`)', |
320 | 320 | replacement=r'\_\_\g<text>\_\_'
|
321 | 321 | )
|
322 | 322 | ]
|
|
0 commit comments