From 082241eb0f042707f6464c9f7b6a03e942e6409f Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Sat, 24 Aug 2024 22:20:38 -0300 Subject: [PATCH] Load lexer by mimetype --- TODO.md | 4 ++-- src/lexer.cr | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/TODO.md b/TODO.md index 91d427d..6bcc1e3 100644 --- a/TODO.md +++ b/TODO.md @@ -8,8 +8,8 @@ * ✅ Implement lexer loader that respects aliases * ✅ Implement lexer loader by file extension * ✅ Add --line-numbers to terminal formatter -* Implement lexer loader by mime type +* ✅ Implement lexer loader by mime type * ✅ Implement Delegating lexers * ✅ Add RstLexer * Add Mako template lexer -* Implement heuristic lexer detection +* ✅ Implement heuristic lexer detection diff --git a/src/lexer.cr b/src/lexer.cr index 39652ba..aa8dc73 100644 --- a/src/lexer.cr +++ b/src/lexer.cr @@ -9,13 +9,21 @@ module Tartrazine # Get the lexer object for a language name # FIXME: support mimetypes - def self.lexer(name : String? = nil, filename : String? = nil) : BaseLexer + def self.lexer(name : String? = nil, filename : String? = nil, mimetype : String? = nil) : BaseLexer return lexer_by_name(name) if name && name != "autodetect" return lexer_by_filename(filename) if filename + return lexer_by_mimetype(mimetype) if mimetype Lexer.from_xml(LexerFiles.get("/#{LEXERS_BY_NAME["plaintext"]}.xml").gets_to_end) end + private def self.lexer_by_mimetype(mimetype : String) : BaseLexer + lexer_file_name = LEXERS_BY_MIMETYPE.fetch(mimetype, nil) + raise Exception.new("Unknown mimetype: #{mimetype}") if lexer_file_name.nil? + + Lexer.from_xml(LexerFiles.get("/#{lexer_file_name}.xml").gets_to_end) + end + private def self.lexer_by_name(name : String) : BaseLexer lexer_file_name = LEXERS_BY_NAME.fetch(name.downcase, nil) return create_delegating_lexer(name) if lexer_file_name.nil? && name.includes? "+"