Diffstat (limited to 'internal/templatemanager')
| -rw-r--r-- | internal/templatemanager/templatemanager.go | 59 |
1 files changed, 6 insertions, 53 deletions
diff --git a/internal/templatemanager/templatemanager.go b/internal/templatemanager/templatemanager.go index ff1214a..75309e4 100644 --- a/internal/templatemanager/templatemanager.go +++ b/internal/templatemanager/templatemanager.go @@ -4,12 +4,8 @@ import ( "bytes" "fmt" "html/template" - "os" "path/filepath" "strings" - "time" - - "github.com/SayaAndy/saya-today-web/l10n" ) type TemplateManager struct { @@ -17,9 +13,8 @@ type TemplateManager struct { } type templateManagerRender struct { - Main string - Tmpl *template.Template - LastModified time.Time + Main string + Tmpl *template.Template } type TemplateManagerTemplates struct { @@ -37,13 +32,6 @@ 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) { @@ -62,15 +50,10 @@ 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, - LastModified: modTime, + Main: filepath.Base(tmplStruct.Files[0]), + Tmpl: tmpl, } } @@ -128,40 +111,10 @@ 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, - LastModified: modTime, + Main: filepath.Base(files[0]), + Tmpl: tmpl, } 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 -} |