summaryrefslogtreecommitdiff
path: root/internal/templatemanager/templatemanager.go
diff refs
from:
to:
flip
diff options
context:
space:
mode:
Diffstat (limited to 'internal/templatemanager/templatemanager.go')
-rw-r--r--internal/templatemanager/templatemanager.go59
1 files changed, 53 insertions, 6 deletions
diff --git a/internal/templatemanager/templatemanager.go b/internal/templatemanager/templatemanager.go
index 75309e4..ff1214a 100644
--- a/internal/templatemanager/templatemanager.go
+++ b/internal/templatemanager/templatemanager.go
@@ -4,8 +4,12 @@ import (
"bytes"
"fmt"
"html/template"
+ "os"
"path/filepath"
"strings"
+ "time"
+
+ "github.com/SayaAndy/saya-today-web/l10n"
)
type TemplateManager struct {
@@ -13,8 +17,9 @@ type TemplateManager struct {
}
type templateManagerRender struct {
- Main string
- Tmpl *template.Template
+ Main string
+ Tmpl *template.Template
+ LastModified time.Time
}
type TemplateManagerTemplates struct {
@@ -32,6 +37,13 @@ var templateFuncMap = template.FuncMap{
return items
},
"replace": strings.ReplaceAll,
+ "fdiv": func(a, b int) float64 {
+ return float64(a) / float64(b)
+ },
+ "l": func(path ...any) any {
+ return l10n.T.GetPath(path...)
+ },
+ "join": strings.Join,
}
func NewTemplateManager(templates ...TemplateManagerTemplates) (*TemplateManager, error) {
@@ -50,10 +62,15 @@ func NewTemplateManager(templates ...TemplateManagerTemplates) (*TemplateManager
if err != nil {
return nil, err
}
+ modTime, err := setLastModified(tmplStruct.Files...)
+ if err != nil {
+ return nil, err
+ }
templateMap[tmplStruct.Name] = templateManagerRender{
- Main: filepath.Base(tmplStruct.Files[0]),
- Tmpl: tmpl,
+ Main: filepath.Base(tmplStruct.Files[0]),
+ Tmpl: tmpl,
+ LastModified: modTime,
}
}
@@ -111,10 +128,40 @@ func (tm *TemplateManager) Add(name string, files ...string) error {
return fmt.Errorf("failed to add template into manager: %w", err)
}
+ modTime, err := setLastModified(files...)
+ if err != nil {
+ return fmt.Errorf("failed to add template into manager: %w", err)
+ }
+
tm.templates[name] = templateManagerRender{
- Main: filepath.Base(files[0]),
- Tmpl: tmpl,
+ Main: filepath.Base(files[0]),
+ Tmpl: tmpl,
+ LastModified: modTime,
}
return nil
}
+
+func (tm *TemplateManager) GetLastModified(name string) (time.Time, error) {
+ tmpl, exists := tm.templates[name]
+ if !exists {
+ return time.Time{}, fmt.Errorf("template %s not found", name)
+ }
+
+ return tmpl.LastModified, nil
+}
+
+func setLastModified(filenames ...string) (time.Time, error) {
+ lastModified := time.Time{}
+ for _, filename := range filenames {
+ stat, err := os.Stat(filename)
+ if err != nil {
+ return lastModified, fmt.Errorf("failed to stat file: path '%s': %w", filename, err)
+ }
+ modTime := stat.ModTime()
+ if lastModified.Before(modTime) {
+ lastModified = modTime
+ }
+ }
+ return lastModified, nil
+}