Skip to content

Commit 7b993a3

Browse files
authored
Default .cfg highlight to INI lexer (rouge-ruby#2074)
Cisco IOS shares the same extension but we want to limit it given the relevant content. For other cases such as `setup.cfg`, we would prefer INI syntax.
1 parent 173ec77 commit 7b993a3

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

lib/rouge/guessers/disambiguation.rb

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ def match?(filename)
6666
end
6767
end
6868

69+
disambiguate '*.cfg' do
70+
next CiscoIos if matches?(/\A\s*(version|banner|interface)\b/)
71+
72+
INI
73+
end
74+
6975
disambiguate '*.pl' do
7076
next Perl if contains?('my $')
7177
next Prolog if contains?(':-')

lib/rouge/lexers/ini.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class INI < RegexLexer
99
tag 'ini'
1010

1111
# TODO add more here
12-
filenames '*.ini', '*.INI', '*.gitconfig'
12+
filenames '*.ini', '*.INI', '*.gitconfig', '*.cfg'
1313
mimetypes 'text/x-ini'
1414

1515
identifier = /[\w\-.]+/

spec/lexers/cisco_ios_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
include Support::Guessing
99

1010
it 'guesses by filename' do
11-
assert_guess :filename => 'foo.cfg'
11+
# Disambiguate with INI
12+
assert_guess :filename => 'foo.cfg', :source => "version 13.4"
13+
assert_guess :filename => 'foo.cfg', :source => "interface FastEthernet0.20"
1214
end
1315

1416
it 'guesses by mimetype' do

spec/lexers/ini_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
it 'guesses by filename' do
1111
assert_guess :filename => 'foo.ini'
1212
assert_guess :filename => '.gitconfig'
13+
assert_guess :filename => 'setup.cfg', :source => "[metadata]\nname = my_package"
1314
end
1415

1516
it 'guesses by mimetype' do

0 commit comments

Comments
 (0)