rustdesk/flutter/web/v1/js/gen_js_from_hbb.py

78 lines
2.3 KiB
Python
Raw Normal View History

2022-01-30 03:39:54 +08:00
#!/usr/bin/env python3
2022-12-26 00:23:03 +08:00
import re
2022-01-30 03:39:54 +08:00
import os
import glob
from tabnanny import check
def pad_start(s, n, c = ' '):
if len(s) >= n:
return s
return c * (n - len(s)) + s
def safe_unicode(s):
res = ""
for c in s:
res += r"\u{}".format(pad_start(hex(ord(c))[2:], 4, '0'))
return res
2022-01-30 03:39:54 +08:00
def main():
print('export const LANGS = {')
2022-05-12 18:31:17 +08:00
for fn in glob.glob('../../../src/lang/*'):
2022-01-30 03:39:54 +08:00
lang = os.path.basename(fn)[:-3]
2022-03-25 18:56:55 +08:00
if lang == 'template': continue
2022-01-30 03:39:54 +08:00
print(' %s: {'%lang)
for ln in open(fn, encoding='utf-8'):
2022-01-30 03:39:54 +08:00
ln = ln.strip()
if ln.startswith('("'):
toks = ln.split('", "')
assert(len(toks) == 2)
a = toks[0][2:]
b = toks[1][:-3]
print(' "%s": "%s",'%(safe_unicode(a), safe_unicode(b)))
2022-01-30 03:39:54 +08:00
print(' },')
print('}')
check_if_retry = ['', False]
KEY_MAP = ['', False]
for ln in open('../../../src/client.rs', encoding='utf-8'):
2022-01-30 03:39:54 +08:00
ln = ln.strip()
if 'check_if_retry' in ln:
check_if_retry[1] = True
continue
if ln.startswith('}') and check_if_retry[1]:
check_if_retry[1] = False
continue
if check_if_retry[1]:
2022-02-05 01:55:23 +08:00
ln = removeComment(ln)
2022-01-30 03:39:54 +08:00
check_if_retry[0] += ln + '\n'
if 'KEY_MAP' in ln:
KEY_MAP[1] = True
continue
if '.collect' in ln and KEY_MAP[1]:
KEY_MAP[1] = False
continue
if KEY_MAP[1] and ln.startswith('('):
2022-02-05 01:55:23 +08:00
ln = removeComment(ln)
2022-01-30 03:39:54 +08:00
toks = ln.split('", Key::')
assert(len(toks) == 2)
a = toks[0][2:]
b = toks[1].replace('ControlKey(ControlKey::', '').replace("Chr('", '').replace("' as _)),", '').replace(')),', '')
KEY_MAP[0] += ' "%s": "%s",\n'%(a, b)
print()
print('export function checkIfRetry(msgtype: string, title: string, text: string, retry_for_relay: boolean) {')
2022-01-30 03:39:54 +08:00
print(' return %s'%check_if_retry[0].replace('to_lowercase', 'toLowerCase').replace('contains', 'indexOf').replace('!', '').replace('")', '") < 0'))
print(';}')
print()
print('export const KEY_MAP: any = {')
print(KEY_MAP[0])
print('}')
for ln in open('../../../Cargo.toml', encoding='utf-8'):
2022-01-30 19:48:41 +08:00
if ln.startswith('version ='):
print('export const ' + ln)
2022-12-26 00:23:03 +08:00
2022-01-30 03:39:54 +08:00
2022-02-05 01:55:23 +08:00
def removeComment(ln):
return re.sub('\s+\/\/.*$', '', ln)
2022-01-30 03:39:54 +08:00
2022-03-25 18:56:55 +08:00
main()