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:
99
internal/code-generator/assets/languageInfo.go.tmpl
Normal file
99
internal/code-generator/assets/languageInfo.go.tmpl
Normal file
@ -0,0 +1,99 @@
|
||||
package data
|
||||
|
||||
// LanguageInfo exposes the data for a language's Linguist YAML entry as a Go struct.
|
||||
// See https://github.com/github/linguist/blob/master/lib/linguist/languages.yml
|
||||
type LanguageInfo struct {
|
||||
Name string
|
||||
FSName string
|
||||
Type Type
|
||||
Color string
|
||||
Group string
|
||||
Aliases []string
|
||||
Extensions []string
|
||||
Interpreters []string
|
||||
Filenames []string
|
||||
MimeType string
|
||||
TMScope string
|
||||
AceMode string
|
||||
CodemirrorMode string
|
||||
Wrap bool
|
||||
LanguageID int
|
||||
}
|
||||
|
||||
// LanguageInfoByName allows accessing LanguageInfo by a language's primary name.
|
||||
var LanguageInfoByName = map[string]LanguageInfo{
|
||||
{{range $language, $info := . -}}
|
||||
"{{$language}}": LanguageInfo{
|
||||
Name: "{{$language}}",
|
||||
FSName: "{{$info.FSName}}",
|
||||
Type: TypeForString("{{$info.Type}}"),
|
||||
Color: "{{$info.Color}}",
|
||||
Group: "{{$info.Group}}",
|
||||
Aliases: []string{
|
||||
{{range $alias := $info.Aliases -}}
|
||||
"{{$alias}}",
|
||||
{{end -}}
|
||||
},
|
||||
Extensions: []string{
|
||||
{{range $extension := $info.Extensions -}}
|
||||
"{{$extension}}",
|
||||
{{end -}}
|
||||
},
|
||||
Interpreters: []string{
|
||||
{{range $interpreter := $info.Interpreters -}}
|
||||
"{{$interpreter}}",
|
||||
{{end -}}
|
||||
},
|
||||
Filenames: []string{
|
||||
{{range $filename := $info.Filenames -}}
|
||||
"{{$filename}}",
|
||||
{{end -}}
|
||||
},
|
||||
MimeType: "{{$info.MimeType}}",
|
||||
TMScope: "{{$info.TMScope}}",
|
||||
AceMode: "{{$info.AceMode}}",
|
||||
CodemirrorMode: "{{$info.CodemirrorMode}}",
|
||||
Wrap: {{$info.Wrap}},
|
||||
LanguageID: {{$info.LanguageID}},
|
||||
},
|
||||
{{end -}}
|
||||
}
|
||||
|
||||
// LanguageInfoByID allows accessing LanguageInfo by a language's ID.
|
||||
var LanguageInfoByID = map[int]LanguageInfo{
|
||||
{{range $language, $info := . -}}
|
||||
{{$info.LanguageID}}: LanguageInfo{
|
||||
Name: "{{$language}}",
|
||||
FSName: "{{$info.FSName}}",
|
||||
Type: TypeForString("{{$info.Type}}"),
|
||||
Color: "{{$info.Color}}",
|
||||
Group: "{{$info.Group}}",
|
||||
Aliases: []string{
|
||||
{{range $alias := $info.Aliases -}}
|
||||
"{{$alias}}",
|
||||
{{end -}}
|
||||
},
|
||||
Extensions: []string{
|
||||
{{range $extension := $info.Extensions -}}
|
||||
"{{$extension}}",
|
||||
{{end -}}
|
||||
},
|
||||
Interpreters: []string{
|
||||
{{range $interpreter := $info.Interpreters -}}
|
||||
"{{$interpreter}}",
|
||||
{{end -}}
|
||||
},
|
||||
Filenames: []string{
|
||||
{{range $filename := $info.Filenames -}}
|
||||
"{{$filename}}",
|
||||
{{end -}}
|
||||
},
|
||||
MimeType: "{{$info.MimeType}}",
|
||||
TMScope: "{{$info.TMScope}}",
|
||||
AceMode: "{{$info.AceMode}}",
|
||||
CodemirrorMode: "{{$info.CodemirrorMode}}",
|
||||
Wrap: {{$info.Wrap}},
|
||||
LanguageID: {{$info.LanguageID}},
|
||||
},
|
||||
{{end -}}
|
||||
}
|
@ -1,5 +1,47 @@
|
||||
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{
|
||||
{{range $language, $type := . -}}
|
||||
"{{ $language }}": {{ $type -}},
|
||||
|
Reference in New Issue
Block a user