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
10 changes: 5 additions & 5 deletions markdown_it/common/normalize_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@
)


def unescape_char(s):
def unescape_char(s: str) -> str:
if s[0] == "\\":
return s[1]
else:
return html.unescape(s)


def unescape_string(s):
def unescape_string(s: str) -> str:
"""Replace entities and backslash escapes with literal characters."""
if re.search(reBackslashOrAmp, s):
return re.sub(reEntityOrEscapedChar, lambda m: unescape_char(m.group()), s)
else:
return s


def normalize_uri(uri):
def normalize_uri(uri: str) -> str:
return quote(uri.encode("utf-8"), safe=str("/@:+?=&()%#*,"))


Expand All @@ -63,7 +63,7 @@ def normalize_uri(uri):
RECODE_HOSTNAME_FOR = ("http", "https", "mailto")


def unescape_normalize_uri(x):
def unescape_normalize_uri(x: str) -> str:
return normalize_uri(unescape_string(x))


Expand Down Expand Up @@ -166,7 +166,7 @@ def normalizeLinkText(link):
GOOD_DATA_RE = re.compile(r"^data:image\/(gif|png|jpeg|webp);")


def validateLink(url: str, validator: Optional[Callable] = None):
def validateLink(url: str, validator: Optional[Callable] = None) -> bool:
"""Validate URL link is allowed in output.

This validator can prohibit more than really needed to prevent XSS.
Expand Down
6 changes: 3 additions & 3 deletions markdown_it/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def escapeRE(string: str) -> str:
# //////////////////////////////////////////////////////////////////////////////


def isSpace(code):
def isSpace(code) -> bool:
return code in {0x09, 0x20}


Expand All @@ -216,7 +216,7 @@ def isSpace(code):
}


def isWhiteSpace(code):
def isWhiteSpace(code: int) -> bool:
r"""Zs (unicode class) || [\t\f\v\r\n]"""
if code >= 0x2000 and code <= 0x200A:
return True
Expand All @@ -231,7 +231,7 @@ def isWhiteSpace(code):


# Currently without astral characters support.
def isPunctChar(ch):
def isPunctChar(ch: str) -> bool:
return UNICODE_PUNCT_RE.search(ch) is not None


Expand Down
2 changes: 1 addition & 1 deletion markdown_it/helpers/parse_link_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self):
self.str = ""


def parseLinkDestination(string, pos, maximum):
def parseLinkDestination(string: str, pos: int, maximum: int) -> _Result:
lines = 0
start = pos
result = _Result()
Expand Down
3 changes: 2 additions & 1 deletion markdown_it/helpers/parse_link_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
returns the end of the label

"""
from markdown_it.rules_inline import StateInline


def parseLinkLabel(state, start, disableNested=False):
def parseLinkLabel(state: StateInline, start: int, disableNested: bool = False) -> int:

labelEnd = -1
oldPos = state.pos
Expand Down
2 changes: 1 addition & 1 deletion markdown_it/helpers/parse_link_title.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __str__(self):
return self.str


def parseLinkTitle(string, pos, maximum):
def parseLinkTitle(string: str, pos: int, maximum: int) -> _Result:
lines = 0
start = pos
result = _Result()
Expand Down
6 changes: 3 additions & 3 deletions markdown_it/rules_block/state_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __repr__(self):
f"(line={self.line},level={self.level},tokens={len(self.tokens)})"
)

def push(self, ttype, tag, nesting):
def push(self, ttype: str, tag: str, nesting: int) -> Token:
"""Push new token to "stream"."""
token = Token(ttype, tag, nesting)
token.block = True
Expand All @@ -129,7 +129,7 @@ def push(self, ttype, tag, nesting):
self.tokens.append(token)
return token

def isEmpty(self, line):
def isEmpty(self, line: int) -> bool:
"""."""
return (self.bMarks[line] + self.tShift[line]) >= self.eMarks[line]

Expand Down Expand Up @@ -183,7 +183,7 @@ def skipCharsBack(self, pos: int, code: int, minimum: int) -> int:
return pos + 1
return pos

def getLines(self, begin: int, end: int, indent, keepLastLF):
def getLines(self, begin: int, end: int, indent: int, keepLastLF: bool) -> str:
"""Cut lines range from source."""
line = begin
if begin >= end:
Expand Down
2 changes: 1 addition & 1 deletion markdown_it/rules_core/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .state_core import StateCore


def block(state: StateCore):
def block(state: StateCore) -> None:

if state.inlineMode:
token = Token("inline", "", 0)
Expand Down
2 changes: 1 addition & 1 deletion markdown_it/rules_core/inline.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .state_core import StateCore


def inline(state: StateCore):
def inline(state: StateCore) -> None:
"""Parse inlines"""
for token in state.tokens:
if token.type == "inline":
Expand Down
10 changes: 5 additions & 5 deletions markdown_it/rules_core/linkify.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
TEST_MAILTO_RE = re.compile(r"^mailto:", flags=re.IGNORECASE)


def isLinkOpen(string: str):
return LINK_OPEN_RE.search(string)
def isLinkOpen(string: str) -> bool:
return bool(LINK_OPEN_RE.search(string))


def isLinkClose(string: str):
return LINK_CLOSE_RE.search(string)
def isLinkClose(string: str) -> bool:
return bool(LINK_CLOSE_RE.search(string))


def linkify(state: StateCore):
def linkify(state: StateCore) -> None:
blockTokens = state.tokens

if not state.md.options.linkify:
Expand Down
2 changes: 1 addition & 1 deletion markdown_it/rules_core/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
NULL_RE = re.compile(r"\0")


def normalize(state: StateCore):
def normalize(state: StateCore) -> None:

# Normalize newlines
string = NEWLINES_RE.sub("\n", state.src)
Expand Down
6 changes: 3 additions & 3 deletions markdown_it/rules_core/replacements.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def replaceFn(match: Match[str]):
return SCOPED_ABBR[match.group(1).lower()]


def replace_scoped(inlineTokens: List[Token]):
def replace_scoped(inlineTokens: List[Token]) -> None:
inside_autolink = 0

for token in inlineTokens:
Expand All @@ -73,7 +73,7 @@ def replace_scoped(inlineTokens: List[Token]):
inside_autolink += 1


def replace_rare(inlineTokens: List[Token]):
def replace_rare(inlineTokens: List[Token]) -> None:
inside_autolink = 0

for token in inlineTokens:
Expand Down Expand Up @@ -108,7 +108,7 @@ def replace_rare(inlineTokens: List[Token]):
inside_autolink += 1


def replace(state: StateCore):
def replace(state: StateCore) -> None:
if not state.md.options.typographer:
return

Expand Down
6 changes: 3 additions & 3 deletions markdown_it/rules_core/smartquotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
APOSTROPHE = "\u2019" # ’


def replaceAt(string: str, index: int, ch: str):
def replaceAt(string: str, index: int, ch: str) -> str:
# When the index is negative, the behavior is different from the js version.
# But basically, the index will not be negative.
assert index >= 0
return string[:index] + ch + string[index + 1 :]


def process_inlines(tokens: List[Token], state: StateCore):
def process_inlines(tokens: List[Token], state: StateCore) -> None:
stack: List[Dict[str, Any]] = []

for i in range(len(tokens)):
Expand Down Expand Up @@ -190,7 +190,7 @@ def process_inlines(tokens: List[Token], state: StateCore):
)


def smartquotes(state: StateCore):
def smartquotes(state: StateCore) -> None:
if not state.md.options.typographer:
return

Expand Down
2 changes: 1 addition & 1 deletion markdown_it/rules_inline/autolink.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
AUTOLINK_RE = re.compile(r"^<([a-zA-Z][a-zA-Z0-9+.\-]{1,31}):([^<>\x00-\x20]*)>")


def autolink(state: StateInline, silent: bool):
def autolink(state: StateInline, silent: bool) -> bool:

pos = state.pos

Expand Down
2 changes: 1 addition & 1 deletion markdown_it/rules_inline/backticks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
regex = re.compile("^ (.+) $")


def backtick(state: StateInline, silent: bool):
def backtick(state: StateInline, silent: bool) -> bool:

pos = state.pos
ch = state.srcCharCode[pos]
Expand Down
2 changes: 1 addition & 1 deletion markdown_it/rules_inline/balance_pairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def processDelimiters(state: StateInline, delimiters, *args):
closerIdx += 1


def link_pairs(state: StateInline):
def link_pairs(state: StateInline) -> None:
tokens_meta = state.tokens_meta
maximum = len(state.tokens_meta)

Expand Down