split GetLanguage into GetLanguage and GetLanguages

This commit is contained in:
Manuel Carmona 2017-06-15 10:26:14 +02:00
parent beda5b73e7
commit bea1bc3af8
3 changed files with 34 additions and 27 deletions

View File

@ -2,7 +2,6 @@ package enry
import (
"math"
"sort"
"gopkg.in/src-d/enry.v1/internal/tokenizer"

View File

@ -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

View File

@ -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"},