diff options
Diffstat (limited to 'src/templates.go')
-rw-r--r-- | src/templates.go | 129 |
1 files changed, 92 insertions, 37 deletions
diff --git a/src/templates.go b/src/templates.go index d6b82b8..076f8dc 100644 --- a/src/templates.go +++ b/src/templates.go @@ -1,11 +1,11 @@ package app import ( - "bytes" "html/template" "io/fs" "log" "strings" + "time" . "git.thomasvoss.com/euro-cash.eu/pkg/try" "git.thomasvoss.com/euro-cash.eu/pkg/watch" @@ -15,12 +15,14 @@ import ( ) type templateData struct { - Debugp bool - Printer i18n.Printer - Printers map[string]i18n.Printer - Code, Type string - Mintages dbx.MintageData - Countries []country + Debugp bool + Printer i18n.Printer + Printers map[string]i18n.Printer + Code, Type, FilterBy string + Year int + CountryMintages dbx.CountryMintageData + YearMintages dbx.YearMintageData + Countries []country } var ( @@ -28,35 +30,51 @@ var ( errorTmpl *template.Template templates map[string]*template.Template funcmap = map[string]any{ - "ifElse": ifElse, - "locales": i18n.Locales, - "map": templateMakeMap, - "safe": asHTML, - "toUpper": strings.ToUpper, - "tuple": templateMakeTuple, - "makeHeaderWithTranslations": makeHeaderWithTranslations, + "ifElse": ifElse, + "locales": i18n.Locales, + "map": templateMakeMap, + "plus1": plus1, + "safe": asHTML, + "toString": toString, + "toUpper": strings.ToUpper, + "tuple": templateMakeTuple, + "withTranslation": withTranslation, + "withTranslations": withTranslations, + "years": years, } ) func BuildTemplates(dir fs.FS) { + buildTemplates(dir, Debugp) +} + +func buildTemplates(dir fs.FS, debugp bool) { ents := Try2(fs.ReadDir(dir, ".")) notFoundTmpl = buildTemplate(dir, "-404") errorTmpl = buildTemplate(dir, "-error") templates = make(map[string]*template.Template, len(ents)) for _, e := range ents { + if e.IsDir() { + continue + } name := e.Name() buildAndSetTemplate(dir, name) - if Debugp { + if debugp { go watch.FileFS(dir, name, func() { defer func() { if p := recover(); p != nil { - log.Print(p) + log.Println(p) } }() - buildAndSetTemplate(dir, name) - log.Printf("Template ā%sā updated\n", name) + if strings.HasPrefix(name, "-") { + buildTemplates(dir, false) + log.Println("All templates updated") + } else { + buildAndSetTemplate(dir, name) + log.Printf("Template ā%sā updated\n", name) + } }) } } @@ -79,15 +97,20 @@ func buildTemplate(dir fs.FS, name string) *template.Template { for i, s := range names { names[i] = s + ".html.tmpl" } - t := template.New("-base.html.tmpl").Funcs(funcmap) - t = t.Funcs(includeIfExists(t)) - return template.Must(t.ParseFS(dir, names[:]...)) + return template.Must(template. + New("-base.html.tmpl"). + Funcs(funcmap). + ParseFS(dir, names[:]...)) } func asHTML(s string) template.HTML { return template.HTML(s) } +func toString(s template.HTML) string { + return string(s) +} + func templateMakeTuple(args ...any) []any { return args } @@ -109,20 +132,6 @@ func templateMakeMap(args ...any) map[string]any { return m } -func includeIfExists(tmpl *template.Template) template.FuncMap { - return template.FuncMap{ - "includeIfExists": func(name string, data any) (template.HTML, error) { - t := tmpl.Lookup(name) - if t == nil { - return "", nil - } - var buf bytes.Buffer - err := t.Execute(&buf, data) - return template.HTML(buf.String()), err - }, - } -} - func ifElse(b bool, x, y any) any { if b { return x @@ -130,8 +139,37 @@ func ifElse(b bool, x, y any) any { return y } -func makeHeaderWithTranslations(tag string, text string, - translations ...[]any) template.HTML { +func withTranslation(tag, bcp, text string, trans template.HTML, + spanAttrs ...string) template.HTML { + name, _, _ := strings.Cut(tag, " ") + + var bob strings.Builder + bob.WriteByte('<') + bob.WriteString(tag) + bob.WriteString(`><span lang="`) + bob.WriteString(bcp) + bob.WriteString(`">`) + bob.WriteString(text) + bob.WriteString("</span>") + + if text != string(trans) { + bob.WriteString(`<br><span class="translation"`) + for _, s := range spanAttrs { + bob.WriteByte(' ') + bob.WriteString(s) + } + bob.WriteByte('>') + bob.WriteString(string(trans)) + bob.WriteString("</span>") + } + + bob.WriteString("</") + bob.WriteString(name) + bob.WriteByte('>') + return template.HTML(bob.String()) +} + +func withTranslations(tag string, text string, translations ...[]any) template.HTML { var bob strings.Builder bob.WriteByte('<') bob.WriteString(tag) @@ -161,10 +199,27 @@ func makeHeaderWithTranslations(tag string, text string, return template.HTML(bob.String()) } +func plus1(x int) int { + return x + 1 +} + +func years() []int { + sy, ey := 1999, time.Now().Year() + xs := make([]int, ey-sy+1) + for i := range xs { + xs[i] = sy + i + } + return xs +} + func (td templateData) Get(fmt string, args ...map[string]any) template.HTML { return template.HTML(td.Printer.Get(fmt, args...)) } +func (td templateData) GetC(fmt, ctx string, args ...map[string]any) template.HTML { + return template.HTML(td.Printer.GetC(fmt, ctx, args...)) +} + func (td templateData) GetN(fmtS, fmtP string, n int, args ...map[string]any) template.HTML { return template.HTML(td.Printer.GetN(fmtS, fmtP, n, args...)) } |