1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-01-05 17:40:26 +00:00

Categories: Add category list

* /category lists categories
* /category/<cat> shows the category
This commit is contained in:
Timur Ismagilov 2022-03-20 21:24:54 +03:00
parent cc4f3c9aed
commit 7865f77060
4 changed files with 53 additions and 0 deletions

View File

@ -16,6 +16,16 @@ package categories
import "sync"
// List returns names of all categories.
func List() (categoryList []string) {
mutex.RLock()
for cat, _ := range categoryToHyphae {
categoryList = append(categoryList, cat)
}
mutex.RUnlock()
return categoryList
}
// WithHypha returns what categories have the given hypha. The hypha name must be canonical.
func WithHypha(hyphaName string) (categoryList []string) {
mutex.RLock()

View File

@ -64,3 +64,24 @@ func CategoryPageHTML(meta Meta, catName string) {
log.Println(err)
}
}
func CategoryListHTML(meta Meta) {
var buf strings.Builder
err := categoryT.ExecuteTemplate(&buf, "category list", struct {
Categories []string
}{
categories.List(),
})
if err != nil {
log.Println(err)
}
_, err = io.WriteString(meta.W, BaseHTML(
"Category list",
buf.String(),
meta.Lc,
meta.U,
))
if err != nil {
log.Println(err)
}
}

View File

@ -51,4 +51,21 @@
</li>
</ul>
</main>
{{end}}
{{define "category list"}}
<main class="main-width category-list">
<h1>Category list</h1>
{{if len .Categories}}
<ul class="category-list__entries">
{{range .Categories}}
<li class="category-list__entry">
<a class="wikilink" href="/category/{{.}}">{{beautifulName .}}</a>
</li>
{{end}}
</ul>
{{else}}
<p>This wiki has no categories.</p>
{{end}}
</main>
{{end}}

View File

@ -12,6 +12,11 @@ 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")
r.PathPrefix("/category").HandlerFunc(handlerListCategory).Methods("GET")
}
func handlerListCategory(w http.ResponseWriter, rq *http.Request) {
views.CategoryListHTML(views.MetaFrom(w, rq))
}
func handlerCategory(w http.ResponseWriter, rq *http.Request) {