diff --git a/README.md b/README.md index b1afeca..a271690 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/common.go b/common.go index a1f3f01..a5a4248 100644 --- a/common.go +++ b/common.go @@ -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 "" +} diff --git a/common_test.go b/common_test.go index 19b0d08..a2613e4 100644 --- a/common_test.go +++ b/common_test.go @@ -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 diff --git a/data/groups.go b/data/groups.go new file mode 100644 index 0000000..6cba1b3 --- /dev/null +++ b/data/groups.go @@ -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", +} diff --git a/internal/code-generator/assets/groups.go.tmpl b/internal/code-generator/assets/groups.go.tmpl new file mode 100644 index 0000000..382c31b --- /dev/null +++ b/internal/code-generator/assets/groups.go.tmpl @@ -0,0 +1,7 @@ +package data + +var LanguagesGroup = map[string]string{ + {{range $language, $group := . -}} + "{{$language}}": "{{$group -}}", + {{end -}} +} diff --git a/internal/code-generator/generator/colors.go b/internal/code-generator/generator/colors.go index 4dc2934..e375ea3 100644 --- a/internal/code-generator/generator/colors.go +++ b/internal/code-generator/generator/colors.go @@ -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 } diff --git a/internal/code-generator/generator/generator_test.go b/internal/code-generator/generator/generator_test.go index c9d3ffa..1d919d5 100644 --- a/internal/code-generator/generator/generator_test.go +++ b/internal/code-generator/generator/generator_test.go @@ -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, + }, } } diff --git a/internal/code-generator/generator/groups.go b/internal/code-generator/generator/groups.go new file mode 100644 index 0000000..db2550d --- /dev/null +++ b/internal/code-generator/generator/groups.go @@ -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) +} diff --git a/internal/code-generator/generator/langinfo.go b/internal/code-generator/generator/langinfo.go index e0a8a8b..7d85797 100644 --- a/internal/code-generator/generator/langinfo.go +++ b/internal/code-generator/generator/langinfo.go @@ -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"` diff --git a/internal/code-generator/generator/test_files/groups.gold b/internal/code-generator/generator/test_files/groups.gold new file mode 100644 index 0000000..6cba1b3 --- /dev/null +++ b/internal/code-generator/generator/test_files/groups.gold @@ -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", +} diff --git a/internal/code-generator/main.go b/internal/code-generator/main.go index 9b3a2a9..51fc224 100644 --- a/internal/code-generator/main.go +++ b/internal/code-generator/main.go @@ -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 { diff --git a/utils.go b/utils.go index 179357e..7cb3914 100644 --- a/utils.go +++ b/utils.go @@ -80,5 +80,9 @@ func GetColor(language string) string { return color } + if color, ok := data.LanguagesColor[GetLanguageGroup(language)]; ok { + return color + } + return "#cccccc" } diff --git a/utils_test.go b/utils_test.go index a7cb644..beeaaca 100644 --- a/utils_test.go +++ b/utils_test.go @@ -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 {