feat: Implement quote escaping
This commit is contained in:
commit
fdd0c2ba1c
|
@ -0,0 +1,66 @@
|
|||
#! /usr/bin/env python
|
||||
|
||||
|
||||
def putchar(string, highlight=None):
|
||||
match highlight:
|
||||
case "red":
|
||||
return f"\033[31;1;1m{string}\033[0m"
|
||||
case "green":
|
||||
return f"\033[32;1;1m{string}\033[0m"
|
||||
case "blue":
|
||||
return f"\033[34;1;1m{string}\033[0m"
|
||||
case _:
|
||||
return string
|
||||
|
||||
|
||||
with open("/etc/nixos/components/programs/imunes.nix", "r") as file:
|
||||
stack = []
|
||||
prev_char = None
|
||||
prev_prev_char = None
|
||||
sequential = 0
|
||||
prev_sequential = 0
|
||||
while (line := file.readline()):
|
||||
for char in line:
|
||||
format = None
|
||||
|
||||
if len(stack) != 0 and stack[-1][0] == "\"":
|
||||
if char == "\"":
|
||||
if prev_char != "\\":
|
||||
stack.pop()
|
||||
format = "green"
|
||||
else:
|
||||
if char == "{":
|
||||
if prev_char == "$":
|
||||
if prev_prev_char != "\\":
|
||||
stack.append(("}", (",", ";")))
|
||||
format = "blue"
|
||||
else:
|
||||
if len(stack) != 0 and stack[-1][0] == "''":
|
||||
if char == "'":
|
||||
prev_sequential = sequential
|
||||
sequential += 1
|
||||
elif char == "{" and prev_char == "$" and prev_sequential % 3 != 2:
|
||||
stack.append(("}", (",", ";")))
|
||||
format = "blue"
|
||||
else:
|
||||
if sequential % 3 == 2 and char != "$":
|
||||
stack.pop()
|
||||
format = "green"
|
||||
prev_sequential = sequential
|
||||
sequential = 0
|
||||
else:
|
||||
if len(stack) != 0 and char in stack[-1][0]:
|
||||
stack.pop()
|
||||
format = "blue"
|
||||
|
||||
if char == "'" and prev_char == "'":
|
||||
format = "green"
|
||||
stack.append(("''", ()))
|
||||
|
||||
if char == "\"":
|
||||
format = "green"
|
||||
stack.append(("\"", ()))
|
||||
|
||||
print(putchar(char, format), end="")
|
||||
prev_prev_char = prev_char
|
||||
prev_char = char
|
Loading…
Reference in New Issue