-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpost_process.py
36 lines (26 loc) · 955 Bytes
/
post_process.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
import re
import sys
def insert_empty_lines(text):
# Step 1: Insert an empty line between each paragraph
return re.sub(r'(\r?\n)(?!\r?\n)', r'\1\1', text)
def remove_extra_lines(text):
# Step 2: Reduce multiple blank lines to a single blank line
return re.sub(r'(\r?\n){2,}', r'\1\1', text)
def main():
if len(sys.argv) != 2:
print("Usage: post_process.py <file>")
sys.exit(1)
filename = sys.argv[1]
try:
with open(filename, 'r', encoding='utf-8') as file:
content = file.read()
# Perform the two-step process
content = insert_empty_lines(content)
content = remove_extra_lines(content)
with open(filename, 'w', encoding='utf-8') as file:
file.write(content)
print(f"Processed file saved: {filename}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()