import { ColorTag , Language , tagColor } from "." ; export function preprocess ( string ) { { const stack = [ ] ; string = string . replace ( /<(\/?)color(?:=#([0-9A-F]{8}))?>/g , ( _match , isClosed , color ) => { if ( isClosed ) return `</${ stack . pop ( ) }>` ; const tag = tagColor [ color ] ; stack . push ( tag ) ; return `<${ tag }>` ; } ) ; } if ( string . startsWith ( "#" ) ) { string = string . substring ( 1 ) . replace ( /\{LAYOUT_(PC|PS|MOBILE)#(.*?)\}/g , ( _match , layout , text ) => layout === "PC" ? text : "" ) ; } return string ; } export function parseBulletPoints ( strings ) { strings . push ( "" ) ; const { strs } = strings . reduce ( ( { strs , arr } , str ) => { const isBullet = str . startsWith ( "·" ) ; if ( isBullet ) str = str . slice ( 1 ) ; if ( arr ) { if ( isBullet ) arr . push ( str ) ; else { strs . push ( arr as any ) ; arr = undefined ; } } else if ( isBullet ) { arr = [ str ] ; } if ( ! isBullet ) strs . push ( str ) ; return { strs , arr } ; } , { strs : [ ] as string [ ] , arr : undefined as string [ ] | undefined } ) ; strs . pop ( ) ; return strs ; } function plungeUtil ( lang , string , low ) { const res = low ? "$2" : "$3" ; string = string . split ( '|' ) [ 0 ] ; switch ( lang ) { case "chs" : case "cht" : case "ja" : string = string . replace ( /((\S{2})\/(\S{2}))/ , res ) ; break ; case "th" : string = string . replace ( /((\S{3})\/(\S{3}))/ , res ) ; default : string = string . replace ( /((\S+)\/(\S+))/ , res ) ; break ; } return string ; } const paragraph = string => { let parsed = string . split ( "\\n" ) . map ( s => s || "<br/>" ) ; while ( parsed [ parsed . length - 1 ] === "<br/>" ) parsed . pop ( ) ; return { ... parseBulletPoints ( parsed ) } ; } ; const autoFields = string => { const strings = string . split ( "\\n\\n<strong>" ) . filter ( s => s ) . map ( s => "<strong>" + s ) ; return strings . map ( s => ( { ... paragraph ( s ) } ) ) ; } ; export const parsingFunctions = { autoName : ( lang , string ) => { if ( string . includes ( "·" ) ) { const index = string . indexOf ( "·" ) ; string = string . slice ( index + 1 ) ; } else if ( string . includes ( ":" ) ) { string = string . split ( ":" ) [ 1 ] . trim ( ) ; } else if ( string . includes ( " - " ) ) { const index = string . indexOf ( " - " ) ; string = string . slice ( index + 3 ) ; } if ( ! string ) throw ( `${ lang } has invalid "name"` ) ; return string ; } , autoFields : ( lang , string , keys ) => { const strings = autoFields ( string ) ; if ( strings . length === 3 ) { const [ normal , charged , plunging ] = strings ; return { normal , charged , plunging } as any ; } else if ( strings . length === 4 ) { const [ , charkey ] = keys ; if ( charkey === "KaedeharaKazuha" ) { const [ normal , charged , plunging , plunging_midare ] = strings ; return { normal , charged , plunging , plunging_midare } as any ; } if ( charkey === "Tartaglia" ) { const [ normal , charged , riptide , plunging ] = strings ; return { normal , charged , riptide , plunging } as any ; } } throw `parsing fields error: ${ string }` ; } , paragraph : ( lang , string ) => paragraph ( string ) , skillParam : ( lang , string ) => { string = string . split ( '|' ) [ 0 ] ; return string ; } , plungeLow : ( lang , string ) => plungeUtil ( lang , string , true ) , plungeHigh : ( lang , string ) => plungeUtil ( lang , string , false ) , string : ( lang , string ) => string } ;