2019-11-06 14:10:47 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package highlight
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-02-17 08:59:26 -05:00
|
|
|
gohtml "html"
|
2019-11-06 14:10:47 -05:00
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/alecthomas/chroma"
|
|
|
|
"github.com/alecthomas/chroma/formatters/html"
|
|
|
|
"github.com/alecthomas/chroma/lexers"
|
|
|
|
"github.com/alecthomas/chroma/styles"
|
2019-11-24 07:56:37 -05:00
|
|
|
hl "github.com/yuin/goldmark-highlighting"
|
2019-11-06 14:10:47 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func New(cfg Config) Highlighter {
|
|
|
|
return Highlighter{
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Highlighter struct {
|
|
|
|
cfg Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h Highlighter) Highlight(code, lang, optsStr string) (string, error) {
|
|
|
|
cfg := h.cfg
|
|
|
|
if optsStr != "" {
|
|
|
|
if err := applyOptionsFromString(optsStr, &cfg); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return highlight(code, lang, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func highlight(code, lang string, cfg Config) (string, error) {
|
|
|
|
w := &strings.Builder{}
|
|
|
|
var lexer chroma.Lexer
|
|
|
|
if lang != "" {
|
|
|
|
lexer = lexers.Get(lang)
|
|
|
|
}
|
|
|
|
|
2019-12-02 02:31:23 -05:00
|
|
|
if lexer == nil && cfg.GuessSyntax {
|
|
|
|
lexer = lexers.Analyse(code)
|
|
|
|
if lexer == nil {
|
|
|
|
lexer = lexers.Fallback
|
|
|
|
}
|
|
|
|
lang = strings.ToLower(lexer.Config().Name)
|
|
|
|
}
|
|
|
|
|
2019-11-06 14:10:47 -05:00
|
|
|
if lexer == nil {
|
|
|
|
wrapper := getPreWrapper(lang)
|
|
|
|
fmt.Fprint(w, wrapper.Start(true, ""))
|
2020-02-17 08:59:26 -05:00
|
|
|
fmt.Fprint(w, gohtml.EscapeString(code))
|
2019-11-06 14:10:47 -05:00
|
|
|
fmt.Fprint(w, wrapper.End(true))
|
|
|
|
return w.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
style := styles.Get(cfg.Style)
|
|
|
|
if style == nil {
|
|
|
|
style = styles.Fallback
|
|
|
|
}
|
2020-02-17 08:59:26 -05:00
|
|
|
lexer = chroma.Coalesce(lexer)
|
2019-11-06 14:10:47 -05:00
|
|
|
|
|
|
|
iterator, err := lexer.Tokenise(nil, code)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
options := cfg.ToHTMLOptions()
|
|
|
|
options = append(options, getHtmlPreWrapper(lang))
|
|
|
|
|
|
|
|
formatter := html.New(options...)
|
|
|
|
|
2019-11-23 09:45:04 -05:00
|
|
|
fmt.Fprint(w, `<div class="highlight">`)
|
2019-11-06 14:10:47 -05:00
|
|
|
if err := formatter.Format(w, style, iterator); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-11-23 09:45:04 -05:00
|
|
|
fmt.Fprint(w, `</div>`)
|
2019-11-06 14:10:47 -05:00
|
|
|
|
|
|
|
return w.String(), nil
|
|
|
|
}
|
2019-11-23 09:45:04 -05:00
|
|
|
|
2019-11-06 14:10:47 -05:00
|
|
|
func GetCodeBlockOptions() func(ctx hl.CodeBlockContext) []html.Option {
|
|
|
|
return func(ctx hl.CodeBlockContext) []html.Option {
|
|
|
|
var language string
|
|
|
|
if l, ok := ctx.Language(); ok {
|
|
|
|
language = string(l)
|
|
|
|
}
|
|
|
|
return []html.Option{
|
|
|
|
getHtmlPreWrapper(language),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPreWrapper(language string) preWrapper {
|
|
|
|
return preWrapper{language: language}
|
|
|
|
}
|
2019-11-23 09:45:04 -05:00
|
|
|
|
2019-11-06 14:10:47 -05:00
|
|
|
func getHtmlPreWrapper(language string) html.Option {
|
|
|
|
return html.WithPreWrapper(getPreWrapper(language))
|
|
|
|
}
|
|
|
|
|
|
|
|
type preWrapper struct {
|
|
|
|
language string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p preWrapper) Start(code bool, styleAttr string) string {
|
|
|
|
w := &strings.Builder{}
|
|
|
|
fmt.Fprintf(w, "<pre%s>", styleAttr)
|
|
|
|
var language string
|
|
|
|
if code {
|
|
|
|
language = p.language
|
|
|
|
}
|
|
|
|
WriteCodeTag(w, language)
|
|
|
|
return w.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteCodeTag(w io.Writer, language string) {
|
|
|
|
fmt.Fprint(w, "<code")
|
|
|
|
if language != "" {
|
2019-11-23 09:45:04 -05:00
|
|
|
fmt.Fprint(w, ` class="language-`+language+`"`)
|
|
|
|
fmt.Fprint(w, ` data-lang="`+language+`"`)
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
fmt.Fprint(w, ">")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p preWrapper) End(code bool) string {
|
|
|
|
return "</code></pre>"
|
|
|
|
}
|