Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement getting HTML color code for languages #233

Merged
merged 1 commit into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 244 additions & 0 deletions data/colors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/code-generator/assets/colors.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package data

var LanguagesColor = map[string]string{
{{range $language, $color := . -}}
"{{$language}}": "{{$color -}}",
{{end -}}
}
47 changes: 47 additions & 0 deletions internal/code-generator/generator/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package generator

import (
"bytes"
"io"
"io/ioutil"

"gopkg.in/yaml.v2"
)

// Colors generates a map in Go with language name -> color string.
// It is of generator.File type.
func Colors(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
}

langColorMap := buildLanguageColorMap(languages)

buf := &bytes.Buffer{}
if err := executeMimeTemplate(buf, langColorMap, tmplPath, tmplName, commit); err != nil {
return err
}

return formatedWrite(outPath, buf.Bytes())
}

func buildLanguageColorMap(languages map[string]*languageInfo) map[string]string {
langColorMap := make(map[string]string)
for lang, info := range languages {
if len(info.Color) != 0 {
langColorMap[lang] = info.Color
}
}

return langColorMap
}

func executeColorTemplate(out io.Writer, langColorMap map[string]string, tmplPath, tmplName, commit string) error {
return executeTemplate(out, tmplName, tmplPath, commit, nil, langColorMap)
}
15 changes: 15 additions & 0 deletions internal/code-generator/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ const (
mimeTypeGold = testDir + "/mimeType.gold"
mimeTypeTestTmplPath = assetsDir + "/mimeType.go.tmpl"
mimeTypeTestTmplName = "mimeType.go.tmpl"

// colors test
colorsGold = testDir + "/colors.gold"
colorsTestTmplPath = assetsDir + "/colors.go.tmpl"
colorsTestTmplName = "colors.go.tmpl"
)

type GeneratorTestSuite struct {
Expand Down Expand Up @@ -243,6 +248,16 @@ func (s *GeneratorTestSuite) SetupSuite() {
generate: MimeType,
wantOut: mimeTypeGold,
},
{
name: "Colors()",
fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
samplesDir: "",
tmplPath: colorsTestTmplPath,
tmplName: colorsTestTmplName,
commit: commit,
generate: Colors,
wantOut: colorsGold,
},
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/code-generator/generator/langinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "sort"

type languageInfo struct {
Type string `yaml:"type,omitempty"`
Color string `yaml:"color,omitempty"`
Aliases []string `yaml:"aliases,omitempty"`
Extensions []string `yaml:"extensions,omitempty,flow"`
Interpreters []string `yaml:"interpreters,omitempty,flow"`
Expand Down
Loading