diff --git a/js/__tests__/fixtures/PLEDIT_WITH_EQUALS.txt b/js/__tests__/fixtures/PLEDIT_WITH_EQUALS.txt new file mode 100755 index 00000000..0b59e8d4 --- /dev/null +++ b/js/__tests__/fixtures/PLEDIT_WITH_EQUALS.txt @@ -0,0 +1,8 @@ +[text] +current=#606060 = current song title highlight +normal=#000000 = normal song title color +selectedbg=#6685A5 = selected song background +normalbg=#7897B7 = the normal background +MBFG=#000000 = MiniBrowser's font color +mbbg=#7897B7 = MiniBrowsere's font background color +font=Ariel = the font diff --git a/js/utils.test.ts b/js/utils.test.ts index 957a2abb..8f26ff2f 100644 --- a/js/utils.test.ts +++ b/js/utils.test.ts @@ -265,6 +265,24 @@ bar = baz expect(actual).toEqual(expected); }); + it("can parse a pledit.txt file that uses = to mark comments", () => { + const pledit = fixture("PLEDIT_WITH_EQUALS.TXT"); + const actual = parseIni(pledit); + + const expected = { + text: { + normal: "#000000", + mbfg: "#000000", + current: "#606060", + normalbg: "#7897B7", + selectedbg: "#6685A5", + mbbg: "#7897B7", + font: "Ariel", + }, + }; + expect(actual).toEqual(expected); + }); + it("allows quotes around values", () => { const actual = parseIni(` [foo] diff --git a/js/utils.ts b/js/utils.ts index 71550b42..cd14e57c 100644 --- a/js/utils.ts +++ b/js/utils.ts @@ -101,14 +101,23 @@ export const parseViscolors = (text: string): string[] => { }; const SECTION_REGEX = /^\s*\[(.+?)\]\s*$/; -const PROPERTY_REGEX = /^\s*([^;].*)\s*=\s*(.*)\s*$/; +const PROPERTY_REGEX = /^\s*([^;][^=]*)\s*=\s*(.*)\s*$/; export const parseIni = (text: string): IniData => { let section: string, match; return text.split(/[\r\n]+/g).reduce((data: IniData, line) => { if ((match = line.match(PROPERTY_REGEX)) && section != null) { - const value = match[2].replace(/(^")|("$)|(^')|('$)/gi, ""); - data[section][match[1].trim().toLowerCase()] = value; + const key = match[1].trim().toLowerCase(); + const value = match[2] + // Ignore anything after a second `=` + // TODO: What if this is inside quotes or escaped? + .replace(/=.*$/g, "") + .trim() + // Strip quotes + // TODO: What about escaped quotes? + // TODO: What about unbalanced quotes? + .replace(/(^")|("$)|(^')|('$)/g, ""); + data[section][key] = value; } else if ((match = line.match(SECTION_REGEX))) { section = match[1].trim().toLowerCase(); data[section] = {};