From 35339a2918240c7948d2748b2e8d484ea5cd9a76 Mon Sep 17 00:00:00 2001 From: Jordan Date: Sat, 15 Jan 2022 10:56:09 -0700 Subject: http, templates: normalize paper metadata --- http.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'http.go') diff --git a/http.go b/http.go index 26cc788..adaabc7 100644 --- a/http.go +++ b/http.go @@ -12,25 +12,32 @@ import ( var templateDir = getTemplateDir() -var indexTemp = template.Must(template.ParseFiles( +var funcMap = template.FuncMap{ + "normalizeStr": normalizeStr, +} + +var indexTemp = template.Must(template.New("index.html").Funcs(funcMap).ParseFiles( filepath.Join(templateDir, "layout.html"), filepath.Join(templateDir, "index.html"), filepath.Join(templateDir, "list.html"), )) -var adminTemp = template.Must(template.ParseFiles( + +var adminTemp = template.Must(template.New("admin.html").Funcs(funcMap).ParseFiles( filepath.Join(templateDir, "admin.html"), filepath.Join(templateDir, "layout.html"), filepath.Join(templateDir, "list.html"), )) -var editTemp = template.Must(template.ParseFiles( + +var editTemp = template.Must(template.New("admin-edit.html").Funcs(funcMap).ParseFiles( filepath.Join(templateDir, "admin-edit.html"), filepath.Join(templateDir, "layout.html"), filepath.Join(templateDir, "list.html"), )) -func cat(cat string) string { +func normalizeStr(s string) string { - return strings.Replace(cat, "-", "‑", -1) + trim := strings.TrimPrefix(strings.TrimSuffix(s, "\n"), "\n") + return strings.Join(strings.Fields(trim), " ") } // getTemplateDir returns the absolute path of the templates directory, @@ -58,7 +65,10 @@ func (papers *Papers) IndexHandler(w http.ResponseWriter, r *http.Request) { return } res := Resp{Papers: *papers} - indexTemp.Execute(w, &res) + err := indexTemp.Execute(w, &res) + if err != nil { + fmt.Println(err) + } } // AdminHandler renders the index of papers stored in papers.Path with -- cgit v1.2.3-54-g00ecf