More CSS & sorting of T array by keys

This commit is contained in:
Simon Martens
2025-01-16 16:19:19 +01:00
parent e88bb5974a
commit ff310265e4
10 changed files with 190 additions and 40 deletions

View File

@@ -9,15 +9,25 @@ import (
"sync"
)
var embed_cache sync.Map
type Embedder struct {
embed_cache sync.Map
fs fs.FS
}
func NewEmbedder(fs fs.FS) *Embedder {
return &Embedder{
fs: fs,
embed_cache: sync.Map{},
}
}
// INFO: We initialize the cache in both functions, which is only valid if both of these get
// called in the same context, eg. when creating a template engine.
func EmbedSafe(fs fs.FS) func(string) template.HTML {
func (e *Embedder) EmbedSafe() func(string) template.HTML {
return func(path string) template.HTML {
path = strings.TrimSpace(path)
path = filepath.Clean(path)
val, err := getFileData(fs, path)
val, err := e.getFileData(path)
if err != nil {
return template.HTML("")
}
@@ -26,11 +36,11 @@ func EmbedSafe(fs fs.FS) func(string) template.HTML {
}
}
func Embed(fs fs.FS) func(string) string {
func (e *Embedder) Embed() func(string) string {
return func(path string) string {
path = strings.TrimSpace(path)
path = filepath.Clean(path)
val, err := getFileData(fs, path)
val, err := e.getFileData(path)
if err != nil {
return ""
}
@@ -39,16 +49,12 @@ func Embed(fs fs.FS) func(string) string {
}
}
func ClearEmbedCache() {
embed_cache.Clear()
}
func getFileData(fs fs.FS, path string) ([]byte, error) {
if val, ok := embed_cache.Load(path); ok {
func (e *Embedder) getFileData(path string) ([]byte, error) {
if val, ok := e.embed_cache.Load(path); ok {
return val.([]byte), nil
}
f, err := fs.Open(path)
f, err := e.fs.Open(path)
if err != nil {
return nil, err
}
@@ -59,12 +65,11 @@ func getFileData(fs fs.FS, path string) ([]byte, error) {
return nil, err
}
embed_cache.Store(path, data)
e.embed_cache.Store(path, data)
return data, nil
}
func EmbedXSLT(fs fs.FS) func(string) template.HTML {
embed_cache.Clear()
func (e *Embedder) EmbedXSLT() func(string) template.HTML {
return func(path string) template.HTML {
path = strings.TrimSpace(path)
path = filepath.Clean(path)
@@ -76,7 +81,7 @@ func EmbedXSLT(fs fs.FS) func(string) template.HTML {
return template.HTML("[ERROR: " + "file is not an XSLT file" + "]")
}
val, err := getFileData(fs, path)
val, err := e.getFileData(path)
if err != nil {
return template.HTML("[ERROR: " + err.Error() + "]")
}