Add GetLanguageID function

The Linguist-defined language IDs are important to our use case because they are
used as database identifiers. This adds a new generator to extract the language
IDs into a map and uses that to implement GetLanguageID.

Because one language has the ID 0, there is no way to tell if a language name is
found or not. If desired, we could add this by returning (string, bool) from
GetLanguageID. But none of the other functions that take language names do this,
so I didn't want to introduce it here.
This commit is contained in:
Luke Francl
2021-04-13 11:49:21 -07:00
parent 7f5d84ad74
commit eb043e80a8
7 changed files with 676 additions and 0 deletions

View File

@ -0,0 +1,7 @@
package data
var IDByLanguage = map[string]int{
{{range $language, $id := . -}}
"{{$language}}": {{$id -}},
{{end -}}
}

View File

@ -0,0 +1,48 @@
package generator
import (
"bytes"
"io"
"io/ioutil"
yaml "gopkg.in/yaml.v2"
)
// ID generates a map in Go with language name -> language ID.
// It is of generator.File type.
func ID(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) error {
data, err := ioutil.ReadFile(fileToParse)
if err != nil {
return err
}
languages := make(map[string]*languageInfo)
if err := yaml.Unmarshal(data, &languages); err != nil {
return err
}
langMimeMap := buildLanguageIDMap(languages)
buf := &bytes.Buffer{}
if err := executeIDTemplate(buf, langMimeMap, tmplPath, tmplName, commit); err != nil {
return err
}
return formatedWrite(outPath, buf.Bytes())
}
func buildLanguageIDMap(languages map[string]*languageInfo) map[string]int {
langIDMap := make(map[string]int)
for lang, info := range languages {
// NOTE: 0 is a valid language ID so checking the zero value would skip one language
if info.LanguageID != nil {
langIDMap[lang] = *info.LanguageID
}
}
return langIDMap
}
func executeIDTemplate(out io.Writer, langIDMap map[string]int, tmplPath, tmplName, commit string) error {
return executeTemplate(out, tmplName, tmplPath, commit, nil, langIDMap)
}

View File

@ -11,6 +11,7 @@ type languageInfo struct {
Interpreters []string `yaml:"interpreters,omitempty,flow"`
Filenames []string `yaml:"filenames,omitempty,flow"`
MimeType string `yaml:"codemirror_mime_type,omitempty,flow"`
LanguageID *int `yaml:"language_id,omitempty"`
}
func getAlphabeticalOrderedKeys(languages map[string]*languageInfo) []string {

View File

@ -85,6 +85,11 @@ var (
groupsTmplPath = filepath.Join(assetsDir, "groups.go.tmpl")
groupsTmpl = "groups.go.tmpl"
// id.go generation
idFile = "data/id.go"
idTmplPath = "internal/code-generator/assets/id.go.tmpl"
idTmpl = "id.go.tmpl"
commitPath = filepath.Join(".linguist", ".git", "HEAD")
)
@ -118,6 +123,7 @@ func main() {
{generator.MimeType, languagesYAML, "", mimeTypeFile, mimeTypeTmplPath, mimeTypeTmpl, commit},
{generator.Colors, languagesYAML, "", colorsFile, colorsTmplPath, colorsTmpl, commit},
{generator.Groups, languagesYAML, "", groupsFile, groupsTmplPath, groupsTmpl, commit},
{generator.ID, languagesYAML, "", idFile, idTmplPath, idTmpl, commit},
}
for _, file := range fileList {