forked from montanaflynn/github-colors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (107 loc) · 2.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"bytes"
"encoding/base64"
"html/template"
"io/ioutil"
"log"
"net/http"
"sort"
"strings"
colorful "github.com/lucasb-eyer/go-colorful"
yaml "gopkg.in/yaml.v2"
)
type image struct {
LangName string
LangColor string
TextColor string
}
type imageLink struct {
EncodedName string
ImageName string
}
type readme []imageLink
func main() {
// parse templates
tmpl, err := template.ParseFiles("./image.tmpl", "./readme.tmpl")
if err != nil {
log.Fatal(err)
}
// get and decode yaml
baseURL := "https://raw.githubusercontent.com"
req, err := http.Get(baseURL + "/github/linguist/master/lib/linguist/languages.yml")
if err != nil {
log.Fatal(err)
}
defer req.Body.Close()
var languageData map[string]map[string]interface{}
err = yaml.NewDecoder(req.Body).Decode(&languageData)
if err != nil {
log.Fatal(err)
}
// generate svgs and readme
readmeData := readme{}
// sort languages by name
keys := make([]string, len(languageData))
for key := range languageData {
keys = append(keys, key)
}
less := func(i, j int) bool {
return strings.ToLower(keys[i]) < strings.ToLower(keys[j])
}
sort.Slice(keys, less)
// find languages with colors
for _, lang := range keys {
meta := languageData[lang]
if meta["color"] != nil {
color, ok := meta["color"].(string)
if ok {
// check if color is light or dark to determine text color
c, err := colorful.Hex(color)
if err != nil {
log.Fatal(err)
}
_, _, l := c.Hcl()
textColor := "#FFF"
if l > 0.7 {
textColor = "#000"
}
// create svg images with file name as base64
imageName := base64.StdEncoding.EncodeToString([]byte(lang))
svgBuffer := bytes.Buffer{}
img := image{
LangName: lang,
LangColor: color,
TextColor: textColor,
}
err = tmpl.ExecuteTemplate(&svgBuffer, "image.tmpl", img)
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile("./svgs/"+imageName+".svg", svgBuffer.Bytes(), 0644)
if err != nil {
log.Fatal(err)
}
// encode any spaces
encodedName := strings.Replace(lang, " ", "%20", -1)
// encode any single quotes
encodedName = strings.Replace(encodedName, "'", "'", -1)
// add language to readme
readmeData = append(readmeData, imageLink{
EncodedName: encodedName,
ImageName: imageName,
})
}
}
}
readmeBuffer := bytes.Buffer{}
err = tmpl.ExecuteTemplate(&readmeBuffer, "readme.tmpl", readmeData)
if err != nil {
log.Fatal(err)
}
// create README.md
err = ioutil.WriteFile("./README.md", readmeBuffer.Bytes(), 0644)
if err != nil {
log.Fatal(err)
}
}