mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-06-19 06:33:06 -03:00
Expose LanguageInfo with all Linguist data
As discussed in https://github.com/go-enry/go-enry/issues/54, this provides an API for accessing a LanguageInfo struct which is populated with all the data from the Linguist YAML source file. Functions are provided to access the LanguageInfo by name or ID. The other top-level functions like GetLanguageExtensions, GetLanguageGroup, etc. could in principle be implemented using this structure, which would simplify the code generation. But that would be a big change so I didn't do any of that. Perhaps in the next major version something like that would make sense.
This commit is contained in:
@ -1,17 +1,29 @@
|
||||
package generator
|
||||
|
||||
import "sort"
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"sort"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type languageInfo struct {
|
||||
Type string `yaml:"type,omitempty"`
|
||||
Color string `yaml:"color,omitempty"`
|
||||
Group string `yaml:"group,omitempty"`
|
||||
Aliases []string `yaml:"aliases,omitempty"`
|
||||
Extensions []string `yaml:"extensions,omitempty,flow"`
|
||||
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"`
|
||||
FSName string `yaml:"fs_name"`
|
||||
Type string `yaml:"type,omitempty"`
|
||||
Color string `yaml:"color,omitempty"`
|
||||
Group string `yaml:"group,omitempty"`
|
||||
Aliases []string `yaml:"aliases,omitempty"`
|
||||
Extensions []string `yaml:"extensions,omitempty,flow"`
|
||||
Interpreters []string `yaml:"interpreters,omitempty,flow"`
|
||||
Filenames []string `yaml:"filenames,omitempty,flow"`
|
||||
MimeType string `yaml:"codemirror_mime_type,omitempty,flow"`
|
||||
TMScope string `yaml:"tm_scope"`
|
||||
AceMode string `yaml:"ace_mode"`
|
||||
CodemirrorMode string `yaml:"codemirror_mode"`
|
||||
Wrap bool `yaml:"wrap"`
|
||||
LanguageID *int `yaml:"language_id,omitempty"`
|
||||
}
|
||||
|
||||
func getAlphabeticalOrderedKeys(languages map[string]*languageInfo) []string {
|
||||
@ -23,3 +35,28 @@ func getAlphabeticalOrderedKeys(languages map[string]*languageInfo) []string {
|
||||
sort.Strings(keyList)
|
||||
return keyList
|
||||
}
|
||||
|
||||
// LanguageInfo generates maps in Go with language name -> LanguageInfo and language ID -> LanguageInfo.
|
||||
// It is of generator.File type.
|
||||
func LanguageInfo(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
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
if err := executeLanguageInfoTemplate(buf, languages, tmplPath, tmplName, commit); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return formatedWrite(outPath, buf.Bytes())
|
||||
}
|
||||
|
||||
func executeLanguageInfoTemplate(out io.Writer, languages map[string]*languageInfo, tmplPath, tmplName, commit string) error {
|
||||
return executeTemplate(out, tmplName, tmplPath, commit, nil, languages) // TODO: use function map to put language by id?
|
||||
}
|
||||
|
@ -3,6 +3,48 @@
|
||||
|
||||
package data
|
||||
|
||||
// Type represent language's type. Either data, programming, markup, prose, or unknown.
|
||||
type Type int
|
||||
|
||||
// Type's values.
|
||||
const (
|
||||
TypeUnknown Type = iota
|
||||
TypeData
|
||||
TypeProgramming
|
||||
TypeMarkup
|
||||
TypeProse
|
||||
)
|
||||
|
||||
func (t Type) String() string {
|
||||
switch t {
|
||||
case TypeData:
|
||||
return "data"
|
||||
case TypeProgramming:
|
||||
return "programming"
|
||||
case TypeMarkup:
|
||||
return "markup"
|
||||
case TypeProse:
|
||||
return "prose"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func TypeForString(s string) Type {
|
||||
switch s {
|
||||
case "data":
|
||||
return TypeData
|
||||
case "programming":
|
||||
return TypeProgramming
|
||||
case "markup":
|
||||
return TypeMarkup
|
||||
case "prose":
|
||||
return TypeProse
|
||||
default:
|
||||
return TypeUnknown
|
||||
}
|
||||
}
|
||||
|
||||
var LanguagesType = map[string]int{
|
||||
"1C Enterprise": 2,
|
||||
"4D": 2,
|
||||
|
@ -2,9 +2,10 @@ package generator
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var typeToTypeConst = map[string]int{
|
||||
|
Reference in New Issue
Block a user