From 7b993a35b3fc5eb20140a6c6b8d68fc772a7c0cd Mon Sep 17 00:00:00 2001 From: Tan Le Date: Wed, 18 Sep 2024 11:05:57 +1000 Subject: [PATCH] Default .cfg highlight to INI lexer (#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. --- lib/rouge/guessers/disambiguation.rb | 6 ++++++ lib/rouge/lexers/ini.rb | 2 +- spec/lexers/cisco_ios_spec.rb | 4 +++- spec/lexers/ini_spec.rb | 1 + 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/rouge/guessers/disambiguation.rb b/lib/rouge/guessers/disambiguation.rb index e3031f7ca5..6f2cfd5d91 100644 --- a/lib/rouge/guessers/disambiguation.rb +++ b/lib/rouge/guessers/disambiguation.rb @@ -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?(':-') diff --git a/lib/rouge/lexers/ini.rb b/lib/rouge/lexers/ini.rb index b44014e7b9..ae5464ba5e 100644 --- a/lib/rouge/lexers/ini.rb +++ b/lib/rouge/lexers/ini.rb @@ -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\-.]+/ diff --git a/spec/lexers/cisco_ios_spec.rb b/spec/lexers/cisco_ios_spec.rb index fb8d836ed9..f8a1b713aa 100644 --- a/spec/lexers/cisco_ios_spec.rb +++ b/spec/lexers/cisco_ios_spec.rb @@ -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 diff --git a/spec/lexers/ini_spec.rb b/spec/lexers/ini_spec.rb index 3cefe49532..fa1f6c3f84 100644 --- a/spec/lexers/ini_spec.rb +++ b/spec/lexers/ini_spec.rb @@ -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