-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadfile.py
123 lines (89 loc) · 2.91 KB
/
loadfile.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import re
# Define regular expressions to match different components
comment_pattern = r'--\s(.+)'
lang_pattern = r'local L = LANG\.CreateLanguage\("([^"]+)"\)'
singleline_text_pattern = r'L\.([a-zA-Z_][a-zA-Z_0-9]*)\s*=\s*"((?:\\"|[^"])*)"'
multiline_text_pattern_open = r'L\.([a-zA-Z_][a-zA-Z_0-9]*)\s*=\s*\[\[([^\]]*)'
multiline_text_pattern_close = r'(.*?)\]\]'
multiline_single_line = r'L\.([a-zA-Z_][a-zA-Z_0-9]*)\s*=\s*\[\[(.*?)\]\]'
text_param_pattern = r'{([^{}]+)}'
def loadfile(path):
# Create a list to store the data
lines = []
data = []
# Open and read the file
with open(path, "r", encoding="utf-8") as file:
try:
lines = file.readlines()
except UnicodeDecodeError:
print("ERROR: " + path)
return
line_counter = 0
is_multiline = False
# Iterate through the lines
for line in lines:
line = line.strip()
# Check for comments
lang_match = re.match(lang_pattern, line)
if lang_match:
data.append({
"type" : "code",
"identifier": "local",
"content" : lang_match.group(0)
})
line_counter += 1
continue
# Check for comments
comment_match = re.match(comment_pattern, line)
if comment_match:
data.append({
"type" : "comment",
"content" : comment_match.group(1)
})
line_counter += 1
continue
singleline_text_match = re.search(singleline_text_pattern, line)
if singleline_text_match and line[0:2] != "--":
data.append({
"type" : "single",
"identifier": singleline_text_match.group(1),
"content" : singleline_text_match.group(2)
})
data[line_counter]["params"] = re.findall(text_param_pattern, data[line_counter]["content"])
line_counter += 1
continue
multisingleline_text_match = re.search(multiline_single_line, line)
if multisingleline_text_match and line[0:2] != "--":
data.append({
"type" : "multi",
"identifier": multisingleline_text_match.group(1),
"content" : multisingleline_text_match.group(2)
})
data[line_counter]["params"] = re.findall(text_param_pattern, data[line_counter]["content"])
line_counter += 1
continue
multiline_text_match_open = re.search(multiline_text_pattern_open, line)
if multiline_text_match_open and line[0:2] != "--":
data.append({
"type" : "multi",
"identifier": multiline_text_match_open.group(1),
"content" : multiline_text_match_open.group(2)
})
is_multiline = True
continue
multiline_text_match_close = re.search(multiline_text_pattern_close, line)
if multiline_text_match_close and line[0:2] != "--":
data[line_counter]["content"] += "\n" + multiline_text_match_close.group(1)
data[line_counter]["params"] = re.findall(text_param_pattern, data[line_counter]["content"])
line_counter += 1
is_multiline = False
continue
if is_multiline and line[0:2] != "--":
data[line_counter]["content"] += "\n" + line
continue
data.append({
"type": "empty",
"content": ""
})
line_counter += 1
return data