forked from alecthomas/chroma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
187 lines (176 loc) · 4.24 KB
/
registry.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
package chroma
import (
"path/filepath"
"sort"
"strings"
)
var (
ignoredSuffixes = [...]string{
// Editor backups
"~", ".bak", ".old", ".orig",
// Debian and derivatives apt/dpkg/ucf backups
".dpkg-dist", ".dpkg-old", ".ucf-dist", ".ucf-new", ".ucf-old",
// Red Hat and derivatives rpm backups
".rpmnew", ".rpmorig", ".rpmsave",
// Build system input/template files
".in",
}
)
// LexerRegistry is a registry of Lexers.
type LexerRegistry struct {
Lexers Lexers
byName map[string]Lexer
byAlias map[string]Lexer
}
// NewLexerRegistry creates a new LexerRegistry of Lexers.
func NewLexerRegistry() *LexerRegistry {
return &LexerRegistry{
byName: map[string]Lexer{},
byAlias: map[string]Lexer{},
}
}
// Names of all lexers, optionally including aliases.
func (l *LexerRegistry) Names(withAliases bool) []string {
out := []string{}
for _, lexer := range l.Lexers {
config := lexer.Config()
out = append(out, config.Name)
if withAliases {
out = append(out, config.Aliases...)
}
}
sort.Strings(out)
return out
}
// Get a Lexer by name, alias or file extension.
func (l *LexerRegistry) Get(name string) Lexer {
if lexer := l.byName[name]; lexer != nil {
return lexer
}
if lexer := l.byAlias[name]; lexer != nil {
return lexer
}
if lexer := l.byName[strings.ToLower(name)]; lexer != nil {
return lexer
}
if lexer := l.byAlias[strings.ToLower(name)]; lexer != nil {
return lexer
}
candidates := PrioritisedLexers{}
// Try file extension.
if lexer := l.Match("filename." + name); lexer != nil {
candidates = append(candidates, lexer)
}
// Try exact filename.
if lexer := l.Match(name); lexer != nil {
candidates = append(candidates, lexer)
}
if len(candidates) == 0 {
return nil
}
sort.Sort(candidates)
return candidates[0]
}
// MatchMimeType attempts to find a lexer for the given MIME type.
func (l *LexerRegistry) MatchMimeType(mimeType string) Lexer {
matched := PrioritisedLexers{}
for _, l := range l.Lexers {
for _, lmt := range l.Config().MimeTypes {
if mimeType == lmt {
matched = append(matched, l)
}
}
}
if len(matched) != 0 {
sort.Sort(matched)
return matched[0]
}
return nil
}
// Match returns the first lexer matching filename.
func (l *LexerRegistry) Match(filename string) Lexer {
filename = filepath.Base(filename)
matched := PrioritisedLexers{}
// First, try primary filename matches.
for _, lexer := range l.Lexers {
config := lexer.Config()
for _, glob := range config.Filenames {
ok, err := filepath.Match(glob, filename)
if err != nil { // nolint
panic(err)
} else if ok {
matched = append(matched, lexer)
} else {
for _, suf := range &ignoredSuffixes {
ok, err := filepath.Match(glob+suf, filename)
if err != nil {
panic(err)
} else if ok {
matched = append(matched, lexer)
break
}
}
}
}
}
if len(matched) > 0 {
sort.Sort(matched)
return matched[0]
}
matched = nil
// Next, try filename aliases.
for _, lexer := range l.Lexers {
config := lexer.Config()
for _, glob := range config.AliasFilenames {
ok, err := filepath.Match(glob, filename)
if err != nil { // nolint
panic(err)
} else if ok {
matched = append(matched, lexer)
} else {
for _, suf := range &ignoredSuffixes {
ok, err := filepath.Match(glob+suf, filename)
if err != nil {
panic(err)
} else if ok {
matched = append(matched, lexer)
break
}
}
}
}
}
if len(matched) > 0 {
sort.Sort(matched)
return matched[0]
}
return nil
}
// Analyse text content and return the "best" lexer..
func (l *LexerRegistry) Analyse(text string) Lexer {
var picked Lexer
highest := float32(0.0)
for _, lexer := range l.Lexers {
if analyser, ok := lexer.(Analyser); ok {
weight := analyser.AnalyseText(text)
if weight > highest {
picked = lexer
highest = weight
}
}
}
return picked
}
// Register a Lexer with the LexerRegistry.
func (l *LexerRegistry) Register(lexer Lexer) Lexer {
lexer.SetRegistry(l)
config := lexer.Config()
l.byName[config.Name] = lexer
l.byName[strings.ToLower(config.Name)] = lexer
for _, alias := range config.Aliases {
l.byAlias[alias] = lexer
l.byAlias[strings.ToLower(alias)] = lexer
}
l.Lexers = append(l.Lexers, lexer)
return lexer
}