Module: Snibbets::Lexers

Defined in:
lib/snibbets/lexers.rb

Overview

Lexer definitions

Class Method Summary collapse

Class Method Details

.build_lexersObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/snibbets/lexers.rb', line 12

def build_lexers
  lex = []
  LEXERS_DB.split(/\n/).each do |line|
    key = line.match(/(?mi)^((, )?[^,]+?)+?(?=\[)/)[0]
    keys = key.split(/,/).map(&:strip)
    value = line.match(/\[(.*?)\]/)[1]
    values = value.split(/,/).map(&:strip)

    lex << {
      lexer: keys.shift,
      aliases: keys,
      extensions: values
    }
  end

  lex
end

.ext_to_lang(ext) ⇒ Object



39
40
41
42
43
44
# File 'lib/snibbets/lexers.rb', line 39

def ext_to_lang(ext)
  return nil if ext.nil?

  matches = lexers.select { |lex| lex[:extensions].map(&:downcase).include?(ext.downcase) }
  matches.map { |lex| lex[:lexer] }.first || ext
end

.lang_to_ext(lexer) ⇒ Object



46
47
48
49
50
51
# File 'lib/snibbets/lexers.rb', line 46

def lang_to_ext(lexer)
  return nil if lexer.nil?

  matches = lexers.select { |lex| lex[:lexer] == lexer || lex[:aliases].map(&:downcase).include?(lexer.downcase) }
  matches.map { |lex| lex[:extensions].first }.first || lexer
end

.lexersObject



8
9
10
# File 'lib/snibbets/lexers.rb', line 8

def lexers
  @lexers ||= build_lexers
end

.normalize_lexer(lexer) ⇒ Object



34
35
36
37
# File 'lib/snibbets/lexers.rb', line 34

def normalize_lexer(lexer)
  matches = LEXER_NORMALIZERS.select { |k, v| v.include? lexer }
  matches.count.positive? ? matches.keys[0] : lexer
end

.skylight_lexer?(lexer) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/snibbets/lexers.rb', line 30

def skylight_lexer?(lexer)
  SKYLIGHT_LEXERS_DB.split(/\n/).include?(lexer)
end

.syntax_from_extension(filename) ⇒ Object



53
54
55
56
57
58
# File 'lib/snibbets/lexers.rb', line 53

def syntax_from_extension(filename)
  exts = filename.split(/\./)[1..-2]
  return nil if exts.count.zero?

  ext_to_lang(exts[-1])
end