@@ -76,7 +76,10 @@ export async function cleanParser(
76
76
return translatedChunk ;
77
77
}
78
78
79
- export async function splitParser ( filePath : PathLike , logError : Function ) : Promise < [ boolean , string ] [ ] > {
79
+ export async function splitParser (
80
+ filePath : PathLike ,
81
+ logError : Function
82
+ ) : Promise < [ boolean , string ] [ ] > {
80
83
// Create a SAX parser in strict mode to split source into chunks.
81
84
const parser = createParser ( ) ;
82
85
@@ -137,7 +140,7 @@ export async function splitParser(filePath: PathLike, logError: Function): Promi
137
140
segments . length > 0 &&
138
141
segments [ segments . length - 1 ] [ 0 ] &&
139
142
segments [ segments . length - 1 ] [ 1 ] . length + currentSegment . length <
140
- Number ( MAXLEN )
143
+ Number ( MAXLEN )
141
144
) {
142
145
segments [ segments . length - 1 ] [ 1 ] += currentSegment ;
143
146
} else {
@@ -186,121 +189,123 @@ export async function splitParser(filePath: PathLike, logError: Function): Promi
186
189
return segments ;
187
190
}
188
191
189
- export async function recurSplitParser ( ori : string , filePath : PathLike , logError : Function ) : Promise < string [ ] > {
190
- let subTranslated : string [ ] = [ ] ;
191
- // continue splitting the chunk
192
- // Create a SAX parser in strict mode to split source into chunks.
193
- await new Promise < void > ( ( resolve , reject ) => {
194
- const subParser = createParser ( ) ;
192
+ export async function recurSplitParser (
193
+ ori : string ,
194
+ filePath : PathLike ,
195
+ logError : Function
196
+ ) : Promise < string [ ] > {
197
+ let subTranslated : string [ ] = [ ] ;
198
+ // continue splitting the chunk
199
+ // Create a SAX parser in strict mode to split source into chunks.
200
+ await new Promise < void > ( ( resolve , reject ) => {
201
+ const subParser = createParser ( ) ;
195
202
196
- let subCurrentDepth = 0 ;
197
- let subCurrentSegment = "" ;
198
- const subSegments : [ boolean , string ] [ ] = [ ] ;
199
- let subIsRecording = false ;
203
+ let subCurrentDepth = 0 ;
204
+ let subCurrentSegment = "" ;
205
+ const subSegments : [ boolean , string ] [ ] = [ ] ;
206
+ let subIsRecording = false ;
200
207
201
- subParser . on ( "opentag" , node => {
202
- if ( node . name === "WRAPPER" ) return ;
208
+ subParser . on ( "opentag" , node => {
209
+ if ( node . name === "WRAPPER" ) return ;
203
210
204
- subCurrentDepth ++ ;
211
+ subCurrentDepth ++ ;
205
212
206
- if ( subCurrentDepth === 2 ) subIsRecording = true ;
213
+ if ( subCurrentDepth === 2 ) subIsRecording = true ;
207
214
208
- if ( subIsRecording ) {
209
- subCurrentSegment += `<${ node . name } ${ formatAttributes ( node . attributes ) } >` ;
210
- } else {
211
- subSegments . push ( [
212
- false ,
213
- `<${ node . name } ${ formatAttributes ( node . attributes ) } >`
214
- ] ) ;
215
- }
216
- } ) ;
215
+ if ( subIsRecording ) {
216
+ subCurrentSegment += `<${ node . name } ${ formatAttributes ( node . attributes ) } >` ;
217
+ } else {
218
+ subSegments . push ( [
219
+ false ,
220
+ `<${ node . name } ${ formatAttributes ( node . attributes ) } >`
221
+ ] ) ;
222
+ }
223
+ } ) ;
217
224
218
- subParser . on ( "text" , text => {
219
- text = strongEscapeXML ( text ) ;
220
- if ( subIsRecording ) {
221
- subCurrentSegment += text ;
222
- } else if (
223
- subSegments . length > 0 &&
224
- subSegments [ subSegments . length - 1 ] [ 0 ]
225
- ) {
226
- subSegments [ subSegments . length - 1 ] [ 1 ] += text ;
227
- } else if (
228
- text . trim ( ) === "" ||
229
- text . trim ( ) === "," ||
230
- text . trim ( ) === "."
231
- ) {
232
- subSegments . push ( [ false , text ] ) ;
233
- } else {
234
- subSegments . push ( [ true , text ] ) ;
235
- }
236
- } ) ;
225
+ subParser . on ( "text" , text => {
226
+ text = strongEscapeXML ( text ) ;
227
+ if ( subIsRecording ) {
228
+ subCurrentSegment += text ;
229
+ } else if (
230
+ subSegments . length > 0 &&
231
+ subSegments [ subSegments . length - 1 ] [ 0 ]
232
+ ) {
233
+ subSegments [ subSegments . length - 1 ] [ 1 ] += text ;
234
+ } else if (
235
+ text . trim ( ) === "" ||
236
+ text . trim ( ) === "," ||
237
+ text . trim ( ) === "."
238
+ ) {
239
+ subSegments . push ( [ false , text ] ) ;
240
+ } else {
241
+ subSegments . push ( [ true , text ] ) ;
242
+ }
243
+ } ) ;
237
244
238
- subParser . on ( "cdata" , cdata => {
239
- if ( subIsRecording ) {
240
- subCurrentSegment += `<![CDATA[${ cdata } ]]>` ;
241
- }
242
- } ) ;
245
+ subParser . on ( "cdata" , cdata => {
246
+ if ( subIsRecording ) {
247
+ subCurrentSegment += `<![CDATA[${ cdata } ]]>` ;
248
+ }
249
+ } ) ;
243
250
244
- subParser . on ( "closetag" , tagName => {
245
- if ( tagName === "WRAPPER" ) {
246
- return ;
247
- }
251
+ subParser . on ( "closetag" , tagName => {
252
+ if ( tagName === "WRAPPER" ) {
253
+ return ;
254
+ }
248
255
249
- subCurrentSegment += `</${ tagName } >` ;
256
+ subCurrentSegment += `</${ tagName } >` ;
250
257
251
- if ( subCurrentDepth === 2 ) {
252
- // We are closing a segment element.
253
- if ( ignoredTags . includes ( tagName ) ) {
254
- subSegments . push ( [ false , subCurrentSegment ] ) ;
255
- } else if (
256
- subSegments . length > 0 &&
257
- subSegments [ subSegments . length - 1 ] [ 0 ] &&
258
- subSegments [ subSegments . length - 1 ] [ 1 ] . length +
258
+ if ( subCurrentDepth === 2 ) {
259
+ // We are closing a segment element.
260
+ if ( ignoredTags . includes ( tagName ) ) {
261
+ subSegments . push ( [ false , subCurrentSegment ] ) ;
262
+ } else if (
263
+ subSegments . length > 0 &&
264
+ subSegments [ subSegments . length - 1 ] [ 0 ] &&
265
+ subSegments [ subSegments . length - 1 ] [ 1 ] . length +
259
266
subCurrentSegment . length <
260
267
Number ( MAXLEN )
261
- ) {
262
- subSegments [ subSegments . length - 1 ] [ 1 ] += subCurrentSegment ;
263
- } else {
264
- subSegments . push ( [ true , subCurrentSegment ] ) ;
265
- }
266
- subCurrentSegment = "" ;
267
- subIsRecording = false ;
268
+ ) {
269
+ subSegments [ subSegments . length - 1 ] [ 1 ] += subCurrentSegment ;
270
+ } else {
271
+ subSegments . push ( [ true , subCurrentSegment ] ) ;
268
272
}
273
+ subCurrentSegment = "" ;
274
+ subIsRecording = false ;
275
+ }
269
276
270
- if ( subCurrentDepth === 1 ) {
271
- subSegments . push ( [ false , `</${ tagName } >` ] ) ;
272
- subCurrentSegment = "" ;
273
- }
277
+ if ( subCurrentDepth === 1 ) {
278
+ subSegments . push ( [ false , `</${ tagName } >` ] ) ;
279
+ subCurrentSegment = "" ;
280
+ }
274
281
275
- subCurrentDepth -- ;
276
- } ) ;
282
+ subCurrentDepth -- ;
283
+ } ) ;
277
284
278
- subParser . on ( "comment" , comment => {
279
- if ( subIsRecording ) {
280
- subCurrentSegment += `<!-- ${ comment } -->` ;
281
- } else {
282
- subSegments . push ( [ false , `<!-- ${ comment } -->` ] ) ;
283
- }
284
- } ) ;
285
-
286
- subParser . on ( "end" , async ( ) =>
287
- resolve ( )
288
- ) ;
289
-
290
- subParser . on ( "error" , err => {
291
- logError ( `Error in subParser for ${ filePath } :` , err , filePath ) ;
292
- // Try to recover and continue
293
- try {
294
- subParser . _parser . error = null ;
295
- subParser . _parser . resume ( ) ;
296
- } catch ( resumeErr ) {
297
- logError ( `Could not recover from parser error:` , resumeErr , filePath ) ;
298
- reject ( err ) ;
299
- }
300
- } ) ;
285
+ subParser . on ( "comment" , comment => {
286
+ if ( subIsRecording ) {
287
+ subCurrentSegment += `<!-- ${ comment } -->` ;
288
+ } else {
289
+ subSegments . push ( [ false , `<!-- ${ comment } -->` ] ) ;
290
+ }
291
+ } ) ;
292
+
293
+ subParser . on ( "end" , async ( ) => resolve ( ) ) ;
301
294
302
- Readable . from ( "<WRAPPER>" + ori + "</WRAPPER>" ) . pipe ( subParser ) ;
295
+ subParser . on ( "error" , err => {
296
+ logError ( `Error in subParser for ${ filePath } :` , err , filePath ) ;
297
+ // Try to recover and continue
298
+ try {
299
+ subParser . _parser . error = null ;
300
+ subParser . _parser . resume ( ) ;
301
+ } catch ( resumeErr ) {
302
+ logError ( `Could not recover from parser error:` , resumeErr , filePath ) ;
303
+ reject ( err ) ;
304
+ }
303
305
} ) ;
304
306
305
- return subTranslated ;
307
+ Readable . from ( "<WRAPPER>" + ori + "</WRAPPER>" ) . pipe ( subParser ) ;
308
+ } ) ;
309
+
310
+ return subTranslated ;
306
311
}
0 commit comments