-
Notifications
You must be signed in to change notification settings - Fork 0
/
format
34 lines (25 loc) · 829 Bytes
/
format
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
#!/usr/bin/env python3
"""
usage: ./format <filepaths>...
Format script for lettering. Makes everything lowercase except pronoun 'I'.
"""
import os
import sys
import re
def main(argv: list) -> None:
if not argv:
print("usage: ./format <filepaths>...")
for filename in argv:
if os.path.exists(filename):
with open(filename, mode="r+", encoding="utf-8") as f:
lines = f.readlines()
lines = [process(x) for x in lines]
f.seek(0)
f.writelines(lines)
else:
print(f"Couldn't find file {filename}")
def process(line: str) -> str:
line = re.sub(r"[‘’']", r"'", line.lower())
return re.sub(r"(^|\W)i(?:(')(ll|ve|d|m))?($|\W)", r"\1I\2\3\4", line)
if __name__ == "__main__":
main(sys.argv[1:])