-
Notifications
You must be signed in to change notification settings - Fork 51
/
build.py
executable file
·305 lines (262 loc) · 8.54 KB
/
build.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
"""Script to build the Bikeshed (.bs) files for the UI Events spec."""
from __future__ import print_function
import os.path
import re
import subprocess
import sys
DEBUG = False
# This Parser processes the base *.txt files in the section/ directory to produce a set
# of output *.bs files that can be used as input to Bikeshed to create the final .html
# file.
# This pre-processing marks up the Key and Code values (with appropriate links) and
# generates HTML tables from the ASCII markup.
class Parser():
"""Pre-bikeshed parser for uievents spec."""
TABLE_TYPE_EVENT_SEQUENCE = 'event-sequence'
TABLE_TYPE_EVENT_DEFINITION = 'event-definition'
def __init__(self):
self.curr_src = ''
self.curr_dst = ''
self.in_table = False
self.table_type = ''
self.table_header_data = []
self.table_column_format = []
self.table_row_data = []
self.is_header_row = False
self.id = '0'
self.event = 'evy'
self.desc = 'desc'
def error(self, msg):
print(self.curr_src, self.line)
print('Error: %s' % (msg))
sys.exit(1)
def event_type(self, type):
if type == '' or type == '...':
return type
return '<a><code>' + type + '</code></a>'
def table_row(self):
# Don't print header row for event-definition.
if self.is_header_row and self.table_type == Parser.TABLE_TYPE_EVENT_DEFINITION:
return ''
if self.is_header_row:
self.table_row_data = self.table_header_data
if len(self.table_row_data) == 0:
return ''
if self.is_header_row:
row = '<thead><tr>'
else:
row = '<tr>'
for i in range(0, len(self.table_row_data)):
data = self.table_row_data[i]
colname = self.table_header_data[i]
align = self.table_column_format[i]
style = ''
if align == 'right':
style = ' style="text-align:right"'
elif align == 'center':
style = ' style="text-align:center"'
pre = '<td%s>' % style
post = '</td>'
if self.is_header_row or colname == '%':
pre = '<th%s>' % style
post = '</th>'
if colname == '#':
pre = '<td class="cell-number"%s>' % style
if self.is_header_row:
data = ''
if not self.is_header_row and data != '':
if colname == 'Event Type':
data = self.event_type(data)
if colname == 'DOM Interface':
data = '{{' + data + '}}'
row += pre + self.process_text(data) + post
if self.is_header_row:
row += '</tr></thead>\n'
else:
row += '</tr>\n'
return row
def process_text(self, desc):
m = re.match(r'^(.*)EVENT{(.+?)}(.*)$', desc)
if m:
pre = self.process_text(m.group(1))
name = m.group(2)
post = self.process_text(m.group(3))
desc = pre + self.event_type(name) + post
m = re.match(r'^(.*)CODE{(.*?)}(.*)$', desc)
if m:
pre = self.process_text(m.group(1))
name = m.group(2)
post = self.process_text(m.group(3))
desc = '%s<code class="code">"<a href="http://www.w3.org/TR/uievents-code/#code-%s">%s</a>"</code>%s' % (pre, name, name, post)
m = re.match(r'^(.*)KEY{(.+?)}(.*)$', desc)
if m:
pre = self.process_text(m.group(1))
name = m.group(2)
post = self.process_text(m.group(3))
desc = '%s<code class="key">"<a href="http://www.w3.org/TR/uievents-key/#key-%s">%s</a>"</code>%s' % (pre, name, name, post)
m = re.match(r'^(.*)KEY_NOLINK{(.+?)}(.*)$', desc)
if m:
pre = self.process_text(m.group(1))
name = m.group(2)
post = self.process_text(m.group(3))
desc = '%s<code class="key">"%s"</code>%s' % (pre, name, post)
m = re.match(r'^(.*)KEYCAP{(.+?)}(.*)$', desc)
if m:
pre = self.process_text(m.group(1))
name = m.group(2)
post = self.process_text(m.group(3))
desc = pre + '<code class="keycap">' + name + '</code>' + post
m = re.match(r'^(.*)GLYPH{(.*?)}(.*)$', desc)
if m:
pre = self.process_text(m.group(1))
name = m.group(2)
post = self.process_text(m.group(3))
desc = pre + '<code class="glyph">"' + name + '"</code>' + post
m = re.match(r'^(.*)UNI{(.+?)}(.*)$', desc)
if m:
pre = self.process_text(m.group(1))
name = m.group(2)
post = self.process_text(m.group(3))
if name[0:2] != 'U+':
self.error('Invalid Unicode value (expected U+xxxx): %s\n' % name)
desc = pre + '<code class="unicode">' + name + '</code>' + post
return desc
def process_line(self, line):
if self.in_table:
# Header rows begin with '=|'
m = re.match(r'^\s*\=\|(.*)\|$', line)
if m:
self.table_header_data = [x.strip() for x in m.group(1).split('|')]
self.is_header_row = True
return ''
# New data rows begin with '+|'
m = re.match(r'^\s*\+\|(.*)\|$', line)
if m:
result = self.table_row()
self.table_row_data = [x.strip() for x in m.group(1).split('|')]
self.is_header_row = False
return result
# Tables end with: '++--'
m = re.match(r'^\s*\+\+--', line)
if m:
self.in_table = False
return self.table_row() + '</table>\n'
# Separator lines begin with ' +' and end with '+'
# They may only contain '-', '+' and 'o'.
m = re.match(r'^\s* \+([\-\+o]+)\+', line)
if m:
# Separator lines may contain column formatting info.
num_columns = len(self.table_header_data)
format_data = [x.strip() for x in m.group(1).split('+')]
if len(format_data) != num_columns:
self.error('Unexpected number of columns (%d) in row (expected %d):\n%s'
% (len(format_data), num_columns, line))
for i in range(0, len(self.table_header_data)):
align = 'left'
if len(format_data[i]) != 0:
if format_data[i][0] == 'o':
align = 'left'
elif format_data[i][-1] == 'o':
align = 'right'
elif 'o' in format_data[i]:
align = 'center'
self.table_column_format.append(align)
return ''
# Row continued from previous line: ' |'
m = re.match(r'^\s*\|(.*)\|', line)
if m:
num_columns = len(self.table_header_data)
extra_data = [x.strip() for x in m.group(1).split('|')]
if len(extra_data) != num_columns:
self.error('Unexpected number of columns (%d) in row (expected %d):\n%s'
% (len(extra_data), num_columns, line))
for i in range(0, len(self.table_header_data)):
if len(extra_data[i]) != 0:
if self.is_header_row:
self.table_header_data[i] += ' ' + extra_data[i]
else:
self.table_row_data[i] += ' ' + extra_data[i]
return ''
self.error('Expected table line: ' + line)
return('')
# Tables begin with: ++---+----+-------+
m = re.match(r'^\s*\+\+--[\-\+]*\+(?P<class> [\-a-z]+)?$', line)
if m:
table_class = m.group('class')
if table_class == None:
table_class = 'event-sequence-table'
self.table_type = Parser.TABLE_TYPE_EVENT_SEQUENCE
else:
table_class = table_class[1:]
self.table_type = Parser.TABLE_TYPE_EVENT_DEFINITION
self.in_table = True
#self.table_type = Parser.TABLE_TYPE_EVENT_SEQUENCE
self.table_header_data = []
self.table_column_format = []
self.table_row_data = []
return '<table class="%s">\n' % table_class
return self.process_text(line.rstrip()) + '\n'
def process(self, src, dst):
print('Processing', src)
self.curr_src = src
self.curr_dst = dst
if not os.path.isfile(src):
self.error('File "%s" doesn\'t exist\n' % src)
try:
infile = open(src, 'r')
except IOError as e:
self.error('Unable to open "%s" for reading: %s\n' % (src, e))
try:
outfile = open(dst, 'w')
except IOError as e:
self.error('Unable to open "%s" for writing: %s\n' % (dst, e))
self.line = 0
for line in infile:
self.line += 1
new_line = self.process_line(line)
outfile.write(new_line)
outfile.close()
infile.close()
def process_main_spec():
sections = [
'introduction',
'conventions',
'event-interfaces',
'event-uievent',
'event-focusevent',
'event-mouseevent',
'event-wheelevent',
'event-inputevent',
'event-keyboardevent',
'event-compositionevent',
'keyboard',
'external-algorithms',
'legacy-event-initializers',
'legacy-key-attributes',
'legacy-event-types',
'extending-events',
'security',
'acknowledgements',
'glossary',
]
# Generate an .include file for each .txt file in the sections/ directory.
# These .include files are referenced by the main index.bs file.
for section in sections:
infilename = 'sections/' + section + '.txt'
outfilename = 'sections/' + section + '.include'
# Generate the full include file for bikeshed.
parser = Parser()
parser.process(infilename, outfilename)
if '--includes-only' in sys.argv:
print('Skipped bikeshedding because of --includes-only.')
else:
print('Bikeshedding...')
cmd = ["bikeshed", "spec"]
if DEBUG:
cmd.append('--line-numbers')
subprocess.call(cmd)
def main():
process_main_spec()
if __name__ == '__main__':
main()