From 32b546a212eaaa3ce33737523c1e24abdbdf486b Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Thu, 10 Dec 2020 12:44:36 +0000 Subject: [PATCH] Factor out the download url into a function --- lib/template.go | 28 ++++++++++++++++++++-------- templates/book.html | 4 ++-- templates/book_list.html | 2 +- templates/submission.html | 2 +- templates/submission_moderate.html | 2 +- templates/submissions.html | 2 +- 6 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/template.go b/lib/template.go index 2720039..ae8e311 100644 --- a/lib/template.go +++ b/lib/template.go @@ -8,8 +8,10 @@ import ( "encoding/json" "errors" + "fmt" "io" "net/http" + "net/url" "path" "strings" "time" @@ -17,6 +19,11 @@ import ( "gitlab.com/trantor/trantor/lib/database" ) +var tmpl_funcs = map[string]interface{}{ + "strings_join": stringsJoin, + "download_url": downloadUrl, +} + type Status struct { BaseURL string FullURL string @@ -68,14 +75,12 @@ func InitTemplate(assetsPath string) *Template { var t Template templatePath := path.Join(assetsPath, "templates") - t.html, err = html_tmpl.New("html").Funcs(html_tmpl.FuncMap{ - "strings_join": strings.Join, - }).ParseGlob(path.Join(templatePath, "*.html")) + t.html, err = html_tmpl.New("html").Funcs(tmpl_funcs).ParseGlob(path.Join(templatePath, "*.html")) if err != nil { log.Critical("Error loading html templates: ", err) } - t.opds, err = txt_tmpl.ParseGlob(path.Join(templatePath, "*.opds")) + t.opds, err = txt_tmpl.New("opds").Funcs(tmpl_funcs).ParseGlob(path.Join(templatePath, "*.opds")) if err != nil { log.Critical("Error loading opds templates: ", err) } @@ -83,6 +88,15 @@ func InitTemplate(assetsPath string) *Template { return &t } +func stringsJoin(strs []string) string { + return strings.Join(strs, ", ") +} + +func downloadUrl(book database.Book) string { + fileName := url.PathEscape(fmt.Sprintf("%s - %s.epub", strings.Join(book.Authors, ", "), book.Title)) + return fmt.Sprintf("/download/%s/%s", book.ID, fileName) +} + type DevTemplateExecutor struct { assetsPath string tpe string @@ -106,11 +120,9 @@ func (e DevTemplateExecutor) ExecuteTemplate(wr io.Writer, name string, data int for _, f := range []string{"header.html", "footer.html", "book_list.html"} { included_files = append(included_files, path.Join(templatePath, f)) } - t = html_tmpl.Must(html_tmpl.New("html").Funcs(html_tmpl.FuncMap{ - "strings_join": strings.Join, - }).ParseFiles(included_files...)) + t = html_tmpl.Must(html_tmpl.New("html").Funcs(tmpl_funcs).ParseFiles(included_files...)) case "txt": - t = txt_tmpl.Must(txt_tmpl.ParseFiles(file)) + t = txt_tmpl.Must(txt_tmpl.New("txt").Funcs(tmpl_funcs).ParseFiles(file)) } return t.ExecuteTemplate(wr, name, data) } diff --git a/templates/book.html b/templates/book.html index c1b7fca..83f7dd0 100644 --- a/templates/book.html +++ b/templates/book.html @@ -50,7 +50,7 @@ function delBook(){
@@ -104,7 +104,7 @@ function delBook(){
- +
diff --git a/templates/book_list.html b/templates/book_list.html index a6ad965..37e2beb 100644 --- a/templates/book_list.html +++ b/templates/book_list.html @@ -15,7 +15,7 @@
diff --git a/templates/submission.html b/templates/submission.html index d3e646c..46e8f82 100644 --- a/templates/submission.html +++ b/templates/submission.html @@ -58,7 +58,7 @@

{{end}}
diff --git a/templates/submission_moderate.html b/templates/submission_moderate.html index 1efc99d..1ad6a50 100644 --- a/templates/submission_moderate.html +++ b/templates/submission_moderate.html @@ -51,7 +51,7 @@

- download + download read it!
diff --git a/templates/submissions.html b/templates/submissions.html index ef354e1..7ea466c 100644 --- a/templates/submissions.html +++ b/templates/submissions.html @@ -56,7 +56,7 @@

{{end}}
- download + download read it!