mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-06-23 03:34:05 +00:00
Categories: Refactor a little
* Introduce views.Meta for passing common stuff around. * Store both category-related templates in one HTML file, which is go:embed:ded. * Fix a bug.
This commit is contained in:
parent
d2a4285e7f
commit
ea0c2f35d1
@ -20,7 +20,11 @@ import "sync"
|
|||||||
func WithHypha(hyphaName string) (categoryList []string) {
|
func WithHypha(hyphaName string) (categoryList []string) {
|
||||||
mutex.RLock()
|
mutex.RLock()
|
||||||
defer mutex.RUnlock()
|
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.
|
// 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.
|
||||||
|
@ -42,12 +42,6 @@ func InitCategories() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Found", len(categoryToHyphae), "categories")
|
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 {
|
type categoryNode struct {
|
||||||
|
25
views/base.go
Normal file
25
views/base.go
Normal file
@ -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,
|
||||||
|
}
|
||||||
|
}
|
@ -1,88 +1,35 @@
|
|||||||
package views
|
package views
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"github.com/bouncepaw/mycorrhiza/hyphae/categories"
|
"github.com/bouncepaw/mycorrhiza/hyphae/categories"
|
||||||
"github.com/bouncepaw/mycorrhiza/l18n"
|
|
||||||
"github.com/bouncepaw/mycorrhiza/user"
|
|
||||||
"github.com/bouncepaw/mycorrhiza/util"
|
"github.com/bouncepaw/mycorrhiza/util"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const categoriesCardTmpl = `{{$hyphaName := .HyphaName
|
//go:embed categories.html
|
||||||
}}<aside class="layout-card categories-card">
|
var fs embed.FS
|
||||||
<h2 class="layout-card__title">Categories</h2>
|
|
||||||
<ul class="categories-card__entries">
|
|
||||||
{{range .Categories}}
|
|
||||||
<li class="categories-card__entry">
|
|
||||||
<a class="categories-card__link" href="/category/{{.}}">{{beautifulName .}}</a>
|
|
||||||
<form method="POST" action="/remove-from-category" class="categories-card__remove-form">
|
|
||||||
<input type="hidden" name="cat" value="{{.}}">
|
|
||||||
<input type="hidden" name="hypha" value="{{$hyphaName}}">
|
|
||||||
<input type="submit" value="X">
|
|
||||||
</form>
|
|
||||||
</li>
|
|
||||||
{{end}}
|
|
||||||
<li class="categories-card__entry categories-card__add-to-cat">
|
|
||||||
<form method="POST" action="/add-to-category" class="categories-card__add-form">
|
|
||||||
<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>`
|
|
||||||
|
|
||||||
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 (
|
var (
|
||||||
categoriesCardT *template.Template
|
categoryT *template.Template
|
||||||
categoryPageT *template.Template
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
categoriesCardT = template.Must(template.
|
categoryT = template.Must(template.
|
||||||
New("category card").
|
New("category").
|
||||||
Funcs(template.FuncMap{
|
Funcs(
|
||||||
|
template.FuncMap{
|
||||||
"beautifulName": util.BeautifulName,
|
"beautifulName": util.BeautifulName,
|
||||||
}).
|
}).
|
||||||
Parse(categoriesCardTmpl))
|
ParseFS(fs, "*"))
|
||||||
categoryPageT = template.Must(template.
|
|
||||||
New("category page").
|
|
||||||
Funcs(template.FuncMap{
|
|
||||||
"beautifulName": util.BeautifulName,
|
|
||||||
}).
|
|
||||||
Parse(categoryPageTmpl))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func categoryCardHTML(hyphaName string) string {
|
func categoryCardHTML(hyphaName string) string {
|
||||||
var buf strings.Builder
|
var buf strings.Builder
|
||||||
err := categoriesCardT.Execute(&buf, struct {
|
err := categoryT.ExecuteTemplate(&buf, "category card", struct {
|
||||||
HyphaName string
|
HyphaName string
|
||||||
Categories []string
|
Categories []string
|
||||||
}{
|
}{
|
||||||
@ -95,9 +42,9 @@ func categoryCardHTML(hyphaName string) string {
|
|||||||
return buf.String()
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func CategoryPageHTML(w io.Writer, rq *http.Request, catName string) {
|
func CategoryPageHTML(meta Meta, catName string) {
|
||||||
var buf strings.Builder
|
var buf strings.Builder
|
||||||
err := categoryPageT.Execute(&buf, struct {
|
err := categoryT.ExecuteTemplate(&buf, "category page", struct {
|
||||||
CatName string
|
CatName string
|
||||||
Hyphae []string
|
Hyphae []string
|
||||||
}{
|
}{
|
||||||
@ -107,10 +54,13 @@ func CategoryPageHTML(w io.Writer, rq *http.Request, catName string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
io.WriteString(w, BaseHTML(
|
_, err = io.WriteString(meta.W, BaseHTML(
|
||||||
"Category "+util.BeautifulName(catName),
|
"Category "+util.BeautifulName(catName),
|
||||||
buf.String(),
|
buf.String(),
|
||||||
l18n.FromRequest(rq),
|
meta.Lc,
|
||||||
user.FromRequest(rq),
|
meta.U,
|
||||||
))
|
))
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
51
views/categories.html
Normal file
51
views/categories.html
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
{{define "category card"}}
|
||||||
|
{{$hyphaName := .HyphaName}}
|
||||||
|
<aside class="layout-card categories-card">
|
||||||
|
<h2 class="layout-card__title">Categories</h2>
|
||||||
|
<ul class="categories-card__entries">
|
||||||
|
{{range .Categories}}
|
||||||
|
<li class="categories-card__entry">
|
||||||
|
<a class="categories-card__link" href="/category/{{.}}">{{beautifulName .}}</a>
|
||||||
|
<form method="POST" action="/remove-from-category" class="categories-card__remove-form">
|
||||||
|
<input type="hidden" name="cat" value="{{.}}">
|
||||||
|
<input type="hidden" name="hypha" value="{{$hyphaName}}">
|
||||||
|
<input type="submit" value="X">
|
||||||
|
</form>
|
||||||
|
</li>
|
||||||
|
{{end}}
|
||||||
|
<li class="categories-card__entry categories-card__add-to-cat">
|
||||||
|
<form method="POST" action="/add-to-category" class="categories-card__add-form">
|
||||||
|
<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>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{define "category page"}}
|
||||||
|
{{$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>
|
||||||
|
{{end}}
|
@ -18,7 +18,7 @@ func handlerCategory(w http.ResponseWriter, rq *http.Request) {
|
|||||||
var (
|
var (
|
||||||
catName = util.HyphaNameFromRq(rq, "category")
|
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) {
|
func handlerRemoveFromCategory(w http.ResponseWriter, rq *http.Request) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user