Class: Rouge::Guessers::GlobMapping
- Inherits:
-
Rouge::Guesser
- Object
- Rouge::Guesser
- Rouge::Guessers::GlobMapping
- Includes:
- Util
- Defined in:
- lib/rouge/guessers/glob_mapping.rb
Overview
This class allows for custom behavior with glob -> lexer name mappings
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#glob_map ⇒ Object
readonly
Returns the value of attribute glob_map.
Class Method Summary collapse
Instance Method Summary collapse
- #filter(lexers) ⇒ Object
-
#initialize(glob_map, filename) ⇒ GlobMapping
constructor
A new instance of GlobMapping.
Methods included from Util
Methods inherited from Rouge::Guesser
Constructor Details
#initialize(glob_map, filename) ⇒ GlobMapping
Returns a new instance of GlobMapping.
26 27 28 29 |
# File 'lib/rouge/guessers/glob_mapping.rb', line 26 def initialize(glob_map, filename) @glob_map = glob_map @filename = filename end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
25 26 27 |
# File 'lib/rouge/guessers/glob_mapping.rb', line 25 def filename @filename end |
#glob_map ⇒ Object (readonly)
Returns the value of attribute glob_map.
25 26 27 |
# File 'lib/rouge/guessers/glob_mapping.rb', line 25 def glob_map @glob_map end |
Class Method Details
.by_pairs(mapping, filename) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rouge/guessers/glob_mapping.rb', line 10 def self.by_pairs(mapping, filename) glob_map = {} mapping.each do |(glob, lexer_name)| lexer = Lexer.find(lexer_name) # ignore unknown lexers next unless lexer glob_map[lexer.name] ||= [] glob_map[lexer.name] << glob end new(glob_map, filename) end |
Instance Method Details
#filter(lexers) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rouge/guessers/glob_mapping.rb', line 31 def filter(lexers) basename = File.basename(filename) collect_best(lexers) do |lexer| (@glob_map[lexer.name] || []).map do |pattern| if test_glob(pattern, basename) # specificity is better the fewer wildcards there are -pattern.scan(/[*?\[]/).size end end.compact.min end end |