Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Systemd configuration files #3053

Merged
merged 2 commits into from Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Expand Up @@ -1233,6 +1233,10 @@
"title": "Swift",
"owner": "chrischares"
},
"systemd": {
"title": "Systemd configuration file",
"owner": "RunDevelopment"
},
"t4-templating": {
"title": "T4 templating",
"owner": "RunDevelopment"
Expand Down
74 changes: 74 additions & 0 deletions components/prism-systemd.js
@@ -0,0 +1,74 @@
// https://www.freedesktop.org/software/systemd/man/systemd.syntax.html

(function (Prism) {

var comment = {
pattern: /^[;#].*/m,
greedy: true
};

var quotesSource = /"(?:[^\r\n"\\]|\\(?:[^\r]|\r\n?))*"(?!\S)/.source;

Prism.languages.systemd = {
'comment': comment,

'section': {
pattern: /^\[[^\n\r\[\]]*\](?=[ \t]*$)/m,
greedy: true,
inside: {
'punctuation': /^\[|\]$/,
'section-name': {
pattern: /[\s\S]+/,
alias: 'selector'
},
}
},

'key': {
pattern: /^[^\s=]+(?=[ \t]*=)/m,
greedy: true,
alias: 'attr-name'
},
'value': {
// This pattern is quite complex because of two properties:
// 1) Quotes (strings) must be preceded by a space. Since we can't use lookbehinds, we have to "resolve"
// the lookbehind. You will see this in the main loop where spaces are handled separately.
// 2) Line continuations.
// After line continuations, empty lines and comments are ignored so we have to consume them.
pattern: RegExp(
/(=[ \t]*(?!\s))/.source +
// the value either starts with quotes or not
'(?:' + quotesSource + '|(?=[^"\r\n]))' +
// main loop
'(?:' + (
/[^\s\\]/.source +
// handle spaces separately because of quotes
'|' + '[ \t]+(?:(?![ \t"])|' + quotesSource + ')' +
// line continuation
'|' + /\\[\r\n]+(?:[#;].*[\r\n]+)*(?![#;])/.source
) +
')*'
),
lookbehind: true,
greedy: true,
alias: 'attr-value',
inside: {
'comment': comment,
'quoted': {
pattern: RegExp(/(^|\s)/.source + quotesSource),
lookbehind: true,
greedy: true,
},
'punctuation': /\\$/m,

'boolean': {
pattern: /^(?:false|no|off|on|true|yes)$/,
greedy: true
}
}
},

'operator': /=/
};

}(Prism));
1 change: 1 addition & 0 deletions components/prism-systemd.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions examples/prism-systemd.html
@@ -0,0 +1,20 @@
<h2>Full example</h2>
<pre><code># Source: https://www.freedesktop.org/software/systemd/man/systemd.syntax.html

[Section A]
KeyOne=value 1
KeyTwo=value 2

# a comment

[Section B]
Setting="something" "some thing" "…"
KeyTwo=value 2 \
value 2 continued

[Section C]
KeyThree=value 3\
# this line is ignored
; this line is ignored too
value 3 continued
</code></pre>
1 change: 1 addition & 0 deletions plugins/show-language/prism-show-language.js
Expand Up @@ -212,6 +212,7 @@
"sqf": "SQF: Status Quo Function (Arma 3)",
"sql": "SQL",
"iecst": "Structured Text (IEC 61131-3)",
"systemd": "Systemd configuration file",
"t4-templating": "T4 templating",
"t4-cs": "T4 Text Templates (C#)",
"t4": "T4 Text Templates (C#)",
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions tests/languages/systemd/boolean_feature.test
@@ -0,0 +1,46 @@
foo=on
foo=true
foo=yes
foo=off
foo=false
foo=no

----------------------------------------------------

[
["key", "foo"],
["operator", "="],
["value", [
["boolean", "on"]
]],

["key", "foo"],
["operator", "="],
["value", [
["boolean", "true"]
]],

["key", "foo"],
["operator", "="],
["value", [
["boolean", "yes"]
]],

["key", "foo"],
["operator", "="],
["value", [
["boolean", "off"]
]],

["key", "foo"],
["operator", "="],
["value", [
["boolean", "false"]
]],

["key", "foo"],
["operator", "="],
["value", [
["boolean", "no"]
]]
]
9 changes: 9 additions & 0 deletions tests/languages/systemd/comment_feature.test
@@ -0,0 +1,9 @@
# comment
; comment

----------------------------------------------------

[
["comment", "# comment"],
["comment", "; comment"]
]
9 changes: 9 additions & 0 deletions tests/languages/systemd/key_feature.test
@@ -0,0 +1,9 @@
foo=
foo =

----------------------------------------------------

[
["key", "foo"], ["operator", "="],
["key", "foo"], ["operator", "="]
]
11 changes: 11 additions & 0 deletions tests/languages/systemd/section_feature.test
@@ -0,0 +1,11 @@
[Section Foo]

----------------------------------------------------

[
["section", [
["punctuation", "["],
["section-name", "Section Foo"],
["punctuation", "]"]
]]
]
49 changes: 49 additions & 0 deletions tests/languages/systemd/value_feature.test
@@ -0,0 +1,49 @@
foo= value 2

foo="something" "some thing" "…"
foo= "something" "some thing" "…"
foo=value 2 \
value 2 continued

foo=value 3\
# this line is ignored
; this line is ignored too
value 3 continued

----------------------------------------------------

[
["key", "foo"], ["operator", "="], ["value", ["value 2"]],

["key", "foo"],
["operator", "="],
["value", [
["quoted", "\"something\""],
["quoted", "\"some thing\""],
["quoted", "\"…\""]
]],

["key", "foo"],
["operator", "="],
["value", [
["quoted", "\"something\""],
["quoted", "\"some thing\""],
["quoted", "\"…\""]
]],

["key", "foo"],
["operator", "="],
["value", [
"value 2 ", ["punctuation", "\\"],
"\r\n value 2 continued"
]],

["key", "foo"],
["operator", "="],
["value", [
"value 3", ["punctuation", "\\"],
["comment", "# this line is ignored"],
["comment", "; this line is ignored too"],
"\r\n value 3 continued"
]]
]