Skip to content

Commit

Permalink
Add systemd lexer (#1568)
Browse files Browse the repository at this point in the history
This commit adds a lexer for systemd unit files. These unit files can
have multiple extensions but this commit only supports the `*.service`
file glob at this stage.

Co-authored-by: Michael Camilleri <mike@inqk.net>
  • Loading branch information
jljouannic and pyrmont committed Sep 8, 2020
1 parent 2d4976c commit f039145
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rouge/demos/systemd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[Unit]
Description=Snap Daemon
Requires=snapd.socket
OnFailure=snapd.failure.service
34 changes: 34 additions & 0 deletions lib/rouge/lexers/systemd.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
module Lexers
class SystemD < RegexLexer
tag 'systemd'
aliases 'unit-file'
filenames '*.service'
mimetypes 'text/x-systemd-unit'
desc 'A lexer for systemd unit files'

state :root do
rule %r/\s+/, Text
rule %r/[;#].*/, Comment
rule %r/\[.*?\]$/, Keyword
rule %r/(.*?)(=)(.*)(\\\n)/ do
groups Name::Tag, Punctuation, Text, Str::Escape
push :continuation
end
rule %r/(.*?)(=)(.*)/ do
groups Name::Tag, Punctuation, Text
end
end

state :continuation do
rule %r/(.*?)(\\\n)/ do
groups Text, Str::Escape
end
rule %r/(.*)'/, Text, :pop!
end
end
end
end
18 changes: 18 additions & 0 deletions spec/lexers/systemd_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

describe Rouge::Lexers::SystemD do
let(:subject) { Rouge::Lexers::SystemD::new }

describe 'guessing' do
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'unit.service'
end

it 'guesses by mimetype' do
assert_guess :mimetype => 'text/x-systemd-unit'
end
end
end
23 changes: 23 additions & 0 deletions spec/visual/samples/systemd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[Unit]
Description=Snap Daemon
Requires=snapd.socket
OnFailure=snapd.failure.service
# This is handled by snapd
# X-Snapd-Snap: do-not-start

[Service]
# Disabled because it breaks lxd
# (https://bugs.launchpad.net/snapd/+bug/1709536)
#Nice=-5
OOMScoreAdjust=-900
ExecStart=/usr/lib/snapd/snapd
EnvironmentFile=-/etc/environment
Restart=always
WatchdogSec=5m
Type=notify
SuccessExitStatus=42
RestartPreventExitStatus=42
KillMode=process

[Install]
WantedBy=multi-user.target

0 comments on commit f039145

Please sign in to comment.