git.templeos.me/xultist/go-enry/v2@v2.0.0-20230215093429-6ef3e87f47c0/internal/code-generator/assets/alias.go.tmpl (about)

     1  package data
     2  
     3  import "strings"
     4  
     5  // LanguageByAliasMap keeps alias for different languages and use the name of the languages as an alias too.
     6  // All the keys (alias or not) are written in lower case and the whitespaces has been replaced by underscores.
     7  var LanguageByAliasMap = map[string]string{
     8  	{{range $alias, $language := . -}}
     9  		"{{ $alias }}":	{{ printf "%q" $language -}},
    10  	{{end -}}
    11  }
    12  
    13  // LanguageByAlias looks up the language name by it's alias or name.
    14  // It mirrors the logic of github linguist and is needed e.g for heuristcs.yml
    15  // that mixes names and aliases in a language field (see XPM example).
    16  func LanguageByAlias(langOrAlias string) (lang string, ok bool) {
    17  	k := convertToAliasKey(langOrAlias)
    18  	lang, ok = LanguageByAliasMap[k]
    19  	return
    20  }
    21  
    22  
    23  // convertToAliasKey converts language name to a key in LanguageByAliasMap.
    24  // Following
    25  //   - internal.code-generator.generator.convertToAliasKey()
    26  //   - GetLanguageByAlias()
    27  //
    28  // conventions.
    29  // It is here to avoid dependency on "generate" and "enry" packages.
    30  func convertToAliasKey(langName string) string {
    31  	ak := strings.SplitN(langName, `,`, 2)[0]
    32  	ak = strings.Replace(ak, ` `, `_`, -1)
    33  	ak = strings.ToLower(ak)
    34  	return ak
    35  }