-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquishify.py
79 lines (50 loc) · 1.61 KB
/
Squishify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def squishify(tx):
txt = tx.split('\n')
txt = [line for line in txt if not line.replace(' ','')==''] # Remove Blank Lines
l=0
c_index = 0
commenting = False
while l<len(txt):
line = txt[l]
if line[indents_in(line)] == '!':
commenting = True
c_index = indents_in(line)+1
del txt[l]
l -= 1
elif commenting:
if indents_in(line)<c_index:
commenting = False
else:
del txt[l]
l -= 1
l+=1
# print(txt,'txoo----')
txt.append('')
script = [] # Because String is not editable
indents = []
for li,line in enumerate(txt):
script.append(list(line))
for x,car in enumerate(line):
if car == ' ' and line[x-1].isalnum() and (line[x+1].isalnum() or line[x+1] == '\''):
script[li][x] = '`'
# ----------------------------
i1 = indents_in(txt[li])
line2 = txt[clip(li+1,txt)]
i2 = indents_in(line2)
# ----------------------------
if i1 < i2:
script[li] += '{'
indents.append(i1)
elif i2 < i1:
for x in range(len(indents[indents.index(i2):])):
script[li] += ';};'
del indents[indents.index(i2):]
# ------Indent id----------------------
return ''.join([''.join(line) + ';' for line in script]).replace(' ', '')
def clip(x,y):
return max(0, min(x, len(y)-1))
def indents_in(v):
for x,val in enumerate(v):
if not val == ' ':
return x
return 0