1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-01-28 03:29:41 +02:00

Add lexers.MatchMimeType(mimeType).

This commit is contained in:
Alec Thomas 2017-09-19 11:52:23 +10:00
parent d80c30ae4e
commit 00d5486e6b
2 changed files with 22 additions and 4 deletions

View File

@ -115,9 +115,15 @@ func listAll() {
filenames := []string{}
filenames = append(filenames, config.Filenames...)
filenames = append(filenames, config.AliasFilenames...)
fmt.Printf(" aliases: %s\n", strings.Join(config.Aliases, " "))
fmt.Printf(" filenames: %s\n", strings.Join(filenames, " "))
fmt.Printf(" mimetypes: %s\n", strings.Join(config.MimeTypes, " "))
if len(config.Aliases) > 0 {
fmt.Printf(" aliases: %s\n", strings.Join(config.Aliases, " "))
}
if len(filenames) > 0 {
fmt.Printf(" filenames: %s\n", strings.Join(filenames, " "))
}
if len(config.MimeTypes) > 0 {
fmt.Printf(" mimetypes: %s\n", strings.Join(config.MimeTypes, " "))
}
}
fmt.Println()
fmt.Printf("styles:")

View File

@ -40,7 +40,19 @@ func Get(name string) chroma.Lexer {
return nil
}
// Match returns all lexers matching filename.
// MatchMimeType attempts to find a lexer for the given MIME type.
func MatchMimeType(mimeType string) chroma.Lexer {
for _, l := range Registry.Lexers {
for _, lmt := range l.Config().MimeTypes {
if mimeType == lmt {
return l
}
}
}
return nil
}
// Match returns the first lexer matching filename.
func Match(filename string) chroma.Lexer {
filename = filepath.Base(filename)
for _, lexer := range Registry.Lexers {