Sha256: 9a0952187e0a52048c0e2fed8d8a7ae84f1609e411d79a1afc736553affe9a15

Contents?: true

Size: 1.17 KB

Versions: 7

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true
require_relative 'lexers_db'

module Snibbets
  # Lexer definitions
  module Lexers
    class << self
      def lexers
        @lexers ||= build_lexers
      end

      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

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

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

      def syntax_from_extension(filename)
        exts = filename.split(/\./)[1..-2]
        ext_to_lang(exts[-1])
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
snibbets-2.0.24 lib/snibbets/lexers.rb
snibbets-2.0.23 lib/snibbets/lexers.rb
snibbets-2.0.22 lib/snibbets/lexers.rb
snibbets-2.0.21 lib/snibbets/lexers.rb
snibbets-2.0.20 lib/snibbets/lexers.rb
snibbets-2.0.19 lib/snibbets/lexers.rb
snibbets-2.0.18 lib/snibbets/lexers.rb