Skip to content

Commit be0040e

Browse files
authored
Porting Cisco IOS configuration lexer from pygments-routerlexers (#1875)
* Porting Cisco IOS configuration lexer from pygments-routerlexers * Fixing Cisco IOS sample for linter
1 parent 7beff01 commit be0040e

4 files changed

Lines changed: 157 additions & 0 deletions

File tree

lib/rouge/demos/cisco_ios

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
interface FastEthernet0.20
2+
encapsulation dot1Q 20
3+
no ip route-cache
4+
bridge-group 1
5+
no bridge-group 1 source-learning
6+
bridge-group 1 spanning-disabled
7+
8+
! Supports shortened versions of config words, too
9+
inter gi0.10
10+
encap dot1q 10 native
11+
inter gi0
12+
13+
banner login # Authenticate yourself! #
14+
15+
! Supports #, $ and % to delimit banners, and multiline
16+
banner motd $
17+
Attention!
18+
We will be having scheduled system maintenance on this device.
19+
$

lib/rouge/lexers/cisco_ios.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# -*- coding: utf-8 -*- #
2+
# frozen_string_literal: true
3+
4+
# Based on/regexes mostly from Brandon Bennett's pygments-routerlexers:
5+
# https://github.com/nemith/pygments-routerlexers
6+
7+
module Rouge
8+
module Lexers
9+
class CiscoIos < RegexLexer
10+
title 'Cisco IOS'
11+
desc 'Cisco IOS configuration lexer'
12+
tag 'cisco_ios'
13+
filenames '*.cfg'
14+
mimetypes 'text/x-cisco-conf'
15+
16+
state :root do
17+
rule %r/^!.*/, Comment::Single
18+
19+
rule %r/^(version\s+)(.*)$/ do
20+
groups Keyword, Num::Float
21+
end
22+
23+
rule %r/(desc*r*i*p*t*i*o*n*)(.*?)$/ do
24+
groups Keyword, Comment::Single
25+
end
26+
27+
rule %r/^(inte*r*f*a*c*e*|controller|router \S+|voice translation-\S+|voice-port|line)(.*)$/ do
28+
groups Keyword::Type, Name::Function
29+
end
30+
31+
rule %r/(password|secret)(\s+[57]\s+)(\S+)/ do
32+
groups Keyword, Num, String::Double
33+
end
34+
35+
rule %r/(permit|deny)/, Operator::Word
36+
37+
rule %r/^(banner\s+)(motd\s+|login\s+)([#$%])/ do
38+
groups Keyword, Name::Function, Str::Delimiter
39+
push :cisco_ios_text
40+
end
41+
42+
rule %r/^(dial-peer\s+\S+\s+)(\S+)(.*?)$/ do
43+
groups Keyword, Name::Attribute, Keyword
44+
end
45+
46+
rule %r/^(vlan\s+)(\d+)$/ do
47+
groups Keyword, Name::Attribute
48+
end
49+
50+
# IPv4 Address/Prefix
51+
rule %r/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/\d{1,2})?/, Num
52+
53+
# NSAP
54+
rule %r/49\.\d{4}\.\d{4}\.\d{4}\.\d{4}\.\d{2}/, Num
55+
56+
# MAC Address
57+
rule %r/[a-f0-9]{4}\.[a-f0-9]{4}\.[a-f0-9]{4}/, Num::Hex
58+
59+
rule %r/^(\s*no\s+)(\S+)/ do
60+
groups Keyword::Constant, Keyword
61+
end
62+
63+
rule %r/^[^\n\r]\s*\S+/, Keyword
64+
65+
# Obfuscated Passwords
66+
rule %r/\*+/, Name::Entity
67+
68+
rule %r/(?<= )\d+(?= )/, Num
69+
70+
# Newline catcher, avoid errors on empty lines
71+
rule %r/\n+/m, Text
72+
73+
# This one goes last, a text catch-all
74+
rule %r/./, Text
75+
end
76+
77+
state :cisco_ios_text do
78+
rule %r/[^#$%]/, Text
79+
rule %r/[#$%]/, Str::Delimiter, :pop!
80+
end
81+
end
82+
end
83+
end

spec/lexers/cisco_ios_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*- #
2+
# frozen_string_literal: true
3+
4+
describe Rouge::Lexers::CiscoIos do
5+
let(:subject) { Rouge::Lexers::CiscoIos.new }
6+
7+
describe 'guessing' do
8+
include Support::Guessing
9+
10+
it 'guesses by filename' do
11+
assert_guess :filename => 'foo.cfg'
12+
end
13+
14+
it 'guesses by mimetype' do
15+
assert_guess :mimetype => 'text/x-cisco-conf'
16+
end
17+
end
18+
end

spec/visual/samples/cisco_ios

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
! This sample is given by Glitch Works, LLC from the following article:
2+
! http://www.glitchwrks.com/2016/10/06/cisco-access-points
3+
4+
interface FastEthernet0
5+
no ip address
6+
no ip route-cache
7+
duplex auto
8+
speed auto
9+
10+
interface FastEthernet0.20
11+
encapsulation dot1Q 20
12+
no ip route-cache
13+
bridge-group 1
14+
no bridge-group 1 source-learning
15+
bridge-group 1 spanning-disabled
16+
17+
interface BVI1
18+
ip address dhcp client-id FastEthernet0
19+
no ip route cache
20+
21+
! Supports shortened versions of config words, too
22+
conf ter
23+
inter gi0.10
24+
encap dot1q 10 native
25+
inter gi0
26+
bridge-group 2
27+
inter gi0.10
28+
no encap dot1q 10 native
29+
encap dot1q 10
30+
31+
banner login # Authenticate yourself! #
32+
33+
! Supports #, $ and % to delimit banners, and multiline
34+
banner motd $
35+
Attention!
36+
We will be having scheduled system maintenance on this device.
37+
$

0 commit comments

Comments
 (0)