gen_js_from_hbb.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. import re
  3. import os
  4. import glob
  5. from tabnanny import check
  6. def pad_start(s, n, c = ' '):
  7. if len(s) >= n:
  8. return s
  9. return c * (n - len(s)) + s
  10. def safe_unicode(s):
  11. res = ""
  12. for c in s:
  13. res += r"\u{}".format(pad_start(hex(ord(c))[2:], 4, '0'))
  14. return res
  15. def main():
  16. print('export const LANGS = {')
  17. for fn in glob.glob('../../../src/lang/*'):
  18. lang = os.path.basename(fn)[:-3]
  19. if lang == 'template': continue
  20. print(' %s: {'%lang)
  21. for ln in open(fn, encoding='utf-8'):
  22. ln = ln.strip()
  23. if ln.startswith('("'):
  24. toks = ln.split('", "')
  25. assert(len(toks) == 2)
  26. a = toks[0][2:]
  27. b = toks[1][:-3]
  28. print(' "%s": "%s",'%(safe_unicode(a), safe_unicode(b)))
  29. print(' },')
  30. print('}')
  31. check_if_retry = ['', False]
  32. KEY_MAP = ['', False]
  33. for ln in open('../../../src/client.rs', encoding='utf-8'):
  34. ln = ln.strip()
  35. if 'check_if_retry' in ln:
  36. check_if_retry[1] = True
  37. continue
  38. if ln.startswith('}') and check_if_retry[1]:
  39. check_if_retry[1] = False
  40. continue
  41. if check_if_retry[1]:
  42. ln = removeComment(ln)
  43. check_if_retry[0] += ln + '\n'
  44. if 'KEY_MAP' in ln:
  45. KEY_MAP[1] = True
  46. continue
  47. if '.collect' in ln and KEY_MAP[1]:
  48. KEY_MAP[1] = False
  49. continue
  50. if KEY_MAP[1] and ln.startswith('('):
  51. ln = removeComment(ln)
  52. toks = ln.split('", Key::')
  53. assert(len(toks) == 2)
  54. a = toks[0][2:]
  55. b = toks[1].replace('ControlKey(ControlKey::', '').replace("Chr('", '').replace("' as _)),", '').replace(')),', '')
  56. KEY_MAP[0] += ' "%s": "%s",\n'%(a, b)
  57. print()
  58. print('export function checkIfRetry(msgtype: string, title: string, text: string, retry_for_relay: boolean) {')
  59. print(' return %s'%check_if_retry[0].replace('to_lowercase', 'toLowerCase').replace('contains', 'indexOf').replace('!', '').replace('")', '") < 0'))
  60. print(';}')
  61. print()
  62. print('export const KEY_MAP: any = {')
  63. print(KEY_MAP[0])
  64. print('}')
  65. for ln in open('../../../Cargo.toml', encoding='utf-8'):
  66. if ln.startswith('version ='):
  67. print('export const ' + ln)
  68. def removeComment(ln):
  69. return re.sub('\s+\/\/.*$', '', ln)
  70. main()