-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjsToCldrKb.py
More file actions
422 lines (366 loc) · 12.6 KB
/
jsToCldrKb.py
File metadata and controls
422 lines (366 loc) · 12.6 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# -*- coding: utf-8 -*-
import argparse
import json
import logging
import os
import re
import sys
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import ElementTree
# From .js keyboard files, creats LDML for CLDR keyboards
# http://unicode.org/Public/cldr/37/
# Started 17-June-2020
def parseArgs():
parser = argparse.ArgumentParser(description='Read CLDR Keyboard LDML files')
parser.add_argument(
'--infile', nargs='*',
default='/Users/craig/Desktop/Projects/cldr-keyboards-37.0/keyboards/windows/ga-t-k0-windows.xml', # Zero or more
help='names of cldr keyboard file to convert')
parser.add_argument('outfile', nargs='*', default=None, # Zero or more
help='names of JavaScript keyboard file to convert')
args = parser.parse_args()
#var FR_CH_T_K0_WINDOWS =
test_js_data =\
"""{"id":"pcm","title":"Naijíriá Píjin","mappings":{"":{"":"{{̀}}234567890-={{́}}wertyuiop[]/asdfghjkl{{ọ}}{{ẹ}}zcvbnm,.;'"},"s":{"":"{{̀}}!@#$₦%&*()_+{{́}}WERTYUIOP{}?ASDFGHJKL{{Ọ}}{{Ẹ}}ZCVBNM<>:"},"c,cl":{"":"`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"},"sl":{"":"`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"},"l":{"":"~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?"},"sc,scl":{"":"~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?"}},"transform":{"à":"à","è":"è","ò":"ò","À":"À","È":"È","Ò":"Ò","á":"á","é":"é","ó":"ó","Á":"Á","É":"É","Ó":"Ó","ạ":"ạ","ẹ":"ẹ","ọ":"ọ","Ạ":"Ạ","Ẹ":"Ẹ","Ọ":"Ọ","''":"̣"},"historyPruneRegex":"̀|́|̣"}"""
# "id": "fr",
# "locale" : "pcm",
# "dir": "ltr",
# "title": "Naijíriá Píjin",
# "mappings": {
# "": {
#
# """{
# "": "{{\u0300}}1\u003234567890-={{\u0301}}wertyuiop[]/asdfghjkl{{ọ}}{{ẹ}}zcvbnm,.;\''"
# },
# "s": {
# "": "°+\\"*ç%&/()=?`QWERTZUIOPü!£ASDFGHJKLöäYXCVBNM;:_"
# },
# "l": {
# "": "§1234567890\'^QWERTZUIOPè¨$ASDFGHJKLéàYXCVBNM,.-"
# },
# "sl": {
# "": "°+\\"*ç%&/()=?`qwertzuiopü!£asdfghjklöäyxcvbnm;:_"
# },
# "cl": {
# "": "{{}}¦@#°§¬|¢{{}}{{}}´~{{}}{{}}€{{}}{{}}{{}}{{}}{{}}{{}}{{}}[]}{{}}{{}}{{}}{{}}{{}}{{}}{{}}{{}}{{}}{{}}{{{}}{{}}{{}}{{}}{{}}{{}}{{}}{{}}{{}}{{}}"
# },
# "c,cl": {
# "": ""
# }
# },
# "transform" : {
# "a\u0300": "à",
# "e\u0300": "è",
# "o\u0300": "ò",
#
# "A\u0300": "À",
# "E\u0300": "È",
# "O\u0300": "Ò",
#
# "a\u0301": "á",
# "e\u0301": "é",
# "o\u0301": "ó",
#
# "A\u0301": "Á",
# "E\u0301": "É",
# "O\u0301": "Ó",
#
# "a\u0323": "ạ",
# "e\u0323": "ẹ",
# "o\u0323": "ọ",
#
# "A\u0323": "Ạ",
# "E\u0323": "Ẹ",
# "O\u0323": "Ọ",
# "\'\'": "\u0323"
# }
# }
# """
class kbTree(ET.TreeBuilder):
def doctype(self, name, pubid, system):
x = name
class layout():
def __init__(self, outfilename=None):
self.header_info = \
"""// CLDR from JavaScript keyboard data
//
"""
self.source_file = ''
self.locale = 'locale'
self.id = 'id'
self.dir = 'rtl'
self.title = 'title'
# Mappings from Row/Columns to output values
self.mappings = {}
# Rules for CLDR transforms
# self.transforms = []
if outfilename:
self.outputname = outfilename
self.id = os.path.split(os.path.splitext(outfilename)[0])[1]
# self.outfile = open(outfilename, 'w')
else:
self.outputname = None
# self.outfile = sys.stdout
self.maprows = ['E', 'D', 'C', 'B', 'A'] # , 'A']
# Mappings:
# A03 -> space
# B01 - B10
# C01 - C11
# D01 - D13
# E01 - E13
self.row_base = {'A': 3, 'B': 1, 'C': 1, 'D': 1, 'E': 0}
self.row_max = {'A': 4, 'B': 11, 'C': 12, 'D': 14, 'E': 13}
self.maplayers = {'': '', 'shift': 's',
'shift+caps?': 's,sl',
'caps+shift': 'sl',
'altL': 'c', 'altR': 'c', 'shift+alt': 's,sc',
'ctrl+caps?': 'c,cl',
'caps': 'l',
'ctrl+alt+caps?': 'sc,slc',
'ctrl+alt+shift+caps?': 'sc,slc',
'altR+caps? ctrl+alt+caps?': 'cl'
# Others?
}
self.layers_to_modifiers = {
'': '',
's': 'shift',
's,sl': 'shift+caps?',
'sl': 'caps+shift',
'c': 'ctrl+alt',
',c': 'ctrl?',
's,sc': 'shift+alt',
'c,cl': 'ctrl+caps?',
'l': 'caps',
'al': 'shift+caps',
'sc,slc': 'ctrl+alt+caps?',
'sc,scl': 'ctrl+shift+caps?',
'cl': 'altR+caps? ctrl+alt+caps?'
# Others?
}
self.mapping_parse = re.compile(r'(\{\{[^\}]*\}\}|.)')
def outputLdml(self, parsed_json):
# Create a tree
tag = 'keyboard'
if 'locale' in parsed_json:
attrib = {'locale': parsed_json['locale']}
else:
attrib = {'locale': parsed_json['id']}
elem = Element(tag, attrib)
tree = ElementTree(elem)
version = Element('version', {'platform': '10', 'number': '1.0'})
elem.append(version)
names = Element('names')
name = Element('name', {'value': parsed_json['title']})
names.append(name)
elem.append(names)
settings = Element('settings', {'fallback': 'omit',
'transformPartial': 'hide'})
elem.append(settings)
displayMap = None
# Get all the key mappings for each level
# Output each layer
for layer in parsed_json['mappings']:
try:
modifier_value = self.layers_to_modifiers[layer]
except:
modifier_value = 'UNKNOWN'
print('Modifier value unknown: %s', layer)
modifiers = {'modifiers': modifier_value}
if modifier_value:
keymap = Element('keyMap', modifiers)
else:
keymap = Element('keyMap')
elements = parsed_json['mappings'][layer]
for start_point in elements:
# Get the key mappings.
mapping_string = elements[start_point]
codes = self.mapping_parse.split(mapping_string)
maps = []
if start_point == '':
# The whole list E00 ... A3
keys = []
for row in self.maprows:
self.row_base[row]
self.row_max[row]
for col in range(self.row_base[row], self.row_max[row]):
keys.append('%s%02d' % (row, col))
else:
keys=[]
print('STARTING SOMEWHERE ELSE')
index = 0
# If there's an A03 element, add this too!
# TODO:
# For anything with keycaps, e.g., {{S||ᐊᐱᓯ||\u202f}},
# add display mapping.
for code in codes:
if code and index < len(keys):
output = code
# Remove {{ }} if needed
if code[0] == '{' and len(code) > 1 and code[1]== '{':
if code[2:5] == "S||":
parts = code.split('||')
display = parts[1]
output = parts[2][:-2]
if display:
if not displayMap:
displayMap = Element('displayMap')
displayElement = Element('display', {'mapOutput': output,
'display': display})
displayMap.append(displayElement)
else:
# Get the contents only
output = code[2:-2]
if output:
map_content = {'iso': keys[index],
'to': output}
map = Element('map', map_content)
keymap.append(map)
index += 1
elem.append(keymap)
if displayMap:
elem.append(displayMap)
# transform items
if parsed_json['transform']:
transforms = Element('transforms', {'type': 'simple'})
keys = parsed_json['transform'].keys()
for key in keys:
key_parts = key.split('|')
for part in key_parts:
to_item = parsed_json['transform'][key]
transform = Element('transform',
{'from': part, 'to': to_item})
transforms.append(transform)
elem.append(transforms)
xml_output = ET.tostring(elem, encoding='utf-8')
# Add header info
doctype = '<!DOCTYPE keyboard SYSTEM "../dtd/ldmlKeyboard.dtd">\n'
return doctype + xml_output
def parseJS(self, jsText):
return
def outputJavaScript(self, outfile):
# Open file requested.
# Heading comments
self.outfile.write(self.header_info)
loadname = self.locale.replace('-', '_').upper()
self.outfile.write('var %s = {\n' % loadname)
self.outfile.write(' "id": "%s",\n' % self.id)
self.outfile.write(' "locale" : "%s",\n' % self.locale)
self.outfile.write(' "dir": "%s",\n' % self.dir)
self.outfile.write(' "title": "%s",\n' % self.title)
self.printMappings()
if self.transforms:
self.printTransforms()
self.outfile.write('}\n')
self.outfile.write('\n')
self.outfile.write('google.elements.keyboard.loadme(%s);\n' % loadname)
self.outfile.write('var extern = %s;\n' % loadname)
self.outfile.close()
return
def keyPositionsToString(self, keymap):
str_list = ['"']
for row in self.maprows:
for i in range(self.row_base[row], self.row_max[row]+1):
key = row + '%02d' % i
if key in keymap:
str_list.append(keymap[key])
else:
str_list.append(' ')
str_list.append('"')
str_list.append(' +\n "')
return ''.join(str_list[0:-1])
def printMappings(self):
self.outfile.write(' "mappings": {\n')
for key in self.mappings:
self.outfile.write(' "%s": {\n' % key)
# TODO: Get mappings in E01-A03 order, filling in gaps
self.outfile.write(' "": %s\n },\n' %
self.keyPositionsToString(self.mappings[key]))
self.outfile.write(' },\n')
return
def printTransforms(self):
self.outfile.write(' "transform" : {\n')
for trans in self.transforms:
self.outfile.write('%s\n' % trans)
self.outfile.write(' },\n')
def addTransforms(self, transforms):
if transforms.attrib['type'] == 'simple':
for child in transforms:
from_part = child.attrib['from']
# Fix special cases for RegEx handling ^
from_part = from_part.replace('^', '\\\\^')
self.transforms.append(
' "%s": "%s",' % (from_part, child.attrib['to']))
else:
# Deal with more complex transformations.
return
return
def addKeyMapping(self, new_map, key, output):
# print(' addMapping %s output = %s' % (key, output))
# Special case for C12
if key == "C12":
key = "D13"
new_map[key] = output
return
def addDisplayMappings(self):
# For any that differ in what is on the keycap from the output
return
def parseKeyMap(self, keymap):
#print(' KEYMAP %s has %s' % (keymap.tag, keymap.attrib))
try:
modifiers = keymap.attrib['modifiers']
except:
modifiers = ''
# print('KeyMap modifiers = %s' % modifiers)
try:
level = self.maplayers[modifiers]
except:
level = 'unknown:' + modifiers
new_map = {}
for item in keymap:
key = item.attrib['iso']
output = item.attrib['to']
self.addKeyMapping(new_map, key, output)
self.mappings[level] = new_map
return
def parseLdml(self, filename):
tree = ET.parse(filename)
root = tree.getroot()
#print('root = %s, attributes = %s' % (root, root.attrib))
self.locale = root.attrib['locale']
for child in root:
# print('TAG = %s, attrib = %s' % (child.tag, child.attrib))
if child.tag == "names":
print('// name = %s' % child.attrib)
self.title = ''
for name in child:
attribs = name.attrib
print('// name in title = %s' % attribs['value'])
self.title += attribs['value']
elif child.tag == "settings":
# print('settings = %s' % child.attrib)
settings_attrib = child.attrib
elif child.tag == "keyMap":
try:
modifiers = child.attrib['modifiers']
except:
modifiers = ''
self.parseKeyMap(child)
elif child.tag == "transforms":
self.addTransforms(child)
def main(argv):
parsed_json = json.loads(test_js_data)
args = parseArgs()
#print('args = %s' % args)
infile = sys.argv[1]
#print('Input = %s' % infile)
if len(sys.argv) > 2:
outname = sys.argv[2]
#print('Output = %s' % outfile)
else:
outname = None
parser = layout(outname)
parser.parseJS(infile)
parser.outputLdml(parsed_json)
if __name__ == "__main__":
main(sys.argv)