mirror of
https://github.com/captbaritone/webamp.git
synced 2026-01-23 18:25:30 +00:00
Handle = as comment separator in .ini files
This commit is contained in:
parent
7995ca9918
commit
577b5e3c17
3 changed files with 38 additions and 3 deletions
8
js/__tests__/fixtures/PLEDIT_WITH_EQUALS.txt
Executable file
8
js/__tests__/fixtures/PLEDIT_WITH_EQUALS.txt
Executable 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
|
||||
|
|
@ -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]
|
||||
|
|
|
|||
15
js/utils.ts
15
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] = {};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue