mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-05-23 16:40:08 -03:00
split GetLanguage into GetLanguage and GetLanguages
This commit is contained in:
parent
beda5b73e7
commit
bea1bc3af8
@ -2,7 +2,6 @@ package enry
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"sort"
|
||||
|
||||
"gopkg.in/src-d/enry.v1/internal/tokenizer"
|
||||
|
59
common.go
59
common.go
@ -26,20 +26,8 @@ var DefaultStrategies = []Strategy{
|
||||
|
||||
// GetLanguage applies a sequence of strategies based on the given filename and content
|
||||
// to find out the most probably language to return.
|
||||
func GetLanguage(filename string, content []byte) string {
|
||||
var languages []string
|
||||
candidates := []string{}
|
||||
for _, strategy := range DefaultStrategies {
|
||||
languages = strategy(filename, content, candidates)
|
||||
if len(languages) == 1 {
|
||||
return languages[0]
|
||||
}
|
||||
|
||||
if len(languages) > 0 {
|
||||
candidates = append(candidates, languages...)
|
||||
}
|
||||
}
|
||||
|
||||
func GetLanguage(filename string, content []byte) (language string) {
|
||||
languages := GetLanguages(filename, content)
|
||||
return firstLanguage(languages)
|
||||
}
|
||||
|
||||
@ -51,17 +39,6 @@ func firstLanguage(languages []string) string {
|
||||
return languages[0]
|
||||
}
|
||||
|
||||
func getLanguageByStrategy(strategy Strategy, filename string, content []byte, candidates []string) (string, bool) {
|
||||
languages := strategy(filename, content, candidates)
|
||||
return getFirstLanguageAndSafe(languages)
|
||||
}
|
||||
|
||||
func getFirstLanguageAndSafe(languages []string) (language string, safe bool) {
|
||||
language = firstLanguage(languages)
|
||||
safe = len(languages) == 1
|
||||
return
|
||||
}
|
||||
|
||||
// GetLanguageByModeline returns detected language. If there are more than one possibles languages
|
||||
// it returns the first language by alphabetically order and safe to false.
|
||||
func GetLanguageByModeline(content []byte) (language string, safe bool) {
|
||||
@ -110,6 +87,17 @@ func GetLanguageByClassifier(content []byte, candidates []string) (language stri
|
||||
return getLanguageByStrategy(GetLanguagesByClassifier, "", content, candidates)
|
||||
}
|
||||
|
||||
func getLanguageByStrategy(strategy Strategy, filename string, content []byte, candidates []string) (string, bool) {
|
||||
languages := strategy(filename, content, candidates)
|
||||
return getFirstLanguageAndSafe(languages)
|
||||
}
|
||||
|
||||
func getFirstLanguageAndSafe(languages []string) (language string, safe bool) {
|
||||
language = firstLanguage(languages)
|
||||
safe = len(languages) == 1
|
||||
return
|
||||
}
|
||||
|
||||
// GetLanguageBySpecificClassifier returns the most probably language for the given content using
|
||||
// classifier to detect language.
|
||||
func GetLanguageBySpecificClassifier(content []byte, candidates []string, classifier Classifier) (language string, safe bool) {
|
||||
@ -117,6 +105,25 @@ func GetLanguageBySpecificClassifier(content []byte, candidates []string, classi
|
||||
return getFirstLanguageAndSafe(languages)
|
||||
}
|
||||
|
||||
// GetLanguages applies a sequence of strategies based on the given filename and content
|
||||
// to find out the most probably languages to return.
|
||||
func GetLanguages(filename string, content []byte) []string {
|
||||
var languages []string
|
||||
candidates := []string{}
|
||||
for _, strategy := range DefaultStrategies {
|
||||
languages = strategy(filename, content, candidates)
|
||||
if len(languages) == 1 {
|
||||
return languages
|
||||
}
|
||||
|
||||
if len(languages) > 0 {
|
||||
candidates = append(candidates, languages...)
|
||||
}
|
||||
}
|
||||
|
||||
return languages
|
||||
}
|
||||
|
||||
// GetLanguagesByModeline returns a slice of possible languages for the given content, filename will be ignored.
|
||||
// It is comply with the signature to be a Strategy type.
|
||||
func GetLanguagesByModeline(filename string, content []byte, candidates []string) []string {
|
||||
@ -242,7 +249,7 @@ func GetLanguagesByVimModeline(filename string, content []byte, candidates []str
|
||||
// GetLanguagesByFilename returns a slice of possible languages for the given filename, content and candidates
|
||||
// will be ignored. It is comply with the signature to be a Strategy type.
|
||||
func GetLanguagesByFilename(filename string, content []byte, candidates []string) []string {
|
||||
return languagesByFilename[filename]
|
||||
return languagesByFilename[filepath.Base(filename)]
|
||||
}
|
||||
|
||||
// GetLanguagesByShebang returns a slice of possible languages for the given content, filename and candidates
|
||||
|
@ -25,6 +25,7 @@ func (s *SimpleLinguistTestSuite) TestGetLanguage() {
|
||||
filename string
|
||||
content []byte
|
||||
expected string
|
||||
safe bool
|
||||
}{
|
||||
{name: "TestGetLanguage_1", filename: "foo.py", content: []byte{}, expected: "Python"},
|
||||
{name: "TestGetLanguage_2", filename: "foo.m", content: []byte(":- module"), expected: "Mercury"},
|
||||
|
Loading…
x
Reference in New Issue
Block a user