66
|
1 package lexers
|
|
2
|
|
3 import (
|
|
4 "embed"
|
|
5 "io/fs"
|
|
6
|
|
7 "github.com/alecthomas/chroma/v2"
|
|
8 )
|
|
9
|
|
10 //go:embed embedded
|
|
11 var embedded embed.FS
|
|
12
|
|
13 // GlobalLexerRegistry is the global LexerRegistry of Lexers.
|
|
14 var 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.
|
|
28 func Names(withAliases bool) []string {
|
|
29 return GlobalLexerRegistry.Names(withAliases)
|
|
30 }
|
|
31
|
|
32 // Get a Lexer by name, alias or file extension.
|
|
33 func Get(name string) chroma.Lexer {
|
|
34 return GlobalLexerRegistry.Get(name)
|
|
35 }
|
|
36
|
|
37 // MatchMimeType attempts to find a lexer for the given MIME type.
|
|
38 func MatchMimeType(mimeType string) chroma.Lexer {
|
|
39 return GlobalLexerRegistry.MatchMimeType(mimeType)
|
|
40 }
|
|
41
|
|
42 // Match returns the first lexer matching filename.
|
|
43 func Match(filename string) chroma.Lexer {
|
|
44 return GlobalLexerRegistry.Match(filename)
|
|
45 }
|
|
46
|
|
47 // Register a Lexer with the global registry.
|
|
48 func Register(lexer chroma.Lexer) chroma.Lexer {
|
|
49 return GlobalLexerRegistry.Register(lexer)
|
|
50 }
|
|
51
|
|
52 // Analyse text content and return the "best" lexer..
|
|
53 func Analyse(text string) chroma.Lexer {
|
|
54 return GlobalLexerRegistry.Analyse(text)
|
|
55 }
|
|
56
|
|
57 // PlaintextRules is used for the fallback lexer as well as the explicit
|
|
58 // plaintext lexer.
|
|
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 // Fallback lexer if no other is found.
|
|
69 var Fallback chroma.Lexer = chroma.MustNewLexer(&chroma.Config{
|
|
70 Name: "fallback",
|
|
71 Filenames: []string{"*"},
|
|
72 Priority: -1,
|
|
73 }, PlaintextRules)
|