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-22 13:57:25 +00:00
|
|
|
|
{{define "categories"}}Категории{{end}}
|
2022-03-23 12:46:08 +00:00
|
|
|
|
{{define "placeholder"}}Имя категории...{{end}}
|
2022-03-22 13:57:25 +00:00
|
|
|
|
{{define "remove from category title"}}Убрать гифу из этой категории{{end}}
|
2022-03-23 12:46:08 +00:00
|
|
|
|
{{define "add to category title"}}Добавить гифу в эту категорию{{end}}
|
|
|
|
|
{{define "category list heading"}}Список категорий{{end}}
|
|
|
|
|
{{define "no categories"}}В этой вики нет категорий.{{end}}
|
|
|
|
|
{{define "category x"}}Категория {{. | beautifulName}}{{end}}
|
2022-03-20 19:24:40 +00:00
|
|
|
|
`
|
|
|
|
|
|
2022-03-20 08:21:59 +00:00
|
|
|
|
var (
|
2022-03-23 12:46:08 +00:00
|
|
|
|
categoryTemplatesEn *template.Template
|
|
|
|
|
categoryTemplatesRu *template.Template
|
2022-03-20 08:21:59 +00:00
|
|
|
|
)
|
2022-03-19 20:57:33 +00:00
|
|
|
|
|
|
|
|
|
func init() {
|
2022-03-23 12:46:08 +00:00
|
|
|
|
categoryTemplatesEn = template.Must(template.
|
2022-03-20 09:04:08 +00:00
|
|
|
|
New("category").
|
|
|
|
|
Funcs(
|
|
|
|
|
template.FuncMap{
|
|
|
|
|
"beautifulName": util.BeautifulName,
|
|
|
|
|
}).
|
2022-03-22 13:40:40 +00:00
|
|
|
|
ParseFS(fs, "categories.html"))
|
2022-03-23 12:46:08 +00:00
|
|
|
|
categoryTemplatesRu = template.Must(template.Must(categoryTemplatesEn.Clone()).Parse(categoriesRu))
|
2022-03-19 20:57:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 12:46:08 +00:00
|
|
|
|
func localizedTemplates(meta Meta) *template.Template {
|
|
|
|
|
if meta.Lc.Locale == "ru" {
|
|
|
|
|
return categoryTemplatesRu
|
|
|
|
|
}
|
|
|
|
|
return categoryTemplatesEn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func localizedTemplateAsString(meta Meta, name string, datum ...interface{}) string {
|
2022-03-19 20:57:33 +00:00
|
|
|
|
var buf strings.Builder
|
2022-03-23 12:46:08 +00:00
|
|
|
|
var err error
|
|
|
|
|
if len(datum) == 1 {
|
|
|
|
|
err = localizedTemplates(meta).ExecuteTemplate(&buf, name, datum[0])
|
|
|
|
|
} else {
|
|
|
|
|
err = localizedTemplates(meta).ExecuteTemplate(&buf, name, nil)
|
|
|
|
|
}
|
2022-03-22 13:40:40 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2022-03-23 12:46:08 +00:00
|
|
|
|
return buf.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func categoryCard(meta Meta, hyphaName string) string {
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
err := localizedTemplates(meta).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-23 12:46:08 +00:00
|
|
|
|
err := localizedTemplates(meta).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-23 12:46:08 +00:00
|
|
|
|
localizedTemplateAsString(meta, "category x", catName),
|
2022-03-20 08:21:59 +00:00
|
|
|
|
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
|
2022-03-23 12:46:08 +00:00
|
|
|
|
err := localizedTemplates(meta).ExecuteTemplate(&buf, "category list", struct {
|
2022-03-20 18:24:54 +00:00
|
|
|
|
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-23 12:46:08 +00:00
|
|
|
|
localizedTemplateAsString(meta, "category list heading"),
|
2022-03-20 18:24:54 +00:00
|
|
|
|
buf.String(),
|
|
|
|
|
meta.Lc,
|
|
|
|
|
meta.U,
|
|
|
|
|
))
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
}
|
|
|
|
|
}
|