mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-04 01:34:27 +00:00 
			
		
		
		
	Use existing analyzer module for language detection for highlighting (#13522)
* Use existing analyzer module for language detction for highlighting Thanks @lafriks for pointing out we can reuse existing code for more reliable language detection here. * Update modules/highlight/highlight.go Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
		@@ -13,6 +13,7 @@ import (
 | 
				
			|||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"code.gitea.io/gitea/modules/analyze"
 | 
				
			||||||
	"code.gitea.io/gitea/modules/log"
 | 
						"code.gitea.io/gitea/modules/log"
 | 
				
			||||||
	"code.gitea.io/gitea/modules/setting"
 | 
						"code.gitea.io/gitea/modules/setting"
 | 
				
			||||||
	"github.com/alecthomas/chroma/formatters/html"
 | 
						"github.com/alecthomas/chroma/formatters/html"
 | 
				
			||||||
@@ -117,9 +118,11 @@ func File(numLines int, fileName string, code []byte) map[int]string {
 | 
				
			|||||||
		fileName = "test." + val
 | 
							fileName = "test." + val
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	lexer := lexers.Match(fileName)
 | 
						language := analyze.GetCodeLanguage(fileName, code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						lexer := lexers.Get(language)
 | 
				
			||||||
	if lexer == nil {
 | 
						if lexer == nil {
 | 
				
			||||||
		lexer = lexers.Analyse(string(code))
 | 
							lexer = lexers.Match(fileName)
 | 
				
			||||||
		if lexer == nil {
 | 
							if lexer == nil {
 | 
				
			||||||
			lexer = lexers.Fallback
 | 
								lexer = lexers.Fallback
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user