-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_prompt.py
51 lines (40 loc) · 1.52 KB
/
process_prompt.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
import argparse
import re
def replace_tags(file_path, compress):
# Define the replacements for compression and decompression
if compress:
replacements = {
r'<span style="color:red; font-weight:bold;">': '#1#',
r'</span>': '#2#'
}
else:
replacements = {
r'#1#': '<span style="color:red; font-weight:bold;">',
r'#2#': '</span>'
}
# Read the content of the file
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Perform replacements based on the replacement dictionary
for old, new in replacements.items():
content = re.sub(re.escape(old), new, content)
# Output the modified content
return content
def main():
# Setup argument parser
parser = argparse.ArgumentParser(description='Compress or decompress HTML tags in a file.')
parser.add_argument('compress', type=int, help='Compress flag (1 for compress, 0 for decompress)')
parser.add_argument('file_path', type=str, help='Path to the file to be processed')
args = parser.parse_args()
# Convert compress to boolean
compress = bool(args.compress)
# Replace tags based on compress value
result = replace_tags(args.file_path, compress)
# Print the result to stdout
# rewrite the file
with open(args.file_path, 'w', encoding='utf-8') as file:
file.write(result)
print(result)
if __name__ == '__main__':
main()
# python process_prompt.py 1 output.txt