-
Notifications
You must be signed in to change notification settings - Fork 10
/
languages.go
192 lines (155 loc) · 3.85 KB
/
languages.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package gfmrun
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
var (
DefaultLanguagesYml = filepath.Join(getCacheDir(), "languages.yml")
DefaultLanguagesYmlURL = "https://raw.githubusercontent.com/github/linguist/master/lib/linguist/languages.yml"
)
type Languages struct {
Map map[string]*LanguageDefinition
}
func (l *Languages) denormalize() {
for key, def := range l.Map {
func() {
lowkey := strings.ToLower(key)
def.Name = lowkey
if def.Aliases == nil {
def.Aliases = []string{}
}
for _, alias := range def.Aliases {
if alias == lowkey {
return
}
}
def.Aliases = append(def.Aliases, lowkey)
}()
for _, alias := range def.Aliases {
l.Map[alias] = &LanguageDefinition{
Name: alias,
Type: def.Type,
Aliases: []string{},
Interpreters: def.Interpreters,
AceMode: def.AceMode,
Group: def.Group,
Canonical: def,
}
}
l.Map[def.Name] = def
}
}
func (l *Languages) Lookup(identifier string) *LanguageDefinition {
lowerIdent := strings.ToLower(identifier)
for key, def := range l.Map {
if key == lowerIdent {
return selfOrCanonical(def)
}
}
for _, def := range l.Map {
if def.Aliases == nil {
def.Aliases = []string{}
}
for _, alias := range def.Aliases {
if alias == lowerIdent {
return selfOrCanonical(def)
}
}
if def.AceMode != "" && def.AceMode == lowerIdent {
return selfOrCanonical(def)
}
if def.Group != "" && strings.ToLower(def.Group) == lowerIdent {
return selfOrCanonical(def)
}
}
return nil
}
func selfOrCanonical(def *LanguageDefinition) *LanguageDefinition {
if def.Canonical != nil {
return def.Canonical
}
return def
}
type LanguageDefinition struct {
Name string `json:"-"`
Type string `json:"type,omitempty" yaml:"type"`
Aliases []string `json:"aliases,omitempty" yaml:"aliases"`
Interpreters []string `json:"interpreters,omitempty" yaml:"interpreters"`
AceMode string `json:"ace_mode,omitempty" yaml:"ace_mode"`
Group string `json:"group,omitempty" yaml:"group"`
Canonical *LanguageDefinition `json:"-"`
}
func LoadLanguages(languagesYml string) (*Languages, error) {
if languagesYml == "" {
languagesYml = DefaultLanguagesYml
}
rawBytes, err := os.ReadFile(languagesYml)
if err != nil {
return nil, err
}
return loadLanguagesFromBytes(rawBytes)
}
func loadLanguagesFromBytes(languagesYmlBytes []byte) (*Languages, error) {
m := map[string]*LanguageDefinition{}
err := yaml.Unmarshal(languagesYmlBytes, &m)
if err != nil {
return nil, err
}
langs := &Languages{Map: m}
langs.denormalize()
return langs, nil
}
func PullLanguagesYml(srcURL, destFile string) error {
if srcURL == "" {
srcURL = DefaultLanguagesYmlURL
}
if destFile == "" {
destFile = DefaultLanguagesYml
}
err := os.MkdirAll(filepath.Dir(destFile), os.FileMode(0750))
if err != nil {
return err
}
resp, err := http.Get(srcURL)
if err != nil {
return err
}
if resp.StatusCode > 299 {
return fmt.Errorf("fetching %q returned status %v", srcURL, resp.StatusCode)
}
respBodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
outTmp, err := os.CreateTemp("", "gfmrun-linguist")
if err != nil {
_ = outTmp.Close()
return err
}
_ = outTmp.Close()
defer func() { _ = os.Remove(outTmp.Name()) }()
err = os.WriteFile(outTmp.Name(), respBodyBytes, os.FileMode(0640))
if err != nil {
return err
}
// Atomic copy of downloaded language file
in, err := os.Open(outTmp.Name())
if err != nil {
return err
}
defer func() { _ = in.Close() }()
out, err := os.Create(destFile)
if err != nil {
return err
}
defer func() { _ = out.Close() }()
if _, err = io.Copy(out, in); err != nil {
return err
}
return out.Sync()
}