mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-12-05 00:00:34 +00:00
Load lexer by mimetype
This commit is contained in:
parent
df88047ca8
commit
082241eb0f
4
TODO.md
4
TODO.md
@ -8,8 +8,8 @@
|
|||||||
* ✅ Implement lexer loader that respects aliases
|
* ✅ Implement lexer loader that respects aliases
|
||||||
* ✅ Implement lexer loader by file extension
|
* ✅ Implement lexer loader by file extension
|
||||||
* ✅ Add --line-numbers to terminal formatter
|
* ✅ Add --line-numbers to terminal formatter
|
||||||
* Implement lexer loader by mime type
|
* ✅ Implement lexer loader by mime type
|
||||||
* ✅ Implement Delegating lexers
|
* ✅ Implement Delegating lexers
|
||||||
* ✅ Add RstLexer
|
* ✅ Add RstLexer
|
||||||
* Add Mako template lexer
|
* Add Mako template lexer
|
||||||
* Implement heuristic lexer detection
|
* ✅ Implement heuristic lexer detection
|
||||||
|
10
src/lexer.cr
10
src/lexer.cr
@ -9,13 +9,21 @@ module Tartrazine
|
|||||||
|
|
||||||
# Get the lexer object for a language name
|
# Get the lexer object for a language name
|
||||||
# FIXME: support mimetypes
|
# 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_name(name) if name && name != "autodetect"
|
||||||
return lexer_by_filename(filename) if filename
|
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)
|
Lexer.from_xml(LexerFiles.get("/#{LEXERS_BY_NAME["plaintext"]}.xml").gets_to_end)
|
||||||
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
|
private def self.lexer_by_name(name : String) : BaseLexer
|
||||||
lexer_file_name = LEXERS_BY_NAME.fetch(name.downcase, nil)
|
lexer_file_name = LEXERS_BY_NAME.fetch(name.downcase, nil)
|
||||||
return create_delegating_lexer(name) if lexer_file_name.nil? && name.includes? "+"
|
return create_delegating_lexer(name) if lexer_file_name.nil? && name.includes? "+"
|
||||||
|
Loading…
Reference in New Issue
Block a user