Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions mdit_py_plugins/attrs/index.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import List, Optional

from markdown_it import MarkdownIt
from markdown_it.common.utils import isSpace
from markdown_it.rules_block import StateBlock
from markdown_it.rules_core import StateCore
from markdown_it.rules_inline import StateInline
Expand Down Expand Up @@ -113,7 +112,7 @@ def _find_opening(tokens: List[Token], index: int) -> Optional[int]:


def _span_rule(state: StateInline, silent: bool):
if state.srcCharCode[state.pos] != 0x5B: # /* [ */
if state.src[state.pos] != "[":
return False

maximum = state.posMax
Expand Down Expand Up @@ -165,16 +164,16 @@ def _attr_block_rule(
maximum = state.eMarks[startLine]

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

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

try:
Expand Down
25 changes: 19 additions & 6 deletions mdit_py_plugins/colon_fence.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ def _rule(state: StateBlock, startLine: int, endLine: int, silent: bool):
if pos + 3 > maximum:
return False

marker = state.srcCharCode[pos]
marker = state.src[pos]

# /* : */
if marker != 0x3A:
if marker != ":":
return False

# scan marker length
mem = pos
pos = state.skipChars(pos, marker)
pos = _skipCharsStr(state, pos, marker)

length = pos - mem

Expand Down Expand Up @@ -77,13 +76,13 @@ def _rule(state: StateBlock, startLine: int, endLine: int, silent: bool):
# test
break

if state.srcCharCode[pos] != marker:
if state.src[pos] != marker:
continue

if is_code_block(state, nextLine):
continue

pos = state.skipChars(pos, marker)
pos = _skipCharsStr(state, pos, marker)

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


def _skipCharsStr(state: StateBlock, pos: int, ch: str) -> int:
"""Skip character string from given position."""
# TODO this can be replaced with StateBlock.skipCharsStr in markdown-it-py 3.0.0
while True:
try:
current = state.src[pos]
except IndexError:
break
if current != ch:
break
pos += 1
return pos


def _render(self, tokens, idx, options, env):
token = tokens[idx]
info = unescapeAll(token.info).strip() if token.info else ""
Expand Down
7 changes: 3 additions & 4 deletions mdit_py_plugins/container/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Callable, Optional

from markdown_it import MarkdownIt
from markdown_it.common.utils import charCodeAt
from markdown_it.rules_block import StateBlock

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

min_markers = 3
marker_str = marker
marker_char = charCodeAt(marker_str, 0)
marker_char = marker_str[0]
marker_len = len(marker_str)
validate = validate or validateDefault
render = render or renderDefault
Expand All @@ -63,7 +62,7 @@ def container_func(state: StateBlock, startLine: int, endLine: int, silent: bool

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

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

if marker_char != state.srcCharCode[start]:
if marker_char != state.src[start]:
continue

if is_code_block(state, nextLine):
Expand Down
16 changes: 6 additions & 10 deletions mdit_py_plugins/deflist/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def deflist_plugin(md: MarkdownIt):
~ Definition 2b

"""
isSpace = md.utils.isSpace

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

# Check bullet
marker = state.srcCharCode[start]
marker = state.src[start]
start += 1
if marker != 0x7E and marker != 0x3A: # ~ :
if marker != "~" and marker != ":":
return -1

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

while pos < maximum:
ch = state.srcCharCode[pos]

if isSpace(ch):
if ch == 0x09:
offset += 4 - offset % 4
else:
offset += 1
if state.src[pos] == "\t":
offset += 4 - offset % 4
elif state.src[pos] == " ":
offset += 1
else:
break

Expand Down
19 changes: 8 additions & 11 deletions mdit_py_plugins/dollarmath/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def is_escaped(state: StateInline, back_pos: int, mod: int = 0) -> bool:
backslashes = 0
while back_pos >= 0:
back_pos = back_pos - 1
if state.srcCharCode[back_pos] == 0x5C: # /* \ */
if state.src[back_pos] == "\\":
backslashes += 1
else:
break
Expand Down Expand Up @@ -153,13 +153,13 @@ def _math_inline_dollar(state: StateInline, silent: bool) -> bool:
# TODO options:
# even/odd backslash escaping

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

if not allow_space:
# whitespace not allowed straight after opening $
try:
if isWhiteSpace(state.srcCharCode[state.pos + 1]):
if isWhiteSpace(ord(state.src[state.pos + 1])):
return False
except IndexError:
return False
Expand All @@ -176,7 +176,7 @@ def _math_inline_dollar(state: StateInline, silent: bool) -> bool:
return False

try:
is_double = allow_double and state.srcCharCode[state.pos + 1] == 0x24
is_double = allow_double and state.src[state.pos + 1] == "$"
except IndexError:
return False

Expand All @@ -185,7 +185,7 @@ def _math_inline_dollar(state: StateInline, silent: bool) -> bool:
found_closing = False
while not found_closing:
try:
end = state.srcCharCode.index(0x24, pos)
end = state.src.index("$", pos)
except ValueError:
return False

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

try:
if is_double and state.srcCharCode[end + 1] != 0x24:
if is_double and state.src[end + 1] != "$":
pos = end + 1
continue
except IndexError:
Expand All @@ -211,7 +211,7 @@ def _math_inline_dollar(state: StateInline, silent: bool) -> bool:
if not allow_space:
# whitespace not allowed straight before closing $
try:
if isWhiteSpace(state.srcCharCode[end - 1]):
if isWhiteSpace(ord(state.src[end - 1])):
return False
except IndexError:
return False
Expand Down Expand Up @@ -274,10 +274,7 @@ def _math_block_dollar(
if startPos + 2 > end:
return False

if (
state.srcCharCode[startPos] != 0x24
or state.srcCharCode[startPos + 1] != 0x24
): # /* $ */
if state.src[startPos] != "$" or state.src[startPos + 1] != "$":
return False

# search for end of block
Expand Down
6 changes: 3 additions & 3 deletions mdit_py_plugins/field_list/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ def _fieldlist_rule(state: StateBlock, startLine: int, endLine: int, silent: boo

# find indent to start of body on first line
while pos < maximum:
ch = state.srcCharCode[pos]
ch = state.src[pos]

if ch == 0x09: # \t
if ch == "\t":
first_line_body_indent += (
4 - (first_line_body_indent + state.bsCount[nextLine]) % 4
)
elif ch == 0x20: # \s
elif ch == " ":
first_line_body_indent += 1
else:
break
Expand Down
36 changes: 17 additions & 19 deletions mdit_py_plugins/footnote/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import List, Optional

from markdown_it import MarkdownIt
from markdown_it.common.utils import isSpace
from markdown_it.helpers import parseLinkLabel
from markdown_it.rules_block import StateBlock
from markdown_it.rules_inline import StateInline
Expand Down Expand Up @@ -69,23 +68,23 @@ def footnote_def(state: StateBlock, startLine: int, endLine: int, silent: bool):
if start + 4 > maximum:
return False

if state.srcCharCode[start] != 0x5B: # /* [ */
if state.src[start] != "[":
return False
if state.srcCharCode[start + 1] != 0x5E: # /* ^ */
if state.src[start + 1] != "^":
return False

pos = start + 2
while pos < maximum:
if state.srcCharCode[pos] == 0x20:
if state.src[pos] == " ":
return False
if state.srcCharCode[pos] == 0x5D: # /* ] */
if state.src[pos] == "]":
break
pos += 1

if pos == start + 2: # no empty footnote labels
return False
pos += 1
if pos >= maximum or state.srcCharCode[pos] != 0x3A: # /* : */
if pos >= maximum or state.src[pos] != ":":
return False
if silent:
return True
Expand Down Expand Up @@ -113,13 +112,12 @@ def footnote_def(state: StateBlock, startLine: int, endLine: int, silent: bool):
)

while pos < maximum:
ch = state.srcCharCode[pos]
ch = state.src[pos]

if isSpace(ch):
if ch == 0x09:
offset += 4 - offset % 4
else:
offset += 1
if ch == "\t":
offset += 4 - offset % 4
elif ch == " ":
offset += 1

else:
break
Expand Down Expand Up @@ -162,9 +160,9 @@ def footnote_inline(state: StateInline, silent: bool):

if start + 2 >= maximum:
return False
if state.srcCharCode[start] != 0x5E: # /* ^ */
if state.src[start] != "^":
return False
if state.srcCharCode[start + 1] != 0x5B: # /* [ */
if state.src[start + 1] != "[":
return False

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

if "footnotes" not in state.env or "refs" not in state.env["footnotes"]:
return False
if state.srcCharCode[start] != 0x5B: # /* [ */
if state.src[start] != "[":
return False
if state.srcCharCode[start + 1] != 0x5E: # /* ^ */
if state.src[start + 1] != "^":
return False

pos = start + 2
while pos < maximum:
if state.srcCharCode[pos] == 0x20:
if state.src[pos] == " ":
return False
if state.srcCharCode[pos] == 0x0A:
if state.src[pos] == "\n":
return False
if state.srcCharCode[pos] == 0x5D: # /* ] */
if state.src[pos] == "]":
break
pos += 1

Expand Down
7 changes: 3 additions & 4 deletions mdit_py_plugins/front_matter/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from math import floor

from markdown_it import MarkdownIt
from markdown_it.common.utils import charCodeAt
from markdown_it.rules_block import StateBlock

from mdit_py_plugins.utils import is_code_block
Expand Down Expand Up @@ -33,7 +32,7 @@ def front_matter_plugin(md: MarkdownIt):
def make_front_matter_rule():
min_markers = 3
marker_str = "-"
marker_char = charCodeAt(marker_str, 0)
marker_char = marker_str[0]
marker_len = len(marker_str)

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

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

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

if marker_char != state.srcCharCode[start]:
if marker_char != state.src[start]:
continue

if is_code_block(state, nextLine):
Expand Down
Loading