diff --git a/markdown_it/common/normalize_url.py b/markdown_it/common/normalize_url.py index e3c4742b..c7504c7b 100644 --- a/markdown_it/common/normalize_url.py +++ b/markdown_it/common/normalize_url.py @@ -38,14 +38,14 @@ ) -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) @@ -53,7 +53,7 @@ def unescape_string(s): return s -def normalize_uri(uri): +def normalize_uri(uri: str) -> str: return quote(uri.encode("utf-8"), safe=str("/@:+?=&()%#*,")) @@ -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)) @@ -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. diff --git a/markdown_it/common/utils.py b/markdown_it/common/utils.py index 8ff69e1c..a2162ed8 100644 --- a/markdown_it/common/utils.py +++ b/markdown_it/common/utils.py @@ -197,7 +197,7 @@ def escapeRE(string: str) -> str: # ////////////////////////////////////////////////////////////////////////////// -def isSpace(code): +def isSpace(code) -> bool: return code in {0x09, 0x20} @@ -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 @@ -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 diff --git a/markdown_it/helpers/parse_link_destination.py b/markdown_it/helpers/parse_link_destination.py index 82955d08..4e91cb4f 100644 --- a/markdown_it/helpers/parse_link_destination.py +++ b/markdown_it/helpers/parse_link_destination.py @@ -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() diff --git a/markdown_it/helpers/parse_link_label.py b/markdown_it/helpers/parse_link_label.py index b0e4f0f1..20e3c148 100644 --- a/markdown_it/helpers/parse_link_label.py +++ b/markdown_it/helpers/parse_link_label.py @@ -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 diff --git a/markdown_it/helpers/parse_link_title.py b/markdown_it/helpers/parse_link_title.py index 69f9aa87..048b4e60 100644 --- a/markdown_it/helpers/parse_link_title.py +++ b/markdown_it/helpers/parse_link_title.py @@ -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() diff --git a/markdown_it/rules_block/state_block.py b/markdown_it/rules_block/state_block.py index 0b29d8a8..01c32d9e 100644 --- a/markdown_it/rules_block/state_block.py +++ b/markdown_it/rules_block/state_block.py @@ -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 @@ -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] @@ -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: diff --git a/markdown_it/rules_core/block.py b/markdown_it/rules_core/block.py index b47a5d8f..fa1c52c4 100644 --- a/markdown_it/rules_core/block.py +++ b/markdown_it/rules_core/block.py @@ -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) diff --git a/markdown_it/rules_core/inline.py b/markdown_it/rules_core/inline.py index 232d145a..32a95ada 100644 --- a/markdown_it/rules_core/inline.py +++ b/markdown_it/rules_core/inline.py @@ -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": diff --git a/markdown_it/rules_core/linkify.py b/markdown_it/rules_core/linkify.py index 83091d36..eac7aa65 100644 --- a/markdown_it/rules_core/linkify.py +++ b/markdown_it/rules_core/linkify.py @@ -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: diff --git a/markdown_it/rules_core/normalize.py b/markdown_it/rules_core/normalize.py index 35e7a1c1..14b2f679 100644 --- a/markdown_it/rules_core/normalize.py +++ b/markdown_it/rules_core/normalize.py @@ -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) diff --git a/markdown_it/rules_core/replacements.py b/markdown_it/rules_core/replacements.py index 5a135277..ee3b9edb 100644 --- a/markdown_it/rules_core/replacements.py +++ b/markdown_it/rules_core/replacements.py @@ -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: @@ -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: @@ -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 diff --git a/markdown_it/rules_core/smartquotes.py b/markdown_it/rules_core/smartquotes.py index 3b25d30c..c3211191 100644 --- a/markdown_it/rules_core/smartquotes.py +++ b/markdown_it/rules_core/smartquotes.py @@ -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)): @@ -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 diff --git a/markdown_it/rules_inline/autolink.py b/markdown_it/rules_inline/autolink.py index 59444092..b07396e2 100644 --- a/markdown_it/rules_inline/autolink.py +++ b/markdown_it/rules_inline/autolink.py @@ -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 diff --git a/markdown_it/rules_inline/backticks.py b/markdown_it/rules_inline/backticks.py index b843c94e..67e41c73 100644 --- a/markdown_it/rules_inline/backticks.py +++ b/markdown_it/rules_inline/backticks.py @@ -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] diff --git a/markdown_it/rules_inline/balance_pairs.py b/markdown_it/rules_inline/balance_pairs.py index 643461b2..deff41e6 100644 --- a/markdown_it/rules_inline/balance_pairs.py +++ b/markdown_it/rules_inline/balance_pairs.py @@ -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)