2022-03-19 20:57:33 +00:00
|
|
|
package views
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae/categories"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
|
|
|
"html/template"
|
2022-03-20 08:21:59 +00:00
|
|
|
"io"
|
2022-03-19 20:57:33 +00:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-03-20 19:24:40 +00:00
|
|
|
const categoriesRu = `
|
|
|
|
{{define "empty cat"}}Эта категория пуста.{{end}}
|
|
|
|
{{define "add hypha"}}Добавить в категорию{{end}}
|
|
|
|
{{define "cat"}}Категория{{end}}
|
|
|
|
{{define "hypha name"}}Имя гифы{{end}}
|
|
|
|
`
|
|
|
|
|
2022-03-20 08:21:59 +00:00
|
|
|
var (
|
2022-03-20 09:04:08 +00:00
|
|
|
categoryT *template.Template
|
2022-03-20 08:21:59 +00:00
|
|
|
)
|
2022-03-19 20:57:33 +00:00
|
|
|
|
|
|
|
func init() {
|
2022-03-20 09:04:08 +00:00
|
|
|
categoryT = template.Must(template.
|
|
|
|
New("category").
|
|
|
|
Funcs(
|
|
|
|
template.FuncMap{
|
|
|
|
"beautifulName": util.BeautifulName,
|
|
|
|
}).
|
2022-03-22 13:40:40 +00:00
|
|
|
ParseFS(fs, "categories.html"))
|
2022-03-19 20:57:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 21:24:40 +00:00
|
|
|
func categoryCard(hyphaName string) string {
|
2022-03-19 20:57:33 +00:00
|
|
|
var buf strings.Builder
|
2022-03-22 13:40:40 +00:00
|
|
|
t, err := categoryT.Clone()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
err = t.ExecuteTemplate(&buf, "category card", struct {
|
2022-03-19 20:57:33 +00:00
|
|
|
HyphaName string
|
|
|
|
Categories []string
|
|
|
|
}{
|
|
|
|
hyphaName,
|
|
|
|
categories.WithHypha(hyphaName),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
2022-03-20 08:21:59 +00:00
|
|
|
|
2022-03-20 21:24:40 +00:00
|
|
|
func CategoryPage(meta Meta, catName string) {
|
2022-03-20 08:21:59 +00:00
|
|
|
var buf strings.Builder
|
2022-03-22 13:40:40 +00:00
|
|
|
var t, err = categoryT.Clone()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2022-03-20 19:24:40 +00:00
|
|
|
if meta.Lc.Locale == "ru" {
|
2022-03-22 13:40:40 +00:00
|
|
|
_, err = t.Parse(categoriesRu)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2022-03-20 19:24:40 +00:00
|
|
|
}
|
2022-03-22 13:40:40 +00:00
|
|
|
err = t.ExecuteTemplate(&buf, "category page", struct {
|
2022-03-20 08:21:59 +00:00
|
|
|
CatName string
|
|
|
|
Hyphae []string
|
|
|
|
}{
|
|
|
|
catName,
|
|
|
|
categories.Contents(catName),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-03-20 21:24:40 +00:00
|
|
|
_, err = io.WriteString(meta.W, Base(
|
2022-03-20 08:21:59 +00:00
|
|
|
"Category "+util.BeautifulName(catName),
|
|
|
|
buf.String(),
|
2022-03-20 09:04:08 +00:00
|
|
|
meta.Lc,
|
|
|
|
meta.U,
|
2022-03-20 08:21:59 +00:00
|
|
|
))
|
2022-03-20 09:04:08 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-03-20 08:21:59 +00:00
|
|
|
}
|
2022-03-20 18:24:54 +00:00
|
|
|
|
2022-03-20 21:24:40 +00:00
|
|
|
func CategoryList(meta Meta) {
|
2022-03-20 18:24:54 +00:00
|
|
|
var buf strings.Builder
|
|
|
|
err := categoryT.ExecuteTemplate(&buf, "category list", struct {
|
|
|
|
Categories []string
|
|
|
|
}{
|
|
|
|
categories.List(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-03-20 21:24:40 +00:00
|
|
|
_, err = io.WriteString(meta.W, Base(
|
2022-03-20 18:24:54 +00:00
|
|
|
"Category list",
|
|
|
|
buf.String(),
|
|
|
|
meta.Lc,
|
|
|
|
meta.U,
|
|
|
|
))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|