Skip to content

Commit

Permalink
Added support for Systemd configuration files (#3053)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Sep 6, 2021
1 parent 87e5a37 commit 8df825e
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 2 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# comment
; comment

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

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

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

[
["key", "foo"], ["operator", "="],
["key", "foo"], ["operator", "="]
]
11 changes: 11 additions & 0 deletions tests/languages/systemd/section_feature.test
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
]]
]

0 comments on commit 8df825e

Please sign in to comment.