-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-redden.py
executable file
·43 lines (34 loc) · 1.12 KB
/
remove-redden.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
#!/usr/bin/env python3
# call like this:
# ./remove-redden.py $(ag -l redden src/)
# TODO: fail on nested REMOVE
# TODO: explicitly ignore escaped delimiters
import sys
REMOVE = '\\redden'
DELIMITERS = ['{', '}']
def mark_removals(text):
removals = []
for i in range(0, len(text)):
if text[i:i+len(REMOVE)+1] == REMOVE + DELIMITERS[0]:
removals.append([i, i+len(REMOVE)+1])
depth = 1
pos = i + len(REMOVE)+1
while depth > 0:
pos = pos + 1
if text[pos] == DELIMITERS[0]:
depth = depth + 1
elif text[pos] == DELIMITERS[1]:
depth = depth - 1
removals.append([pos, pos+1])
return removals
def remove(text, removals):
for i in reversed(removals):
text = text[:i[0]] + text[i[1]:]
return text
for i in range(1, len(sys.argv)):
with open(sys.argv[i], 'r') as infile:
instring = infile.read()
removals = mark_removals(instring)
outstring = remove(instring, removals)
with open(sys.argv[i], 'w') as outfile:
outfile.write(outstring)