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

✨ adds a lexer for systemd unit files #1568

Merged
merged 3 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/rouge/demos/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
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', '*.socket', '*.device', '*.mount', '*.automount', '*.swap', '*.target', '*.path', '*.timer', '*.slice', '*.scope'
jljouannic marked this conversation as resolved.
Show resolved Hide resolved
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
28 changes: 28 additions & 0 deletions spec/lexers/systemd_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- 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'
assert_guess :filename => 'unit.socket'
assert_guess :filename => 'unit.device'
assert_guess :filename => 'unit.mount'
assert_guess :filename => 'unit.automount'
assert_guess :filename => 'unit.swap'
assert_guess :filename => 'unit.target'
assert_guess :filename => 'unit.path'
assert_guess :filename => 'unit.timer'
assert_guess :filename => 'unit.slice'
assert_guess :filename => 'unit.scope'
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