Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored xin branch #1

Open
wants to merge 1 commit into
base: xin
Choose a base branch
from
Open
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: 5 additions & 4 deletions markdown_it/common/html_re.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Regexps to match html elements
"""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 12-39 refactored with the following changes:


import re

attr_name = "[a-zA-Z_:][a-zA-Z0-9:._-]*"
Expand All @@ -9,11 +10,11 @@
single_quoted = "'[^']*'"
double_quoted = '"[^"]*"'

attr_value = "(?:" + unquoted + "|" + single_quoted + "|" + double_quoted + ")"
attr_value = f'(?:{unquoted}|{single_quoted}|{double_quoted})'

attribute = "(?:\\s+" + attr_name + "(?:\\s*=\\s*" + attr_value + ")?)"
attribute = f'(?:\\s+{attr_name}(?:\\s*=\\s*{attr_value})?)'

open_tag = "<[A-Za-z][A-Za-z0-9\\-]*" + attribute + "*\\s*\\/?>"
open_tag = f'<[A-Za-z][A-Za-z0-9\\-]*{attribute}*\\s*\\/?>'

close_tag = "<\\/[A-Za-z][A-Za-z0-9\\-]*\\s*>"
comment = "<!---->|<!--(?:-?[^>-])(?:-?[^-])*-->"
Expand All @@ -36,5 +37,5 @@
+ cdata
+ ")"
)
HTML_OPEN_CLOSE_TAG_STR = "^(?:" + open_tag + "|" + close_tag + ")"
HTML_OPEN_CLOSE_TAG_STR = f'^(?:{open_tag}|{close_tag})'
HTML_OPEN_CLOSE_TAG_RE = re.compile(HTML_OPEN_CLOSE_TAG_STR)
22 changes: 5 additions & 17 deletions markdown_it/common/normalize_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function unescape_char refactored with the following changes:



def unescape_string(s: str) -> str:
Expand Down Expand Up @@ -76,8 +73,7 @@ def normalizeLink(url: str) -> str:
^^^^^^^^^^^
"""
(scheme, netloc, path, params, query, fragment) = urlparse(url)
if scheme in RECODE_HOSTNAME_FOR:
url = urlunparse(
return urlunparse(
Comment on lines -79 to +76
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function normalizeLink refactored with the following changes:

(
scheme,
unescape_normalize_uri(netloc),
Expand All @@ -86,11 +82,7 @@ def normalizeLink(url: str) -> str:
normalize_uri(query),
unescape_normalize_uri(fragment),
)
)
else:
url = unescape_normalize_uri(url)

return url
) if scheme in RECODE_HOSTNAME_FOR else unescape_normalize_uri(url)

# TODO the selective encoding below should probably be done here,
# something like:
Expand Down Expand Up @@ -126,8 +118,7 @@ def normalizeLinkText(link: str) -> str:
~~~~~~~~~~~
"""
(scheme, netloc, path, params, query, fragment) = urlparse(link)
if scheme in RECODE_HOSTNAME_FOR:
url = urlunparse(
return urlunparse(
Comment on lines -129 to +121
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function normalizeLinkText refactored with the following changes:

(
scheme,
unescape_unquote(netloc),
Expand All @@ -136,10 +127,7 @@ def normalizeLinkText(link: str) -> str:
unquote(query),
unescape_unquote(fragment),
)
)
else:
url = unescape_unquote(link)
return url
) if scheme in RECODE_HOSTNAME_FOR else unescape_unquote(link)

# TODO the selective encoding below should probably be done here,
# something like:
Expand Down
7 changes: 3 additions & 4 deletions markdown_it/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def isValidEntityCode(c: int) -> bool:
# never used
if c >= 0xFDD0 and c <= 0xFDEF:
return False
if ((c & 0xFFFF) == 0xFFFF) or ((c & 0xFFFF) == 0xFFFE):
if c & 0xFFFF in [0xFFFF, 0xFFFE]:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function isValidEntityCode refactored with the following changes:

return False
# control codes
if c >= 0x00 and c <= 0x08:
Expand Down Expand Up @@ -150,7 +150,7 @@ def unescapeAll(string: str) -> str:


ESCAPABLE = r"""\\!"#$%&'()*+,./:;<=>?@\[\]^`{}|_~-"""
ESCAPE_CHAR = re.compile(r"\\([" + ESCAPABLE + r"])")
ESCAPE_CHAR = re.compile(f'\\\\([{ESCAPABLE}])')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 153-153 refactored with the following changes:



def stripEscape(string: str) -> str:
Expand Down Expand Up @@ -190,8 +190,7 @@ def escapeHtml(raw: str) -> str:


def escapeRE(string: str) -> str:
string = REGEXP_ESCAPE_RE.sub("\\$&", string)
return string
return REGEXP_ESCAPE_RE.sub("\\$&", string)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function escapeRE refactored with the following changes:



# //////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions markdown_it/helpers/parse_link_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ def parseLinkDestination(string: str, pos: int, maximum: int) -> _Result:
pos += 2
continue

if code == 0x28: # /* ( */)
if code == 0x28:
level += 1
if level > 32:
return result

if code == 0x29: # /* ) */)
elif code == 0x29:
if level == 0:
break
level -= 1
Expand Down
12 changes: 5 additions & 7 deletions markdown_it/helpers/parse_link_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

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

labelEnd = -1
oldPos = state.pos
found = False

Expand All @@ -27,17 +26,16 @@ def parseLinkLabel(state: StateInline, start: int, disableNested: bool = False)

prevPos = state.pos
state.md.inline.skipToken(state)
if marker == 0x5B: # /* [ */)
if prevPos == state.pos - 1:
if prevPos == state.pos - 1:
if marker == 0x5B:
# increase level if we find text `[`,
# which is not a part of any token
level += 1
elif disableNested:
elif disableNested:
if marker == 0x5B:
state.pos = oldPos
return -1
if found:
labelEnd = state.pos

labelEnd = state.pos if found else -1
# restore old state
state.pos = oldPos

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 @@ -27,7 +27,7 @@ def parseLinkTitle(string: str, pos: int, maximum: int) -> _Result:
marker = charCodeAt(string, pos)

# /* " */ /* ' */ /* ( */
if marker != 0x22 and marker != 0x27 and marker != 0x28:
if marker not in [0x22, 0x27, 0x28]:
return result

pos += 1
Expand Down
6 changes: 2 additions & 4 deletions markdown_it/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,9 @@ def configure(

if "components" in config:
for name, component in config["components"].items():
rules = component.get("rules", None)
if rules:
if rules := component.get("rules", None):
self[name].ruler.enableOnly(rules)
rules2 = component.get("rules2", None)
if rules2:
if rules2 := component.get("rules2", None):
Comment on lines -125 to +127
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MarkdownIt.configure refactored with the following changes:

self[name].ruler2.enableOnly(rules2)

return self
Expand Down
62 changes: 24 additions & 38 deletions markdown_it/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,12 @@ def renderInline(
:param options: params of parser instance
:param env: additional data from parsed input (references, for example)
"""
result = ""

for i, token in enumerate(tokens):
if token.type in self.rules:
result += self.rules[token.type](tokens, i, options, env)
else:
result += self.renderToken(tokens, i, options, env)

return result
return "".join(
self.rules[token.type](tokens, i, options, env)
if token.type in self.rules
else self.renderToken(tokens, i, options, env)
for i, token in enumerate(tokens)
)
Comment on lines -108 to +113
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function RendererHTML.renderInline refactored with the following changes:


def renderToken(
self,
Expand Down Expand Up @@ -161,19 +158,18 @@ def renderToken(
if token.block:
needLf = True

if token.nesting == 1:
if idx + 1 < len(tokens):
nextToken = tokens[idx + 1]
if token.nesting == 1 and idx + 1 < len(tokens):
nextToken = tokens[idx + 1]

if nextToken.type == "inline" or nextToken.hidden:
# Block-level tag containing an inline tag.
#
needLf = False
if nextToken.type == "inline" or nextToken.hidden:
# Block-level tag containing an inline tag.
#
needLf = False

elif nextToken.nesting == -1 and nextToken.tag == token.tag:
# Opening tag + closing tag of the same type. E.g. `<li></li>`.
#
needLf = False
elif nextToken.nesting == -1 and nextToken.tag == token.tag:
# Opening tag + closing tag of the same type. E.g. `<li></li>`.
#
needLf = False
Comment on lines -164 to +172
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function RendererHTML.renderToken refactored with the following changes:


result += ">\n" if needLf else ">"

Expand All @@ -182,12 +178,10 @@ def renderToken(
@staticmethod
def renderAttrs(token: Token) -> str:
"""Render token attributes to string."""
result = ""

for key, value in token.attrItems():
result += " " + escapeHtml(key) + '="' + escapeHtml(str(value)) + '"'

return result
return "".join(
f' {escapeHtml(key)}' + '="' + escapeHtml(str(value)) + '"'
for key, value in token.attrItems()
)
Comment on lines -185 to +184
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function RendererHTML.renderAttrs refactored with the following changes:


def renderInlineAsText(
self,
Expand Down Expand Up @@ -221,13 +215,9 @@ def renderInlineAsText(

def code_inline(self, tokens: Sequence[Token], idx: int, options, env) -> str:
token = tokens[idx]
return (
"<code"
return (("<code"
+ self.renderAttrs(token)
+ ">"
+ escapeHtml(tokens[idx].content)
+ "</code>"
)
+ ">" + escapeHtml(token.content)) + "</code>")
Comment on lines -224 to +220
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function RendererHTML.code_inline refactored with the following changes:


def code_block(
self,
Expand All @@ -238,13 +228,9 @@ def code_block(
) -> str:
token = tokens[idx]

return (
"<pre"
return (("<pre"
+ self.renderAttrs(token)
+ "><code>"
+ escapeHtml(tokens[idx].content)
+ "</code></pre>\n"
)
+ "><code>" + escapeHtml(token.content)) + "</code></pre>\n")
Comment on lines -241 to +233
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function RendererHTML.code_block refactored with the following changes:


def fence(
self,
Expand Down
11 changes: 5 additions & 6 deletions markdown_it/ruler.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ def __init__(self):

def __find__(self, name: str) -> int:
"""Find rule index by name"""
for i, rule in enumerate(self.__rules__):
if rule.name == name:
return i
return -1
return next(
(i for i, rule in enumerate(self.__rules__) if rule.name == name), -1
)
Comment on lines -79 to +81
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Ruler.__find__ refactored with the following changes:

  • Use the built-in function next instead of a for-loop (use-next)


def __compile__(self) -> None:
"""Build rules lookup cache"""
Expand Down Expand Up @@ -176,7 +175,7 @@ def enable(self, names: Union[str, Iterable[str]], ignoreInvalid: bool = False):
idx = self.__find__(name)
if (idx < 0) and ignoreInvalid:
continue
if (idx < 0) and not ignoreInvalid:
if idx < 0:
Comment on lines -179 to +178
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Ruler.enable refactored with the following changes:

raise KeyError(f"Rules manager: invalid rule name {name}")
self.__rules__[idx].enabled = True
result.append(name)
Expand Down Expand Up @@ -212,7 +211,7 @@ def disable(self, names: Union[str, Iterable[str]], ignoreInvalid: bool = False)
idx = self.__find__(name)
if (idx < 0) and ignoreInvalid:
continue
if (idx < 0) and not ignoreInvalid:
if idx < 0:
Comment on lines -215 to +214
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Ruler.disable refactored with the following changes:

raise KeyError(f"Rules manager: invalid rule name {name}")
self.__rules__[idx].enabled = False
result.append(name)
Expand Down
57 changes: 25 additions & 32 deletions markdown_it/rules_block/blockquote.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,16 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool):
while pos < max:
ch = state.srcCharCode[pos]

if isSpace(ch):
if ch == 0x09: # / tab /
offset += (
4
- (offset + state.bsCount[startLine] + (1 if adjustTab else 0)) % 4
)
else:
offset += 1
if not isSpace(ch):
break

if ch == 0x09: # / tab /
offset += (
4
- (offset + state.bsCount[startLine] + (1 if adjustTab else 0)) % 4
)
else:
break
offset += 1

pos += 1

Expand Down Expand Up @@ -188,22 +187,21 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool):
while pos < max:
ch = state.srcCharCode[pos]

if isSpace(ch):
if ch == 0x09:
offset += (
4
- (
offset
+ state.bsCount[nextLine]
+ (1 if adjustTab else 0)
)
% 4
)
else:
offset += 1
else:
if not isSpace(ch):
break

if ch == 0x09:
offset += (
4
- (
offset
+ state.bsCount[nextLine]
+ (1 if adjustTab else 0)
)
% 4
)
else:
offset += 1
pos += 1

lastLineEmpty = pos >= max
Expand All @@ -226,15 +224,10 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool):
if lastLineEmpty:
break

# Case 3: another tag found.
terminate = False

for terminatorRule in terminatorRules:
if terminatorRule(state, nextLine, endLine, True):
terminate = True
break

if terminate:
if terminate := any(
terminatorRule(state, nextLine, endLine, True)
for terminatorRule in terminatorRules
):
# Quirk to enforce "hard termination mode" for paragraphs;
# normally if you call `tokenize(state, startLine, nextLine)`,
# paragraphs will look below nextLine for paragraph continuation,
Expand Down
Loading