mirror of
https://gitea.com/gitea/tea.git
synced 2024-11-27 12:56:54 +01:00
89e93d90b3
Merge branch 'master' into use-glamour select Glamour Theme based on BackgroundColor Merge branch 'master' into use-glamour Merge branch 'master' into use-glamour update termev update go.mod label color colorate use glamour for issue content Vendor: Add glamour Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/181 Reviewed-by: techknowlogick <techknowlogick@gitea.io> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
45 lines
891 B
Go
45 lines
891 B
Go
// Package quick provides simple, no-configuration source code highlighting.
|
|
package quick
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/alecthomas/chroma"
|
|
"github.com/alecthomas/chroma/formatters"
|
|
"github.com/alecthomas/chroma/lexers"
|
|
"github.com/alecthomas/chroma/styles"
|
|
)
|
|
|
|
// Highlight some text.
|
|
//
|
|
// Lexer, formatter and style may be empty, in which case a best-effort is made.
|
|
func Highlight(w io.Writer, source, lexer, formatter, style string) error {
|
|
// Determine lexer.
|
|
l := lexers.Get(lexer)
|
|
if l == nil {
|
|
l = lexers.Analyse(source)
|
|
}
|
|
if l == nil {
|
|
l = lexers.Fallback
|
|
}
|
|
l = chroma.Coalesce(l)
|
|
|
|
// Determine formatter.
|
|
f := formatters.Get(formatter)
|
|
if f == nil {
|
|
f = formatters.Fallback
|
|
}
|
|
|
|
// Determine style.
|
|
s := styles.Get(style)
|
|
if s == nil {
|
|
s = styles.Fallback
|
|
}
|
|
|
|
it, err := l.Tokenise(nil, source)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return f.Format(w, s, it)
|
|
}
|