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

Categories: Implement category pages

This commit is contained in:
Timur Ismagilov 2022-03-20 11:21:59 +03:00
parent f5cbd5622d
commit d2a4285e7f
4 changed files with 96 additions and 4 deletions

View File

@ -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

View File

@ -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}}
<li class="categories-card__entry categories-card__add-to-cat">
<form method="POST" action="/add-to-category" class="categories-card__add-form">
<label for="_cat-name">
<input type="text">
<input type="text" name="cat" id="_cat-name">
<input type="hidden" name="hypha" value="{{$hyphaName}}">
<input type="submit" value="Add to category">
</form>
</li>
</ul>
</aside>`
var categoriesCardT *template.Template
const categoryPageTmpl = `{{$catName := .CatName
}}<main class="main-width category">
<h1>Category <i>{{$catName}}</i></h1>
{{if len .Hyphae}}
<p>This page lists all hyphae in the category.</p>
{{else}}
<p>This category has no hyphae.</p>
{{end}}
<ul class="category__entries">
{{range .Hyphae}}
<li class="category__entry">
<a class="wikilink" href="/hypha/{{.}}">{{beautifulName .}}</a>
</li>
{{end}}
<li class="category__entry category__add-to-cat">
<form method="POST" action="/add-to-category" class="category__add-form">
<input type="text" name="hypha" id="_hypha-name" placeholder="Hypha name">
<input type="hidden" name="cat" value="{{$catName}}">
<input type="submit" value="Add hypha to category">
</form>
</li>
</ul>
</main>`
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),
))
}

30
web/categories.go Normal file
View File

@ -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) {
}

View File

@ -108,6 +108,7 @@ func Handler() http.Handler {
initStuff(wikiRouter)
initSearch(wikiRouter)
initBacklinks(wikiRouter)
initCategories(wikiRouter)
// Admin routes.
if cfg.UseAuth {