1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-17 20:58:08 +02:00

#170 Add lexer for systemd config files (#171)

See: https://www.freedesktop.org/software/systemd/man/systemd.syntax.html#
This commit is contained in:
Peter Lamby 2018-09-18 11:45:27 +02:00 committed by Alec Thomas
parent e36baa268a
commit c4123fc163
3 changed files with 66 additions and 0 deletions

29
lexers/s/systemd.go Normal file
View File

@ -0,0 +1,29 @@
package s
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
var SYSTEMD = internal.Register(MustNewLexer(
&Config{
Name: "SYSTEMD",
Aliases: []string{"systemd"},
Filenames: []string{"*.service"},
MimeTypes: []string{"text/plain"},
},
Rules {
"root": {
{`\s+`, Text, nil},
{`[;#].*`, Comment, nil},
{`\[.*?\]$`, Keyword, nil},
{`(.*?)(=)(.*)(\\\n)`, ByGroups(NameAttribute, Operator, LiteralString, Text), Push("continuation")},
{`(.*?)(=)(.*)`, ByGroups(NameAttribute, Operator, LiteralString), nil},
},
"continuation": {
{`(.*?)(\\\n)`, ByGroups(LiteralString, Text), nil},
{`(.*)`, LiteralString, Pop(1)},
},
},
))

11
lexers/testdata/systemd.actual vendored Normal file
View File

@ -0,0 +1,11 @@
[Section A]
KeyOne=value 1
KeyTwo=value 2
# a comment
[Section B]
Setting="something" "some thing" "…"
KeyTwo=value 2 \
value 2 continued

26
lexers/testdata/systemd.expected vendored Normal file
View File

@ -0,0 +1,26 @@
[
{"type":"Keyword","value":"[Section A]"},
{"type":"Text","value":"\n"},
{"type":"NameAttribute","value":"KeyOne"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"value 1"},
{"type":"Text","value":"\n"},
{"type":"NameAttribute","value":"KeyTwo"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"value 2"},
{"type":"Text","value":"\n\n"},
{"type":"Comment","value":"# a comment"},
{"type":"Text","value":"\n\n"},
{"type":"Keyword","value":"[Section B]"},
{"type":"Text","value":"\n"},
{"type":"NameAttribute","value":"Setting"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"\"something\" \"some thing\" \"…\""},
{"type":"Text","value":"\n"},
{"type":"NameAttribute","value":"KeyTwo"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"value 2 "},
{"type":"Text","value":"\\\n"},
{"type":"LiteralString","value":" value 2 continued"},
{"type":"Text","value":"\n\n"}
]