2017-06-02 00:17:21 +10:00
|
|
|
package formatters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/alecthomas/chroma"
|
2017-06-06 15:59:48 +10:00
|
|
|
"github.com/alecthomas/chroma/styles"
|
2017-06-02 00:17:21 +10:00
|
|
|
)
|
|
|
|
|
2017-06-06 15:59:48 +10:00
|
|
|
// A Formatter for Chroma lexers.
|
2017-06-02 00:17:21 +10:00
|
|
|
type Formatter interface {
|
2017-06-06 15:59:48 +10:00
|
|
|
// 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) {
|
|
|
|
return func(t *chroma.Token) { io.WriteString(w, t.Value) }, nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Fallback formatter.
|
|
|
|
var Fallback = noop
|
|
|
|
|
|
|
|
// Registry of Formatters.
|
|
|
|
var Registry = map[string]Formatter{}
|
|
|
|
|
|
|
|
// Get formatter by name.
|
|
|
|
//
|
|
|
|
// If the given formatter is not found, the Fallback formatter will be returned.
|
|
|
|
func Get(name string) Formatter {
|
|
|
|
if f, ok := Registry[name]; ok {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
return Fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a named formatter.
|
|
|
|
func Register(name string, formatter Formatter) Formatter {
|
|
|
|
Registry[name] = formatter
|
|
|
|
return formatter
|
2017-06-02 00:17:21 +10:00
|
|
|
}
|