diff options
author | zeripath <art27@cantab.net> | 2021-11-17 20:37:00 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-17 20:37:00 +0000 |
commit | 3c4724d70e4ac7bfc06b97f6fad8936f97479b6b (patch) | |
tree | 754286def789b823e020d3ccfafae148d0017b62 /modules/highlight | |
parent | 81a4fc752833101dd7d6b4f612bccc4b29c98dff (diff) | |
download | gitea-3c4724d70e4ac7bfc06b97f6fad8936f97479b6b.tar.gz gitea-3c4724d70e4ac7bfc06b97f6fad8936f97479b6b.zip |
Add .gitattribute assisted language detection to blame, diff and render (#17590)
Use check attribute code to check the assigned language of a file and send that in to
chroma as a hint for the language of the file.
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/highlight')
-rw-r--r-- | modules/highlight/highlight.go | 36 | ||||
-rw-r--r-- | modules/highlight/highlight_test.go | 2 |
2 files changed, 30 insertions, 8 deletions
diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index 9a876d2a6b..04bd30bceb 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -55,7 +55,7 @@ func NewContext() { } // Code returns a HTML version of code string with chroma syntax highlighting classes -func Code(fileName, code string) string { +func Code(fileName, language, code string) string { NewContext() // diff view newline will be passed as empty, change to literal \n so it can be copied @@ -69,9 +69,23 @@ func Code(fileName, code string) string { } var lexer chroma.Lexer - if val, ok := highlightMapping[filepath.Ext(fileName)]; ok { - //use mapped value to find lexer - lexer = lexers.Get(val) + + if len(language) > 0 { + lexer = lexers.Get(language) + + if lexer == nil { + // Attempt stripping off the '?' + if idx := strings.IndexByte(language, '?'); idx > 0 { + lexer = lexers.Get(language[:idx]) + } + } + } + + if lexer == nil { + if val, ok := highlightMapping[filepath.Ext(fileName)]; ok { + //use mapped value to find lexer + lexer = lexers.Get(val) + } } if lexer == nil { @@ -119,7 +133,7 @@ func CodeFromLexer(lexer chroma.Lexer, code string) string { } // File returns a slice of chroma syntax highlighted lines of code -func File(numLines int, fileName string, code []byte) []string { +func File(numLines int, fileName, language string, code []byte) []string { NewContext() if len(code) > sizeLimit { @@ -139,8 +153,16 @@ func File(numLines int, fileName string, code []byte) []string { htmlw := bufio.NewWriter(&htmlbuf) var lexer chroma.Lexer - if val, ok := highlightMapping[filepath.Ext(fileName)]; ok { - lexer = lexers.Get(val) + + // provided language overrides everything + if len(language) > 0 { + lexer = lexers.Get(language) + } + + if lexer == nil { + if val, ok := highlightMapping[filepath.Ext(fileName)]; ok { + lexer = lexers.Get(val) + } } if lexer == nil { diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go index 29a15c0b53..3f47b6a48f 100644 --- a/modules/highlight/highlight_test.go +++ b/modules/highlight/highlight_test.go @@ -96,7 +96,7 @@ steps: for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := File(tt.numLines, tt.fileName, []byte(tt.code)); !reflect.DeepEqual(got, tt.want) { + if got := File(tt.numLines, tt.fileName, "", []byte(tt.code)); !reflect.DeepEqual(got, tt.want) { t.Errorf("File() = %v, want %v", got, tt.want) } }) |