mirror of
https://github.com/alecthomas/chroma.git
synced 2026-05-22 10:15:46 +02:00
Generalise and support 8, 256 and 16m colour terminals.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package lexers
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"github.com/danwakefield/fnmatch"
|
||||
|
||||
"github.com/alecthomas/chroma"
|
||||
)
|
||||
|
||||
type prioritisedLexers []chroma.Lexer
|
||||
|
||||
func (p prioritisedLexers) Len() int { return len(p) }
|
||||
func (p prioritisedLexers) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
func (p prioritisedLexers) Less(i, j int) bool { return p[i].Config().Priority < p[j].Config().Priority }
|
||||
|
||||
// Registry of Lexers.
|
||||
var Registry = struct {
|
||||
Lexers []chroma.Lexer
|
||||
byName map[string]chroma.Lexer
|
||||
}{
|
||||
byName: map[string]chroma.Lexer{},
|
||||
}
|
||||
|
||||
// Names of all lexers, optionally including aliases.
|
||||
func Names(withAliases bool) []string {
|
||||
out := []string{}
|
||||
for _, lexer := range Registry.Lexers {
|
||||
config := lexer.Config()
|
||||
out = append(out, config.Name)
|
||||
if withAliases {
|
||||
out = append(out, config.Aliases...)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Get a Lexer by name.
|
||||
func Get(name string) chroma.Lexer {
|
||||
lexer, ok := Registry.byName[name]
|
||||
if ok {
|
||||
return lexer
|
||||
}
|
||||
return Fallback
|
||||
}
|
||||
|
||||
// Match returns all lexers matching filename.
|
||||
func Match(filename string) []chroma.Lexer {
|
||||
filename = filepath.Base(filename)
|
||||
lexers := prioritisedLexers{}
|
||||
for _, lexer := range Registry.Lexers {
|
||||
config := lexer.Config()
|
||||
for _, glob := range config.Filenames {
|
||||
if fnmatch.Match(glob, filename, 0) {
|
||||
lexers = append(lexers, lexer)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Sort(lexers)
|
||||
return lexers
|
||||
}
|
||||
|
||||
// Register a Lexer with the global registry.
|
||||
func Register(lexer chroma.Lexer, err error) chroma.Lexer {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
config := lexer.Config()
|
||||
Registry.byName[config.Name] = lexer
|
||||
for _, alias := range config.Aliases {
|
||||
Registry.byName[alias] = lexer
|
||||
}
|
||||
Registry.Lexers = append(Registry.Lexers, lexer)
|
||||
return lexer
|
||||
}
|
||||
Reference in New Issue
Block a user