Skip to content

Commit 42d24fa

Browse files
authored
INI: Consistently mimic Win32 INI parsing (#2779)
1 parent e6c0d29 commit 42d24fa

File tree

6 files changed

+255
-57
lines changed

6 files changed

+255
-57
lines changed

components/prism-ini.js

+38-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,42 @@
11
Prism.languages.ini= {
2-
'comment': /^[ \t]*[;#].*$/m,
3-
'selector': /^[ \t]*\[.*?\]/m,
4-
'constant': /^[ \t]*[^\s=]+?(?=[ \t]*=)/m,
5-
'attr-value': {
6-
pattern: /=.*/,
2+
3+
/**
4+
* The component mimics the behavior of the Win32 API parser.
5+
*
6+
* @see {@link https://github.com/PrismJS/prism/issues/2775#issuecomment-787477723}
7+
*/
8+
9+
'comment': {
10+
pattern: /(^[ \f\t\v]*)[#;][^\n\r]*/m,
11+
lookbehind: true
12+
},
13+
'header': {
14+
pattern: /(^[ \f\t\v]*)\[[^\n\r\]]*\]?/m,
15+
lookbehind: true,
716
inside: {
8-
'punctuation': /^[=]/
17+
'section-name': {
18+
pattern: /(^\[[ \f\t\v]*)[^ \f\t\v\]]+(?:[ \f\t\v]+[^ \f\t\v\]]+)*/,
19+
lookbehind: true,
20+
alias: 'selector'
21+
},
22+
'punctuation': /\[|\]/
923
}
10-
}
24+
},
25+
'key': {
26+
pattern: /(^[ \f\t\v]*)[^ \f\n\r\t\v=]+(?:[ \f\t\v]+[^ \f\n\r\t\v=]+)*(?=[ \f\t\v]*=)/m,
27+
lookbehind: true,
28+
alias: 'attr-name'
29+
},
30+
'value': {
31+
pattern: /(=[ \f\t\v]*)[^ \f\n\r\t\v]+(?:[ \f\t\v]+[^ \f\n\r\t\v]+)*/,
32+
lookbehind: true,
33+
alias: 'attr-value',
34+
inside: {
35+
'inner-value': {
36+
pattern: /^("|').+(?=\1$)/,
37+
lookbehind: true
38+
}
39+
}
40+
},
41+
'punctuation': /=/
1142
};

components/prism-ini.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+50-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,50 @@
1-
;
2-
; foobar
3-
# foobar
4-
5-
----------------------------------------------------
6-
7-
[
8-
["comment", ";"],
9-
["comment", "; foobar"],
10-
["comment", "# foobar"]
11-
]
12-
13-
----------------------------------------------------
14-
15-
Checks for comments.
1+
#
2+
;
3+
#
4+
;
5+
#
6+
#
7+
#
8+
#;
9+
#[foo]
10+
#foo=bar
11+
#foobar
12+
;
13+
;
14+
;
15+
;#
16+
;[foo]
17+
;foo=bar
18+
;foobar
19+
foo#bar
20+
foobar#
21+
foo;bar
22+
foobar;
23+
24+
----------------------------------------------------
25+
26+
[
27+
["comment", "#"],
28+
["comment", ";"],
29+
["comment", "#"],
30+
["comment", ";"],
31+
["comment", "#"],
32+
["comment", "#\t"],
33+
["comment", "# "],
34+
["comment", "#;"],
35+
["comment", "#[foo]"],
36+
["comment", "#foo=bar"],
37+
["comment", "#foobar"],
38+
["comment", ";"],
39+
["comment", ";\t"],
40+
["comment", "; "],
41+
["comment", ";#"],
42+
["comment", ";[foo]"],
43+
["comment", ";foo=bar"],
44+
["comment", ";foobar"],
45+
"\nfoo#bar\nfoobar#\nfoo;bar\nfoobar;"
46+
]
47+
48+
----------------------------------------------------
49+
50+
Checks for comments.
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[foo1]
2+
[ "foo2" ]
3+
[ foo3 ]
4+
[" foo4 "]
5+
["foo5 bar5"]
6+
["foo6"]
7+
[foo7
8+
[foo8 bar8]
9+
[foo9[bar9]
10+
[foo10]
11+
[foo11]bar11]
12+
13+
----------------------------------------------------
14+
15+
[
16+
["header", [
17+
["punctuation", "["],
18+
["section-name", "foo1"],
19+
["punctuation", "]"]
20+
]],
21+
22+
["header", [
23+
["punctuation", "["],
24+
["section-name", "\"foo2\""],
25+
["punctuation", "]"]
26+
]],
27+
28+
["header", [
29+
["punctuation", "["],
30+
["section-name", "foo3"],
31+
["punctuation", "]"]
32+
]],
33+
34+
["header", [
35+
["punctuation", "["],
36+
["section-name", "\" foo4 \""],
37+
["punctuation", "]"]
38+
]],
39+
40+
["header", [
41+
["punctuation", "["],
42+
["section-name", "\"foo5 bar5\""],
43+
["punctuation", "]"]
44+
]],
45+
46+
["header", [
47+
["punctuation", "["],
48+
["section-name", "\"foo6\""],
49+
["punctuation", "]"]
50+
]],
51+
52+
["header", [
53+
["punctuation", "["],
54+
["section-name", "foo7"]
55+
]],
56+
57+
["header", [
58+
["punctuation", "["],
59+
["section-name", "foo8 bar8"],
60+
["punctuation", "]"]
61+
]],
62+
63+
["header", [
64+
["punctuation", "["],
65+
["section-name", "foo9[bar9"],
66+
["punctuation", "]"]
67+
]],
68+
69+
["header", [
70+
["punctuation", "["],
71+
["section-name", "foo10"],
72+
["punctuation", "]"]
73+
]],
74+
75+
["header", [
76+
["punctuation", "["],
77+
["section-name", "foo11"],
78+
["punctuation", "]"]
79+
]],
80+
81+
"bar11]"
82+
]
83+
84+
----------------------------------------------------
85+
86+
Checks for headers (and section names).
+69-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,69 @@
1-
foo=Bar Baz
2-
foobar=42
3-
4-
----------------------------------------------------
5-
6-
[
7-
["constant", "foo"],
8-
["attr-value", [
9-
["punctuation", "="],
10-
"Bar Baz"
11-
]],
12-
["constant", "foobar"],
13-
["attr-value", [
14-
["punctuation", "="],
15-
"42"
16-
]]
17-
]
18-
19-
----------------------------------------------------
20-
21-
Checks for key/value pairs.
1+
bar1 =
2+
"bar2" =
3+
'bar3' =
4+
bar4 =
5+
" bar5 "=
6+
"bar6"=
7+
' bar7 '=
8+
'bar8'=
9+
= baz9
10+
= "baz10"
11+
= 'baz11'
12+
= baz12
13+
=" baz13 "
14+
="b14"a14"z14"
15+
="baz15"
16+
="baz16
17+
=' baz17 '
18+
='baz18'
19+
=b19"a19"z19
20+
=b20"az20
21+
=ba21"z21
22+
=baz22
23+
=baz23"
24+
bar24
25+
bar25 baz25=qux25
26+
bar26=
27+
bar27==baz27
28+
bar28=baz28
29+
bar29=baz29 qux29
30+
bar30=baz30=qux30
31+
32+
----------------------------------------------------
33+
34+
[
35+
["key", "bar1"], ["punctuation", "="],
36+
["key", "\"bar2\""], ["punctuation", "="],
37+
["key", "'bar3'"], ["punctuation", "="],
38+
["key", "bar4"], ["punctuation", "="],
39+
["key", "\" bar5 \""], ["punctuation", "="],
40+
["key", "\"bar6\""], ["punctuation", "="],
41+
["key", "' bar7 '"], ["punctuation", "="],
42+
["key", "'bar8'"], ["punctuation", "="],
43+
["punctuation", "="], ["value", ["baz9"]],
44+
["punctuation", "="], ["value", ["\"", ["inner-value", "baz10"], "\""]],
45+
["punctuation", "="], ["value", ["'", ["inner-value", "baz11"], "'"]],
46+
["punctuation", "="], ["value", ["baz12"]],
47+
["punctuation", "="], ["value", ["\"", ["inner-value", " baz13 "], "\""]],
48+
["punctuation", "="], ["value", ["\"", ["inner-value", "b14\"a14\"z14"], "\""]],
49+
["punctuation", "="], ["value", ["\"", ["inner-value", "baz15"], "\""]],
50+
["punctuation", "="], ["value", ["\"baz16"]],
51+
["punctuation", "="], ["value", ["'", ["inner-value", " baz17 "], "'"]],
52+
["punctuation", "="], ["value", ["'", ["inner-value", "baz18"], "'"]],
53+
["punctuation", "="], ["value", ["b19\"a19\"z19"]],
54+
["punctuation", "="], ["value", ["b20\"az20"]],
55+
["punctuation", "="], ["value", ["ba21\"z21"]],
56+
["punctuation", "="], ["value", ["baz22"]],
57+
["punctuation", "="], ["value", ["baz23\""]],
58+
"\nbar24\n",
59+
["key", "bar25 baz25"], ["punctuation", "="], ["value", ["qux25"]],
60+
["key", "bar26"], ["punctuation", "="],
61+
["key", "bar27"], ["punctuation", "="], ["value", ["=baz27"]],
62+
["key", "bar28"], ["punctuation", "="], ["value", ["baz28"]],
63+
["key", "bar29"], ["punctuation", "="], ["value", ["baz29 qux29"]],
64+
["key", "bar30"], ["punctuation", "="], ["value", ["baz30=qux30"]]
65+
]
66+
67+
----------------------------------------------------
68+
69+
Checks for key-value pairs.
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
[owner]
2-
[foobar]
3-
4-
----------------------------------------------------
5-
6-
[
7-
["selector", "[owner]"],
8-
["selector", "[foobar]"]
9-
]
10-
11-
----------------------------------------------------
12-
13-
Checks for section titles.
1+
=
2+
3+
----------------------------------------------------
4+
5+
[
6+
["punctuation", "="]
7+
]
8+
9+
----------------------------------------------------
10+
11+
Checks for punctuation marks.

0 commit comments

Comments
 (0)