...
1
2 package quick
3
4 import (
5 "io"
6
7 "github.com/alecthomas/chroma"
8 "github.com/alecthomas/chroma/formatters"
9 "github.com/alecthomas/chroma/lexers"
10 "github.com/alecthomas/chroma/styles"
11 )
12
13
14
15
16 func Highlight(w io.Writer, source, lexer, formatter, style string) error {
17
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
28 f := formatters.Get(formatter)
29 if f == nil {
30 f = formatters.Fallback
31 }
32
33
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 }
45
View as plain text