1// Package quick provides simple, no-configuration source code highlighting.
 2package quick
 3
 4import (
 5	"io"
 6
 7	"github.com/alecthomas/chroma/v2"
 8	"github.com/alecthomas/chroma/v2/formatters"
 9	"github.com/alecthomas/chroma/v2/lexers"
10	"github.com/alecthomas/chroma/v2/styles"
11)
12
13// Highlight some text.
14//
15// Lexer, formatter and style may be empty, in which case a best-effort is made.
16func Highlight(w io.Writer, source, lexer, formatter, style string) error {
17	// Determine lexer.
18	l := lexers.Get(lexer)
19	if l == nil {
20		l = lexers.Analyse(source)
21	}
22	if l == nil {
23		l = lexers.Fallback
24	}
25	l = chroma.Coalesce(l)
26
27	// Determine formatter.
28	f := formatters.Get(formatter)
29	if f == nil {
30		f = formatters.Fallback
31	}
32
33	// Determine style.
34	s := styles.Get(style)
35	if s == nil {
36		s = styles.Fallback
37	}
38
39	it, err := l.Tokenise(nil, source)
40	if err != nil {
41		return err
42	}
43	return f.Format(w, s, it)
44}