2015-12-16 22:13:12 -05:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2020-06-30 17:34:03 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2015-12-16 22:13:12 -05:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-01-31 11:19:02 -05:00
|
|
|
package highlight
|
2015-12-16 22:13:12 -05:00
|
|
|
|
|
|
|
import (
|
2020-06-30 17:34:03 -04:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2020-09-02 16:19:42 -04:00
|
|
|
gohtml "html"
|
2020-06-30 17:34:03 -04:00
|
|
|
"path/filepath"
|
2015-12-16 22:13:12 -05:00
|
|
|
"strings"
|
2020-06-30 17:34:03 -04:00
|
|
|
"sync"
|
2015-12-17 22:31:34 -05:00
|
|
|
|
2020-11-13 16:13:41 -05:00
|
|
|
"code.gitea.io/gitea/modules/analyze"
|
2020-06-30 17:34:03 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2016-11-10 11:24:48 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-05-13 05:31:23 -04:00
|
|
|
"github.com/alecthomas/chroma"
|
2020-06-30 17:34:03 -04:00
|
|
|
"github.com/alecthomas/chroma/formatters/html"
|
|
|
|
"github.com/alecthomas/chroma/lexers"
|
|
|
|
"github.com/alecthomas/chroma/styles"
|
2015-12-16 22:13:12 -05:00
|
|
|
)
|
|
|
|
|
2020-06-30 17:34:03 -04:00
|
|
|
// don't index files larger than this many bytes for performance purposes
|
|
|
|
const sizeLimit = 1000000
|
|
|
|
|
2015-12-16 22:13:12 -05:00
|
|
|
var (
|
2020-06-30 17:34:03 -04:00
|
|
|
// For custom user mapping
|
|
|
|
highlightMapping = map[string]string{}
|
|
|
|
|
|
|
|
once sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewContext loads custom highlight map from local config
|
|
|
|
func NewContext() {
|
|
|
|
once.Do(func() {
|
|
|
|
keys := setting.Cfg.Section("highlight.mapping").Keys()
|
|
|
|
for i := range keys {
|
|
|
|
highlightMapping[keys[i].Name()] = keys[i].Value()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Code returns a HTML version of code string with chroma syntax highlighting classes
|
|
|
|
func Code(fileName, code string) string {
|
|
|
|
NewContext()
|
2015-12-16 22:13:12 -05:00
|
|
|
|
2020-07-11 01:43:12 -04:00
|
|
|
// diff view newline will be passed as empty, change to literal \n so it can be copied
|
|
|
|
// preserve literal newline in blame view
|
|
|
|
if code == "" || code == "\n" {
|
2020-07-08 17:02:38 -04:00
|
|
|
return "\n"
|
|
|
|
}
|
2020-07-11 01:43:12 -04:00
|
|
|
|
2020-06-30 17:34:03 -04:00
|
|
|
if len(code) > sizeLimit {
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
formatter := html.New(html.WithClasses(true),
|
|
|
|
html.WithLineNumbers(false),
|
|
|
|
html.PreventSurroundingPre(true),
|
|
|
|
)
|
|
|
|
if formatter == nil {
|
|
|
|
log.Error("Couldn't create chroma formatter")
|
|
|
|
return code
|
2015-12-16 22:13:12 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:34:03 -04:00
|
|
|
htmlbuf := bytes.Buffer{}
|
|
|
|
htmlw := bufio.NewWriter(&htmlbuf)
|
|
|
|
|
2021-05-13 05:31:23 -04:00
|
|
|
var lexer chroma.Lexer
|
2020-06-30 17:34:03 -04:00
|
|
|
if val, ok := highlightMapping[filepath.Ext(fileName)]; ok {
|
2021-05-13 05:31:23 -04:00
|
|
|
//use mapped value to find lexer
|
|
|
|
lexer = lexers.Get(val)
|
2015-12-16 22:13:12 -05:00
|
|
|
}
|
2015-12-17 22:31:34 -05:00
|
|
|
|
2020-06-30 17:34:03 -04:00
|
|
|
if lexer == nil {
|
2021-05-13 05:31:23 -04:00
|
|
|
lexer = lexers.Match(fileName)
|
|
|
|
if lexer == nil {
|
|
|
|
lexer = lexers.Fallback
|
|
|
|
}
|
2017-06-09 20:39:16 -04:00
|
|
|
}
|
2015-12-16 22:13:12 -05:00
|
|
|
|
2020-07-11 01:43:12 -04:00
|
|
|
iterator, err := lexer.Tokenise(nil, string(code))
|
2020-06-30 17:34:03 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Can't tokenize code: %v", err)
|
|
|
|
return code
|
2015-12-17 22:31:34 -05:00
|
|
|
}
|
2020-06-30 17:34:03 -04:00
|
|
|
// style not used for live site but need to pass something
|
|
|
|
err = formatter.Format(htmlw, styles.GitHub, iterator)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Can't format code: %v", err)
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
|
|
|
|
htmlw.Flush()
|
2020-07-11 01:43:12 -04:00
|
|
|
// Chroma will add newlines for certain lexers in order to highlight them properly
|
|
|
|
// Once highlighted, strip them here so they don't cause copy/paste trouble in HTML output
|
|
|
|
return strings.TrimSuffix(htmlbuf.String(), "\n")
|
2015-12-17 22:31:34 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:34:03 -04:00
|
|
|
// File returns map with line lumbers and HTML version of code with chroma syntax highlighting classes
|
|
|
|
func File(numLines int, fileName string, code []byte) map[int]string {
|
|
|
|
NewContext()
|
|
|
|
|
|
|
|
if len(code) > sizeLimit {
|
|
|
|
return plainText(string(code), numLines)
|
|
|
|
}
|
|
|
|
formatter := html.New(html.WithClasses(true),
|
|
|
|
html.WithLineNumbers(false),
|
|
|
|
html.PreventSurroundingPre(true),
|
|
|
|
)
|
|
|
|
|
|
|
|
if formatter == nil {
|
|
|
|
log.Error("Couldn't create chroma formatter")
|
|
|
|
return plainText(string(code), numLines)
|
|
|
|
}
|
|
|
|
|
|
|
|
htmlbuf := bytes.Buffer{}
|
|
|
|
htmlw := bufio.NewWriter(&htmlbuf)
|
|
|
|
|
2021-05-13 05:31:23 -04:00
|
|
|
var lexer chroma.Lexer
|
2020-06-30 17:34:03 -04:00
|
|
|
if val, ok := highlightMapping[filepath.Ext(fileName)]; ok {
|
2021-05-13 05:31:23 -04:00
|
|
|
lexer = lexers.Get(val)
|
2020-06-30 17:34:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if lexer == nil {
|
2021-05-13 05:31:23 -04:00
|
|
|
language := analyze.GetCodeLanguage(fileName, code)
|
|
|
|
|
|
|
|
lexer = lexers.Get(language)
|
2020-06-30 17:34:03 -04:00
|
|
|
if lexer == nil {
|
2021-05-13 05:31:23 -04:00
|
|
|
lexer = lexers.Match(fileName)
|
|
|
|
if lexer == nil {
|
|
|
|
lexer = lexers.Fallback
|
|
|
|
}
|
2020-06-30 17:34:03 -04:00
|
|
|
}
|
2015-12-16 22:13:12 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:34:03 -04:00
|
|
|
iterator, err := lexer.Tokenise(nil, string(code))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Can't tokenize code: %v", err)
|
|
|
|
return plainText(string(code), numLines)
|
2015-12-16 22:13:12 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:34:03 -04:00
|
|
|
err = formatter.Format(htmlw, styles.GitHub, iterator)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Can't format code: %v", err)
|
|
|
|
return plainText(string(code), numLines)
|
2015-12-16 22:13:12 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:34:03 -04:00
|
|
|
htmlw.Flush()
|
|
|
|
m := make(map[int]string, numLines)
|
|
|
|
for k, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) {
|
|
|
|
line := k + 1
|
2020-07-08 17:02:38 -04:00
|
|
|
content := string(v)
|
|
|
|
//need to keep lines that are only \n so copy/paste works properly in browser
|
|
|
|
if content == "" {
|
|
|
|
content = "\n"
|
|
|
|
}
|
|
|
|
m[line] = content
|
2015-12-17 22:31:34 -05:00
|
|
|
}
|
2020-06-30 17:34:03 -04:00
|
|
|
return m
|
|
|
|
}
|
2015-12-17 22:31:34 -05:00
|
|
|
|
2020-06-30 17:34:03 -04:00
|
|
|
// return unhiglighted map
|
|
|
|
func plainText(code string, numLines int) map[int]string {
|
|
|
|
m := make(map[int]string, numLines)
|
|
|
|
for k, v := range strings.SplitN(string(code), "\n", numLines) {
|
|
|
|
line := k + 1
|
2020-07-08 17:02:38 -04:00
|
|
|
content := string(v)
|
|
|
|
//need to keep lines that are only \n so copy/paste works properly in browser
|
|
|
|
if content == "" {
|
|
|
|
content = "\n"
|
|
|
|
}
|
2020-09-02 16:19:42 -04:00
|
|
|
m[line] = gohtml.EscapeString(content)
|
2020-06-30 17:34:03 -04:00
|
|
|
}
|
|
|
|
return m
|
2015-12-16 22:13:12 -05:00
|
|
|
}
|