|
|
@ -10,6 +10,7 @@ import ( |
|
|
|
"bytes" |
|
|
|
"bytes" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
gohtml "html" |
|
|
|
gohtml "html" |
|
|
|
|
|
|
|
"io" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
"sync" |
|
|
|
"sync" |
|
|
@ -26,7 +27,7 @@ import ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// don't index files larger than this many bytes for performance purposes
|
|
|
|
// don't index files larger than this many bytes for performance purposes
|
|
|
|
const sizeLimit = 1000000 |
|
|
|
const sizeLimit = 1024 * 1024 |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
|
// For custom user mapping
|
|
|
|
// For custom user mapping
|
|
|
@ -46,7 +47,6 @@ func NewContext() { |
|
|
|
highlightMapping[keys[i].Name()] = keys[i].Value() |
|
|
|
highlightMapping[keys[i].Name()] = keys[i].Value() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// The size 512 is simply a conservative rule of thumb
|
|
|
|
// The size 512 is simply a conservative rule of thumb
|
|
|
|
c, err := lru.New2Q(512) |
|
|
|
c, err := lru.New2Q(512) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -60,7 +60,7 @@ func NewContext() { |
|
|
|
func Code(fileName, language, code string) string { |
|
|
|
func Code(fileName, language, code string) string { |
|
|
|
NewContext() |
|
|
|
NewContext() |
|
|
|
|
|
|
|
|
|
|
|
// diff view newline will be passed as empty, change to literal \n so it can be copied
|
|
|
|
// diff view newline will be passed as empty, change to literal '\n' so it can be copied
|
|
|
|
// preserve literal newline in blame view
|
|
|
|
// preserve literal newline in blame view
|
|
|
|
if code == "" || code == "\n" { |
|
|
|
if code == "" || code == "\n" { |
|
|
|
return "\n" |
|
|
|
return "\n" |
|
|
@ -128,36 +128,32 @@ func CodeFromLexer(lexer chroma.Lexer, code string) string { |
|
|
|
return code |
|
|
|
return code |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
htmlw.Flush() |
|
|
|
_ = htmlw.Flush() |
|
|
|
// Chroma will add newlines for certain lexers in order to highlight them properly
|
|
|
|
// 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
|
|
|
|
// Once highlighted, strip them here, so they don't cause copy/paste trouble in HTML output
|
|
|
|
return strings.TrimSuffix(htmlbuf.String(), "\n") |
|
|
|
return strings.TrimSuffix(htmlbuf.String(), "\n") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// File returns a slice of chroma syntax highlighted lines of code
|
|
|
|
// File returns a slice of chroma syntax highlighted HTML lines of code
|
|
|
|
func File(numLines int, fileName, language string, code []byte) []string { |
|
|
|
func File(fileName, language string, code []byte) ([]string, error) { |
|
|
|
NewContext() |
|
|
|
NewContext() |
|
|
|
|
|
|
|
|
|
|
|
if len(code) > sizeLimit { |
|
|
|
if len(code) > sizeLimit { |
|
|
|
return plainText(string(code), numLines) |
|
|
|
return PlainText(code), nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
formatter := html.New(html.WithClasses(true), |
|
|
|
formatter := html.New(html.WithClasses(true), |
|
|
|
html.WithLineNumbers(false), |
|
|
|
html.WithLineNumbers(false), |
|
|
|
html.PreventSurroundingPre(true), |
|
|
|
html.PreventSurroundingPre(true), |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
if formatter == nil { |
|
|
|
htmlBuf := bytes.Buffer{} |
|
|
|
log.Error("Couldn't create chroma formatter") |
|
|
|
htmlWriter := bufio.NewWriter(&htmlBuf) |
|
|
|
return plainText(string(code), numLines) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
htmlbuf := bytes.Buffer{} |
|
|
|
|
|
|
|
htmlw := bufio.NewWriter(&htmlbuf) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var lexer chroma.Lexer |
|
|
|
var lexer chroma.Lexer |
|
|
|
|
|
|
|
|
|
|
|
// provided language overrides everything
|
|
|
|
// provided language overrides everything
|
|
|
|
if len(language) > 0 { |
|
|
|
if language != "" { |
|
|
|
lexer = lexers.Get(language) |
|
|
|
lexer = lexers.Get(language) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -168,9 +164,9 @@ func File(numLines int, fileName, language string, code []byte) []string { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if lexer == nil { |
|
|
|
if lexer == nil { |
|
|
|
language := analyze.GetCodeLanguage(fileName, code) |
|
|
|
guessLanguage := analyze.GetCodeLanguage(fileName, code) |
|
|
|
|
|
|
|
|
|
|
|
lexer = lexers.Get(language) |
|
|
|
lexer = lexers.Get(guessLanguage) |
|
|
|
if lexer == nil { |
|
|
|
if lexer == nil { |
|
|
|
lexer = lexers.Match(fileName) |
|
|
|
lexer = lexers.Match(fileName) |
|
|
|
if lexer == nil { |
|
|
|
if lexer == nil { |
|
|
@ -181,54 +177,43 @@ func File(numLines int, fileName, language string, code []byte) []string { |
|
|
|
|
|
|
|
|
|
|
|
iterator, err := lexer.Tokenise(nil, string(code)) |
|
|
|
iterator, err := lexer.Tokenise(nil, string(code)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error("Can't tokenize code: %v", err) |
|
|
|
return nil, fmt.Errorf("can't tokenize code: %w", err) |
|
|
|
return plainText(string(code), numLines) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = formatter.Format(htmlw, styles.GitHub, iterator) |
|
|
|
err = formatter.Format(htmlWriter, styles.GitHub, iterator) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error("Can't format code: %v", err) |
|
|
|
return nil, fmt.Errorf("can't format code: %w", err) |
|
|
|
return plainText(string(code), numLines) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
htmlw.Flush() |
|
|
|
_ = htmlWriter.Flush() |
|
|
|
finalNewLine := false |
|
|
|
|
|
|
|
if len(code) > 0 { |
|
|
|
|
|
|
|
finalNewLine = code[len(code)-1] == '\n' |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m := make([]string, 0, numLines) |
|
|
|
// at the moment, Chroma generates stable output `<span class="line"><span class="cl">...\n</span></span>` for each line
|
|
|
|
for _, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) { |
|
|
|
htmlStr := htmlBuf.String() |
|
|
|
content := v |
|
|
|
lines := strings.Split(htmlStr, `<span class="line"><span class="cl">`) |
|
|
|
// need to keep lines that are only \n so copy/paste works properly in browser
|
|
|
|
m := make([]string, 0, len(lines)) |
|
|
|
if content == "" { |
|
|
|
for i := 1; i < len(lines); i++ { |
|
|
|
content = "\n" |
|
|
|
line := lines[i] |
|
|
|
} else if content == `</span><span class="w">` { |
|
|
|
line = strings.TrimSuffix(line, "</span></span>") |
|
|
|
content += "\n</span>" |
|
|
|
m = append(m, line) |
|
|
|
} else if content == `</span></span><span class="line"><span class="cl">` { |
|
|
|
|
|
|
|
content += "\n" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
content = strings.TrimSuffix(content, `<span class="w">`) |
|
|
|
|
|
|
|
content = strings.TrimPrefix(content, `</span>`) |
|
|
|
|
|
|
|
m = append(m, content) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if finalNewLine { |
|
|
|
return m, nil |
|
|
|
m = append(m, "<span class=\"w\">\n</span>") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return m |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// return unhiglighted map
|
|
|
|
// PlainText returns non-highlighted HTML for code
|
|
|
|
func plainText(code string, numLines int) []string { |
|
|
|
func PlainText(code []byte) []string { |
|
|
|
m := make([]string, 0, numLines) |
|
|
|
r := bufio.NewReader(bytes.NewReader(code)) |
|
|
|
for _, v := range strings.SplitN(code, "\n", numLines) { |
|
|
|
m := make([]string, 0, bytes.Count(code, []byte{'\n'})+1) |
|
|
|
content := v |
|
|
|
for { |
|
|
|
// need to keep lines that are only \n so copy/paste works properly in browser
|
|
|
|
content, err := r.ReadString('\n') |
|
|
|
if content == "" { |
|
|
|
if err != nil && err != io.EOF { |
|
|
|
content = "\n" |
|
|
|
log.Error("failed to read string from buffer: %v", err) |
|
|
|
} |
|
|
|
break |
|
|
|
m = append(m, gohtml.EscapeString(content)) |
|
|
|
} |
|
|
|
|
|
|
|
if content == "" && err == io.EOF { |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
s := gohtml.EscapeString(content) |
|
|
|
|
|
|
|
m = append(m, s) |
|
|
|
} |
|
|
|
} |
|
|
|
return m |
|
|
|
return m |
|
|
|
} |
|
|
|
} |
|
|
|