Merge pull request #2 from lafriks-fork/feat/lang_groups

Return group color if language has none
This commit is contained in:
Alexander 2020-03-31 11:17:52 +02:00 committed by GitHub
commit 6c06680bef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 293 additions and 1 deletions

View File

@ -67,6 +67,11 @@ The most accurate guess would be one when both, the file name and the content ar
- `IsDotFile`
- `IsImage`
### Language colors and groups
*enry* exposes function to get language color to use for example in presenting statistics in graphs:
- `GetColor`
- `GetLanguageGroup` can be used to group similar languages together e.g. for `Less` this function will return `CSS`
## Languages
### Go

View File

@ -471,3 +471,12 @@ func GetLanguageByAlias(alias string) (lang string, ok bool) {
return
}
// GetLanguageGroup returns language group or empty string if language does not have group.
func GetLanguageGroup(language string) string {
if group, ok := data.LanguagesGroup[language]; ok {
return group
}
return ""
}

View File

@ -400,6 +400,24 @@ func (s *EnryTestSuite) TestGetLanguageType() {
}
}
func (s *EnryTestSuite) TestGetLanguageGroup() {
tests := []struct {
name string
language string
expected string
}{
{name: "TestGetLanguageGroup_1", language: "BestLanguageEver", expected: ""},
{name: "TestGetLanguageGroup_2", language: "JSX", expected: "JavaScript"},
{name: "TestGetLanguageGroup_3", language: "HTML+PHP", expected: "HTML"},
{name: "TestGetLanguageGroup_4", language: "HTML", expected: ""},
}
for _, test := range tests {
langGroup := GetLanguageGroup(test.language)
assert.Equal(s.T(), test.expected, langGroup, fmt.Sprintf("%v: langGroup = %v, expected: %v", test.name, langGroup, test.expected))
}
}
func (s *EnryTestSuite) TestGetLanguageByAlias() {
tests := []struct {
name string

89
data/groups.go Normal file
View File

@ -0,0 +1,89 @@
// Code generated by github.com/go-enry/go-enry/v2/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: 40992ba7f86889f80dfed3ba95e11e1082200bad
package data
var LanguagesGroup = map[string]string{
"Alpine Abuild": "Shell",
"Apollo Guidance Computer": "Assembly",
"BibTeX": "TeX",
"Bison": "Yacc",
"Blade": "HTML",
"C2hs Haskell": "Haskell",
"Closure Templates": "HTML",
"ColdFusion CFC": "ColdFusion",
"Cython": "Python",
"ECLiPSe": "prolog",
"EJS": "HTML",
"Easybuild": "Python",
"Ecere Projects": "JavaScript",
"EditorConfig": "INI",
"Filterscript": "RenderScript",
"Gentoo Ebuild": "Shell",
"Gentoo Eclass": "Shell",
"Git Attributes": "INI",
"Git Config": "INI",
"Groovy Server Pages": "Groovy",
"HTML+Django": "HTML",
"HTML+ECR": "HTML",
"HTML+EEX": "HTML",
"HTML+ERB": "HTML",
"HTML+PHP": "HTML",
"HTML+Razor": "HTML",
"Haml": "HTML",
"Handlebars": "HTML",
"Ignore List": "INI",
"Isabelle ROOT": "Isabelle",
"JFlex": "Lex",
"JSON with Comments": "JSON",
"JSX": "JavaScript",
"Java Server Pages": "Java",
"JavaScript+ERB": "JavaScript",
"Jison": "Yacc",
"Jison Lex": "Lex",
"Latte": "HTML",
"Less": "CSS",
"Literate Agda": "Agda",
"Literate CoffeeScript": "CoffeeScript",
"Literate Haskell": "Haskell",
"M4Sugar": "M4",
"MUF": "Forth",
"Marko": "HTML",
"Motorola 68K Assembly": "Assembly",
"NPM Config": "INI",
"NumPy": "Python",
"OpenCL": "C",
"OpenRC runscript": "Shell",
"Parrot Assembly": "Parrot",
"Parrot Internal Representation": "Parrot",
"Pic": "Roff",
"PostCSS": "CSS",
"Pug": "HTML",
"Python console": "Python",
"Python traceback": "Python",
"RHTML": "HTML",
"Readline Config": "INI",
"Roff Manpage": "Roff",
"SCSS": "CSS",
"SSH Config": "INI",
"STON": "Smalltalk",
"Sage": "Python",
"Sass": "CSS",
"Scaml": "HTML",
"Slim": "HTML",
"Stylus": "CSS",
"SugarSS": "CSS",
"Svelte": "HTML",
"TSX": "TypeScript",
"Tcsh": "Shell",
"Twig": "HTML",
"Unified Parallel C": "C",
"Unix Assembly": "Assembly",
"Wget Config": "INI",
"X BitMap": "C",
"X PixMap": "C",
"XML Property List": "XML",
"cURL Config": "INI",
"fish": "Shell",
"nanorc": "INI",
}

View File

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

View File

@ -24,7 +24,7 @@ func Colors(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string)
langColorMap := buildLanguageColorMap(languages)
buf := &bytes.Buffer{}
if err := executeMimeTemplate(buf, langColorMap, tmplPath, tmplName, commit); err != nil {
if err := executeColorTemplate(buf, langColorMap, tmplPath, tmplName, commit); err != nil {
return err
}

View File

@ -88,6 +88,11 @@ var (
colorsGold = filepath.Join(testDir, "colors.gold")
colorsTestTmplPath = filepath.Join(assetsDir, "colors.go.tmpl")
colorsTestTmplName = "colors.go.tmpl"
// colors test
groupsGold = filepath.Join(testDir, "groups.gold")
groupsTestTmplPath = filepath.Join(assetsDir, "groups.go.tmpl")
groupsTestTmplName = "groups.go.tmpl"
)
type GeneratorTestSuite struct {
@ -261,6 +266,16 @@ func (s *GeneratorTestSuite) SetupSuite() {
generate: Colors,
wantOut: colorsGold,
},
{
name: "Groups()",
fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
samplesDir: "",
tmplPath: groupsTestTmplPath,
tmplName: groupsTestTmplName,
commit: commit,
generate: Groups,
wantOut: groupsGold,
},
}
}

View File

@ -0,0 +1,47 @@
package generator
import (
"bytes"
"io"
"io/ioutil"
"gopkg.in/yaml.v2"
)
// Groups generates a map in Go with language name -> group name.
// It is of generator.File type.
func Groups(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
}
langGroupMap := buildLanguageGroupMap(languages)
buf := &bytes.Buffer{}
if err := executeGroupTemplate(buf, langGroupMap, tmplPath, tmplName, commit); err != nil {
return err
}
return formatedWrite(outPath, buf.Bytes())
}
func buildLanguageGroupMap(languages map[string]*languageInfo) map[string]string {
langGroupMap := make(map[string]string)
for lang, info := range languages {
if len(info.Group) != 0 {
langGroupMap[lang] = info.Group
}
}
return langGroupMap
}
func executeGroupTemplate(out io.Writer, langColorMap map[string]string, tmplPath, tmplName, commit string) error {
return executeTemplate(out, tmplName, tmplPath, commit, nil, langColorMap)
}

View File

@ -5,6 +5,7 @@ import "sort"
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"`

View File

@ -0,0 +1,89 @@
// Code generated by github.com/go-enry/go-enry/v2/internal/code-generator DO NOT EDIT.
// Extracted from github/linguist commit: 40992ba7f86889f80dfed3ba95e11e1082200bad
package data
var LanguagesGroup = map[string]string{
"Alpine Abuild": "Shell",
"Apollo Guidance Computer": "Assembly",
"BibTeX": "TeX",
"Bison": "Yacc",
"Blade": "HTML",
"C2hs Haskell": "Haskell",
"Closure Templates": "HTML",
"ColdFusion CFC": "ColdFusion",
"Cython": "Python",
"ECLiPSe": "prolog",
"EJS": "HTML",
"Easybuild": "Python",
"Ecere Projects": "JavaScript",
"EditorConfig": "INI",
"Filterscript": "RenderScript",
"Gentoo Ebuild": "Shell",
"Gentoo Eclass": "Shell",
"Git Attributes": "INI",
"Git Config": "INI",
"Groovy Server Pages": "Groovy",
"HTML+Django": "HTML",
"HTML+ECR": "HTML",
"HTML+EEX": "HTML",
"HTML+ERB": "HTML",
"HTML+PHP": "HTML",
"HTML+Razor": "HTML",
"Haml": "HTML",
"Handlebars": "HTML",
"Ignore List": "INI",
"Isabelle ROOT": "Isabelle",
"JFlex": "Lex",
"JSON with Comments": "JSON",
"JSX": "JavaScript",
"Java Server Pages": "Java",
"JavaScript+ERB": "JavaScript",
"Jison": "Yacc",
"Jison Lex": "Lex",
"Latte": "HTML",
"Less": "CSS",
"Literate Agda": "Agda",
"Literate CoffeeScript": "CoffeeScript",
"Literate Haskell": "Haskell",
"M4Sugar": "M4",
"MUF": "Forth",
"Marko": "HTML",
"Motorola 68K Assembly": "Assembly",
"NPM Config": "INI",
"NumPy": "Python",
"OpenCL": "C",
"OpenRC runscript": "Shell",
"Parrot Assembly": "Parrot",
"Parrot Internal Representation": "Parrot",
"Pic": "Roff",
"PostCSS": "CSS",
"Pug": "HTML",
"Python console": "Python",
"Python traceback": "Python",
"RHTML": "HTML",
"Readline Config": "INI",
"Roff Manpage": "Roff",
"SCSS": "CSS",
"SSH Config": "INI",
"STON": "Smalltalk",
"Sage": "Python",
"Sass": "CSS",
"Scaml": "HTML",
"Slim": "HTML",
"Stylus": "CSS",
"SugarSS": "CSS",
"Svelte": "HTML",
"TSX": "TypeScript",
"Tcsh": "Shell",
"Twig": "HTML",
"Unified Parallel C": "C",
"Unix Assembly": "Assembly",
"Wget Config": "INI",
"X BitMap": "C",
"X PixMap": "C",
"XML Property List": "XML",
"cURL Config": "INI",
"fish": "Shell",
"nanorc": "INI",
}

View File

@ -80,6 +80,11 @@ var (
colorsTmplPath = filepath.Join(assetsDir, "colors.go.tmpl")
colorsTmpl = "colors.go.tmpl"
// groups.go generation
groupsFile = filepath.Join("data", "groups.go")
groupsTmplPath = filepath.Join(assetsDir, "groups.go.tmpl")
groupsTmpl = "groups.go.tmpl"
commitPath = filepath.Join(".linguist", ".git", "HEAD")
)
@ -112,6 +117,7 @@ func main() {
{generator.Commit, "", "", commitFile, commitTmplPath, commitTmpl, commit},
{generator.MimeType, languagesYAML, "", mimeTypeFile, mimeTypeTmplPath, mimeTypeTmpl, commit},
{generator.Colors, languagesYAML, "", colorsFile, colorsTmplPath, colorsTmpl, commit},
{generator.Groups, languagesYAML, "", groupsFile, groupsTmplPath, groupsTmpl, commit},
}
for _, file := range fileList {

View File

@ -80,5 +80,9 @@ func GetColor(language string) string {
return color
}
if color, ok := data.LanguagesColor[GetLanguageGroup(language)]; ok {
return color
}
return "#cccccc"
}

View File

@ -142,6 +142,8 @@ func TestGetColor(t *testing.T) {
}{
{name: "TestGetColor_1", language: "Go", expected: "#00ADD8"},
{name: "TestGetColor_2", language: "SomeRandom", expected: "#cccccc"},
{name: "TestGetColor_3", language: "HTML", expected: "#e34c26"},
{name: "TestGetColor_4", language: "HTML+PHP", expected: "#e34c26"},
}
for _, test := range tests {