-
Notifications
You must be signed in to change notification settings - Fork 750
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #763 from jneen/refactor.disambiguators
Refactor: Use manual disambiguators for shared filenames
- Loading branch information
Showing
72 changed files
with
223 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
module Rouge | ||
module Guessers | ||
class Disambiguation < Guesser | ||
include Util | ||
include Lexers | ||
|
||
def initialize(filename, source) | ||
@filename = File.basename(filename) | ||
@source = source | ||
end | ||
|
||
def filter(lexers) | ||
return lexers if lexers.size == 1 | ||
return lexers if lexers.size == Lexer.all.size | ||
|
||
@analyzer = TextAnalyzer.new(get_source(@source)) | ||
|
||
self.class.disambiguators.each do |disambiguator| | ||
next unless disambiguator.match?(@filename) | ||
|
||
filtered = disambiguator.decide!(self) | ||
return filtered if filtered | ||
end | ||
|
||
return lexers | ||
end | ||
|
||
def contains?(text) | ||
return @analyzer.include?(text) | ||
end | ||
|
||
def matches?(re) | ||
return !!(@analyzer =~ re) | ||
end | ||
|
||
@disambiguators = [] | ||
def self.disambiguate(*patterns, &decider) | ||
@disambiguators << Disambiguator.new(patterns, &decider) | ||
end | ||
|
||
def self.disambiguators | ||
@disambiguators | ||
end | ||
|
||
class Disambiguator | ||
include Util | ||
|
||
def initialize(patterns, &decider) | ||
@patterns = patterns | ||
@decider = decider | ||
end | ||
|
||
def decide!(guesser) | ||
out = guesser.instance_eval(&@decider) | ||
case out | ||
when Array then out | ||
when nil then nil | ||
else [out] | ||
end | ||
end | ||
|
||
def match?(filename) | ||
@patterns.any? { |p| test_glob(p, filename) } | ||
end | ||
end | ||
|
||
disambiguate '*.pl' do | ||
next Perl if contains?('my $') | ||
next Prolog if contains?(':-') | ||
next Prolog if matches?(/\A\w+(\(\w+\,\s*\w+\))*\./) | ||
end | ||
|
||
disambiguate '*.h' do | ||
next ObjectiveC if matches?(/@(end|implementation|protocol|property)\b/) | ||
next ObjectiveC if contains?('@"') | ||
|
||
C | ||
end | ||
|
||
disambiguate '*.m' do | ||
next ObjectiveC if matches?(/@(end|implementation|protocol|property)\b/) | ||
next ObjectiveC if contains?('@"') | ||
|
||
next Matlab if matches?(/^\s*?%/) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Rouge | ||
module Guessers | ||
module Util | ||
def test_glob(pattern, path) | ||
File.fnmatch?(pattern, path, File::FNM_DOTMATCH | File::FNM_CASEFOLD) | ||
end | ||
|
||
def get_source(source) | ||
case source | ||
when String | ||
source | ||
when ->(s){ s.respond_to? :read } | ||
source.read | ||
else | ||
raise 'invalid source' | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.