package styles
import (
"embed"
"io/fs"
"sort"
"strings"
"github.com/alecthomas/chroma/v2"
)
var embedded embed .FS
var Registry = func () map [string ]*chroma .Style {
registry := map [string ]*chroma .Style {}
files , err := fs .ReadDir (embedded , "." )
if err != nil {
panic (err )
}
for _ , file := range files {
if file .IsDir () {
continue
}
r , err := embedded .Open (file .Name ())
if err != nil {
panic (err )
}
style , err := chroma .NewXMLStyle (r )
if err != nil {
panic (err )
}
registry [strings .ToLower (style .Name )] = style
_ = r .Close ()
}
return registry
}()
var Fallback = Registry ["swapoff" ]
func Register (style *chroma .Style ) *chroma .Style {
Registry [strings .ToLower (style .Name )] = style
return style
}
func Names () []string {
out := []string {}
for name := range Registry {
out = append (out , name )
}
sort .Strings (out )
return out
}
func Get (name string ) *chroma .Style {
if style , ok := Registry [strings .ToLower (name )]; ok {
return style
}
return Fallback
}
func GetForMode (name string , mode chroma .Mode ) *chroma .Style {
style := Get (name )
if style .Mode () == mode {
return style
}
if style .Counterpart == "" {
return style
}
counterpart , ok := Registry [style .Counterpart ]
if !ok || counterpart .Mode () != mode {
return style
}
return counterpart
}
func RegisterPair (a , b *chroma .Style ) {
Register (a )
Register (b )
a .Counterpart = strings .ToLower (b .Name )
b .Counterpart = strings .ToLower (a .Name )
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .