2017-06-13 13:56:07 +02:00
|
|
|
package enry
|
2016-07-13 19:05:09 +02:00
|
|
|
|
2017-05-29 10:05:16 +02:00
|
|
|
import (
|
2017-06-12 13:42:20 +02:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2021-10-11 22:32:29 +02:00
|
|
|
"fmt"
|
2021-06-18 18:35:49 +02:00
|
|
|
"path"
|
2017-05-29 10:05:16 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-06-08 12:28:36 +02:00
|
|
|
|
2020-03-19 17:31:29 +01:00
|
|
|
"github.com/go-enry/go-enry/v2/data"
|
|
|
|
"github.com/go-enry/go-enry/v2/regex"
|
2016-07-18 16:20:12 +02:00
|
|
|
)
|
2016-07-13 19:05:09 +02:00
|
|
|
|
2017-05-29 10:05:16 +02:00
|
|
|
// OtherLanguage is used as a zero value when a function can not return a specific language.
|
2017-06-12 13:42:20 +02:00
|
|
|
const OtherLanguage = ""
|
2016-07-13 19:05:09 +02:00
|
|
|
|
2017-05-31 12:07:46 +02:00
|
|
|
// Strategy type fix the signature for the functions that can be used as a strategy.
|
2017-06-12 13:42:20 +02:00
|
|
|
type Strategy func(filename string, content []byte, candidates []string) (languages []string)
|
2017-05-31 12:07:46 +02:00
|
|
|
|
2019-02-14 12:47:45 +01:00
|
|
|
// DefaultStrategies is a sequence of strategies used by GetLanguage to detect languages.
|
2017-06-12 13:42:20 +02:00
|
|
|
var DefaultStrategies = []Strategy{
|
2017-05-31 12:07:46 +02:00
|
|
|
GetLanguagesByModeline,
|
|
|
|
GetLanguagesByFilename,
|
|
|
|
GetLanguagesByShebang,
|
|
|
|
GetLanguagesByExtension,
|
2020-11-15 14:43:37 +01:00
|
|
|
GetLanguagesByXML,
|
2020-11-26 12:56:25 +01:00
|
|
|
GetLanguagesByManpage,
|
2017-05-31 12:07:46 +02:00
|
|
|
GetLanguagesByContent,
|
2017-06-12 13:42:20 +02:00
|
|
|
GetLanguagesByClassifier,
|
2017-05-31 12:07:46 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 16:03:50 +01:00
|
|
|
// defaultClassifier is a Naive Bayes classifier trained on Linguist samples.
|
2019-10-29 17:56:13 +01:00
|
|
|
var defaultClassifier classifier = &naiveBayes{
|
2017-06-08 12:28:36 +02:00
|
|
|
languagesLogProbabilities: data.LanguagesLogProbabilities,
|
|
|
|
tokensLogProbabilities: data.TokensLogProbabilities,
|
|
|
|
tokensTotal: data.TokensTotal,
|
|
|
|
}
|
|
|
|
|
2017-05-29 10:05:16 +02:00
|
|
|
// GetLanguage applies a sequence of strategies based on the given filename and content
|
|
|
|
// to find out the most probably language to return.
|
2017-06-15 10:26:14 +02:00
|
|
|
func GetLanguage(filename string, content []byte) (language string) {
|
|
|
|
languages := GetLanguages(filename, content)
|
2017-06-12 13:42:20 +02:00
|
|
|
return firstLanguage(languages)
|
|
|
|
}
|
|
|
|
|
|
|
|
func firstLanguage(languages []string) string {
|
2019-03-14 13:26:00 +01:00
|
|
|
for _, l := range languages {
|
|
|
|
if l != "" {
|
|
|
|
return l
|
|
|
|
}
|
2017-05-25 12:34:32 +02:00
|
|
|
}
|
2019-03-14 13:26:00 +01:00
|
|
|
return OtherLanguage
|
2017-06-12 13:42:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
return getLanguageByStrategy(GetLanguagesByModeline, "", content, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLanguageByEmacsModeline returns detected language. If there are more than one possibles languages
|
|
|
|
// it returns the first language by alphabetically order and safe to false.
|
|
|
|
func GetLanguageByEmacsModeline(content []byte) (language string, safe bool) {
|
|
|
|
return getLanguageByStrategy(GetLanguagesByEmacsModeline, "", content, nil)
|
2016-07-18 16:20:12 +02:00
|
|
|
}
|
|
|
|
|
2017-06-12 13:42:20 +02:00
|
|
|
// GetLanguageByVimModeline returns detected language. If there are more than one possibles languages
|
|
|
|
// it returns the first language by alphabetically order and safe to false.
|
|
|
|
func GetLanguageByVimModeline(content []byte) (language string, safe bool) {
|
|
|
|
return getLanguageByStrategy(GetLanguagesByVimModeline, "", content, nil)
|
2017-05-29 10:05:16 +02:00
|
|
|
}
|
|
|
|
|
2017-06-12 13:42:20 +02:00
|
|
|
// GetLanguageByFilename returns detected language. If there are more than one possibles languages
|
|
|
|
// it returns the first language by alphabetically order and safe to false.
|
|
|
|
func GetLanguageByFilename(filename string) (language string, safe bool) {
|
|
|
|
return getLanguageByStrategy(GetLanguagesByFilename, filename, nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLanguageByShebang returns detected language. If there are more than one possibles languages
|
|
|
|
// it returns the first language by alphabetically order and safe to false.
|
|
|
|
func GetLanguageByShebang(content []byte) (language string, safe bool) {
|
|
|
|
return getLanguageByStrategy(GetLanguagesByShebang, "", content, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLanguageByExtension returns detected language. If there are more than one possibles languages
|
|
|
|
// it returns the first language by alphabetically order and safe to false.
|
|
|
|
func GetLanguageByExtension(filename string) (language string, safe bool) {
|
|
|
|
return getLanguageByStrategy(GetLanguagesByExtension, filename, nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLanguageByContent returns detected language. If there are more than one possibles languages
|
|
|
|
// it returns the first language by alphabetically order and safe to false.
|
2017-08-08 11:58:02 +02:00
|
|
|
func GetLanguageByContent(filename string, content []byte) (language string, safe bool) {
|
|
|
|
return getLanguageByStrategy(GetLanguagesByContent, filename, content, nil)
|
2017-06-12 13:42:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLanguageByClassifier returns the most probably language detected for the given content. It uses
|
2019-10-29 16:03:50 +01:00
|
|
|
// defaultClassifier, if no candidates are provided it returns OtherLanguage.
|
2017-06-12 13:42:20 +02:00
|
|
|
func GetLanguageByClassifier(content []byte, candidates []string) (language string, safe bool) {
|
|
|
|
return getLanguageByStrategy(GetLanguagesByClassifier, "", content, candidates)
|
|
|
|
}
|
|
|
|
|
2017-06-15 10:26:14 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLanguages applies a sequence of strategies based on the given filename and content
|
2021-04-13 21:02:15 +02:00
|
|
|
// to find out the most probable languages to return.
|
|
|
|
//
|
|
|
|
// If it finds a strategy that produces a single result, it will be returned;
|
|
|
|
// otherise the last strategy that returned multiple results will be returned.
|
|
|
|
// If the content is binary, no results will be returned. This matches the
|
|
|
|
// behavior of Linguist.detect: https://github.com/github/linguist/blob/aad49acc0624c70d654a8dce447887dbbc713c7a/lib/linguist.rb#L14-L49
|
|
|
|
//
|
2018-07-30 22:29:22 +02:00
|
|
|
// At least one of arguments should be set. If content is missing, language detection will be based on the filename.
|
|
|
|
// The function won't read the file, given an empty content.
|
2017-06-15 10:26:14 +02:00
|
|
|
func GetLanguages(filename string, content []byte) []string {
|
2017-07-04 11:22:01 +02:00
|
|
|
if IsBinary(content) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 10:26:14 +02:00
|
|
|
var languages []string
|
|
|
|
for _, strategy := range DefaultStrategies {
|
2021-04-13 21:02:15 +02:00
|
|
|
candidates := strategy(filename, content, languages)
|
|
|
|
// No candidates, continue to next strategy without updating languages
|
|
|
|
if len(candidates) == 0 {
|
|
|
|
continue
|
2017-06-15 10:26:14 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 21:02:15 +02:00
|
|
|
// Only one candidate match, return it
|
|
|
|
if len(candidates) == 1 {
|
|
|
|
return candidates
|
2017-06-15 10:26:14 +02:00
|
|
|
}
|
2021-04-13 21:02:15 +02:00
|
|
|
|
|
|
|
// Save the candidates from this strategy to pass onto to the next strategy, like Linguist
|
|
|
|
languages = candidates
|
2017-06-15 10:26:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return languages
|
|
|
|
}
|
|
|
|
|
2017-08-08 14:58:01 +02:00
|
|
|
// GetLanguagesByModeline returns a slice of possible languages for the given content.
|
2017-06-21 15:18:27 +02:00
|
|
|
// It complies with the signature to be a Strategy type.
|
2017-08-08 14:58:01 +02:00
|
|
|
func GetLanguagesByModeline(_ string, content []byte, candidates []string) []string {
|
2017-06-12 13:42:20 +02:00
|
|
|
headFoot := getHeaderAndFooter(content)
|
|
|
|
var languages []string
|
|
|
|
for _, getLang := range modelinesFunc {
|
|
|
|
languages = getLang("", headFoot, candidates)
|
|
|
|
if len(languages) > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return languages
|
2017-05-29 10:05:16 +02:00
|
|
|
}
|
|
|
|
|
2017-06-12 13:42:20 +02:00
|
|
|
var modelinesFunc = []Strategy{
|
|
|
|
GetLanguagesByEmacsModeline,
|
|
|
|
GetLanguagesByVimModeline,
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHeaderAndFooter(content []byte) []byte {
|
|
|
|
const searchScope = 5
|
2017-11-08 13:20:52 +01:00
|
|
|
|
|
|
|
if len(content) == 0 {
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2017-06-12 13:42:20 +02:00
|
|
|
if bytes.Count(content, []byte("\n")) < 2*searchScope {
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
|
|
|
header := headScope(content, searchScope)
|
|
|
|
footer := footScope(content, searchScope)
|
|
|
|
headerAndFooter := make([]byte, 0, len(content[:header])+len(content[footer:]))
|
|
|
|
headerAndFooter = append(headerAndFooter, content[:header]...)
|
|
|
|
headerAndFooter = append(headerAndFooter, content[footer:]...)
|
|
|
|
return headerAndFooter
|
|
|
|
}
|
|
|
|
|
|
|
|
func headScope(content []byte, scope int) (index int) {
|
|
|
|
for i := 0; i < scope; i++ {
|
|
|
|
eol := bytes.IndexAny(content, "\n")
|
|
|
|
content = content[eol+1:]
|
|
|
|
index += eol
|
|
|
|
}
|
|
|
|
|
|
|
|
return index + scope - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func footScope(content []byte, scope int) (index int) {
|
|
|
|
for i := 0; i < scope; i++ {
|
|
|
|
index = bytes.LastIndexAny(content, "\n")
|
|
|
|
content = content[:index]
|
|
|
|
}
|
|
|
|
|
|
|
|
return index + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2018-08-28 17:27:18 +02:00
|
|
|
reEmacsModeline = regex.MustCompile(`.*-\*-\s*(.+?)\s*-\*-.*(?m:$)`)
|
|
|
|
reEmacsLang = regex.MustCompile(`.*(?i:mode)\s*:\s*([^\s;]+)\s*;*.*`)
|
|
|
|
reVimModeline = regex.MustCompile(`(?:(?m:\s|^)vi(?:m[<=>]?\d+|m)?|[\t\x20]*ex)\s*[:]\s*(.*)(?m:$)`)
|
|
|
|
reVimLang = regex.MustCompile(`(?i:filetype|ft|syntax)\s*=(\w+)(?:\s|:|$)`)
|
2017-06-12 13:42:20 +02:00
|
|
|
)
|
|
|
|
|
2017-08-08 14:58:01 +02:00
|
|
|
// GetLanguagesByEmacsModeline returns a slice of possible languages for the given content.
|
|
|
|
// It complies with the signature to be a Strategy type.
|
|
|
|
func GetLanguagesByEmacsModeline(_ string, content []byte, _ []string) []string {
|
2017-06-12 13:42:20 +02:00
|
|
|
matched := reEmacsModeline.FindAllSubmatch(content, -1)
|
|
|
|
if matched == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// only take the last matched line, discard previous lines
|
|
|
|
lastLineMatched := matched[len(matched)-1][1]
|
|
|
|
matchedAlias := reEmacsLang.FindSubmatch(lastLineMatched)
|
|
|
|
var alias string
|
|
|
|
if matchedAlias != nil {
|
|
|
|
alias = string(matchedAlias[1])
|
|
|
|
} else {
|
|
|
|
alias = string(lastLineMatched)
|
|
|
|
}
|
|
|
|
|
|
|
|
language, ok := GetLanguageByAlias(alias)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{language}
|
|
|
|
}
|
|
|
|
|
2017-08-08 14:58:01 +02:00
|
|
|
// GetLanguagesByVimModeline returns a slice of possible languages for the given content.
|
|
|
|
// It complies with the signature to be a Strategy type.
|
|
|
|
func GetLanguagesByVimModeline(_ string, content []byte, _ []string) []string {
|
2017-06-12 13:42:20 +02:00
|
|
|
matched := reVimModeline.FindAllSubmatch(content, -1)
|
|
|
|
if matched == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// only take the last matched line, discard previous lines
|
|
|
|
lastLineMatched := matched[len(matched)-1][1]
|
|
|
|
matchedAlias := reVimLang.FindAllSubmatch(lastLineMatched, -1)
|
|
|
|
if matchedAlias == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
alias := string(matchedAlias[0][1])
|
|
|
|
if len(matchedAlias) > 1 {
|
|
|
|
// cases:
|
|
|
|
// matchedAlias = [["syntax=ruby " "ruby"] ["ft=python " "python"] ["filetype=perl " "perl"]] returns OtherLanguage;
|
|
|
|
// matchedAlias = [["syntax=python " "python"] ["ft=python " "python"] ["filetype=python " "python"]] returns "Python";
|
|
|
|
for _, match := range matchedAlias {
|
|
|
|
otherAlias := string(match[1])
|
|
|
|
if otherAlias != alias {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
language, ok := GetLanguageByAlias(alias)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{language}
|
|
|
|
}
|
|
|
|
|
2017-08-08 14:58:01 +02:00
|
|
|
// GetLanguagesByFilename returns a slice of possible languages for the given filename.
|
|
|
|
// It complies with the signature to be a Strategy type.
|
|
|
|
func GetLanguagesByFilename(filename string, _ []byte, _ []string) []string {
|
2017-12-07 16:45:19 +01:00
|
|
|
if filename == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-08 12:28:36 +02:00
|
|
|
return data.LanguagesByFilename[filepath.Base(filename)]
|
2017-05-29 10:05:16 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 14:58:01 +02:00
|
|
|
// GetLanguagesByShebang returns a slice of possible languages for the given content.
|
|
|
|
// It complies with the signature to be a Strategy type.
|
|
|
|
func GetLanguagesByShebang(_ string, content []byte, _ []string) (languages []string) {
|
2017-06-12 13:42:20 +02:00
|
|
|
interpreter := getInterpreter(content)
|
2017-06-08 12:28:36 +02:00
|
|
|
return data.LanguagesByInterpreter[interpreter]
|
2017-06-12 13:42:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2018-08-28 17:27:18 +02:00
|
|
|
shebangExecHack = regex.MustCompile(`exec (\w+).+\$0.+\$@`)
|
|
|
|
pythonVersion = regex.MustCompile(`python\d\.\d+`)
|
2021-09-25 18:17:49 +02:00
|
|
|
envOptArgs = regex.MustCompile(`-[i0uCSv]*|--\S+`)
|
|
|
|
envVarArgs = regex.MustCompile(`\S+=\S+`)
|
2017-06-12 13:42:20 +02:00
|
|
|
)
|
|
|
|
|
2021-09-25 18:17:49 +02:00
|
|
|
func getInterpreter(data []byte) string {
|
2017-06-12 13:42:20 +02:00
|
|
|
line := getFirstLine(data)
|
|
|
|
if !hasShebang(line) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip shebang
|
|
|
|
line = bytes.TrimSpace(line[2:])
|
|
|
|
splitted := bytes.Fields(line)
|
2017-07-25 10:25:43 +02:00
|
|
|
if len(splitted) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-06-18 18:35:49 +02:00
|
|
|
// Extract interpreter name from path. Use path.Base because
|
|
|
|
// shebang on Cygwin/Windows still use a forward slash
|
2021-09-25 18:17:49 +02:00
|
|
|
interpreter := path.Base(string(splitted[0]))
|
2021-06-18 18:35:49 +02:00
|
|
|
|
|
|
|
// #!/usr/bin/env [...]
|
|
|
|
if interpreter == "env" {
|
|
|
|
if len(splitted) == 1 {
|
|
|
|
// /usr/bin/env with no arguments
|
|
|
|
return ""
|
2017-06-12 13:42:20 +02:00
|
|
|
}
|
2021-09-25 18:17:49 +02:00
|
|
|
for len(splitted) > 2 {
|
|
|
|
if envOptArgs.Match(splitted[1]) || envVarArgs.Match(splitted[1]) {
|
|
|
|
splitted = append(splitted[:1], splitted[2:]...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2021-06-18 18:35:49 +02:00
|
|
|
interpreter = path.Base(string(splitted[1]))
|
2017-06-12 13:42:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if interpreter == "sh" {
|
|
|
|
interpreter = lookForMultilineExec(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pythonVersion.MatchString(interpreter) {
|
|
|
|
interpreter = interpreter[:strings.Index(interpreter, `.`)]
|
|
|
|
}
|
|
|
|
|
2019-08-05 13:44:52 +02:00
|
|
|
// If osascript is called with argument -l it could be different language so do not relay on it
|
|
|
|
// To match linguist behaviour, see ref https://github.com/github/linguist/blob/d95bae794576ab0ef2fcb41a39eb61ea5302c5b5/lib/linguist/shebang.rb#L63
|
|
|
|
if interpreter == "osascript" && bytes.Contains(line, []byte("-l")) {
|
|
|
|
interpreter = ""
|
|
|
|
}
|
|
|
|
|
2021-09-25 18:17:49 +02:00
|
|
|
return interpreter
|
2017-06-12 13:42:20 +02:00
|
|
|
}
|
|
|
|
|
2020-11-15 14:43:37 +01:00
|
|
|
func getFirstLines(content []byte, count int) []byte {
|
|
|
|
nlpos := -1
|
|
|
|
for ; count > 0; count-- {
|
|
|
|
pos := bytes.IndexByte(content[nlpos+1:], '\n')
|
|
|
|
if pos < 0 {
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
nlpos += pos + 1
|
2017-06-12 13:42:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 15:07:57 +02:00
|
|
|
return content[:nlpos]
|
2017-05-29 10:05:16 +02:00
|
|
|
}
|
|
|
|
|
2020-11-15 14:43:37 +01:00
|
|
|
func getFirstLine(content []byte) []byte {
|
|
|
|
return getFirstLines(content, 1)
|
|
|
|
}
|
|
|
|
|
2017-06-12 13:42:20 +02:00
|
|
|
func hasShebang(line []byte) bool {
|
|
|
|
const shebang = `#!`
|
|
|
|
prefix := []byte(shebang)
|
|
|
|
return bytes.HasPrefix(line, prefix)
|
2017-05-29 10:05:16 +02:00
|
|
|
}
|
|
|
|
|
2017-06-12 13:42:20 +02:00
|
|
|
func lookForMultilineExec(data []byte) string {
|
|
|
|
const magicNumOfLines = 5
|
|
|
|
interpreter := "sh"
|
|
|
|
|
|
|
|
buf := bufio.NewScanner(bytes.NewReader(data))
|
|
|
|
for i := 0; i < magicNumOfLines && buf.Scan(); i++ {
|
|
|
|
line := buf.Bytes()
|
|
|
|
if shebangExecHack.Match(line) {
|
|
|
|
interpreter = shebangExecHack.FindStringSubmatch(string(line))[1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := buf.Err(); err != nil {
|
|
|
|
return interpreter
|
|
|
|
}
|
|
|
|
|
|
|
|
return interpreter
|
|
|
|
}
|
|
|
|
|
2017-08-08 14:58:01 +02:00
|
|
|
// GetLanguagesByExtension returns a slice of possible languages for the given filename.
|
|
|
|
// It complies with the signature to be a Strategy type.
|
|
|
|
func GetLanguagesByExtension(filename string, _ []byte, _ []string) []string {
|
2017-06-13 13:56:07 +02:00
|
|
|
if !strings.Contains(filename, ".") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = strings.ToLower(filename)
|
|
|
|
dots := getDotIndexes(filename)
|
|
|
|
for _, dot := range dots {
|
|
|
|
ext := filename[dot:]
|
2017-06-08 12:28:36 +02:00
|
|
|
languages, ok := data.LanguagesByExtension[ext]
|
2017-06-13 13:56:07 +02:00
|
|
|
if ok {
|
|
|
|
return languages
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-17 10:27:28 +02:00
|
|
|
var (
|
|
|
|
manpageExtension = regex.MustCompile(`\.(?:[1-9](?:[a-z_]+[a-z_0-9]*)?|0p|n|man|mdoc)(?:\.in)?$`)
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetLanguagesByManpage returns a slice of possible manpage languages for the given filename.
|
|
|
|
// It complies with the signature to be a Strategy type.
|
|
|
|
func GetLanguagesByManpage(filename string, _ []byte, _ []string) []string {
|
|
|
|
filename = strings.ToLower(filename)
|
|
|
|
|
|
|
|
// Check if matches Roff man page filenames
|
|
|
|
if manpageExtension.Match([]byte(filename)) {
|
|
|
|
return []string{
|
|
|
|
"Roff Manpage",
|
|
|
|
"Roff",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-15 14:43:37 +01:00
|
|
|
var (
|
|
|
|
xmlHeader = regex.MustCompile(`<?xml version=`)
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetLanguagesByXML returns a slice of possible XML language for the given filename.
|
|
|
|
// It complies with the signature to be a Strategy type.
|
|
|
|
func GetLanguagesByXML(_ string, content []byte, candidates []string) []string {
|
|
|
|
if len(candidates) > 0 {
|
|
|
|
return candidates
|
|
|
|
}
|
|
|
|
|
|
|
|
header := getFirstLines(content, 2)
|
|
|
|
|
|
|
|
// Check if contains XML header
|
|
|
|
if xmlHeader.Match(header) {
|
|
|
|
return []string{
|
|
|
|
"XML",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-13 13:56:07 +02:00
|
|
|
func getDotIndexes(filename string) []int {
|
|
|
|
dots := make([]int, 0, 2)
|
|
|
|
for i, letter := range filename {
|
|
|
|
if letter == rune('.') {
|
|
|
|
dots = append(dots, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dots
|
2017-05-29 10:05:16 +02:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:07:14 +02:00
|
|
|
// GetLanguagesByContent returns a slice of languages for the given content.
|
2019-04-08 16:07:10 +02:00
|
|
|
// It is a Strategy that uses content-based regexp heuristics and a filename extension.
|
2017-08-08 14:58:01 +02:00
|
|
|
func GetLanguagesByContent(filename string, content []byte, _ []string) []string {
|
2017-12-07 16:45:19 +01:00
|
|
|
if filename == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-29 10:05:16 +02:00
|
|
|
ext := strings.ToLower(filepath.Ext(filename))
|
2019-02-14 12:47:45 +01:00
|
|
|
|
|
|
|
heuristic, ok := data.ContentHeuristics[ext]
|
2017-05-31 12:07:46 +02:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:47:45 +01:00
|
|
|
return heuristic.Match(content)
|
2017-05-31 12:07:46 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:26:19 +01:00
|
|
|
// GetLanguagesByClassifier returns a sorted slice of possible languages ordered by
|
|
|
|
// decreasing language's probability. If there are not candidates it returns nil.
|
|
|
|
// It is a Strategy that uses a pre-trained defaultClassifier.
|
2017-06-12 13:42:20 +02:00
|
|
|
func GetLanguagesByClassifier(filename string, content []byte, candidates []string) (languages []string) {
|
|
|
|
if len(candidates) == 0 {
|
|
|
|
return nil
|
2017-05-31 12:07:46 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 17:56:13 +01:00
|
|
|
return getLanguagesBySpecificClassifier(content, candidates, defaultClassifier)
|
2017-05-31 12:07:46 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 17:56:13 +01:00
|
|
|
// getLanguagesBySpecificClassifier returns a slice of possible languages. It takes in a Classifier to be used.
|
|
|
|
func getLanguagesBySpecificClassifier(content []byte, candidates []string, classifier classifier) (languages []string) {
|
2017-06-12 13:42:20 +02:00
|
|
|
mapCandidates := make(map[string]float64)
|
|
|
|
for _, candidate := range candidates {
|
|
|
|
mapCandidates[candidate]++
|
2017-05-31 12:07:46 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 17:56:13 +01:00
|
|
|
return classifier.classify(content, mapCandidates)
|
2017-05-29 10:05:16 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:26:19 +01:00
|
|
|
// GetLanguageExtensions returns all extensions associated with the given language.
|
2017-05-29 10:05:16 +02:00
|
|
|
func GetLanguageExtensions(language string) []string {
|
2017-06-08 12:28:36 +02:00
|
|
|
return data.ExtensionsByLanguage[language]
|
2017-05-29 10:05:16 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 20:49:21 +02:00
|
|
|
// GetLanguageID returns the ID for the language. IDs are assigned by GitHub.
|
2021-04-23 01:55:42 +02:00
|
|
|
// The input must be the canonical language name. Aliases are not supported.
|
2021-04-13 20:49:21 +02:00
|
|
|
//
|
2021-04-23 01:55:42 +02:00
|
|
|
// NOTE: The zero value (0) is a valid language ID, so this API mimics the Go
|
|
|
|
// map API. Use the second return value to check if the language was found.
|
|
|
|
func GetLanguageID(language string) (int, bool) {
|
|
|
|
id, ok := data.IDByLanguage[language]
|
|
|
|
return id, ok
|
2021-04-13 20:49:21 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 10:05:16 +02:00
|
|
|
// Type represent language's type. Either data, programming, markup, prose, or unknown.
|
|
|
|
type Type int
|
|
|
|
|
|
|
|
// Type's values.
|
|
|
|
const (
|
2021-10-11 22:32:29 +02:00
|
|
|
Unknown Type = Type(data.TypeUnknown)
|
|
|
|
Data = Type(data.TypeData)
|
|
|
|
Programming = Type(data.TypeProgramming)
|
|
|
|
Markup = Type(data.TypeMarkup)
|
|
|
|
Prose = Type(data.TypeProse)
|
2017-05-29 10:05:16 +02:00
|
|
|
)
|
|
|
|
|
2017-06-12 13:42:20 +02:00
|
|
|
// GetLanguageType returns the type of the given language.
|
2017-05-29 10:05:16 +02:00
|
|
|
func GetLanguageType(language string) (langType Type) {
|
2017-06-08 12:28:36 +02:00
|
|
|
intType, ok := data.LanguagesType[language]
|
|
|
|
langType = Type(intType)
|
2017-05-29 10:05:16 +02:00
|
|
|
if !ok {
|
|
|
|
langType = Unknown
|
|
|
|
}
|
|
|
|
return langType
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLanguageByAlias returns either the language related to the given alias and ok set to true
|
|
|
|
// or Otherlanguage and ok set to false if the alias is not recognized.
|
|
|
|
func GetLanguageByAlias(alias string) (lang string, ok bool) {
|
2019-02-14 12:47:45 +01:00
|
|
|
lang, ok = data.LanguageByAlias(alias)
|
2017-05-29 10:05:16 +02:00
|
|
|
if !ok {
|
|
|
|
lang = OtherLanguage
|
2016-07-13 19:05:09 +02:00
|
|
|
}
|
2016-07-18 16:20:12 +02:00
|
|
|
|
2016-07-13 19:05:09 +02:00
|
|
|
return
|
|
|
|
}
|
2020-03-21 14:37:39 +01:00
|
|
|
|
|
|
|
// GetLanguageGroup returns language group or empty string if language does not have group.
|
|
|
|
func GetLanguageGroup(language string) string {
|
|
|
|
if group, ok := data.LanguagesGroup[language]; ok {
|
|
|
|
return group
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
2021-10-11 22:32:29 +02:00
|
|
|
|
|
|
|
// GetLanguageInfo returns the LanguageInfo for a given language name, or an error if not found.
|
|
|
|
func GetLanguageInfo(language string) (data.LanguageInfo, error) {
|
2021-10-12 22:29:39 +02:00
|
|
|
id, ok := GetLanguageID(language)
|
|
|
|
if !ok {
|
|
|
|
return data.LanguageInfo{}, fmt.Errorf("language %q not found", language)
|
2021-10-11 22:32:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-12 22:29:39 +02:00
|
|
|
return GetLanguageInfoByID(id)
|
2021-10-11 22:32:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 01:20:14 +02:00
|
|
|
// GetLanguageInfoByID returns the LanguageInfo for a given language ID, or an error if not found.
|
2021-10-11 22:32:29 +02:00
|
|
|
func GetLanguageInfoByID(id int) (data.LanguageInfo, error) {
|
|
|
|
if info, ok := data.LanguageInfoByID[id]; ok {
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.LanguageInfo{}, fmt.Errorf("language %q not found", id)
|
|
|
|
}
|