2017-06-02 00:17:21 +10:00
|
|
|
package formatters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/alecthomas/chroma"
|
|
|
|
)
|
|
|
|
|
2017-06-06 21:23:50 +10:00
|
|
|
// NoOp formatter.
|
|
|
|
var NoOp = Register("noop", chroma.FormatterFunc(func(w io.Writer, s *chroma.Style) (func(*chroma.Token), error) {
|
2017-06-06 15:59:48 +10:00
|
|
|
return func(t *chroma.Token) { io.WriteString(w, t.Value) }, nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// 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
|
|
|
}
|