diff options
Diffstat (limited to 'src/templates.go')
-rw-r--r-- | src/templates.go | 107 |
1 files changed, 80 insertions, 27 deletions
diff --git a/src/templates.go b/src/templates.go index a7d7f2a..6a43fcb 100644 --- a/src/templates.go +++ b/src/templates.go @@ -1,26 +1,27 @@ 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" - "git.thomasvoss.com/euro-cash.eu/src/dbx" "git.thomasvoss.com/euro-cash.eu/src/i18n" ) 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 CountryMintageTable + YearMintages YearMintageTable + Countries []country } var ( @@ -28,17 +29,26 @@ var ( errorTmpl *template.Template templates map[string]*template.Template funcmap = map[string]any{ + "evenp": evenp, "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") @@ -50,16 +60,21 @@ func BuildTemplates(dir fs.FS) { } 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) + } }) } } @@ -82,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 } @@ -112,18 +132,8 @@ 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 evenp(n int) bool { + return n&1 == 0 } func ifElse(b bool, x, y any) any { @@ -133,6 +143,36 @@ func ifElse(b bool, x, y any) any { return y } +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('<') @@ -163,6 +203,19 @@ func withTranslations(tag string, text string, translations ...[]any) template.H 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...)) } |