-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcommand_docs.py
169 lines (147 loc) · 5.55 KB
/
command_docs.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import re, json, polib, sys
from warnings import warn
def printFlag(arg):
return f'[-{arg["flag"]}]'
def printArg(arg):
name = arg['name'] + ('...' if arg['type'].endswith('...') else '')
if 'default' in arg:
return f'[{name}]'
else:
return f'<{name}>'
def getSubCmds(args):
subs = {}
for arg in args:
if 'subName' in arg and not arg['subName'].startswith('_'):
subs[arg['subName']] = arg
return subs
def printUsage(args, sub=''):
usage = []
subs = getSubCmds(args)
if subs and not sub:
text = '<'
for arg in subs.values():
text += arg['subName'] + '|'
usage.append(text[:-1] + '>')
else:
flagtext = ''
sublist = []
def processflagtext():
nonlocal flagtext
if flagtext:
usage.append(flagtext + ']')
flagtext = ''
for arg in args:
if 'subName' in arg:
processflagtext()
if arg['subName'] == sub:
usage.append(sub)
text = printUsage(arg.get('args', []))
if text:
usage.append(text[1:])
break
elif arg['subName'].startswith('_'):
if arg['subName'] == '_x':
text = printUsage(arg.get('args', []))
if text:
usage.append(text[1:])
break
else:
sublist.append(printUsage(arg.get('args', []))[1:])
elif 'flag' in arg:
if not flagtext:
flagtext = '[-'
flagtext += arg['flag']
if 'name' in arg:
flagtext += ' ' + printArg(arg)
processflagtext()
elif 'name' in arg:
processflagtext()
usage.append(printArg(arg))
processflagtext()
if sublist:
usage.append('(' + '|'.join(sublist) + ')')
return ' ' + ' '.join(usage) if usage else ''
commands_folder = '../WorldEdit/src/server/commands'
commands = {}
## Get location of commands
with open(commands_folder + '/command_list.ts') as file:
line = file.readline()
while line:
match = re.match(r'import "\.(/.+).js";', line)
if match:
commands[commands_folder + match.group(1) + '.ts'] = {}
line = file.readline()
## Get command data
for path in commands:
with open(path) as file:
line = file.readline()
jsonStr = None
while line:
if re.match(r'const registerInformation = {', line):
jsonStr = '{\n'
elif re.match(r'};', line):
jsonStr += '}'
break
elif jsonStr:
if re.match(r'\s+default:.+\n', line):
line = '\tdefault: 1' + (',\n' if line[:-1].endswith(',') else '\n')
elif re.match(r'\s+range:.+\n', line):
line = '\trange: 1' + (',\n' if line[:-1].endswith(',') else '\n')
line = re.sub(r'(\s+)(.+?):(.+)', r'\1"\2":\3', line)
jsonStr += line.replace("'", '"').split('//')[0]
line = file.readline()
jsonStr = re.sub(r'(\w+?)(?=: )', r'"\1"', jsonStr)
jsonStr = re.sub(r',(\s*?)([}\]])', r'\1\2', jsonStr)
try:
commands[path] = json.loads(jsonStr)
except Exception as e:
print(e, '\n' + jsonStr, file=sys.stderr)
exit(1)
texts_file = '../WorldEdit/texts/en_US.po'
texts = {
'commands.help.description': 'Get a list of commands available and a quick description for each of them'
}
## Get texts
for entry in polib.pofile(texts_file):
if entry.msgid != '':
texts[entry.msgid] = entry.msgstr.replace('\\"', '"')
commandspage = 'docs/commands.md'
prevfile = ''
with open(commandspage, 'r') as file:
for line in file.readlines():
prevfile += line
if line.startswith('<!--COMMANDAREA-->'):
break
with open(commandspage, 'w') as file:
file.write(prevfile)
def printCommand(command, sub=None):
file.write('!!! note ""\n\t\n')
aliases = ''
for alias in command.get('aliases', []):
aliases += ' (or ' if not aliases else ''
aliases += f';{alias}, '
aliases = ')'.join(aliases.rsplit(', ', 1))
if sub:
name = f'{command["name"]} {sub["subName"]}'
desc = command['description'] + '.' + sub['subName']
perm = sub.get('permission', '')
args = printUsage(command.get('usage', []), sub['subName'])
aliases = ''
else:
name = command['name']
desc = command['description']
perm = command.get('permission', '')
args = printUsage(command.get('usage', []))
if not desc in texts:
print(f'WARNING: There is no text value for \033[93m{desc}\033[0m.')
perm = f'`{perm}`' if perm else ''
file.write(f'\t**;{name}{aliases}**\n\n')
file.write(f'\t|**Description**|{texts.get(desc, desc)}|\n')
file.write('\t|:--|:--|\n')
file.write(f'\t|**Permission**|{perm}|\n')
file.write(f'\t|**Usage**|`;{command["name"]}{args}`|\n')
file.write('\n')
for command in commands.values():
printCommand(command)
for sub in getSubCmds(command.get('usage', [])).values():
printCommand(command, sub)