mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-07-11 19:59:48 +00:00
split GetLanguage into GetLanguage and GetLanguages
This commit is contained in:
@ -2,7 +2,6 @@ package enry
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"gopkg.in/src-d/enry.v1/internal/tokenizer"
|
"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
|
// GetLanguage applies a sequence of strategies based on the given filename and content
|
||||||
// to find out the most probably language to return.
|
// to find out the most probably language to return.
|
||||||
func GetLanguage(filename string, content []byte) string {
|
func GetLanguage(filename string, content []byte) (language string) {
|
||||||
var languages []string
|
languages := GetLanguages(filename, content)
|
||||||
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...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return firstLanguage(languages)
|
return firstLanguage(languages)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,17 +39,6 @@ func firstLanguage(languages []string) string {
|
|||||||
return languages[0]
|
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
|
// GetLanguageByModeline returns detected language. If there are more than one possibles languages
|
||||||
// it returns the first language by alphabetically order and safe to false.
|
// it returns the first language by alphabetically order and safe to false.
|
||||||
func GetLanguageByModeline(content []byte) (language string, safe bool) {
|
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)
|
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
|
// GetLanguageBySpecificClassifier returns the most probably language for the given content using
|
||||||
// classifier to detect language.
|
// classifier to detect language.
|
||||||
func GetLanguageBySpecificClassifier(content []byte, candidates []string, classifier Classifier) (language string, safe bool) {
|
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)
|
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.
|
// 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.
|
// It is comply with the signature to be a Strategy type.
|
||||||
func GetLanguagesByModeline(filename string, content []byte, candidates []string) []string {
|
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
|
// 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.
|
// will be ignored. It is comply with the signature to be a Strategy type.
|
||||||
func GetLanguagesByFilename(filename string, content []byte, candidates []string) []string {
|
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
|
// GetLanguagesByShebang returns a slice of possible languages for the given content, filename and candidates
|
||||||
|
@ -25,6 +25,7 @@ func (s *SimpleLinguistTestSuite) TestGetLanguage() {
|
|||||||
filename string
|
filename string
|
||||||
content []byte
|
content []byte
|
||||||
expected string
|
expected string
|
||||||
|
safe bool
|
||||||
}{
|
}{
|
||||||
{name: "TestGetLanguage_1", filename: "foo.py", content: []byte{}, expected: "Python"},
|
{name: "TestGetLanguage_1", filename: "foo.py", content: []byte{}, expected: "Python"},
|
||||||
{name: "TestGetLanguage_2", filename: "foo.m", content: []byte(":- module"), expected: "Mercury"},
|
{name: "TestGetLanguage_2", filename: "foo.m", content: []byte(":- module"), expected: "Mercury"},
|
||||||
|
Reference in New Issue
Block a user