...
1 package lexers
2
3 import (
4 "embed"
5 "io/fs"
6
7 "github.com/alecthomas/chroma/v2"
8 )
9
10
11 var embedded embed.FS
12
13
14 var GlobalLexerRegistry = func() *chroma.LexerRegistry {
15 reg := chroma.NewLexerRegistry()
16
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
28 func Names(withAliases bool) []string {
29 return GlobalLexerRegistry.Names(withAliases)
30 }
31
32
33 func Get(name string) chroma.Lexer {
34 return GlobalLexerRegistry.Get(name)
35 }
36
37
38 func MatchMimeType(mimeType string) chroma.Lexer {
39 return GlobalLexerRegistry.MatchMimeType(mimeType)
40 }
41
42
43 func Match(filename string) chroma.Lexer {
44 return GlobalLexerRegistry.Match(filename)
45 }
46
47
48 func Register(lexer chroma.Lexer) chroma.Lexer {
49 return GlobalLexerRegistry.Register(lexer)
50 }
51
52
53 func Analyse(text string) chroma.Lexer {
54 return GlobalLexerRegistry.Analyse(text)
55 }
56
57
58
59 func PlaintextRules() chroma.Rules {
60 return chroma.Rules{
61 "root": []chroma.Rule{
62 {`.+`, chroma.Text, nil},
63 {`\n`, chroma.Text, nil},
64 },
65 }
66 }
67
68
69 var Fallback chroma.Lexer = chroma.MustNewLexer(&chroma.Config{
70 Name: "fallback",
71 Filenames: []string{"*"},
72 Priority: -1,
73 }, PlaintextRules)
74
View as plain text