1package lexers
2
3import (
4 "embed"
5 "io/fs"
6
7 "github.com/alecthomas/chroma/v2"
8)
9
10//go:embed embedded
11var embedded embed.FS
12
13// GlobalLexerRegistry is the global LexerRegistry of Lexers.
14var GlobalLexerRegistry = func() *chroma.LexerRegistry {
15 reg := chroma.NewLexerRegistry()
16 // index(reg)
17 paths, err := fs.Glob(embedded, "embedded/*.xml")
18 if err != nil {
19 panic(err)
20 }
21 for _, path := range paths {
22 reg.Register(chroma.MustNewXMLLexer(embedded, path))
23 }
24 return reg
25}()
26
27// Names of all lexers, optionally including aliases.
28func Names(withAliases bool) []string {
29 return GlobalLexerRegistry.Names(withAliases)
30}
31
32// Get a Lexer by name, alias or file extension.
33//
34// Note that this if there isn't an exact match on name or alias, this will
35// call Match(), so it is not efficient.
36func Get(name string) chroma.Lexer {
37 return GlobalLexerRegistry.Get(name)
38}
39
40// MatchMimeType attempts to find a lexer for the given MIME type.
41func MatchMimeType(mimeType string) chroma.Lexer {
42 return GlobalLexerRegistry.MatchMimeType(mimeType)
43}
44
45// Match returns the first lexer matching filename.
46//
47// Note that this iterates over all file patterns in all lexers, so it's not
48// particularly efficient.
49func Match(filename string) chroma.Lexer {
50 return GlobalLexerRegistry.Match(filename)
51}
52
53// Register a Lexer with the global registry.
54func Register(lexer chroma.Lexer) chroma.Lexer {
55 return GlobalLexerRegistry.Register(lexer)
56}
57
58// Analyse text content and return the "best" lexer..
59func Analyse(text string) chroma.Lexer {
60 return GlobalLexerRegistry.Analyse(text)
61}
62
63// PlaintextRules is used for the fallback lexer as well as the explicit
64// plaintext lexer.
65func PlaintextRules() chroma.Rules {
66 return chroma.Rules{
67 "root": []chroma.Rule{
68 {`.+`, chroma.Text, nil},
69 {`\n`, chroma.Text, nil},
70 },
71 }
72}
73
74// Fallback lexer if no other is found.
75var Fallback chroma.Lexer = chroma.MustNewLexer(&chroma.Config{
76 Name: "fallback",
77 Filenames: []string{"*"},
78 Priority: -1,
79}, PlaintextRules)