diff --git a/common.go b/common.go index 73bbfb58..949db714 100644 --- a/common.go +++ b/common.go @@ -455,9 +455,8 @@ func GetLanguageType(language string) (langType Type) { // 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) { - a := strings.Split(alias, `,`)[0] - a = strings.ToLower(a) - lang, ok = data.LanguagesByAlias[a] + a := strings.SplitN(alias, `,`, 2)[0] + lang, ok = data.LanguagesByAlias(a) if !ok { lang = OtherLanguage } diff --git a/data/alias.go b/data/alias.go index 5c066598..8fc3846e 100644 --- a/data/alias.go +++ b/data/alias.go @@ -3,9 +3,11 @@ package data -// LanguagesByAlias keeps alias for different languages and use the name of the languages as an alias too. +import "strings" + +// LanguagesByAliasMap keeps alias for different languages and use the name of the languages as an alias too. // All the keys (alias or not) are written in lower case and the whitespaces has been replaced by underscores. -var LanguagesByAlias = map[string]string{ +var LanguagesByAliasMap = map[string]string{ "1c_enterprise": "1C Enterprise", "abap": "ABAP", "abl": "OpenEdge ABL", @@ -727,3 +729,25 @@ var LanguagesByAlias = map[string]string{ "zimpl": "Zimpl", "zsh": "Shell", } + +// LanguagesByAlias looks up the language name by it's alias or name. +// It mirrors the logic of github linguist and is needed e.g for heuristcs.yml +// that mixes names and aliases in a language field (see XPM example). +func LanguagesByAlias(langOrAlias string) (lang string, ok bool) { + k := ConvertToAliasKey(langOrAlias) + lang, ok = LanguagesByAliasMap[k] + return +} + +// ConvertToAliasKey converts language name to a key in LanguagesByAliasMap. +// Following +// - internal.code-generator.generator.convertToAliasKey() +// - GetLanguageByAlias() +// conventions. +// It is here to avoid dependency on "generate" and "enry" packages. +func ConvertToAliasKey(langName string) string { + ak := strings.SplitN(langName, `,`, 2)[0] + ak = strings.Replace(ak, ` `, `_`, -1) + ak = strings.ToLower(ak) + return ak +} diff --git a/data/heuristics.go b/data/heuristics.go index a45e8e79..dc3663d4 100644 --- a/data/heuristics.go +++ b/data/heuristics.go @@ -22,7 +22,13 @@ func (h *Heuristics) Match(data []byte) []string { var matchedLangs []string for _, matcher := range *h { if matcher.Match(data) { - for _, lang := range matcher.(Rule).GetLanguages() { + for _, langOrAlias := range matcher.(Rule).GetLanguages() { + lang, ok := LanguagesByAlias(langOrAlias) + if !ok { // should never happen + // language name/alias in heuristics.yml is not consistent with languages.yml + // but we do not surface any error on the API + continue + } matchedLangs = append(matchedLangs, lang) } break diff --git a/internal/code-generator/assets/alias.go.tmpl b/internal/code-generator/assets/alias.go.tmpl index d4d6a0ce..84af4b57 100644 --- a/internal/code-generator/assets/alias.go.tmpl +++ b/internal/code-generator/assets/alias.go.tmpl @@ -1,9 +1,34 @@ package data -// LanguagesByAlias keeps alias for different languages and use the name of the languages as an alias too. +import "strings" + +// LanguagesByAliasMap keeps alias for different languages and use the name of the languages as an alias too. // All the keys (alias or not) are written in lower case and the whitespaces has been replaced by underscores. -var LanguagesByAlias = map[string]string{ +var LanguagesByAliasMap = map[string]string{ {{range $alias, $language := . -}} - "{{ $alias }}": {{ printf "%q" $language -}}, + "{{ $alias }}": {{ printf "%q" $language -}}, {{end -}} } + +// LanguagesByAlias looks up the language name by it's alias or name. +// It mirrors the logic of github linguist and is needed e.g for heuristcs.yml +// that mixes names and aliases in a language field (see XPM example). +func LanguagesByAlias(langOrAlias string) (lang string, ok bool) { + k := ConvertToAliasKey(langOrAlias) + lang, ok = LanguagesByAliasMap[k] + return +} + + +// ConvertToAliasKey converts language name to a key in LanguagesByAliasMap. +// Following +// - internal.code-generator.generator.convertToAliasKey() +// - GetLanguageByAlias() +// conventions. +// It is here to avoid dependency on "generate" and "enry" packages. +func ConvertToAliasKey(langName string) string { + ak := strings.SplitN(langName, `,`, 2)[0] + ak = strings.Replace(ak, ` `, `_`, -1) + ak = strings.ToLower(ak) + return ak +}