1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-07-03 00:37:01 +02:00

Move style and formatter API into chroma package.

This commit is contained in:
Alec Thomas
2017-06-06 21:23:50 +10:00
parent ef4a53333b
commit d852022f8d
15 changed files with 261 additions and 253 deletions

View File

@ -4,35 +4,23 @@ import (
"io"
"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/styles"
)
// A Formatter for Chroma lexers.
type Formatter interface {
// Format returns a formatting function for tokens.
Format(w io.Writer, style *styles.Style) (func(*chroma.Token), error)
}
type FormatterFunc func(io.Writer, *styles.Style) (func(*chroma.Token), error)
func (f FormatterFunc) Format(w io.Writer, s *styles.Style) (func(*chroma.Token), error) {
return f(w, s)
}
var noop = Register("noop", FormatterFunc(func(w io.Writer, s *styles.Style) (func(*chroma.Token), error) {
// NoOp formatter.
var 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
}))
// Fallback formatter.
var Fallback = noop
var Fallback = NoOp
// Registry of Formatters.
var Registry = map[string]Formatter{}
var Registry = map[string]chroma.Formatter{}
// Get formatter by name.
//
// If the given formatter is not found, the Fallback formatter will be returned.
func Get(name string) Formatter {
func Get(name string) chroma.Formatter {
if f, ok := Registry[name]; ok {
return f
}
@ -40,7 +28,7 @@ func Get(name string) Formatter {
}
// Register a named formatter.
func Register(name string, formatter Formatter) Formatter {
func Register(name string, formatter chroma.Formatter) chroma.Formatter {
Registry[name] = formatter
return formatter
}