mirror of
https://github.com/go-gitea/gitea
synced 2024-10-31 19:28:58 +01:00
af7ffaa279
* Server-side syntax hilighting for all code This PR does a few things: * Remove all traces of highlight.js * Use chroma library to provide fast syntax hilighting directly on the server * Provide syntax hilighting for diffs * Re-style both unified and split diffs views * Add custom syntax hilighting styling for both regular and arc-green Fixes #7729 Fixes #10157 Fixes #11825 Fixes #7728 Fixes #3872 Fixes #3682 And perhaps gets closer to #9553 * fix line marker * fix repo search * Fix single line select * properly load settings * npm uninstall highlight.js * review suggestion * code review * forgot to call function * fix test * Apply suggestions from code review suggestions from @silverwind thanks Co-authored-by: silverwind <me@silverwind.io> * code review * copy/paste error * Use const for highlight size limit * Update web_src/less/_repository.less Co-authored-by: Lauris BH <lauris@nix.lv> * update size limit to 1MB and other styling tweaks * fix highlighting for certain diff sections * fix test * add worker back as suggested Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Lauris BH <lauris@nix.lv>
114 lines
4.6 KiB
Go
114 lines
4.6 KiB
Go
package g
|
|
|
|
import (
|
|
"strings"
|
|
|
|
. "github.com/alecthomas/chroma" // nolint
|
|
"github.com/alecthomas/chroma/lexers/h"
|
|
"github.com/alecthomas/chroma/lexers/internal"
|
|
)
|
|
|
|
// Go lexer.
|
|
var Go = internal.Register(MustNewLexer(
|
|
&Config{
|
|
Name: "Go",
|
|
Aliases: []string{"go", "golang"},
|
|
Filenames: []string{"*.go"},
|
|
MimeTypes: []string{"text/x-gosrc"},
|
|
},
|
|
Rules{
|
|
"root": {
|
|
{`\n`, Text, nil},
|
|
{`\s+`, Text, nil},
|
|
{`\\\n`, Text, nil},
|
|
{`//(.*?)\n`, CommentSingle, nil},
|
|
{`/(\\\n)?[*](.|\n)*?[*](\\\n)?/`, CommentMultiline, nil},
|
|
{`(import|package)\b`, KeywordNamespace, nil},
|
|
{`(var|func|struct|map|chan|type|interface|const)\b`, KeywordDeclaration, nil},
|
|
{Words(``, `\b`, `break`, `default`, `select`, `case`, `defer`, `go`, `else`, `goto`, `switch`, `fallthrough`, `if`, `range`, `continue`, `for`, `return`), Keyword, nil},
|
|
{`(true|false|iota|nil)\b`, KeywordConstant, nil},
|
|
{Words(``, `\b(\()`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `int`, `int8`, `int16`, `int32`, `int64`, `float`, `float32`, `float64`, `complex64`, `complex128`, `byte`, `rune`, `string`, `bool`, `error`, `uintptr`, `print`, `println`, `panic`, `recover`, `close`, `complex`, `real`, `imag`, `len`, `cap`, `append`, `copy`, `delete`, `new`, `make`), ByGroups(NameBuiltin, Punctuation), nil},
|
|
{Words(``, `\b`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `int`, `int8`, `int16`, `int32`, `int64`, `float`, `float32`, `float64`, `complex64`, `complex128`, `byte`, `rune`, `string`, `bool`, `error`, `uintptr`), KeywordType, nil},
|
|
{`\d+i`, LiteralNumber, nil},
|
|
{`\d+\.\d*([Ee][-+]\d+)?i`, LiteralNumber, nil},
|
|
{`\.\d+([Ee][-+]\d+)?i`, LiteralNumber, nil},
|
|
{`\d+[Ee][-+]\d+i`, LiteralNumber, nil},
|
|
{`\d+(\.\d+[eE][+\-]?\d+|\.\d*|[eE][+\-]?\d+)`, LiteralNumberFloat, nil},
|
|
{`\.\d+([eE][+\-]?\d+)?`, LiteralNumberFloat, nil},
|
|
{`0[0-7]+`, LiteralNumberOct, nil},
|
|
{`0[xX][0-9a-fA-F]+`, LiteralNumberHex, nil},
|
|
{`(0|[1-9][0-9]*)`, LiteralNumberInteger, nil},
|
|
{`'(\\['"\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'`, LiteralStringChar, nil},
|
|
{"(`)([^`]*)(`)", ByGroups(LiteralString, Using(TypeRemappingLexer(GoTextTemplate, TypeMapping{{Other, LiteralString, nil}})), LiteralString), nil},
|
|
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
|
|
{`(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|&&|\|\||<-|\+\+|--|==|!=|:=|\.\.\.|[+\-*/%&])`, Operator, nil},
|
|
{`([a-zA-Z_]\w*)(\s*)(\()`, ByGroups(NameFunction, UsingSelf("root"), Punctuation), nil},
|
|
{`[|^<>=!()\[\]{}.,;:]`, Punctuation, nil},
|
|
{`[^\W\d]\w*`, NameOther, nil},
|
|
},
|
|
},
|
|
).SetAnalyser(func(text string) float32 {
|
|
if strings.Contains(text, "fmt.") && strings.Contains(text, "package ") {
|
|
return 0.5
|
|
}
|
|
if strings.Contains(text, "package ") {
|
|
return 0.1
|
|
}
|
|
return 0.0
|
|
}))
|
|
|
|
var goTemplateRules = Rules{
|
|
"root": {
|
|
{`{{[-]?`, CommentPreproc, Push("template")},
|
|
{`[^{]+`, Other, nil},
|
|
{`{`, Other, nil},
|
|
},
|
|
"template": {
|
|
{`[-]?}}`, CommentPreproc, Pop(1)},
|
|
{`/\*.*?\*/`, Comment, nil},
|
|
{`(?=}})`, CommentPreproc, Pop(1)}, // Terminate the pipeline
|
|
{`\(`, Operator, Push("subexpression")},
|
|
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
|
|
Include("expression"),
|
|
},
|
|
"subexpression": {
|
|
{`\)`, Operator, Pop(1)},
|
|
Include("expression"),
|
|
},
|
|
"expression": {
|
|
{`\s+`, Whitespace, nil},
|
|
{`\(`, Operator, Push("subexpression")},
|
|
{`(range|if|else|while|with|template|end|true|false|nil|and|call|html|index|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|le|gt|ge)\b`, Keyword, nil},
|
|
{`\||:=`, Operator, nil},
|
|
{`[$]?[^\W\d]\w*`, NameOther, nil},
|
|
{`[$]?\.(?:[^\W\d]\w*)?`, NameAttribute, nil},
|
|
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
|
|
{`\d+i`, LiteralNumber, nil},
|
|
{`\d+\.\d*([Ee][-+]\d+)?i`, LiteralNumber, nil},
|
|
{`\.\d+([Ee][-+]\d+)?i`, LiteralNumber, nil},
|
|
{`\d+[Ee][-+]\d+i`, LiteralNumber, nil},
|
|
{`\d+(\.\d+[eE][+\-]?\d+|\.\d*|[eE][+\-]?\d+)`, LiteralNumberFloat, nil},
|
|
{`\.\d+([eE][+\-]?\d+)?`, LiteralNumberFloat, nil},
|
|
{`0[0-7]+`, LiteralNumberOct, nil},
|
|
{`0[xX][0-9a-fA-F]+`, LiteralNumberHex, nil},
|
|
{`(0|[1-9][0-9]*)`, LiteralNumberInteger, nil},
|
|
{`'(\\['"\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'`, LiteralStringChar, nil},
|
|
{"`[^`]*`", LiteralString, nil},
|
|
},
|
|
}
|
|
|
|
var GoHTMLTemplate = internal.Register(DelegatingLexer(h.HTML, MustNewLexer(
|
|
&Config{
|
|
Name: "Go HTML Template",
|
|
Aliases: []string{"go-html-template"},
|
|
},
|
|
goTemplateRules,
|
|
)))
|
|
|
|
var GoTextTemplate = internal.Register(MustNewLexer(
|
|
&Config{
|
|
Name: "Go Text Template",
|
|
Aliases: []string{"go-text-template"},
|
|
},
|
|
goTemplateRules,
|
|
))
|