2017-06-02 00:17:21 +10:00
|
|
|
package formatters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/alecthomas/chroma"
|
2017-07-19 23:51:16 -07:00
|
|
|
"github.com/alecthomas/chroma/formatters/html"
|
2017-06-02 00:17:21 +10:00
|
|
|
)
|
|
|
|
|
2017-07-19 23:51:16 -07:00
|
|
|
var (
|
2017-09-18 13:15:07 +10:00
|
|
|
// NoOp formatter.
|
2017-07-19 23:51:16 -07:00
|
|
|
NoOp = Register("noop", chroma.FormatterFunc(func(w io.Writer, s *chroma.Style) (func(*chroma.Token), error) {
|
|
|
|
return func(t *chroma.Token) { io.WriteString(w, t.Value) }, nil
|
|
|
|
}))
|
2017-09-18 13:15:07 +10:00
|
|
|
// Default HTML formatter outputs self-contained HTML.
|
2017-07-19 23:51:16 -07:00
|
|
|
htmlFull = Register("html", html.New(html.Standalone(), html.WithClasses()))
|
|
|
|
)
|
2017-06-06 15:59:48 +10:00
|
|
|
|
|
|
|
// Fallback formatter.
|
2017-06-06 21:23:50 +10:00
|
|
|
var Fallback = NoOp
|
2017-06-06 15:59:48 +10:00
|
|
|
|
|
|
|
// Registry of Formatters.
|
2017-06-06 21:23:50 +10:00
|
|
|
var Registry = map[string]chroma.Formatter{}
|
2017-06-06 15:59:48 +10:00
|
|
|
|
2017-07-19 23:51:16 -07:00
|
|
|
// Names of registered formatters.
|
|
|
|
func Names() []string {
|
|
|
|
out := []string{}
|
|
|
|
for name := range Registry {
|
|
|
|
out = append(out, name)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2017-06-06 15:59:48 +10:00
|
|
|
// Get formatter by name.
|
|
|
|
//
|
|
|
|
// If the given formatter is not found, the Fallback formatter will be returned.
|
2017-06-06 21:23:50 +10:00
|
|
|
func Get(name string) chroma.Formatter {
|
2017-06-06 15:59:48 +10:00
|
|
|
if f, ok := Registry[name]; ok {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
return Fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a named formatter.
|
2017-06-06 21:23:50 +10:00
|
|
|
func Register(name string, formatter chroma.Formatter) chroma.Formatter {
|
2017-06-06 15:59:48 +10:00
|
|
|
Registry[name] = formatter
|
|
|
|
return formatter
|
2017-06-02 00:17:21 +10:00
|
|
|
}
|