Skip to content

Commit f016794

Browse files
authored
🔧 MAINTAIN: Add more type annotations (#96)
1 parent 047a71f commit f016794

File tree

15 files changed

+32
-31
lines changed

15 files changed

+32
-31
lines changed

markdown_it/common/normalize_url.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,22 @@
3838
)
3939

4040

41-
def unescape_char(s):
41+
def unescape_char(s: str) -> str:
4242
if s[0] == "\\":
4343
return s[1]
4444
else:
4545
return html.unescape(s)
4646

4747

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

5555

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

5959

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

6565

66-
def unescape_normalize_uri(x):
66+
def unescape_normalize_uri(x: str) -> str:
6767
return normalize_uri(unescape_string(x))
6868

6969

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

168168

169-
def validateLink(url: str, validator: Optional[Callable] = None):
169+
def validateLink(url: str, validator: Optional[Callable] = None) -> bool:
170170
"""Validate URL link is allowed in output.
171171
172172
This validator can prohibit more than really needed to prevent XSS.

markdown_it/common/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def escapeRE(string: str) -> str:
197197
# //////////////////////////////////////////////////////////////////////////////
198198

199199

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

203203

@@ -216,7 +216,7 @@ def isSpace(code):
216216
}
217217

218218

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

232232

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

237237

markdown_it/helpers/parse_link_destination.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def __init__(self):
1515
self.str = ""
1616

1717

18-
def parseLinkDestination(string, pos, maximum):
18+
def parseLinkDestination(string: str, pos: int, maximum: int) -> _Result:
1919
lines = 0
2020
start = pos
2121
result = _Result()

markdown_it/helpers/parse_link_label.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
returns the end of the label
66
77
"""
8+
from markdown_it.rules_inline import StateInline
89

910

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

1213
labelEnd = -1
1314
oldPos = state.pos

markdown_it/helpers/parse_link_title.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __str__(self):
1616
return self.str
1717

1818

19-
def parseLinkTitle(string, pos, maximum):
19+
def parseLinkTitle(string: str, pos: int, maximum: int) -> _Result:
2020
lines = 0
2121
start = pos
2222
result = _Result()

markdown_it/rules_block/state_block.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def __repr__(self):
117117
f"(line={self.line},level={self.level},tokens={len(self.tokens)})"
118118
)
119119

120-
def push(self, ttype, tag, nesting):
120+
def push(self, ttype: str, tag: str, nesting: int) -> Token:
121121
"""Push new token to "stream"."""
122122
token = Token(ttype, tag, nesting)
123123
token.block = True
@@ -129,7 +129,7 @@ def push(self, ttype, tag, nesting):
129129
self.tokens.append(token)
130130
return token
131131

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

@@ -183,7 +183,7 @@ def skipCharsBack(self, pos: int, code: int, minimum: int) -> int:
183183
return pos + 1
184184
return pos
185185

186-
def getLines(self, begin: int, end: int, indent, keepLastLF):
186+
def getLines(self, begin: int, end: int, indent: int, keepLastLF: bool) -> str:
187187
"""Cut lines range from source."""
188188
line = begin
189189
if begin >= end:

markdown_it/rules_core/block.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .state_core import StateCore
33

44

5-
def block(state: StateCore):
5+
def block(state: StateCore) -> None:
66

77
if state.inlineMode:
88
token = Token("inline", "", 0)

markdown_it/rules_core/inline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .state_core import StateCore
22

33

4-
def inline(state: StateCore):
4+
def inline(state: StateCore) -> None:
55
"""Parse inlines"""
66
for token in state.tokens:
77
if token.type == "inline":

markdown_it/rules_core/linkify.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
TEST_MAILTO_RE = re.compile(r"^mailto:", flags=re.IGNORECASE)
1515

1616

17-
def isLinkOpen(string: str):
18-
return LINK_OPEN_RE.search(string)
17+
def isLinkOpen(string: str) -> bool:
18+
return bool(LINK_OPEN_RE.search(string))
1919

2020

21-
def isLinkClose(string: str):
22-
return LINK_CLOSE_RE.search(string)
21+
def isLinkClose(string: str) -> bool:
22+
return bool(LINK_CLOSE_RE.search(string))
2323

2424

25-
def linkify(state: StateCore):
25+
def linkify(state: StateCore) -> None:
2626
blockTokens = state.tokens
2727

2828
if not state.md.options.linkify:

markdown_it/rules_core/normalize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
NULL_RE = re.compile(r"\0")
1010

1111

12-
def normalize(state: StateCore):
12+
def normalize(state: StateCore) -> None:
1313

1414
# Normalize newlines
1515
string = NEWLINES_RE.sub("\n", state.src)

markdown_it/rules_core/replacements.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def replaceFn(match: Match[str]):
5959
return SCOPED_ABBR[match.group(1).lower()]
6060

6161

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

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

7575

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

7979
for token in inlineTokens:
@@ -108,7 +108,7 @@ def replace_rare(inlineTokens: List[Token]):
108108
inside_autolink += 1
109109

110110

111-
def replace(state: StateCore):
111+
def replace(state: StateCore) -> None:
112112
if not state.md.options.typographer:
113113
return
114114

markdown_it/rules_core/smartquotes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
APOSTROPHE = "\u2019" # ’
1515

1616

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

2323

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

2727
for i in range(len(tokens)):
@@ -190,7 +190,7 @@ def process_inlines(tokens: List[Token], state: StateCore):
190190
)
191191

192192

193-
def smartquotes(state: StateCore):
193+
def smartquotes(state: StateCore) -> None:
194194
if not state.md.options.typographer:
195195
return
196196

markdown_it/rules_inline/autolink.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
AUTOLINK_RE = re.compile(r"^<([a-zA-Z][a-zA-Z0-9+.\-]{1,31}):([^<>\x00-\x20]*)>")
1010

1111

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

1414
pos = state.pos
1515

markdown_it/rules_inline/backticks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
regex = re.compile("^ (.+) $")
77

88

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

1111
pos = state.pos
1212
ch = state.srcCharCode[pos]

markdown_it/rules_inline/balance_pairs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def processDelimiters(state: StateInline, delimiters, *args):
9191
closerIdx += 1
9292

9393

94-
def link_pairs(state: StateInline):
94+
def link_pairs(state: StateInline) -> None:
9595
tokens_meta = state.tokens_meta
9696
maximum = len(state.tokens_meta)
9797

0 commit comments

Comments
 (0)