Skip to content

Commit 76fbdf9

Browse files
Move shared sub/superscript regex to utility file
The subscript and superscript plugins both use the same regular expressions. This commit standardizes and centralizes the regex into a utility file.
1 parent 7e38cee commit 76fbdf9

3 files changed

Lines changed: 9 additions & 9 deletions

File tree

mdit_py_plugins/subscript/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,18 @@
1010
from __future__ import annotations
1111

1212
from collections.abc import Sequence
13-
import re
1413

1514
from markdown_it import MarkdownIt
1615
from markdown_it.renderer import RendererHTML
1716
from markdown_it.rules_inline import StateInline
1817
from markdown_it.token import Token
1918
from markdown_it.utils import EnvType, OptionsDict
19+
from mdit_py_plugins.utils import UNESCAPE_RE, WHITESPACE_RE
2020

2121
__all__ = ["sub_plugin"]
2222

2323
TILDE_CHAR = "~"
2424

25-
WHITESPACE_RE = re.compile(r"(^|[^\\])(\\\\)*\s")
26-
UNESCAPE_RE = re.compile(r'\\([ \\!"#$%&\'()*+,.\/:;<=>?@[\]^_`{|}~-])')
27-
2825

2926
def tokenize(state: StateInline, silent: bool) -> bool:
3027
"""Parse a ~subscript~ token."""

mdit_py_plugins/superscript/index.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@
2828
OTHER DEALINGS IN THE SOFTWARE.
2929
"""
3030

31-
import re
32-
3331
from markdown_it import MarkdownIt
3432
from markdown_it.rules_inline import StateInline
35-
36-
UNESCAPE_RE = re.compile(r"\\([ \\!\"#$%&'()*+,./:;<=>?@[\]^_`{|}~-])")
37-
WHITESPACE_RE = re.compile(r"(^|[^\\])(\\\\)*\s")
33+
from mdit_py_plugins.utils import UNESCAPE_RE, WHITESPACE_RE
3834

3935

4036
def superscript_plugin(md: MarkdownIt) -> None:

mdit_py_plugins/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
from markdown_it.rules_block import StateBlock
24

35

@@ -10,3 +12,8 @@ def is_code_block(state: StateBlock, line: int) -> bool:
1012
pass
1113

1214
return (state.sCount[line] - state.blkIndent) >= 4
15+
16+
17+
# Regex for subscript and superscript plugins
18+
UNESCAPE_RE = re.compile(r"\\([ \\!\"#$%&'()*+,./:;<=>?@[\]^_`{|}~-])")
19+
WHITESPACE_RE = re.compile(r"(^|[^\\])(\\\\)*\s")

0 commit comments

Comments
 (0)