Skip to content

Commit

Permalink
Default .cfg highlight to INI lexer (rouge-ruby#2074)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tancnle authored Sep 18, 2024
1 parent 173ec77 commit 7b993a3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/rouge/guessers/disambiguation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def match?(filename)
end
end

disambiguate '*.cfg' do
next CiscoIos if matches?(/\A\s*(version|banner|interface)\b/)

INI
end

disambiguate '*.pl' do
next Perl if contains?('my $')
next Prolog if contains?(':-')
Expand Down
2 changes: 1 addition & 1 deletion lib/rouge/lexers/ini.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class INI < RegexLexer
tag 'ini'

# TODO add more here
filenames '*.ini', '*.INI', '*.gitconfig'
filenames '*.ini', '*.INI', '*.gitconfig', '*.cfg'
mimetypes 'text/x-ini'

identifier = /[\w\-.]+/
Expand Down
4 changes: 3 additions & 1 deletion spec/lexers/cisco_ios_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'foo.cfg'
# Disambiguate with INI
assert_guess :filename => 'foo.cfg', :source => "version 13.4"
assert_guess :filename => 'foo.cfg', :source => "interface FastEthernet0.20"
end

it 'guesses by mimetype' do
Expand Down
1 change: 1 addition & 0 deletions spec/lexers/ini_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
it 'guesses by filename' do
assert_guess :filename => 'foo.ini'
assert_guess :filename => '.gitconfig'
assert_guess :filename => 'setup.cfg', :source => "[metadata]\nname = my_package"
end

it 'guesses by mimetype' do
Expand Down

0 comments on commit 7b993a3

Please sign in to comment.