Skip to content

Commit

Permalink
Add SSH config lexer (#1543)
Browse files Browse the repository at this point in the history
This commit adds a lexer for SSH configuration files.

Co-authored-by: Michael Camilleri <mike@inqk.net>
  • Loading branch information
cmbuckley and pyrmont authored Jun 29, 2020
1 parent 16ce5ce commit b33aa4e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rouge/demos/ssh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Host example
Hostname example.com
User user
Port 1234
33 changes: 33 additions & 0 deletions lib/rouge/lexers/ssh.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
module Lexers
class SSH < RegexLexer
tag 'ssh'

title "SSH Config File"
desc 'A lexer for SSH configuration files'
filenames 'ssh_config'

state :root do
rule %r/[a-z0-9]+/i, Keyword, :statement
mixin :base
end

state :statement do
rule %r/\n/, Text, :pop!
rule %r/(?:yes|no|confirm|ask|always|auto|none|force)\b/, Name::Constant

rule %r/\d+/, Num
rule %r/[^#\s;{}$\\]+/, Text
mixin :base
end

state :base do
rule %r/\s+/, Text
rule %r/#.*/, Comment::Single
end
end
end
end
37 changes: 37 additions & 0 deletions spec/lexers/ssh_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

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

describe 'lexing' do
include Support::Lexing

it 'lexes a specification' do
example = 'Host example
Hostname www.example.com
Port 1234
Tunnel no'

assert_tokens_equal example, ["Keyword", "Host"],
["Text", " example\n "],
["Keyword", "Hostname"],
["Text", " www.example.com\n "],
["Keyword", "Port"],
["Text", " "],
["Literal.Number", "1234"],
["Text", "\n "],
["Keyword", "Tunnel"],
["Text", " "],
["Name.Constant", "no"]
end
end

describe 'guessing' do
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => '/etc/ssh_config'
end
end
end
15 changes: 15 additions & 0 deletions spec/visual/samples/ssh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This is the ssh client system-wide configuration file. See
# ssh_config(5) for more information. This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.

Host *
Port 22 # default port
Tunnel no
GSSAPIAuthentication yes
GSSAPIDelegateCredentials no

Host example
Hostname www.example.com
User user
Port 1234

0 comments on commit b33aa4e

Please sign in to comment.