...
1 package formatters
2
3 import (
4 "io"
5 "sort"
6
7 "github.com/alecthomas/chroma/v2"
8 "github.com/alecthomas/chroma/v2/formatters/html"
9 "github.com/alecthomas/chroma/v2/formatters/svg"
10 )
11
12 var (
13
14 NoOp = Register("noop", chroma.FormatterFunc(func(w io.Writer, s *chroma.Style, iterator chroma.Iterator) error {
15 for t := iterator(); t != chroma.EOF; t = iterator() {
16 if _, err := io.WriteString(w, t.Value); err != nil {
17 return err
18 }
19 }
20 return nil
21 }))
22
23 htmlFull = Register("html", html.New(html.Standalone(true), html.WithClasses(true)))
24 SVG = Register("svg", svg.New(svg.EmbedFont("Liberation Mono", svg.FontLiberationMono, svg.WOFF)))
25 )
26
27
28 var Fallback = NoOp
29
30
31 var Registry = map[string]chroma.Formatter{}
32
33
34 func Names() []string {
35 out := []string{}
36 for name := range Registry {
37 out = append(out, name)
38 }
39 sort.Strings(out)
40 return out
41 }
42
43
44
45
46 func Get(name string) chroma.Formatter {
47 if f, ok := Registry[name]; ok {
48 return f
49 }
50 return Fallback
51 }
52
53
54 func Register(name string, formatter chroma.Formatter) chroma.Formatter {
55 Registry[name] = formatter
56 return formatter
57 }
58
View as plain text