Add basic html/rss support for book lists

This commit is contained in:
Las Zenow 2018-04-09 09:47:44 +00:00
parent c0a70a18e1
commit 46762ea17b
11 changed files with 254 additions and 78 deletions

View file

@ -4,6 +4,8 @@ import (
html_tmpl "html/template"
txt_tmpl "text/template"
log "github.com/cihub/seelog"
"encoding/json"
"errors"
"net/http"
@ -51,40 +53,31 @@ type Template struct {
}
func InitTemplate(assetsPath string) *Template {
var err error
var t Template
templatePath := path.Join(assetsPath, "templates")
t.html = html_tmpl.Must(html_tmpl.ParseFiles(
path.Join(templatePath, "header.html"),
path.Join(templatePath, "footer.html"),
path.Join(templatePath, "404.html"),
path.Join(templatePath, "index.html"),
path.Join(templatePath, "about.html"),
path.Join(templatePath, "news.html"),
path.Join(templatePath, "edit_news.html"),
path.Join(templatePath, "book.html"),
path.Join(templatePath, "search.html"),
path.Join(templatePath, "upload.html"),
path.Join(templatePath, "submission.html"),
path.Join(templatePath, "login.html"),
path.Join(templatePath, "new.html"),
path.Join(templatePath, "read.html"),
path.Join(templatePath, "edit.html"),
path.Join(templatePath, "dashboard.html"),
path.Join(templatePath, "settings.html"),
path.Join(templatePath, "help.html"),
path.Join(templatePath, "user_admin.html"),
))
t.html, err = html_tmpl.ParseGlob(path.Join(templatePath, "*.html"))
if err != nil {
log.Critical("Error loading html templates: ", err)
}
t.rss = txt_tmpl.Must(txt_tmpl.ParseFiles(
path.Join(templatePath, "search.rss"),
path.Join(templatePath, "news.rss"),
))
t.rss, err = txt_tmpl.New("rss").Funcs(txt_tmpl.FuncMap{
"book_list": func(books, baseURL interface{}) (map[string]interface{}, error) {
data := make(map[string]interface{}, 2)
data["Books"] = books
data["BaseURL"] = baseURL
return data, nil
},
}).ParseGlob(path.Join(templatePath, "*.rss"))
if err != nil {
log.Critical("Error loading rss templates: ", err)
}
t.opds = txt_tmpl.Must(txt_tmpl.ParseFiles(
path.Join(templatePath, "index.opds"),
path.Join(templatePath, "search.opds"),
))
t.opds, err = txt_tmpl.ParseGlob(path.Join(templatePath, "*.opds"))
if err != nil {
log.Critical("Error loading opds templates: ", err)
}
return &t
}
@ -101,6 +94,8 @@ func loadJson(w http.ResponseWriter, tmpl string, data interface{}) error {
res, err = newsJson(data)
case "search":
res, err = searchJson(data)
case "list":
res, err = listJson(data)
}
if err != nil {
return err
@ -178,6 +173,25 @@ func searchJson(data interface{}) ([]byte, error) {
})
}
func listJson(data interface{}) ([]byte, error) {
list, ok := data.(listData)
if !ok {
return nil, errors.New("Data is not valid")
}
books := make([]map[string]interface{}, len(list.List.Books))
for i, book := range list.List.Books {
books[i] = bookJsonRaw(book)
}
return json.Marshal(map[string]interface{}{
"ID": list.List.ListID,
"title": list.List.Title,
"description": list.List.Description,
"user ": list.List.User.Username,
"books": books,
})
}
func bookJsonRaw(book database.Book) map[string]interface{} {
cover := ""
coverSmall := ""