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 ++++++++++++++++------ templates/admin-edit.html | 3 ++- templates/list.html | 9 ++++++--- 3 files changed, 24 insertions(+), 10 deletions(-) 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 diff --git a/templates/admin-edit.html b/templates/admin-edit.html index d8805f9..d4c9196 100644 --- a/templates/admin-edit.html +++ b/templates/admin-edit.html @@ -65,7 +65,8 @@ {{ if $paper.Meta.Title }} - {{ $paper.Meta.Title }} + + {{- normalizeStr $paper.Meta.Title }}
{{ else }} diff --git a/templates/list.html b/templates/list.html index b9f7fd2..6d6cdaf 100644 --- a/templates/list.html +++ b/templates/list.html @@ -10,12 +10,14 @@
{{ if $paper.Meta.Title }} - {{ $paper.Meta.Title }} + + {{- normalizeStr $paper.Meta.Title }}
{{ else }} - {{ $paper.PaperName }} + + {{- $paper.PaperName }}
{{ end }} @@ -46,7 +48,8 @@ {{ else if $paper.Meta.ArxivID }} {{ if $hasVal }}- {{ end }} - {{ $paper.Meta.ArxivID }} + + {{- $paper.Meta.ArxivID }} {{ else if $paper.Meta.Resource }} {{ if $hasVal }}- {{ end }} -- cgit v1.2.3-54-g00ecf