-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: xin
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def unescape_string(s: str) -> str: | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
( | ||
scheme, | ||
unescape_normalize_uri(netloc), | ||
|
@@ -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: | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
( | ||
scheme, | ||
unescape_unquote(netloc), | ||
|
@@ -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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return False | ||
# control codes | ||
if c >= 0x00 and c <= 0x08: | ||
|
@@ -150,7 +150,7 @@ def unescapeAll(string: str) -> str: | |
|
||
|
||
ESCAPABLE = r"""\\!"#$%&'()*+,./:;<=>?@\[\]^`{}|_~-""" | ||
ESCAPE_CHAR = re.compile(r"\\([" + ESCAPABLE + r"])") | ||
ESCAPE_CHAR = re.compile(f'\\\\([{ESCAPABLE}])') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
|
||
|
||
def stripEscape(string: str) -> str: | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
# ////////////////////////////////////////////////////////////////////////////// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self[name].ruler2.enableOnly(rules2) | ||
|
||
return self | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def renderToken( | ||
self, | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
result += ">\n" if needLf else ">" | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def renderInlineAsText( | ||
self, | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def code_block( | ||
self, | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def fence( | ||
self, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def __compile__(self) -> None: | ||
"""Build rules lookup cache""" | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
raise KeyError(f"Rules manager: invalid rule name {name}") | ||
self.__rules__[idx].enabled = True | ||
result.append(name) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
raise KeyError(f"Rules manager: invalid rule name {name}") | ||
self.__rules__[idx].enabled = False | ||
result.append(name) | ||
|
There was a problem hiding this comment.
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:use-fstring-for-concatenation
)