33
44
55def get_i18n (i18n_response : str ):
6- reg_script = '{name}\("{id}",({fn}|{quote}{any}{quote})\)' .format (
7- quote = "[\" ']" ,
8- name = "([a-z])" ,
9- id = "([a-z0-9]{8})" ,
10- any = "([\s\S]*?)" ,
11- fn = "\(function\([aent ]\){return([\s\S]*?)}\)" ,
6+ reg_script = r '{name}\("{id}",({fn}|{quote}{any}{quote})\)[,;}}] ' .format (
7+ quote = r "[\"']" ,
8+ name = r "([a-z])" ,
9+ id = r "([a-z0-9]{8})" ,
10+ any = r "([\s\S]*?)" ,
11+ fn = r "\(function\([a-z ]\){return([\s\S]*?)}\)" ,
1212 )
1313
1414 res_1 = {
1515 script [1 ]: (script [4 ] if script [3 ] == "" else i18n_format_1 (script [3 ].strip ()))
1616 for script in re .findall (reg_script , i18n_response )
1717 }
1818
19- reg_script = '{{key:"{id}",get:{fn}}}' .format (
20- id = "([a-z0-9]{8})" ,
21- fn = "function\(\){return([\s\S]*?)}" ,
19+ reg_script = r '{{key:"{id}",get:{fn}}}' .format (
20+ id = r "([a-z0-9]{8})" ,
21+ fn = r "function\(\){return([\s\S]*?)}" ,
2222 )
2323
2424 res_2 = {
@@ -32,63 +32,67 @@ def get_i18n(i18n_response: str):
3232 return res_1 | res_2
3333
3434
35- def replace_ver (script ):
35+ def replace_ver (script : str ):
3636 a = re .sub (
37- "{name}\.createElement\({any}\)" .format (
38- name = "([a-z])" ,
39- any = "([\s\S]*?)" ,
37+ r "{name}\.createElement\({any},([^,]*?) \)" .format (
38+ name = r "([a-z])" ,
39+ any = r "([\s\S]*?)" ,
4040 ),
41- " " ,
41+ r"\3 " ,
4242 script ,
4343 )
4444 b = re .sub (
45- "{reg}{join}{name}\({ins}\.{placeholder},{any},{any}\({any}\){any}\){join}{reg}" .format (
46- reg = "(\" |')?" ,
47- join = "(\+)?" ,
48- name = "([a-z])" ,
49- ins = "([aent]|this\.props)" ,
50- placeholder = "([A-Za-z0-9_]+)" ,
51- any = "([\s\S]*?)" ,
45+ r"{reg}{join}{name}\({ins}\.{placeholder},{any}(({notname}\({any}\){any})+?)\){join}{reg}" .format (
46+ reg = r"(\"|')?" ,
47+ join = r"(\+)?" ,
48+ name = r"([a-z])" ,
49+ ins = r"([a-z]|this\.props)" ,
50+ placeholder = r"([A-Za-z0-9_]+)" ,
51+ any = r"([^\(\)]*?)" ,
52+ notname = r"([^\+,][\s\S]|[\+,]?[\"'])" ,
5253 ),
53- r"\1\2\4.\5+ \6\10\11 " ,
54+ r"\1\2'('+ \4.\5, \6\7+')'\12\13 " ,
5455 a ,
5556 )
5657 c = re .sub (
57- "{reg}{join}{name}\({ins}\.{placeholder},{any },{any}\){join}{reg}" .format (
58- reg = "(\" |')?" ,
59- join = "(\+)?" ,
60- name = "([a-z])" ,
61- ins = "([aent ]|this\.props)" ,
62- placeholder = "([A-Za-z0-9_]+)" ,
63- any = "([\s\S]*?)" ,
58+ r "{reg}{join}{name}\({ins}\.{placeholder},{any}\){join}{reg}" .format (
59+ reg = r "(\"|')?" ,
60+ join = r "(\+)?" ,
61+ name = r "([a-z])" ,
62+ ins = r "([a-z ]|this\.props)" ,
63+ placeholder = r "([A-Za-z0-9_]+)" ,
64+ any = r "([\s\S]*?)" ,
6465 ),
65- r"\1\2\4.\5+\6\8\9 " ,
66+ r"\1\2'('+ \4.\5,\6+')'\7\8 " ,
6667 b ,
6768 )
68- d = re .sub (
69- "{reg}{join}{name}\({ins}\.{placeholder},{any}\){join}{reg}" .format (
70- reg = "(\" |')?" ,
71- join = "(\+)?" ,
72- name = "([a-z])" ,
73- ins = "([aent]|this\.props)" ,
74- placeholder = "([A-Za-z0-9_]+)" ,
75- any = "([\s\S]*?)" ,
69+
70+ e = re .sub (
71+ r"{reg}{join}{ins}\.{placeholder}(?={join}{reg})" .format (
72+ reg = r"(\"|')?" ,
73+ join = r"(\+|^|$|,)" ,
74+ ins = r"([a-z]|this\.props)" ,
75+ placeholder = r"([A-Za-z0-9_]+)" ,
7676 ),
77- r"\1\2\4.\5+\6\7\8 " ,
77+ r"\1\2{double_quotation}{\4}{double_quotation} " ,
7878 c ,
79+ ).replace ("{double_quotation}" , '"' )
80+ d = re .sub (
81+ r"{reg},{reg}(?!$)" .format (
82+ reg = r"(\"|')" ,
83+ ),
84+ r"\1+','+\2" ,
85+ e ,
7986 )
80-
81- e = re .sub (
82- "{reg}{join}{ins}\.{placeholder}{join}{reg}" .format (
83- reg = "(\" |')?" ,
84- join = "(\+)?" ,
85- ins = "([aent]|this\.props)" ,
86- placeholder = "([A-Za-z0-9_]+)" ,
87+ output = re .sub (
88+ r"('{any}'|\"{any}\")\+(?={reg})" .format (
89+ reg = r"(\"|')" ,
90+ any = r"([\s\S]*?)" ,
8791 ),
88- r"\1\2{double_quotation}{\4}{double_quotation}\5\6 " ,
92+ r"\1, " ,
8993 d ,
90- ). replace ( "{double_quotation}" , '"' )
91- output = e .replace ("+" , "," )
94+ )
95+ output = output .replace ("\" +','+ \" " , ' ","' )
9296 output = f"[{ output } ]"
9397 try :
9498 return ast .literal_eval (output )
@@ -103,14 +107,14 @@ def replace_ver(script):
103107 return [script ]
104108
105109
106- def i18n_format_1 (script ):
110+ def i18n_format_1 (script : str ):
107111 return "" .join (replace_ver (script ))
108112
109113
110- def i18n_format_2 (script ):
111- data = ["" ]
112- quote = []
113- before = ""
114+ def i18n_format_2 (script : str ):
115+ data : list [ str ] = ["" ]
116+ quote : list [ str ] = []
117+ before : str = ""
114118 for d in script .lstrip ("[" ).rstrip ("]" ):
115119 if d == "\\ " :
116120 before += "\\ "
@@ -128,7 +132,7 @@ def i18n_format_2(script):
128132 quote .append ("+" )
129133 elif d == quote [- 1 ]:
130134 quote .pop ()
131- elif d in ["(" ]:
135+ elif d in ["(" ] and quote [ - 1 ] == "+" :
132136 quote .append (d .replace ("(" , ")" ))
133137 data [- 1 ] += d
134138
0 commit comments