Skip to content

Commit 6f2e2a7

Browse files
authored
🔧 Convert state.srcCharCode -> state.src (#84)
1 parent 85f7ed3 commit 6f2e2a7

File tree

11 files changed

+75
-77
lines changed

11 files changed

+75
-77
lines changed

‎mdit_py_plugins/attrs/index.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import List, Optional
22

33
from markdown_it import MarkdownIt
4-
from markdown_it.common.utils import isSpace
54
from markdown_it.rules_block import StateBlock
65
from markdown_it.rules_core import StateCore
76
from markdown_it.rules_inline import StateInline
@@ -113,7 +112,7 @@ def _find_opening(tokens: List[Token], index: int) -> Optional[int]:
113112

114113

115114
def _span_rule(state: StateInline, silent: bool):
116-
if state.srcCharCode[state.pos] != 0x5B: # /* [ */
115+
if state.src[state.pos] != "[":
117116
return False
118117

119118
maximum = state.posMax
@@ -165,16 +164,16 @@ def _attr_block_rule(
165164
maximum = state.eMarks[startLine]
166165

167166
# if it doesn't start with a {, it's not an attribute block
168-
if state.srcCharCode[pos] != 0x7B: # /* { */
167+
if state.src[pos] != "{":
169168
return False
170169

171170
# find first non-space character from the right
172-
while maximum > pos and isSpace(state.srcCharCode[maximum - 1]):
171+
while maximum > pos and state.src[maximum - 1] in (" ", "\t"):
173172
maximum -= 1
174173
# if it doesn't end with a }, it's not an attribute block
175174
if maximum <= pos:
176175
return False
177-
if state.srcCharCode[maximum - 1] != 0x7D: # /* } */
176+
if state.src[maximum - 1] != "}":
178177
return False
179178

180179
try:

‎mdit_py_plugins/colon_fence.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,14 @@ def _rule(state: StateBlock, startLine: int, endLine: int, silent: bool):
3636
if pos + 3 > maximum:
3737
return False
3838

39-
marker = state.srcCharCode[pos]
39+
marker = state.src[pos]
4040

41-
# /* : */
42-
if marker != 0x3A:
41+
if marker != ":":
4342
return False
4443

4544
# scan marker length
4645
mem = pos
47-
pos = state.skipChars(pos, marker)
46+
pos = _skipCharsStr(state, pos, marker)
4847

4948
length = pos - mem
5049

@@ -77,13 +76,13 @@ def _rule(state: StateBlock, startLine: int, endLine: int, silent: bool):
7776
# test
7877
break
7978

80-
if state.srcCharCode[pos] != marker:
79+
if state.src[pos] != marker:
8180
continue
8281

8382
if is_code_block(state, nextLine):
8483
continue
8584

86-
pos = state.skipChars(pos, marker)
85+
pos = _skipCharsStr(state, pos, marker)
8786

8887
# closing code fence must be at least as long as the opening one
8988
if pos - mem < length:
@@ -113,6 +112,20 @@ def _rule(state: StateBlock, startLine: int, endLine: int, silent: bool):
113112
return True
114113

115114

115+
def _skipCharsStr(state: StateBlock, pos: int, ch: str) -> int:
116+
"""Skip character string from given position."""
117+
# TODO this can be replaced with StateBlock.skipCharsStr in markdown-it-py 3.0.0
118+
while True:
119+
try:
120+
current = state.src[pos]
121+
except IndexError:
122+
break
123+
if current != ch:
124+
break
125+
pos += 1
126+
return pos
127+
128+
116129
def _render(self, tokens, idx, options, env):
117130
token = tokens[idx]
118131
info = unescapeAll(token.info).strip() if token.info else ""

‎mdit_py_plugins/container/index.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Callable, Optional
44

55
from markdown_it import MarkdownIt
6-
from markdown_it.common.utils import charCodeAt
76
from markdown_it.rules_block import StateBlock
87

98
from mdit_py_plugins.utils import is_code_block
@@ -48,7 +47,7 @@ def renderDefault(self, tokens, idx, _options, env):
4847

4948
min_markers = 3
5049
marker_str = marker
51-
marker_char = charCodeAt(marker_str, 0)
50+
marker_char = marker_str[0]
5251
marker_len = len(marker_str)
5352
validate = validate or validateDefault
5453
render = render or renderDefault
@@ -63,7 +62,7 @@ def container_func(state: StateBlock, startLine: int, endLine: int, silent: bool
6362

6463
# Check out the first character quickly,
6564
# this should filter out most of non-containers
66-
if marker_char != state.srcCharCode[start]:
65+
if marker_char != state.src[start]:
6766
return False
6867

6968
# Check out the rest of the marker string
@@ -111,7 +110,7 @@ def container_func(state: StateBlock, startLine: int, endLine: int, silent: bool
111110
# test
112111
break
113112

114-
if marker_char != state.srcCharCode[start]:
113+
if marker_char != state.src[start]:
115114
continue
116115

117116
if is_code_block(state, nextLine):

‎mdit_py_plugins/deflist/index.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def deflist_plugin(md: MarkdownIt):
2424
~ Definition 2b
2525
2626
"""
27-
isSpace = md.utils.isSpace
2827

2928
def skipMarker(state: StateBlock, line: int):
3029
"""Search `[:~][\n ]`, returns next pos after marker on success or -1 on fail."""
@@ -35,9 +34,9 @@ def skipMarker(state: StateBlock, line: int):
3534
return -1
3635

3736
# Check bullet
38-
marker = state.srcCharCode[start]
37+
marker = state.src[start]
3938
start += 1
40-
if marker != 0x7E and marker != 0x3A: # ~ :
39+
if marker != "~" and marker != ":":
4140
return -1
4241

4342
pos = state.skipSpaces(start)
@@ -139,13 +138,10 @@ def deflist(state: StateBlock, startLine: int, endLine: int, silent: bool):
139138
)
140139

141140
while pos < maximum:
142-
ch = state.srcCharCode[pos]
143-
144-
if isSpace(ch):
145-
if ch == 0x09:
146-
offset += 4 - offset % 4
147-
else:
148-
offset += 1
141+
if state.src[pos] == "\t":
142+
offset += 4 - offset % 4
143+
elif state.src[pos] == " ":
144+
offset += 1
149145
else:
150146
break
151147

‎mdit_py_plugins/dollarmath/index.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def is_escaped(state: StateInline, back_pos: int, mod: int = 0) -> bool:
105105
backslashes = 0
106106
while back_pos >= 0:
107107
back_pos = back_pos - 1
108-
if state.srcCharCode[back_pos] == 0x5C: # /* \ */
108+
if state.src[back_pos] == "\\":
109109
backslashes += 1
110110
else:
111111
break
@@ -153,13 +153,13 @@ def _math_inline_dollar(state: StateInline, silent: bool) -> bool:
153153
# TODO options:
154154
# even/odd backslash escaping
155155

156-
if state.srcCharCode[state.pos] != 0x24: # /* $ */
156+
if state.src[state.pos] != "$":
157157
return False
158158

159159
if not allow_space:
160160
# whitespace not allowed straight after opening $
161161
try:
162-
if isWhiteSpace(state.srcCharCode[state.pos + 1]):
162+
if isWhiteSpace(ord(state.src[state.pos + 1])):
163163
return False
164164
except IndexError:
165165
return False
@@ -176,7 +176,7 @@ def _math_inline_dollar(state: StateInline, silent: bool) -> bool:
176176
return False
177177

178178
try:
179-
is_double = allow_double and state.srcCharCode[state.pos + 1] == 0x24
179+
is_double = allow_double and state.src[state.pos + 1] == "$"
180180
except IndexError:
181181
return False
182182

@@ -185,7 +185,7 @@ def _math_inline_dollar(state: StateInline, silent: bool) -> bool:
185185
found_closing = False
186186
while not found_closing:
187187
try:
188-
end = state.srcCharCode.index(0x24, pos)
188+
end = state.src.index("$", pos)
189189
except ValueError:
190190
return False
191191

@@ -194,7 +194,7 @@ def _math_inline_dollar(state: StateInline, silent: bool) -> bool:
194194
continue
195195

196196
try:
197-
if is_double and state.srcCharCode[end + 1] != 0x24:
197+
if is_double and state.src[end + 1] != "$":
198198
pos = end + 1
199199
continue
200200
except IndexError:
@@ -211,7 +211,7 @@ def _math_inline_dollar(state: StateInline, silent: bool) -> bool:
211211
if not allow_space:
212212
# whitespace not allowed straight before closing $
213213
try:
214-
if isWhiteSpace(state.srcCharCode[end - 1]):
214+
if isWhiteSpace(ord(state.src[end - 1])):
215215
return False
216216
except IndexError:
217217
return False
@@ -274,10 +274,7 @@ def _math_block_dollar(
274274
if startPos + 2 > end:
275275
return False
276276

277-
if (
278-
state.srcCharCode[startPos] != 0x24
279-
or state.srcCharCode[startPos + 1] != 0x24
280-
): # /* $ */
277+
if state.src[startPos] != "$" or state.src[startPos + 1] != "$":
281278
return False
282279

283280
# search for end of block

‎mdit_py_plugins/field_list/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ def _fieldlist_rule(state: StateBlock, startLine: int, endLine: int, silent: boo
139139

140140
# find indent to start of body on first line
141141
while pos < maximum:
142-
ch = state.srcCharCode[pos]
142+
ch = state.src[pos]
143143

144-
if ch == 0x09: # \t
144+
if ch == "\t":
145145
first_line_body_indent += (
146146
4 - (first_line_body_indent + state.bsCount[nextLine]) % 4
147147
)
148-
elif ch == 0x20: # \s
148+
elif ch == " ":
149149
first_line_body_indent += 1
150150
else:
151151
break

‎mdit_py_plugins/footnote/index.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from typing import List, Optional
55

66
from markdown_it import MarkdownIt
7-
from markdown_it.common.utils import isSpace
87
from markdown_it.helpers import parseLinkLabel
98
from markdown_it.rules_block import StateBlock
109
from markdown_it.rules_inline import StateInline
@@ -69,23 +68,23 @@ def footnote_def(state: StateBlock, startLine: int, endLine: int, silent: bool):
6968
if start + 4 > maximum:
7069
return False
7170

72-
if state.srcCharCode[start] != 0x5B: # /* [ */
71+
if state.src[start] != "[":
7372
return False
74-
if state.srcCharCode[start + 1] != 0x5E: # /* ^ */
73+
if state.src[start + 1] != "^":
7574
return False
7675

7776
pos = start + 2
7877
while pos < maximum:
79-
if state.srcCharCode[pos] == 0x20:
78+
if state.src[pos] == " ":
8079
return False
81-
if state.srcCharCode[pos] == 0x5D: # /* ] */
80+
if state.src[pos] == "]":
8281
break
8382
pos += 1
8483

8584
if pos == start + 2: # no empty footnote labels
8685
return False
8786
pos += 1
88-
if pos >= maximum or state.srcCharCode[pos] != 0x3A: # /* : */
87+
if pos >= maximum or state.src[pos] != ":":
8988
return False
9089
if silent:
9190
return True
@@ -113,13 +112,12 @@ def footnote_def(state: StateBlock, startLine: int, endLine: int, silent: bool):
113112
)
114113

115114
while pos < maximum:
116-
ch = state.srcCharCode[pos]
115+
ch = state.src[pos]
117116

118-
if isSpace(ch):
119-
if ch == 0x09:
120-
offset += 4 - offset % 4
121-
else:
122-
offset += 1
117+
if ch == "\t":
118+
offset += 4 - offset % 4
119+
elif ch == " ":
120+
offset += 1
123121

124122
else:
125123
break
@@ -162,9 +160,9 @@ def footnote_inline(state: StateInline, silent: bool):
162160

163161
if start + 2 >= maximum:
164162
return False
165-
if state.srcCharCode[start] != 0x5E: # /* ^ */
163+
if state.src[start] != "^":
166164
return False
167-
if state.srcCharCode[start + 1] != 0x5B: # /* [ */
165+
if state.src[start + 1] != "[":
168166
return False
169167

170168
labelStart = start + 2
@@ -208,18 +206,18 @@ def footnote_ref(state: StateInline, silent: bool):
208206

209207
if "footnotes" not in state.env or "refs" not in state.env["footnotes"]:
210208
return False
211-
if state.srcCharCode[start] != 0x5B: # /* [ */
209+
if state.src[start] != "[":
212210
return False
213-
if state.srcCharCode[start + 1] != 0x5E: # /* ^ */
211+
if state.src[start + 1] != "^":
214212
return False
215213

216214
pos = start + 2
217215
while pos < maximum:
218-
if state.srcCharCode[pos] == 0x20:
216+
if state.src[pos] == " ":
219217
return False
220-
if state.srcCharCode[pos] == 0x0A:
218+
if state.src[pos] == "\n":
221219
return False
222-
if state.srcCharCode[pos] == 0x5D: # /* ] */
220+
if state.src[pos] == "]":
223221
break
224222
pos += 1
225223

‎mdit_py_plugins/front_matter/index.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from math import floor
33

44
from markdown_it import MarkdownIt
5-
from markdown_it.common.utils import charCodeAt
65
from markdown_it.rules_block import StateBlock
76

87
from mdit_py_plugins.utils import is_code_block
@@ -33,7 +32,7 @@ def front_matter_plugin(md: MarkdownIt):
3332
def make_front_matter_rule():
3433
min_markers = 3
3534
marker_str = "-"
36-
marker_char = charCodeAt(marker_str, 0)
35+
marker_char = marker_str[0]
3736
marker_len = len(marker_str)
3837

3938
def frontMatter(state: StateBlock, startLine: int, endLine: int, silent: bool):
@@ -44,7 +43,7 @@ def frontMatter(state: StateBlock, startLine: int, endLine: int, silent: bool):
4443

4544
# Check out the first character of the first line quickly,
4645
# this should filter out non-front matter
47-
if startLine != 0 or marker_char != state.srcCharCode[0]:
46+
if startLine != 0 or marker_char != state.src[0]:
4847
return False
4948

5049
# Check out the rest of the marker string
@@ -87,7 +86,7 @@ def frontMatter(state: StateBlock, startLine: int, endLine: int, silent: bool):
8786
# test
8887
break
8988

90-
if marker_char != state.srcCharCode[start]:
89+
if marker_char != state.src[start]:
9190
continue
9291

9392
if is_code_block(state, nextLine):

0 commit comments

Comments
 (0)