-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_lua_api_file.py
215 lines (152 loc) · 5.8 KB
/
make_lua_api_file.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!python3
'''Make lua.api and lua.json.'''
import html, json, os, re, textwrap, urllib.request
import common
def html2json(content):
'''Read html content and extract keywords... and save to lua.json.'''
dic = {}
# Get keywords.
matches = re.findall(r'cannot\sbe\sused\sas\snames:\n+'
r'<pre>\s*(.+?)\s*</pre>', content, re.I|re.S)
if len(matches) == 1:
reserved_keywords = matches[0].split()
for item in reserved_keywords:
dic[item] = item
# Get functions.
matches = re.findall(r'<hr>(.+?)(?=<h\d>|<hr>)', content, re.I|re.S)
if matches:
for item in matches:
# Remove html tags.
item = re.sub('<.+?>', '', item)
item = html.unescape(item)
# Trim empty lines.
item = item.strip()
item = re.sub('\n{3,}', '\n\n', item)
# Skip if starts with these.
if item.startswith(('lua_', 'luaL_')):
continue
# Make newlines consistent after the signature.
if ')\n' in item and not ')\n\n' in item:
if item.find(')\n') < 50:
item = item.replace(')\n', ')\n\n', 1)
# Get key name and add to the dictionary.
key = re.match('[_a-zA-Z][_a-zA-Z.0-9:]+', item).group(0)
dic[key] = item
# Write to json file.
with open('lua.json', 'w') as w:
json.dump(dic, w, indent=4, sort_keys=True)
def make_api():
'''Read from lua.json and save processed information to lua.api.'''
def _value_cleanup(value):
'''Get the signature and the doc.'''
value = value.strip()
# Split text into a list of lines.
lines = value.split('\n')
# Get call signature.
signature = lines[0]
# Prepare the list for doc processing.
lines = lines[1:]
while len(lines) and lines[0] == '':
del lines[0]
# Create the doc string.
doc = ''
for line in lines:
if line.strip() == '':
break
else:
doc += line + '\n'
doc.strip()
# Wrap doc string and add escapes.
lines = textwrap.wrap(doc, width=75)
doc = '\n'.join(lines)
doc = doc.replace('\\', '\\\\')
doc = doc.replace('\n', '\\n')
# Add (-) to signature as not callable,
# else the doc would need to be '' to comply.
if '(' not in signature and doc:
signature += ' (-)'
return [signature, doc]
# Uncallable calltips for these keywords.
custom = {
'for': ('for (-)',
"for Name '=' exp ',' exp [',' exp] do block end\\n"
'for namelist in explist do block end'),
'function': ('function (-)',
'function funcname funcbody\\n'
'local function Name funcbody'),
'goto': ('goto (-)', 'goto Name will goto the label ::Name::'),
'if': ('if (-)',
'if exp then block {elseif exp then block} [else block] end'),
'repeat': ('repeat (-)', 'repeat block until exp'),
'while': ('while (-)', 'while exp do block end')}
# Read the json file.
with open('lua.json') as r:
dic = json.load(r)
# Write the api file.
with open('lua.api', 'w') as w:
for key, value in dic.items():
if key in custom:
signature, doc = custom[key]
else:
signature, doc = _value_cleanup(value)
if doc:
w.write('{} {}\n'.format(signature, doc))
else:
w.write('{}\n'.format(signature))
def clean_manual(content):
'''Clean content of manual.html suitable for the api file.'''
def _repl(item):
'''Replace match if not a needed html tag.'''
item = item.group().lower()
if item == '<hr>':
return item
elif re.match('</{,1}h[r0-6]>', item):
return item
elif re.match('</{,1}pre>', item):
return item
else:
return ''
# Clean manual.html content.
content = re.sub('<.+?>', _repl, content)
for item in (('&', '&'), ('©', ''),
('>', '>'), ('≤', '<='),
('<', '<'), ('·', '.'),
('–', '-'), (' ', ' '),
('π', 'pi'), ('§', '#'),
('‘', '"'), ('’', '"')):
content = content.replace(item[0], item[1])
content = re.sub(r'\n{4,}', r'\n' * 3, content)
content = content.strip() + '\n'
return content
if __name__ == '__main__':
# Set output path.
output = common.settings['output']
if not os.path.isdir(output):
os.makedirs(output)
os.chdir(output)
# Read manual.html from file else read manual.html from lua.org.
if os.path.isfile(os.path.join('lua_chm', 'manual.html')):
print('read manual.html from file')
with open(os.path.join('lua_chm', 'manual.html')) as r:
content = r.read()
else:
print('read manual.html from lua.org')
lua_version = common.lua_version()
if not lua_version:
print('Lua version needs to be major.minor, for example 5.3')
lua_version = input('Lua version: ').strip()
if not re.match(r'\d+\.\d+$', lua_version):
exit('Invalid Lua version')
link = 'https://www.lua.org/manual/{}/manual.html'.format(lua_version)
with urllib.request.urlopen(link) as r:
content = r.read().decode('latin_1')
# Clean content of manual.html.
content = clean_manual(content)
print('output:')
# Make lua.json.
print(' lua.json')
html2json(content)
# Make lua.api.
print(' lua.api')
make_api()
print('done')