Prettify expressions removing whitespace around newlines and adjusting whitespace around parentheses
This commit is contained in:
parent
995bfee084
commit
cd7392c2fd
@ -11,7 +11,7 @@ __author__ = "Davide Testa"
|
|||||||
__email__ = "davide@davte.it"
|
__email__ = "davide@davte.it"
|
||||||
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
|
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
|
||||||
__license__ = "GNU General Public License v3.0"
|
__license__ = "GNU General Public License v3.0"
|
||||||
__version__ = "2.5.21"
|
__version__ = "2.5.22"
|
||||||
__maintainer__ = "Davide Testa"
|
__maintainer__ = "Davide Testa"
|
||||||
__contact__ = "t.me/davte"
|
__contact__ = "t.me/davte"
|
||||||
|
|
||||||
|
@ -183,12 +183,19 @@ def get_operators() -> dict:
|
|||||||
|
|
||||||
calc_buttons = get_calc_buttons()
|
calc_buttons = get_calc_buttons()
|
||||||
operators = get_operators()
|
operators = get_operators()
|
||||||
operators_spacer = re.compile(r"(\d)\s*([+\-*%]|/{1,2})\s*(\d)")
|
|
||||||
|
operators_spacer = re.compile(r"([\d\(\)])\s*([+\-*%]|/{1,2})\s*([\d\(\)])")
|
||||||
spaced_operators = r"\1 \2 \3"
|
spaced_operators = r"\1 \2 \3"
|
||||||
operators_space_remover = re.compile(r"(\d)\s*(\*\*)\s*(\d)")
|
operators_space_remover = re.compile(r"([\d\(\)])\s*(\*\*)\s*([\d\(\)])")
|
||||||
non_spaced_operators = r"\1\2\3"
|
non_spaced_operators = r"\1\2\3"
|
||||||
multiple_newlines_regex = re.compile(r"[\n|\r][\n|\s]{2,}")
|
multiple_newlines_regex = re.compile(r"[\n|\r][\n|\s]{2,}")
|
||||||
multiple_spaces_regex = re.compile(r"\s{2,}")
|
multiple_spaces_regex = re.compile(r"[^\S\n\r]{2,}")
|
||||||
|
parentheses_regex_list = [
|
||||||
|
{'pattern': re.compile(r"[^\S\n\r]*(\()[^\S\n\r]*(\d)"),
|
||||||
|
'replace': r" \1\2"},
|
||||||
|
{'pattern': re.compile(r"(\d)[^\S\n\r]*(\))"),
|
||||||
|
'replace': r"\1\2"}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def prettify_expression(expression):
|
def prettify_expression(expression):
|
||||||
@ -196,11 +203,17 @@ def prettify_expression(expression):
|
|||||||
|
|
||||||
Place a single space around binary operators `+,-,*,%,/,//`, no space
|
Place a single space around binary operators `+,-,*,%,/,//`, no space
|
||||||
around `**`, single newlines and single spaces.
|
around `**`, single newlines and single spaces.
|
||||||
|
No space after `(` or before `)`.
|
||||||
|
No space at the beginning or ending of a line.
|
||||||
"""
|
"""
|
||||||
expression = operators_spacer.sub(spaced_operators, expression)
|
expression = operators_spacer.sub(spaced_operators, expression)
|
||||||
expression = operators_space_remover.sub(non_spaced_operators, expression)
|
expression = operators_space_remover.sub(non_spaced_operators, expression)
|
||||||
|
for regex in parentheses_regex_list:
|
||||||
|
expression = regex['pattern'].sub(regex['replace'], expression)
|
||||||
expression = multiple_newlines_regex.sub('\n', expression)
|
expression = multiple_newlines_regex.sub('\n', expression)
|
||||||
expression = multiple_spaces_regex.sub(' ', expression)
|
expression = multiple_spaces_regex.sub(' ', expression)
|
||||||
|
expression = expression.replace('\n ', '\n')
|
||||||
|
expression = expression.replace(' \n', '\n')
|
||||||
return expression
|
return expression
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user