Handle = as comment separator in .ini files

This commit is contained in:
Jordan Eldredge 2019-06-14 22:28:58 -07:00
parent 7995ca9918
commit 577b5e3c17
3 changed files with 38 additions and 3 deletions

View file

@ -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

View file

@ -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]

View file

@ -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] = {};