diff --git a/src/lib/lyrics/parser.ts b/src/lib/lyrics/parser.ts index 21abf74..8803599 100644 --- a/src/lib/lyrics/parser.ts +++ b/src/lib/lyrics/parser.ts @@ -146,10 +146,12 @@ function tokenParserToText(p: Parser> | Parser[]>) } function lrcLine( - wordDiv = '' + wordDiv = ' ', legacy = false ): Parser { return alt_sc( - apply(seq(squareTS, rep_sc(seq(opt_sc(angleTS), trimmed(rep_sc(anythingTyped(['char', '[', ']'])))))), (r) => { + legacy ? apply(seq(squareTS, trimmed(rep_sc(anythingTyped(['char', '[', ']', '<', '>'])))), (r) => + ['script_item', { start: r[0], text: joinTokens(r[1]) } as any as ScriptItem] // TODO: Complete this + ) : apply(seq(squareTS, rep_sc(seq(opt_sc(angleTS), trimmed(rep_sc(anythingTyped(['char', '[', ']'])))))), (r) => { const start = r[0]; const text = r[1] @@ -188,7 +190,7 @@ export function dumpToken(t: Token | undefined): string { export function parseLRC( input: string, - { wordDiv, strict }: { wordDiv?: string; strict?: boolean } = { wordDiv: ' ' } + { wordDiv, strict, legacy }: { wordDiv?: string; strict?: boolean; legacy?: boolean } = {} ): LrcJsonData { const tokenizer = buildLexer([ [true, /^\[/gu, '['], @@ -205,7 +207,7 @@ export function parseLRC( return lines .map((line) => { - const res = expectEOF(lrcLine(wordDiv).parse(line)); + const res = expectEOF(lrcLine(wordDiv, legacy).parse(line)); if (!res.successful) { if (strict) { throw new Error('Failed to parse full line: ' + dumpToken(line)); diff --git a/src/test/lrcParser.test.ts b/src/test/lrcParser.test.ts index 6310f05..01ae0af 100644 --- a/src/test/lrcParser.test.ts +++ b/src/test/lrcParser.test.ts @@ -22,6 +22,7 @@ describe('LRC parser test', () => { expect(result.ti).toBe("Somebody to Love"); expect(result.ar).toBe("Jefferson Airplane"); + expect(result.scripts!!.length).toBe(3); expect(result.scripts!![0].text).toBe("When the truth is found to be lies"); expect(result.scripts!![0].start).toBe(0); expect(result.scripts!![0].words!![1].beginIndex).toBe("[00:00.00] <00:00.04> When <00:00.16> the".indexOf("the")); @@ -55,4 +56,14 @@ describe('LRC parser test', () => { expect(() => parseLRC(c, { strict: false })).not.toThrow(); } }) + it('Parses a legacy LRC', () => { + const result = parseLRC(test02Text, { wordDiv: ' ', strict: true, legacy: true }); + + expect(result.ti).toBe("Somebody to Love"); + expect(result.ar).toBe("Jefferson Airplane"); + expect(result.scripts!!.length).toBe(3); + expect(result.scripts!![1].text).toBe("<00:07.67> And <00:07.94> all <00:08.36> the <00:08.63> joy <00:10.28> within <00:10.53> you <00:13.09> dies"); + expect(result.scripts!![1].start).toBe(6000 + 470); + result.scripts!!.forEach((s) => expect(s.words).not.toBeDefined()); + }); }); \ No newline at end of file