1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-01-07 10:20:26 +00:00

Use viewutil.Meta in many places

This commit is contained in:
Timur Ismagilov 2022-04-01 22:51:15 +03:00
parent 2769a096e5
commit 78bae127b3
12 changed files with 113 additions and 119 deletions

View File

@ -67,9 +67,8 @@ func AdminPanel(meta viewutil.Meta) {
log.Println(err) log.Println(err)
} }
_, err = io.WriteString(meta.W, Base( _, err = io.WriteString(meta.W, Base(
meta,
templateAsString(localizedAdminTemplates(meta), "panel title"), templateAsString(localizedAdminTemplates(meta), "panel title"),
buf.String(), buf.String(),
meta.Lc,
meta.U,
)) ))
} }

View File

@ -94,10 +94,9 @@ func CategoryPage(meta viewutil.Meta, catName string) {
log.Println(err) log.Println(err)
} }
_, err = io.WriteString(meta.W, Base( _, err = io.WriteString(meta.W, Base(
meta,
localizedCatTemplateAsString(meta, "category x", catName), localizedCatTemplateAsString(meta, "category x", catName),
buf.String(), buf.String(),
meta.Lc,
meta.U,
)) ))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -115,10 +114,9 @@ func CategoryList(meta viewutil.Meta) {
log.Println(err) log.Println(err)
} }
_, err = io.WriteString(meta.W, Base( _, err = io.WriteString(meta.W, Base(
meta,
localizedCatTemplateAsString(meta, "category list heading"), localizedCatTemplateAsString(meta, "category list heading"),
buf.String(), buf.String(),
meta.Lc,
meta.U,
)) ))
if err != nil { if err != nil {
log.Println(err) log.Println(err)

View File

@ -5,8 +5,6 @@ import (
"embed" "embed"
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/l18n"
"github.com/bouncepaw/mycorrhiza/user"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
"log" "log"
"strings" "strings"
@ -54,7 +52,7 @@ func localizedBaseWithWeirdBody(meta Meta) *template.Template {
} }
return BaseEn return BaseEn
}() }()
return m(m(t.Clone()).Parse(`{{define "body"}}.Body{{end}}`)) return m(m(t.Clone()).Parse(`{{define "body"}}{{.Body}}{{end}}`))
} }
type baseData struct { type baseData struct {
@ -67,13 +65,9 @@ type baseData struct {
} }
// Base is a temporary wrapper around BaseEn and BaseRu, meant to facilitate the migration from qtpl. // Base is a temporary wrapper around BaseEn and BaseRu, meant to facilitate the migration from qtpl.
func Base(title, body string, lc *l18n.Localizer, u *user.User, headElements ...string) string { func Base(meta Meta, title, body string, headElements ...string) string {
var w strings.Builder var w strings.Builder
meta := Meta{ meta.W = &w
Lc: lc,
U: u,
W: &w,
}
t := localizedBaseWithWeirdBody(meta) t := localizedBaseWithWeirdBody(meta)
err := t.ExecuteTemplate(&w, "base", baseData{ err := t.ExecuteTemplate(&w, "base", baseData{
Meta: meta, Meta: meta,

View File

@ -71,7 +71,7 @@ func handlerAdminUsers(w http.ResponseWriter, rq *http.Request) {
var lc = l18n.FromRequest(rq) var lc = l18n.FromRequest(rq)
html := views.AdminUsersPanel(userList, lc) html := views.AdminUsersPanel(userList, lc)
html = views.Base(lc.Get("admin.users_title"), html, lc, user.FromRequest(rq)) html = views.Base(viewutil.MetaFrom(w, rq), lc.Get("admin.users_title"), html)
w.Header().Set("Content-Type", mime.TypeByExtension(".html")) w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
io.WriteString(w, html) io.WriteString(w, html)
@ -110,7 +110,7 @@ func handlerAdminUserEdit(w http.ResponseWriter, rq *http.Request) {
var lc = l18n.FromRequest(rq) var lc = l18n.FromRequest(rq)
html := views.AdminUserEdit(u, f, lc) html := views.AdminUserEdit(u, f, lc)
html = views.Base(fmt.Sprintf(lc.Get("admin.user_title"), u.Name), html, lc, user.FromRequest(rq)) html = views.Base(viewutil.MetaFrom(w, rq), fmt.Sprintf(lc.Get("admin.user_title"), u.Name), html)
if f.HasError() { if f.HasError() {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -140,7 +140,7 @@ func handlerAdminUserDelete(w http.ResponseWriter, rq *http.Request) {
var lc = l18n.FromRequest(rq) var lc = l18n.FromRequest(rq)
html := views.AdminUserDelete(u, util.NewFormData(), lc) html := views.AdminUserDelete(u, util.NewFormData(), lc)
html = views.Base(fmt.Sprintf(lc.Get("admin.user_title"), u.Name), html, l18n.FromRequest(rq), user.FromRequest(rq)) html = views.Base(viewutil.MetaFrom(w, rq), fmt.Sprintf(lc.Get("admin.user_title"), u.Name), html)
if f.HasError() { if f.HasError() {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -154,7 +154,7 @@ func handlerAdminUserNew(w http.ResponseWriter, rq *http.Request) {
if rq.Method == http.MethodGet { if rq.Method == http.MethodGet {
// New user form // New user form
html := views.AdminUserNew(util.NewFormData(), lc) html := views.AdminUserNew(util.NewFormData(), lc)
html = views.Base(lc.Get("admin.newuser_title"), html, lc, user.FromRequest(rq)) html = views.Base(viewutil.MetaFrom(w, rq), lc.Get("admin.newuser_title"), html)
w.Header().Set("Content-Type", mime.TypeByExtension(".html")) w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
io.WriteString(w, html) io.WriteString(w, html)
@ -166,7 +166,7 @@ func handlerAdminUserNew(w http.ResponseWriter, rq *http.Request) {
if err != nil { if err != nil {
html := views.AdminUserNew(f.WithError(err), lc) html := views.AdminUserNew(f.WithError(err), lc)
html = views.Base(lc.Get("admin.newuser_title"), html, lc, user.FromRequest(rq)) html = views.Base(viewutil.MetaFrom(w, rq), lc.Get("admin.newuser_title"), html)
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Header().Set("Content-Type", mime.TypeByExtension(".html")) w.Header().Set("Content-Type", mime.TypeByExtension(".html"))

View File

@ -3,6 +3,7 @@ package web
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/viewutil"
"io" "io"
"log" "log"
"mime" "mime"
@ -46,10 +47,9 @@ func handlerRegister(w http.ResponseWriter, rq *http.Request) {
_, _ = io.WriteString( _, _ = io.WriteString(
w, w,
views.Base( views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("auth.register_title"), lc.Get("auth.register_title"),
views.Register(rq), views.Register(rq),
lc,
user.FromRequest(rq),
), ),
) )
} else if rq.Method == http.MethodPost { } else if rq.Method == http.MethodPost {
@ -65,14 +65,13 @@ func handlerRegister(w http.ResponseWriter, rq *http.Request) {
_, _ = io.WriteString( _, _ = io.WriteString(
w, w,
views.Base( views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("auth.register_title"), lc.Get("auth.register_title"),
fmt.Sprintf( fmt.Sprintf(
`<main class="main-width"><p>%s</p><p><a href="/register">%s<a></p></main>`, `<main class="main-width"><p>%s</p><p><a href="/register">%s<a></p></main>`,
err.Error(), err.Error(),
lc.Get("auth.try_again"), lc.Get("auth.try_again"),
), ),
lc,
user.FromRequest(rq),
), ),
) )
} else { } else {
@ -101,7 +100,7 @@ func handlerLogout(w http.ResponseWriter, rq *http.Request) {
} }
_, _ = io.WriteString( _, _ = io.WriteString(
w, w,
views.Base(lc.Get("auth.logout_title"), views.Logout(can, lc), lc, u), views.Base(viewutil.MetaFrom(w, rq), lc.Get("auth.logout_title"), views.Logout(can, lc)),
) )
} else if rq.Method == http.MethodPost { } else if rq.Method == http.MethodPost {
user.LogoutFromRequest(w, rq) user.LogoutFromRequest(w, rq)
@ -118,10 +117,9 @@ func handlerLogin(w http.ResponseWriter, rq *http.Request) {
_, _ = io.WriteString( _, _ = io.WriteString(
w, w,
views.Base( views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("auth.login_title"), lc.Get("auth.login_title"),
views.Login(lc), views.Login(lc),
lc,
user.EmptyUser(),
), ),
) )
} else if rq.Method == http.MethodPost { } else if rq.Method == http.MethodPost {
@ -133,7 +131,7 @@ func handlerLogin(w http.ResponseWriter, rq *http.Request) {
if err != "" { if err != "" {
w.Header().Set("Content-Type", "text/html;charset=utf-8") w.Header().Set("Content-Type", "text/html;charset=utf-8")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
_, _ = io.WriteString(w, views.Base(err, views.LoginError(err, lc), lc, user.EmptyUser())) _, _ = io.WriteString(w, views.Base(viewutil.MetaFrom(w, rq), err, views.LoginError(err, lc)))
return return
} }
http.Redirect(w, rq, "/", http.StatusSeeOther) http.Redirect(w, rq, "/", http.StatusSeeOther)
@ -172,6 +170,7 @@ func handlerTelegramLogin(w http.ResponseWriter, rq *http.Request) {
_, _ = io.WriteString( _, _ = io.WriteString(
w, w,
views.Base( views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("ui.error"), lc.Get("ui.error"),
fmt.Sprintf( fmt.Sprintf(
`<main class="main-width"><p>%s</p><p>%s</p><p><a href="/login">%s<a></p></main>`, `<main class="main-width"><p>%s</p><p>%s</p><p><a href="/login">%s<a></p></main>`,
@ -179,8 +178,6 @@ func handlerTelegramLogin(w http.ResponseWriter, rq *http.Request) {
err.Error(), err.Error(),
lc.Get("auth.go_login"), lc.Get("auth.go_login"),
), ),
lc,
user.FromRequest(rq),
), ),
) )
return return
@ -193,6 +190,7 @@ func handlerTelegramLogin(w http.ResponseWriter, rq *http.Request) {
_, _ = io.WriteString( _, _ = io.WriteString(
w, w,
views.Base( views.Base(
viewutil.MetaFrom(w, rq),
"Error", "Error",
fmt.Sprintf( fmt.Sprintf(
`<main class="main-width"><p>%s</p><p>%s</p><p><a href="/login">%s<a></p></main>`, `<main class="main-width"><p>%s</p><p>%s</p><p><a href="/login">%s<a></p></main>`,
@ -200,8 +198,6 @@ func handlerTelegramLogin(w http.ResponseWriter, rq *http.Request) {
err.Error(), err.Error(),
lc.Get("auth.go_login"), lc.Get("auth.go_login"),
), ),
lc,
user.FromRequest(rq),
), ),
) )
return return

View File

@ -2,12 +2,12 @@ package web
import ( import (
"github.com/bouncepaw/mycorrhiza/hyphae/backlinks" "github.com/bouncepaw/mycorrhiza/hyphae/backlinks"
"github.com/bouncepaw/mycorrhiza/viewutil"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/bouncepaw/mycorrhiza/l18n" "github.com/bouncepaw/mycorrhiza/l18n"
"github.com/bouncepaw/mycorrhiza/user"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/views" "github.com/bouncepaw/mycorrhiza/views"
) )
@ -23,8 +23,8 @@ func handlerBacklinks(w http.ResponseWriter, rq *http.Request) {
lc = l18n.FromRequest(rq) lc = l18n.FromRequest(rq)
) )
util.HTTP200Page(w, views.Base( util.HTTP200Page(w, views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("ui.backlinks_title", &l18n.Replacements{"query": util.BeautifulName(hyphaName)}), lc.Get("ui.backlinks_title", &l18n.Replacements{"query": util.BeautifulName(hyphaName)}),
views.Backlinks(hyphaName, backlinks.YieldHyphaBacklinks, lc), views.Backlinks(hyphaName, backlinks.YieldHyphaBacklinks, lc),
lc, ))
user.FromRequest(rq)))
} }

View File

@ -2,6 +2,7 @@ package web
import ( import (
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/viewutil"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
@ -10,7 +11,6 @@ import (
"github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/l18n" "github.com/bouncepaw/mycorrhiza/l18n"
"github.com/bouncepaw/mycorrhiza/user"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/views" "github.com/bouncepaw/mycorrhiza/views"
) )
@ -42,10 +42,10 @@ func handlerHistory(w http.ResponseWriter, rq *http.Request) {
var lc = l18n.FromRequest(rq) var lc = l18n.FromRequest(rq)
util.HTTP200Page(w, views.Base( util.HTTP200Page(w, views.Base(
viewutil.MetaFrom(w, rq),
fmt.Sprintf(lc.Get("ui.history_title"), util.BeautifulName(hyphaName)), fmt.Sprintf(lc.Get("ui.history_title"), util.BeautifulName(hyphaName)),
views.History(rq, hyphaName, list, lc), views.History(rq, hyphaName, list, lc),
lc, ))
user.FromRequest(rq)))
} }
// handlerRecentChanges displays the /recent-changes/ page. // handlerRecentChanges displays the /recent-changes/ page.
@ -57,10 +57,10 @@ func handlerRecentChanges(w http.ResponseWriter, rq *http.Request) {
} }
var lc = l18n.FromRequest(rq) var lc = l18n.FromRequest(rq)
util.HTTP200Page(w, views.Base( util.HTTP200Page(w, views.Base(
viewutil.MetaFrom(w, rq),
lc.GetPlural("ui.recent_title", n), lc.GetPlural("ui.recent_title", n),
views.RecentChanges(n, lc), views.RecentChanges(n, lc),
lc, ))
user.FromRequest(rq)))
} }
// genericHandlerOfFeeds is a helper function for the web feed handlers. // genericHandlerOfFeeds is a helper function for the web feed handlers.

View File

@ -2,6 +2,7 @@ package web
import ( import (
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/viewutil"
"log" "log"
"net/http" "net/http"
@ -32,31 +33,31 @@ func initMutators(r *mux.Router) {
func handlerRemoveMedia(w http.ResponseWriter, rq *http.Request) { func handlerRemoveMedia(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq) util.PrepareRq(rq)
var ( var (
u = user.FromRequest(rq) u = user.FromRequest(rq)
lc = l18n.FromRequest(rq) lc = l18n.FromRequest(rq)
h = hyphae.ByName(util.HyphaNameFromRq(rq, "delete")) h = hyphae.ByName(util.HyphaNameFromRq(rq, "delete"))
meta = viewutil.MetaFrom(w, rq)
) )
if !u.CanProceed("remove-media") { if !u.CanProceed("remove-media") {
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "no rights") httpErr(meta, lc, http.StatusForbidden, h.CanonicalName(), "no rights")
return return
} }
if rq.Method == "GET" { if rq.Method == "GET" {
util.HTTP200Page( util.HTTP200Page(
w, w,
views.Base( views.Base(
meta,
fmt.Sprintf(lc.Get("ui.ask_remove_media"), util.BeautifulName(h.CanonicalName())), fmt.Sprintf(lc.Get("ui.ask_remove_media"), util.BeautifulName(h.CanonicalName())),
views.RemoveMediaAsk(rq, h.CanonicalName()), views.RemoveMediaAsk(rq, h.CanonicalName())))
lc,
u))
return return
} }
switch h := h.(type) { switch h := h.(type) {
case *hyphae.EmptyHypha, *hyphae.TextualHypha: case *hyphae.EmptyHypha, *hyphae.TextualHypha:
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "no media to remove") httpErr(meta, lc, http.StatusForbidden, h.CanonicalName(), "no media to remove")
return return
case *hyphae.MediaHypha: case *hyphae.MediaHypha:
if err := shroom.RemoveMedia(u, h); err != nil { if err := shroom.RemoveMedia(u, h); err != nil {
httpErr(w, lc, http.StatusInternalServerError, h.CanonicalName(), err.Error()) httpErr(meta, lc, http.StatusInternalServerError, h.CanonicalName(), err.Error())
return return
} }
} }
@ -65,14 +66,15 @@ func handlerRemoveMedia(w http.ResponseWriter, rq *http.Request) {
func handlerDelete(w http.ResponseWriter, rq *http.Request) { func handlerDelete(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq) util.PrepareRq(rq)
var ( var (
u = user.FromRequest(rq) u = user.FromRequest(rq)
lc = l18n.FromRequest(rq) lc = l18n.FromRequest(rq)
h = hyphae.ByName(util.HyphaNameFromRq(rq, "delete")) h = hyphae.ByName(util.HyphaNameFromRq(rq, "delete"))
meta = viewutil.MetaFrom(w, rq)
) )
if !u.CanProceed("delete") { if !u.CanProceed("delete") {
log.Printf("%s has no rights to delete %s\n", u.Name, h.CanonicalName()) log.Printf("%s has no rights to delete %s\n", u.Name, h.CanonicalName())
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "No rights") httpErr(meta, lc, http.StatusForbidden, h.CanonicalName(), "No rights")
return return
} }
@ -80,7 +82,7 @@ func handlerDelete(w http.ResponseWriter, rq *http.Request) {
case *hyphae.EmptyHypha: case *hyphae.EmptyHypha:
log.Printf("%s tries to delete empty hypha %s\n", u.Name, h.CanonicalName()) log.Printf("%s tries to delete empty hypha %s\n", u.Name, h.CanonicalName())
// TODO: localize // TODO: localize
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "Cannot delete an empty hypha") httpErr(meta, lc, http.StatusForbidden, h.CanonicalName(), "Cannot delete an empty hypha")
return return
} }
@ -88,16 +90,15 @@ func handlerDelete(w http.ResponseWriter, rq *http.Request) {
util.HTTP200Page( util.HTTP200Page(
w, w,
views.Base( views.Base(
meta,
fmt.Sprintf(lc.Get("ui.ask_delete"), util.BeautifulName(h.CanonicalName())), fmt.Sprintf(lc.Get("ui.ask_delete"), util.BeautifulName(h.CanonicalName())),
views.DeleteAsk(rq, h.CanonicalName()), views.DeleteAsk(rq, h.CanonicalName())))
lc,
u))
return return
} }
if err := shroom.Delete(u, h.(hyphae.ExistingHypha)); err != nil { if err := shroom.Delete(u, h.(hyphae.ExistingHypha)); err != nil {
log.Println(err) log.Println(err)
httpErr(w, lc, http.StatusInternalServerError, h.CanonicalName(), err.Error()) httpErr(meta, lc, http.StatusInternalServerError, h.CanonicalName(), err.Error())
return return
} }
http.Redirect(w, rq, "/hypha/"+h.CanonicalName(), http.StatusSeeOther) http.Redirect(w, rq, "/hypha/"+h.CanonicalName(), http.StatusSeeOther)
@ -106,21 +107,22 @@ func handlerDelete(w http.ResponseWriter, rq *http.Request) {
func handlerRename(w http.ResponseWriter, rq *http.Request) { func handlerRename(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq) util.PrepareRq(rq)
var ( var (
u = user.FromRequest(rq) u = user.FromRequest(rq)
lc = l18n.FromRequest(rq) lc = l18n.FromRequest(rq)
h = hyphae.ByName(util.HyphaNameFromRq(rq, "rename")) h = hyphae.ByName(util.HyphaNameFromRq(rq, "rename"))
meta = viewutil.MetaFrom(w, rq)
) )
switch h.(type) { switch h.(type) {
case *hyphae.EmptyHypha: case *hyphae.EmptyHypha:
log.Printf("%s tries to rename empty hypha %s", u.Name, h.CanonicalName()) log.Printf("%s tries to rename empty hypha %s", u.Name, h.CanonicalName())
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "Cannot rename an empty hypha") // TODO: localize httpErr(meta, lc, http.StatusForbidden, h.CanonicalName(), "Cannot rename an empty hypha") // TODO: localize
return return
} }
if !u.CanProceed("rename") { if !u.CanProceed("rename") {
log.Printf("%s has no rights to rename %s\n", u.Name, h.CanonicalName()) log.Printf("%s has no rights to rename %s\n", u.Name, h.CanonicalName())
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "No rights") httpErr(meta, lc, http.StatusForbidden, h.CanonicalName(), "No rights")
return return
} }
@ -134,16 +136,15 @@ func handlerRename(w http.ResponseWriter, rq *http.Request) {
util.HTTP200Page( util.HTTP200Page(
w, w,
views.Base( views.Base(
meta,
fmt.Sprintf(lc.Get("ui.ask_rename"), util.BeautifulName(oldHypha.CanonicalName())), fmt.Sprintf(lc.Get("ui.ask_rename"), util.BeautifulName(oldHypha.CanonicalName())),
views.RenameAsk(rq, oldHypha.CanonicalName()), views.RenameAsk(rq, oldHypha.CanonicalName())))
lc,
u))
return return
} }
if err := shroom.Rename(oldHypha, newName, recursive, u); err != nil { if err := shroom.Rename(oldHypha, newName, recursive, u); err != nil {
log.Printf("%s tries to rename %s: %s", u.Name, oldHypha.CanonicalName(), err.Error()) log.Printf("%s tries to rename %s: %s", u.Name, oldHypha.CanonicalName(), err.Error())
httpErr(w, lc, http.StatusForbidden, oldHypha.CanonicalName(), lc.Get(err.Error())) // TODO: localize httpErr(meta, lc, http.StatusForbidden, oldHypha.CanonicalName(), lc.Get(err.Error())) // TODO: localize
return return
} }
http.Redirect(w, rq, "/hypha/"+newName, http.StatusSeeOther) http.Redirect(w, rq, "/hypha/"+newName, http.StatusSeeOther)
@ -160,9 +161,10 @@ func handlerEdit(w http.ResponseWriter, rq *http.Request) {
err error err error
u = user.FromRequest(rq) u = user.FromRequest(rq)
lc = l18n.FromRequest(rq) lc = l18n.FromRequest(rq)
meta = viewutil.MetaFrom(w, rq)
) )
if err := shroom.CanEdit(u, h, lc); err != nil { if err := shroom.CanEdit(u, h, lc); err != nil {
httpErr(w, lc, http.StatusInternalServerError, hyphaName, httpErr(meta, lc, http.StatusInternalServerError, hyphaName,
err.Error()) err.Error())
return return
} }
@ -173,7 +175,7 @@ func handlerEdit(w http.ResponseWriter, rq *http.Request) {
textAreaFill, err = shroom.FetchTextFile(h) textAreaFill, err = shroom.FetchTextFile(h)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
httpErr(w, lc, http.StatusInternalServerError, hyphaName, httpErr(meta, lc, http.StatusInternalServerError, hyphaName,
lc.Get("ui.error_text_fetch")) lc.Get("ui.error_text_fetch"))
return return
} }
@ -181,10 +183,9 @@ func handlerEdit(w http.ResponseWriter, rq *http.Request) {
util.HTTP200Page( util.HTTP200Page(
w, w,
views.Base( views.Base(
meta,
fmt.Sprintf(lc.Get("edit.title"), util.BeautifulName(hyphaName)), fmt.Sprintf(lc.Get("edit.title"), util.BeautifulName(hyphaName)),
views.Editor(rq, hyphaName, textAreaFill, warning), views.Editor(rq, hyphaName, textAreaFill, warning)))
lc,
u))
} }
// handlerUploadText uploads a new text part for the hypha. // handlerUploadText uploads a new text part for the hypha.
@ -198,11 +199,12 @@ func handlerUploadText(w http.ResponseWriter, rq *http.Request) {
message = rq.PostFormValue("message") message = rq.PostFormValue("message")
u = user.FromRequest(rq) u = user.FromRequest(rq)
lc = l18n.FromRequest(rq) lc = l18n.FromRequest(rq)
meta = viewutil.MetaFrom(w, rq)
) )
if action != "Preview" { if action != "Preview" {
if err := shroom.UploadText(h, []byte(textData), message, u); err != nil { if err := shroom.UploadText(h, []byte(textData), message, u); err != nil {
httpErr(w, lc, http.StatusForbidden, hyphaName, err.Error()) httpErr(meta, lc, http.StatusForbidden, hyphaName, err.Error())
return return
} }
} }
@ -213,6 +215,7 @@ func handlerUploadText(w http.ResponseWriter, rq *http.Request) {
util.HTTP200Page( util.HTTP200Page(
w, w,
views.Base( views.Base(
meta,
fmt.Sprintf(lc.Get("edit.preview_title"), util.BeautifulName(hyphaName)), fmt.Sprintf(lc.Get("edit.preview_title"), util.BeautifulName(hyphaName)),
views.Preview( views.Preview(
rq, rq,
@ -220,9 +223,7 @@ func handlerUploadText(w http.ResponseWriter, rq *http.Request) {
textData, textData,
message, message,
"", "",
mycomarkup.BlocksToHTML(ctx, mycomarkup.BlockTree(ctx))), mycomarkup.BlocksToHTML(ctx, mycomarkup.BlockTree(ctx)))))
lc,
u))
} else { } else {
http.Redirect(w, rq, "/hypha/"+hyphaName, http.StatusSeeOther) http.Redirect(w, rq, "/hypha/"+hyphaName, http.StatusSeeOther)
} }
@ -238,13 +239,14 @@ func handlerUploadBinary(w http.ResponseWriter, rq *http.Request) {
u = user.FromRequest(rq) u = user.FromRequest(rq)
lc = l18n.FromRequest(rq) lc = l18n.FromRequest(rq)
file, handler, err = rq.FormFile("binary") file, handler, err = rq.FormFile("binary")
meta = viewutil.MetaFrom(w, rq)
) )
if err != nil { if err != nil {
httpErr(w, lc, http.StatusInternalServerError, hyphaName, httpErr(meta, lc, http.StatusInternalServerError, hyphaName,
err.Error()) err.Error())
} }
if err := shroom.CanAttach(u, h, lc); err != nil { if err := shroom.CanAttach(u, h, lc); err != nil {
httpErr(w, lc, http.StatusInternalServerError, hyphaName, httpErr(meta, lc, http.StatusInternalServerError, hyphaName,
err.Error()) err.Error())
} }
@ -263,7 +265,7 @@ func handlerUploadBinary(w http.ResponseWriter, rq *http.Request) {
) )
if err := shroom.UploadBinary(h, mime, file, u); err != nil { if err := shroom.UploadBinary(h, mime, file, u); err != nil {
httpErr(w, lc, http.StatusInternalServerError, hyphaName, err.Error()) httpErr(meta, lc, http.StatusInternalServerError, hyphaName, err.Error())
return return
} }
http.Redirect(w, rq, "/hypha/"+hyphaName, http.StatusSeeOther) http.Redirect(w, rq, "/hypha/"+hyphaName, http.StatusSeeOther)

View File

@ -48,10 +48,9 @@ func handlerMedia(w http.ResponseWriter, rq *http.Request) {
) )
util.HTTP200Page(w, util.HTTP200Page(w,
views.Base( views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("ui.media_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName)}), lc.Get("ui.media_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName)}),
views.MediaMenu(rq, h, u), views.MediaMenu(rq, h, u)))
lc,
u))
} }
func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) { func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
@ -74,7 +73,7 @@ func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
hyphaName = util.CanonicalName(slug) hyphaName = util.CanonicalName(slug)
h = hyphae.ByName(hyphaName) h = hyphae.ByName(hyphaName)
user = user.FromRequest(rq) user = user.FromRequest(rq)
locale = l18n.FromRequest(rq) lc = l18n.FromRequest(rq)
) )
switch h := h.(type) { switch h := h.(type) {
case *hyphae.EmptyHypha: case *hyphae.EmptyHypha:
@ -82,8 +81,9 @@ func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
io.WriteString(w, "404 not found") io.WriteString(w, "404 not found")
case hyphae.ExistingHypha: case hyphae.ExistingHypha:
util.HTTP200Page(w, views.Base( util.HTTP200Page(w, views.Base(
locale.Get("ui.diff_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName), "rev": revHash}), viewutil.MetaFrom(w, rq),
views.PrimitiveDiff(rq, h, user, revHash), locale, user)) lc.Get("ui.diff_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName), "rev": revHash}),
views.PrimitiveDiff(rq, h, user, revHash)))
} }
} }
@ -134,7 +134,6 @@ func handlerRevision(w http.ResponseWriter, rq *http.Request) {
hyphaName = util.CanonicalName(shorterURL[firstSlashIndex+1:]) hyphaName = util.CanonicalName(shorterURL[firstSlashIndex+1:])
h = hyphae.ByName(hyphaName) h = hyphae.ByName(hyphaName)
contents = fmt.Sprintf(`<p>%s</p>`, lc.Get("ui.revision_no_text")) contents = fmt.Sprintf(`<p>%s</p>`, lc.Get("ui.revision_no_text"))
u = user.FromRequest(rq)
) )
switch h := h.(type) { switch h := h.(type) {
case hyphae.ExistingHypha: case hyphae.ExistingHypha:
@ -157,10 +156,9 @@ func handlerRevision(w http.ResponseWriter, rq *http.Request) {
_, _ = fmt.Fprint( _, _ = fmt.Fprint(
w, w,
views.Base( views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("ui.revision_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName), "rev": revHash}), lc.Get("ui.revision_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName), "rev": revHash}),
page, page,
lc,
u,
), ),
) )
} }
@ -201,7 +199,6 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) {
h = hyphae.ByName(hyphaName) h = hyphae.ByName(hyphaName)
contents string contents string
openGraph string openGraph string
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq) lc = l18n.FromRequest(rq)
) )
@ -209,10 +206,9 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) {
case *hyphae.EmptyHypha: case *hyphae.EmptyHypha:
util.HTTP404Page(w, util.HTTP404Page(w,
views.Base( views.Base(
viewutil.MetaFrom(w, rq),
util.BeautifulName(hyphaName), util.BeautifulName(hyphaName),
views.Hypha(viewutil.MetaFrom(w, rq), h, contents), views.Hypha(viewutil.MetaFrom(w, rq), h, contents),
lc,
u,
openGraph)) openGraph))
case hyphae.ExistingHypha: case hyphae.ExistingHypha:
fileContentsT, errT := os.ReadFile(h.TextFilePath()) fileContentsT, errT := os.ReadFile(h.TextFilePath())
@ -231,10 +227,9 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) {
util.HTTP200Page(w, util.HTTP200Page(w,
views.Base( views.Base(
viewutil.MetaFrom(w, rq),
util.BeautifulName(hyphaName), util.BeautifulName(hyphaName),
views.Hypha(viewutil.MetaFrom(w, rq), h, contents), views.Hypha(viewutil.MetaFrom(w, rq), h, contents),
lc,
u,
openGraph)) openGraph))
} }
} }

View File

@ -1,6 +1,7 @@
package web package web
import ( import (
"github.com/bouncepaw/mycorrhiza/viewutil"
"io" "io"
"net/http" "net/http"
@ -8,7 +9,6 @@ import (
"github.com/bouncepaw/mycorrhiza/l18n" "github.com/bouncepaw/mycorrhiza/l18n"
"github.com/bouncepaw/mycorrhiza/shroom" "github.com/bouncepaw/mycorrhiza/shroom"
"github.com/bouncepaw/mycorrhiza/user"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/views" "github.com/bouncepaw/mycorrhiza/views"
) )
@ -22,17 +22,15 @@ func handlerTitleSearch(w http.ResponseWriter, rq *http.Request) {
_ = rq.ParseForm() _ = rq.ParseForm()
var ( var (
query = rq.FormValue("q") query = rq.FormValue("q")
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq) lc = l18n.FromRequest(rq)
) )
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
_, _ = io.WriteString( _, _ = io.WriteString(
w, w,
views.Base( views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("ui.title_search_title", &l18n.Replacements{"query": query}), lc.Get("ui.title_search_title", &l18n.Replacements{"query": query}),
views.TitleSearch(query, shroom.YieldHyphaNamesContainingString, lc), views.TitleSearch(query, shroom.YieldHyphaNamesContainingString, lc),
lc,
u,
), ),
) )
} }

View File

@ -3,6 +3,7 @@ package web
// stuff.go is used for meta stuff about the wiki or all hyphae at once. // stuff.go is used for meta stuff about the wiki or all hyphae at once.
import ( import (
"github.com/bouncepaw/mycorrhiza/hyphae/backlinks" "github.com/bouncepaw/mycorrhiza/hyphae/backlinks"
"github.com/bouncepaw/mycorrhiza/viewutil"
"io" "io"
"log" "log"
"math/rand" "math/rand"
@ -58,10 +59,11 @@ func handlerHelp(w http.ResponseWriter, rq *http.Request) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
_, _ = io.WriteString( _, _ = io.WriteString(
w, w,
views.Base(lc.Get("help.entry_not_found"), views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("help.entry_not_found"),
views.Help(views.HelpEmptyError(lc), lang, lc), views.Help(views.HelpEmptyError(lc), lang, lc),
lc, ),
user.FromRequest(rq)),
) )
return return
} }
@ -69,10 +71,11 @@ func handlerHelp(w http.ResponseWriter, rq *http.Request) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
_, _ = io.WriteString( _, _ = io.WriteString(
w, w,
views.Base(err.Error(), views.Base(
viewutil.MetaFrom(w, rq),
err.Error(),
views.Help(err.Error(), lang, lc), views.Help(err.Error(), lang, lc),
lc, ),
user.FromRequest(rq)),
) )
return return
} }
@ -84,19 +87,23 @@ func handlerHelp(w http.ResponseWriter, rq *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
_, _ = io.WriteString( _, _ = io.WriteString(
w, w,
views.Base(lc.Get("help.title"), views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("help.title"),
views.Help(result, lang, lc), views.Help(result, lang, lc),
lc, ),
user.FromRequest(rq)),
) )
} }
// handlerList shows a list of all hyphae in the wiki in random order. // handlerList shows a list of all hyphae in the wiki in random order.
func handlerList(w http.ResponseWriter, rq *http.Request) { func handlerList(w http.ResponseWriter, rq *http.Request) {
u := user.FromRequest(rq)
var lc = l18n.FromRequest(rq) var lc = l18n.FromRequest(rq)
util.PrepareRq(rq) util.PrepareRq(rq)
util.HTTP200Page(w, views.Base(lc.Get("ui.list_title"), views.HyphaList(lc), lc, u)) util.HTTP200Page(w, views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("ui.list_title"),
views.HyphaList(lc),
))
} }
// handlerReindex reindexes all hyphae by checking the wiki storage directory anew. // handlerReindex reindexes all hyphae by checking the wiki storage directory anew.
@ -104,7 +111,7 @@ func handlerReindex(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq) util.PrepareRq(rq)
if ok := user.CanProceed(rq, "reindex"); !ok { if ok := user.CanProceed(rq, "reindex"); !ok {
var lc = l18n.FromRequest(rq) var lc = l18n.FromRequest(rq)
httpErr(w, lc, http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.reindex_no_rights")) httpErr(viewutil.MetaFrom(w, rq), lc, http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.reindex_no_rights"))
log.Println("Rejected", rq.URL) log.Println("Rejected", rq.URL)
return return
} }
@ -122,7 +129,7 @@ func handlerUpdateHeaderLinks(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq) util.PrepareRq(rq)
if ok := user.CanProceed(rq, "update-header-links"); !ok { if ok := user.CanProceed(rq, "update-header-links"); !ok {
var lc = l18n.FromRequest(rq) var lc = l18n.FromRequest(rq)
httpErr(w, lc, http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.header_no_rights")) httpErr(viewutil.MetaFrom(w, rq), lc, http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.header_no_rights"))
log.Println("Rejected", rq.URL) log.Println("Rejected", rq.URL)
return return
} }
@ -139,7 +146,7 @@ func handlerRandom(w http.ResponseWriter, rq *http.Request) {
) )
if amountOfHyphae == 0 { if amountOfHyphae == 0 {
var lc = l18n.FromRequest(rq) var lc = l18n.FromRequest(rq)
httpErr(w, lc, http.StatusNotFound, cfg.HomeHypha, lc.Get("ui.random_no_hyphae_tip")) httpErr(viewutil.MetaFrom(w, rq), lc, http.StatusNotFound, cfg.HomeHypha, lc.Get("ui.random_no_hyphae_tip"))
return return
} }
i := rand.Intn(amountOfHyphae) i := rand.Intn(amountOfHyphae)
@ -160,7 +167,11 @@ func handlerAbout(w http.ResponseWriter, rq *http.Request) {
lc = l18n.FromRequest(rq) lc = l18n.FromRequest(rq)
title = lc.Get("ui.about_title", &l18n.Replacements{"name": cfg.WikiName}) title = lc.Get("ui.about_title", &l18n.Replacements{"name": cfg.WikiName})
) )
_, err := io.WriteString(w, views.Base(title, views.AboutHTML(lc), lc, user.FromRequest(rq))) _, err := io.WriteString(w, views.Base(
viewutil.MetaFrom(w, rq),
title,
views.AboutHTML(lc),
))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }

View File

@ -3,6 +3,7 @@ package web
import ( import (
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/viewutil"
"io" "io"
"mime" "mime"
"net/http" "net/http"
@ -21,12 +22,14 @@ import (
var stylesheets = []string{"default.css", "custom.css"} var stylesheets = []string{"default.css", "custom.css"}
// httpErr is used by many handlers to signal errors in a compact way. // httpErr is used by many handlers to signal errors in a compact way.
func httpErr(w http.ResponseWriter, lc *l18n.Localizer, status int, name, errMsg string) { // TODO: get rid of this abomination
w.Header().Set("Content-Type", mime.TypeByExtension(".html")) func httpErr(meta viewutil.Meta, lc *l18n.Localizer, status int, name, errMsg string) {
w.WriteHeader(status) meta.W.(http.ResponseWriter).Header().Set("Content-Type", mime.TypeByExtension(".html"))
meta.W.(http.ResponseWriter).WriteHeader(status)
fmt.Fprint( fmt.Fprint(
w, meta.W,
views.Base( views.Base(
meta,
"Error", "Error",
fmt.Sprintf( fmt.Sprintf(
`<main class="main-width"><p>%s. <a href="/hypha/%s">%s<a></p></main>`, `<main class="main-width"><p>%s. <a href="/hypha/%s">%s<a></p></main>`,
@ -34,8 +37,6 @@ func httpErr(w http.ResponseWriter, lc *l18n.Localizer, status int, name, errMsg
name, name,
lc.Get("ui.error_go_back"), lc.Get("ui.error_go_back"),
), ),
lc,
user.EmptyUser(),
), ),
) )
} }
@ -56,7 +57,7 @@ func handlerUserList(w http.ResponseWriter, rq *http.Request) {
lc := l18n.FromRequest(rq) lc := l18n.FromRequest(rq)
w.Header().Set("Content-Type", mime.TypeByExtension(".html")) w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write([]byte(views.Base(lc.Get("ui.users_title"), views.UserList(lc), lc, user.FromRequest(rq)))) w.Write([]byte(views.Base(viewutil.MetaFrom(w, rq), lc.Get("ui.users_title"), views.UserList(lc))))
} }
func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) { func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) {