| 1 |
package helpers |
| 2 |
|
| 3 |
import ( |
| 4 |
"sort" |
| 5 |
|
| 6 |
"github.com/alecthomas/chroma/v2/lexers" |
| 7 |
"github.com/alecthomas/chroma/v2/styles" |
| 8 |
"github.com/gohugoio/hugo/docshelper" |
| 9 |
) |
| 10 |
|
| 11 |
// This is just a helper used to create some JSON used in the Hugo docs. |
| 12 |
func init() { |
| 13 |
docsProvider := func() docshelper.DocProvider { |
| 14 |
var chromaLexers []any |
| 15 |
|
| 16 |
sort.Sort(lexers.GlobalLexerRegistry.Lexers) |
| 17 |
|
| 18 |
for _, l := range lexers.GlobalLexerRegistry.Lexers { |
| 19 |
|
| 20 |
config := l.Config() |
| 21 |
|
| 22 |
lexerEntry := struct { |
| 23 |
Name string |
| 24 |
Aliases []string |
| 25 |
}{ |
| 26 |
config.Name, |
| 27 |
config.Aliases, |
| 28 |
} |
| 29 |
|
| 30 |
chromaLexers = append(chromaLexers, lexerEntry) |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
var styleInfos []styleInfo |
| 35 |
|
| 36 |
for name := range styles.Registry { |
| 37 |
style := styles.Registry[name] |
| 38 |
|
| 39 |
styleInfos = append(styleInfos, styleInfo{ |
| 40 |
Name: name, |
| 41 |
Counterpart: style.Counterpart, |
| 42 |
Mode: style.Mode().String(), |
| 43 |
}) |
| 44 |
} |
| 45 |
|
| 46 |
sort.Slice(styleInfos, func(i, j int) bool { |
| 47 |
return styleInfos[i].Name < styleInfos[j].Name |
| 48 |
}) |
| 49 |
|
| 50 |
return docshelper.DocProvider{"chroma": map[string]any{ |
| 51 |
"lexers": chromaLexers, |
| 52 |
"styles": styleInfos, |
| 53 |
}} |
| 54 |
} |
| 55 |
|
| 56 |
docshelper.AddDocProviderFunc(docsProvider) |
| 57 |
} |
| 58 |
|
| 59 |
type styleInfo struct { |
| 60 |
Name string `json:"name"` |
| 61 |
Mode string `json:"mode"` |
| 62 |
Counterpart string `json:"counterpart"` |
| 63 |
} |