diff --git a/hyphae/categories/categories.go b/hyphae/categories/categories.go index 188ddb0..b03561d 100644 --- a/hyphae/categories/categories.go +++ b/hyphae/categories/categories.go @@ -20,7 +20,11 @@ import "sync" func WithHypha(hyphaName string) (categoryList []string) { mutex.RLock() defer mutex.RUnlock() - return hyphaToCategories[hyphaName].categoryList + if node, ok := hyphaToCategories[hyphaName]; ok { + return node.categoryList + } else { + return nil + } } // Contents returns what hyphae are in the category. If the returned slice is empty, the category does not exist, and vice versa. The category name must be canonical. diff --git a/hyphae/categories/files.go b/hyphae/categories/files.go index 24a7c55..9064cb4 100644 --- a/hyphae/categories/files.go +++ b/hyphae/categories/files.go @@ -42,12 +42,6 @@ func InitCategories() { } log.Println("Found", len(categoryToHyphae), "categories") - for cat, catNode := range categoryToHyphae { // TODO: remove when not needed - log.Println(cat, "->", catNode.hyphaList) - } - for hyp, hypNode := range hyphaToCategories { - log.Println(hyp, "<-", hypNode.categoryList) - } } type categoryNode struct { diff --git a/views/base.go b/views/base.go new file mode 100644 index 0000000..b45a6d4 --- /dev/null +++ b/views/base.go @@ -0,0 +1,25 @@ +package views + +import ( + "github.com/bouncepaw/mycorrhiza/l18n" + "github.com/bouncepaw/mycorrhiza/user" + "io" + "net/http" +) + +// Meta is a bundle of common stuffs used by views, templates. +type Meta struct { + Lc *l18n.Localizer + U *user.User + W io.Writer + PageTitle string +} + +// MetaFrom makes a Meta from the given data. You are meant to further modify it. +func MetaFrom(w http.ResponseWriter, rq *http.Request) Meta { + return Meta{ + Lc: l18n.FromRequest(rq), + U: user.FromRequest(rq), + W: w, + } +} diff --git a/views/categories.go b/views/categories.go index d9cbdc3..199e85e 100644 --- a/views/categories.go +++ b/views/categories.go @@ -1,88 +1,35 @@ package views import ( + "embed" "github.com/bouncepaw/mycorrhiza/hyphae/categories" - "github.com/bouncepaw/mycorrhiza/l18n" - "github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/util" "html/template" "io" "log" - "net/http" "strings" ) -const categoriesCardTmpl = `{{$hyphaName := .HyphaName -}}` - -const categoryPageTmpl = `{{$catName := .CatName -}}
-

Category {{$catName}}

-{{if len .Hyphae}} -

This page lists all hyphae in the category.

-{{else}} -

This category has no hyphae.

-{{end}} - -
` +//go:embed categories.html +var fs embed.FS var ( - categoriesCardT *template.Template - categoryPageT *template.Template + categoryT *template.Template ) func init() { - categoriesCardT = template.Must(template. - New("category card"). - Funcs(template.FuncMap{ - "beautifulName": util.BeautifulName, - }). - Parse(categoriesCardTmpl)) - categoryPageT = template.Must(template. - New("category page"). - Funcs(template.FuncMap{ - "beautifulName": util.BeautifulName, - }). - Parse(categoryPageTmpl)) + categoryT = template.Must(template. + New("category"). + Funcs( + template.FuncMap{ + "beautifulName": util.BeautifulName, + }). + ParseFS(fs, "*")) } func categoryCardHTML(hyphaName string) string { var buf strings.Builder - err := categoriesCardT.Execute(&buf, struct { + err := categoryT.ExecuteTemplate(&buf, "category card", struct { HyphaName string Categories []string }{ @@ -95,9 +42,9 @@ func categoryCardHTML(hyphaName string) string { return buf.String() } -func CategoryPageHTML(w io.Writer, rq *http.Request, catName string) { +func CategoryPageHTML(meta Meta, catName string) { var buf strings.Builder - err := categoryPageT.Execute(&buf, struct { + err := categoryT.ExecuteTemplate(&buf, "category page", struct { CatName string Hyphae []string }{ @@ -107,10 +54,13 @@ func CategoryPageHTML(w io.Writer, rq *http.Request, catName string) { if err != nil { log.Println(err) } - io.WriteString(w, BaseHTML( + _, err = io.WriteString(meta.W, BaseHTML( "Category "+util.BeautifulName(catName), buf.String(), - l18n.FromRequest(rq), - user.FromRequest(rq), + meta.Lc, + meta.U, )) + if err != nil { + log.Println(err) + } } diff --git a/views/categories.html b/views/categories.html new file mode 100644 index 0000000..7f7f999 --- /dev/null +++ b/views/categories.html @@ -0,0 +1,51 @@ +{{define "category card"}} +{{$hyphaName := .HyphaName}} + +{{end}} + +{{define "category page"}} +{{$catName := .CatName}} +
+

Category {{$catName}}

+ {{if len .Hyphae}} +

This page lists all hyphae in the category.

+ {{else}} +

This category has no hyphae.

+ {{end}} + +
+{{end}} \ No newline at end of file diff --git a/web/categories.go b/web/categories.go index 930cb03..8202010 100644 --- a/web/categories.go +++ b/web/categories.go @@ -18,7 +18,7 @@ func handlerCategory(w http.ResponseWriter, rq *http.Request) { var ( catName = util.HyphaNameFromRq(rq, "category") ) - views.CategoryPageHTML(w, rq, catName) + views.CategoryPageHTML(views.MetaFrom(w, rq), catName) } func handlerRemoveFromCategory(w http.ResponseWriter, rq *http.Request) {