-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregex.py
More file actions
84 lines (61 loc) · 3.1 KB
/
regex.py
File metadata and controls
84 lines (61 loc) · 3.1 KB
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
80
81
82
83
84
# This program takes 2 command line arguments: The original file and the replacement header.
# It writes to regex_output.twee
# Example Usage) python3 .\regex.py .\Twine\aztec_original.twee .\aztec_header.twee
import sys
import re
import json
# replacements = [{'regex': '\$role', 'replace': '$users[$userId]["role"]'},
# {'regex': '\$faction', 'replace': '$users[$userId]["faction"]'},
# {'regex': '<<run[ ]*counter\(([^,]*),"([^"]*)"\)', 'replace': r'<<set $\2 = $\2 + \1'},
# {'regex': '<<run[ ]*changeStats\(([^,]*),"([^"]*)"\)', 'replace': r'<<set $\2 = $\2 + \1'},
# {'regex': '(\$\w+)_sum', 'replace': r'\1'},
# {'regex': 'images\/', 'replace': 'Twine/images/'}
# ]
# replacements = []
# for i in ["Aztecs", "Tlaxcalans", "Spaniards"]:
# for j in ["Loyalty", "Strength", "Wisdom"]:
# replacements.append({'regex': f'{i}_{j}', 'replace' : f'factions["{i}"]["stats"]["{j}"]'})
# for i in ["Marina","Alvarado","Aguilar","Garrido","Olid","Moctezuma","Tlacaelel","Cuauhtemoc","Aztec_Priest","Cacamatzin","Pochteca","Xicotencatl_Elder","Xicotencatl_Younger","Maxixcatl", "Cortes"]:
# for j in ["Loyalty", "Strength", "Wisdom"]:
# replacements.append({'regex': f'{i}_{j}', 'replace' : f'users[$lookup["{i}"]]["stats"]["{j}"]'})
# Opens file
twee_in_file = open(sys.argv[1], encoding="utf-8")
new_twee = twee_in_file.read()
# Removes any <<theyr>> or <</theyr>> tags
# tags_to_remove = ['<<theyr>>', '<</theyr>>']
# for tag in tags_to_remove:
# new_twee = new_twee.replace(tag, "")
# # Iterates through replacement array and makes replacements
# for replacement in replacements:
# new_twee = re.sub(replacement['regex'], replacement['replace'], new_twee)
# # Replaces the header
# header_in_file = open(sys.argv[2], encoding="utf-8")
# twee_header = header_in_file.read()
# regex = re.compile('(:: Story Stylesheet).*(:: Act 1 Scene 1.*?\n)', re.DOTALL)
# new_twee = regex.sub(twee_header, new_twee)
# ## LIVEBLOCK TAGS ##
# whitelist = ["Character Identification"]
# # Whitelist all passages with textboxes in them
# expression = r'(:: ([^\n]+) {"position".*?}.*?)(?:(?=::)|(?=\Z))'
# m = re.findall(expression, new_twee, re.DOTALL)
# for match in m:
# if "<<textbox " in match[0]:
# passage = match[1]
# passage = passage.replace('[Done Breaks]', '')
# passage = passage.replace('[Done]', '')
# whitelist.append(passage)
# Add liveblock to all passages
expression = r'^+'
output = re.findall(expression, new_twee, flags=re.DOTALL)
print(json.dumps(output))
# Find whitelisted passages and remove their liveblocks
# for item in whitelist:
# expression = r'(:: ' + item + r'.*?{"position".*?)\n<<theyr>>(.*?)<</theyr>>\n\n'
# output = re.sub(expression, r'\1\2', output, flags=re.DOTALL)
# new_twee = output
# # Remove first <</theyr>> and append <</theyr>> to the end (fixes edge cases)
# new_twee = new_twee.replace("<</theyr>>", "", 1)
# new_twee += "<</theyr>>"
# Write to output file
out_file = open("regex_output.twee", "w", encoding="utf-8")
out_file.write(new_twee)