diff --git a/hyphae/categories/categories.go b/hyphae/categories/categories.go index 87f21d1..188ddb0 100644 --- a/hyphae/categories/categories.go +++ b/hyphae/categories/categories.go @@ -27,7 +27,11 @@ func WithHypha(hyphaName string) (categoryList []string) { func Contents(catName string) (hyphaList []string) { mutex.RLock() defer mutex.RUnlock() - return categoryToHyphae[catName].hyphaList + if node, ok := categoryToHyphae[catName]; ok { + return node.hyphaList + } else { + return nil + } } var mutex sync.RWMutex diff --git a/views/categories.go b/views/categories.go index 4b62fdc..d9cbdc3 100644 --- a/views/categories.go +++ b/views/categories.go @@ -2,9 +2,13 @@ package views import ( "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" ) @@ -24,15 +28,42 @@ const categoriesCardTmpl = `{{$hyphaName := .HyphaName {{end}}
  • -
  • ` -var categoriesCardT *template.Template +const categoryPageTmpl = `{{$catName := .CatName +}}
    +

    Category {{$catName}}

    +{{if len .Hyphae}} +

    This page lists all hyphae in the category.

    +{{else}} +

    This category has no hyphae.

    +{{end}} + +
    ` + +var ( + categoriesCardT *template.Template + categoryPageT *template.Template +) func init() { categoriesCardT = template.Must(template. @@ -41,6 +72,12 @@ func init() { "beautifulName": util.BeautifulName, }). Parse(categoriesCardTmpl)) + categoryPageT = template.Must(template. + New("category page"). + Funcs(template.FuncMap{ + "beautifulName": util.BeautifulName, + }). + Parse(categoryPageTmpl)) } func categoryCardHTML(hyphaName string) string { @@ -57,3 +94,23 @@ func categoryCardHTML(hyphaName string) string { } return buf.String() } + +func CategoryPageHTML(w io.Writer, rq *http.Request, catName string) { + var buf strings.Builder + err := categoryPageT.Execute(&buf, struct { + CatName string + Hyphae []string + }{ + catName, + categories.Contents(catName), + }) + if err != nil { + log.Println(err) + } + io.WriteString(w, BaseHTML( + "Category "+util.BeautifulName(catName), + buf.String(), + l18n.FromRequest(rq), + user.FromRequest(rq), + )) +} diff --git a/web/categories.go b/web/categories.go new file mode 100644 index 0000000..930cb03 --- /dev/null +++ b/web/categories.go @@ -0,0 +1,30 @@ +package web + +import ( + "github.com/bouncepaw/mycorrhiza/util" + "github.com/bouncepaw/mycorrhiza/views" + "github.com/gorilla/mux" + "net/http" +) + +func initCategories(r *mux.Router) { + r.PathPrefix("/add-to-category").HandlerFunc(handlerAddToCategory).Methods("POST") + r.PathPrefix("/remove-from-category").HandlerFunc(handlerRemoveFromCategory).Methods("POST") + r.PathPrefix("/category/").HandlerFunc(handlerCategory).Methods("GET") +} + +func handlerCategory(w http.ResponseWriter, rq *http.Request) { + util.PrepareRq(rq) + var ( + catName = util.HyphaNameFromRq(rq, "category") + ) + views.CategoryPageHTML(w, rq, catName) +} + +func handlerRemoveFromCategory(w http.ResponseWriter, rq *http.Request) { + +} + +func handlerAddToCategory(w http.ResponseWriter, rq *http.Request) { + +} diff --git a/web/web.go b/web/web.go index 6d6045b..1348208 100644 --- a/web/web.go +++ b/web/web.go @@ -108,6 +108,7 @@ func Handler() http.Handler { initStuff(wikiRouter) initSearch(wikiRouter) initBacklinks(wikiRouter) + initCategories(wikiRouter) // Admin routes. if cfg.UseAuth {