/
api.go
65 lines (57 loc) · 1.15 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package styles
import (
"embed"
"io/fs"
"sort"
"github.com/alecthomas/chroma/v2"
)
//go:embed *.xml
var embedded embed.FS
// Registry of Styles.
var Registry = func() map[string]*chroma.Style {
registry := map[string]*chroma.Style{}
// Register all embedded styles.
files, err := fs.ReadDir(embedded, ".")
if err != nil {
panic(err)
}
for _, file := range files {
if file.IsDir() {
continue
}
r, err := embedded.Open(file.Name())
if err != nil {
panic(err)
}
style, err := chroma.NewXMLStyle(r)
if err != nil {
panic(err)
}
registry[style.Name] = style
_ = r.Close()
}
return registry
}()
// Fallback style. Reassign to change the default fallback style.
var Fallback = Registry["swapoff"]
// Register a chroma.Style.
func Register(style *chroma.Style) *chroma.Style {
Registry[style.Name] = style
return style
}
// Names of all available styles.
func Names() []string {
out := []string{}
for name := range Registry {
out = append(out, name)
}
sort.Strings(out)
return out
}
// Get named style, or Fallback.
func Get(name string) *chroma.Style {
if style, ok := Registry[name]; ok {
return style
}
return Fallback
}