1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-01-05 17:40: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)
}
_, err = io.WriteString(meta.W, Base(
meta,
templateAsString(localizedAdminTemplates(meta), "panel title"),
buf.String(),
meta.Lc,
meta.U,
))
}

View File

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

View File

@ -5,8 +5,6 @@ import (
"embed"
"fmt"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/l18n"
"github.com/bouncepaw/mycorrhiza/user"
"github.com/bouncepaw/mycorrhiza/util"
"log"
"strings"
@ -54,7 +52,7 @@ func localizedBaseWithWeirdBody(meta Meta) *template.Template {
}
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 {
@ -67,13 +65,9 @@ type baseData struct {
}
// 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
meta := Meta{
Lc: lc,
U: u,
W: &w,
}
meta.W = &w
t := localizedBaseWithWeirdBody(meta)
err := t.ExecuteTemplate(&w, "base", baseData{
Meta: meta,

View File

@ -71,7 +71,7 @@ func handlerAdminUsers(w http.ResponseWriter, rq *http.Request) {
var lc = l18n.FromRequest(rq)
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"))
io.WriteString(w, html)
@ -110,7 +110,7 @@ func handlerAdminUserEdit(w http.ResponseWriter, rq *http.Request) {
var lc = l18n.FromRequest(rq)
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() {
w.WriteHeader(http.StatusBadRequest)
@ -140,7 +140,7 @@ func handlerAdminUserDelete(w http.ResponseWriter, rq *http.Request) {
var lc = l18n.FromRequest(rq)
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() {
w.WriteHeader(http.StatusBadRequest)
@ -154,7 +154,7 @@ func handlerAdminUserNew(w http.ResponseWriter, rq *http.Request) {
if rq.Method == http.MethodGet {
// New user form
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"))
io.WriteString(w, html)
@ -166,7 +166,7 @@ func handlerAdminUserNew(w http.ResponseWriter, rq *http.Request) {
if err != nil {
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.Header().Set("Content-Type", mime.TypeByExtension(".html"))

View File

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

View File

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

View File

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

View File

@ -2,6 +2,7 @@ package web
import (
"fmt"
"github.com/bouncepaw/mycorrhiza/viewutil"
"log"
"net/http"
@ -32,31 +33,31 @@ func initMutators(r *mux.Router) {
func handlerRemoveMedia(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
var (
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq)
h = hyphae.ByName(util.HyphaNameFromRq(rq, "delete"))
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq)
h = hyphae.ByName(util.HyphaNameFromRq(rq, "delete"))
meta = viewutil.MetaFrom(w, rq)
)
if !u.CanProceed("remove-media") {
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "no rights")
httpErr(meta, lc, http.StatusForbidden, h.CanonicalName(), "no rights")
return
}
if rq.Method == "GET" {
util.HTTP200Page(
w,
views.Base(
meta,
fmt.Sprintf(lc.Get("ui.ask_remove_media"), util.BeautifulName(h.CanonicalName())),
views.RemoveMediaAsk(rq, h.CanonicalName()),
lc,
u))
views.RemoveMediaAsk(rq, h.CanonicalName())))
return
}
switch h := h.(type) {
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
case *hyphae.MediaHypha:
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
}
}
@ -65,14 +66,15 @@ func handlerRemoveMedia(w http.ResponseWriter, rq *http.Request) {
func handlerDelete(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
var (
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq)
h = hyphae.ByName(util.HyphaNameFromRq(rq, "delete"))
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq)
h = hyphae.ByName(util.HyphaNameFromRq(rq, "delete"))
meta = viewutil.MetaFrom(w, rq)
)
if !u.CanProceed("delete") {
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
}
@ -80,7 +82,7 @@ func handlerDelete(w http.ResponseWriter, rq *http.Request) {
case *hyphae.EmptyHypha:
log.Printf("%s tries to delete empty hypha %s\n", u.Name, h.CanonicalName())
// 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
}
@ -88,16 +90,15 @@ func handlerDelete(w http.ResponseWriter, rq *http.Request) {
util.HTTP200Page(
w,
views.Base(
meta,
fmt.Sprintf(lc.Get("ui.ask_delete"), util.BeautifulName(h.CanonicalName())),
views.DeleteAsk(rq, h.CanonicalName()),
lc,
u))
views.DeleteAsk(rq, h.CanonicalName())))
return
}
if err := shroom.Delete(u, h.(hyphae.ExistingHypha)); err != nil {
log.Println(err)
httpErr(w, lc, http.StatusInternalServerError, h.CanonicalName(), err.Error())
httpErr(meta, lc, http.StatusInternalServerError, h.CanonicalName(), err.Error())
return
}
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) {
util.PrepareRq(rq)
var (
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq)
h = hyphae.ByName(util.HyphaNameFromRq(rq, "rename"))
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq)
h = hyphae.ByName(util.HyphaNameFromRq(rq, "rename"))
meta = viewutil.MetaFrom(w, rq)
)
switch h.(type) {
case *hyphae.EmptyHypha:
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
}
if !u.CanProceed("rename") {
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
}
@ -134,16 +136,15 @@ func handlerRename(w http.ResponseWriter, rq *http.Request) {
util.HTTP200Page(
w,
views.Base(
meta,
fmt.Sprintf(lc.Get("ui.ask_rename"), util.BeautifulName(oldHypha.CanonicalName())),
views.RenameAsk(rq, oldHypha.CanonicalName()),
lc,
u))
views.RenameAsk(rq, oldHypha.CanonicalName())))
return
}
if err := shroom.Rename(oldHypha, newName, recursive, u); err != nil {
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
}
http.Redirect(w, rq, "/hypha/"+newName, http.StatusSeeOther)
@ -160,9 +161,10 @@ func handlerEdit(w http.ResponseWriter, rq *http.Request) {
err error
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq)
meta = viewutil.MetaFrom(w, rq)
)
if err := shroom.CanEdit(u, h, lc); err != nil {
httpErr(w, lc, http.StatusInternalServerError, hyphaName,
httpErr(meta, lc, http.StatusInternalServerError, hyphaName,
err.Error())
return
}
@ -173,7 +175,7 @@ func handlerEdit(w http.ResponseWriter, rq *http.Request) {
textAreaFill, err = shroom.FetchTextFile(h)
if err != nil {
log.Println(err)
httpErr(w, lc, http.StatusInternalServerError, hyphaName,
httpErr(meta, lc, http.StatusInternalServerError, hyphaName,
lc.Get("ui.error_text_fetch"))
return
}
@ -181,10 +183,9 @@ func handlerEdit(w http.ResponseWriter, rq *http.Request) {
util.HTTP200Page(
w,
views.Base(
meta,
fmt.Sprintf(lc.Get("edit.title"), util.BeautifulName(hyphaName)),
views.Editor(rq, hyphaName, textAreaFill, warning),
lc,
u))
views.Editor(rq, hyphaName, textAreaFill, warning)))
}
// 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")
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq)
meta = viewutil.MetaFrom(w, rq)
)
if action != "Preview" {
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
}
}
@ -213,6 +215,7 @@ func handlerUploadText(w http.ResponseWriter, rq *http.Request) {
util.HTTP200Page(
w,
views.Base(
meta,
fmt.Sprintf(lc.Get("edit.preview_title"), util.BeautifulName(hyphaName)),
views.Preview(
rq,
@ -220,9 +223,7 @@ func handlerUploadText(w http.ResponseWriter, rq *http.Request) {
textData,
message,
"",
mycomarkup.BlocksToHTML(ctx, mycomarkup.BlockTree(ctx))),
lc,
u))
mycomarkup.BlocksToHTML(ctx, mycomarkup.BlockTree(ctx)))))
} else {
http.Redirect(w, rq, "/hypha/"+hyphaName, http.StatusSeeOther)
}
@ -238,13 +239,14 @@ func handlerUploadBinary(w http.ResponseWriter, rq *http.Request) {
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq)
file, handler, err = rq.FormFile("binary")
meta = viewutil.MetaFrom(w, rq)
)
if err != nil {
httpErr(w, lc, http.StatusInternalServerError, hyphaName,
httpErr(meta, lc, http.StatusInternalServerError, hyphaName,
err.Error())
}
if err := shroom.CanAttach(u, h, lc); err != nil {
httpErr(w, lc, http.StatusInternalServerError, hyphaName,
httpErr(meta, lc, http.StatusInternalServerError, hyphaName,
err.Error())
}
@ -263,7 +265,7 @@ func handlerUploadBinary(w http.ResponseWriter, rq *http.Request) {
)
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
}
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,
views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("ui.media_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName)}),
views.MediaMenu(rq, h, u),
lc,
u))
views.MediaMenu(rq, h, u)))
}
func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
@ -74,7 +73,7 @@ func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
hyphaName = util.CanonicalName(slug)
h = hyphae.ByName(hyphaName)
user = user.FromRequest(rq)
locale = l18n.FromRequest(rq)
lc = l18n.FromRequest(rq)
)
switch h := h.(type) {
case *hyphae.EmptyHypha:
@ -82,8 +81,9 @@ func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
io.WriteString(w, "404 not found")
case hyphae.ExistingHypha:
util.HTTP200Page(w, views.Base(
locale.Get("ui.diff_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName), "rev": revHash}),
views.PrimitiveDiff(rq, h, user, revHash), locale, user))
viewutil.MetaFrom(w, rq),
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:])
h = hyphae.ByName(hyphaName)
contents = fmt.Sprintf(`<p>%s</p>`, lc.Get("ui.revision_no_text"))
u = user.FromRequest(rq)
)
switch h := h.(type) {
case hyphae.ExistingHypha:
@ -157,10 +156,9 @@ func handlerRevision(w http.ResponseWriter, rq *http.Request) {
_, _ = fmt.Fprint(
w,
views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("ui.revision_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName), "rev": revHash}),
page,
lc,
u,
),
)
}
@ -201,7 +199,6 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) {
h = hyphae.ByName(hyphaName)
contents string
openGraph string
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq)
)
@ -209,10 +206,9 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) {
case *hyphae.EmptyHypha:
util.HTTP404Page(w,
views.Base(
viewutil.MetaFrom(w, rq),
util.BeautifulName(hyphaName),
views.Hypha(viewutil.MetaFrom(w, rq), h, contents),
lc,
u,
openGraph))
case hyphae.ExistingHypha:
fileContentsT, errT := os.ReadFile(h.TextFilePath())
@ -231,10 +227,9 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) {
util.HTTP200Page(w,
views.Base(
viewutil.MetaFrom(w, rq),
util.BeautifulName(hyphaName),
views.Hypha(viewutil.MetaFrom(w, rq), h, contents),
lc,
u,
openGraph))
}
}

View File

@ -1,6 +1,7 @@
package web
import (
"github.com/bouncepaw/mycorrhiza/viewutil"
"io"
"net/http"
@ -8,7 +9,6 @@ import (
"github.com/bouncepaw/mycorrhiza/l18n"
"github.com/bouncepaw/mycorrhiza/shroom"
"github.com/bouncepaw/mycorrhiza/user"
"github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/views"
)
@ -22,17 +22,15 @@ func handlerTitleSearch(w http.ResponseWriter, rq *http.Request) {
_ = rq.ParseForm()
var (
query = rq.FormValue("q")
u = user.FromRequest(rq)
lc = l18n.FromRequest(rq)
)
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(
w,
views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("ui.title_search_title", &l18n.Replacements{"query": query}),
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.
import (
"github.com/bouncepaw/mycorrhiza/hyphae/backlinks"
"github.com/bouncepaw/mycorrhiza/viewutil"
"io"
"log"
"math/rand"
@ -58,10 +59,11 @@ func handlerHelp(w http.ResponseWriter, rq *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = io.WriteString(
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),
lc,
user.FromRequest(rq)),
),
)
return
}
@ -69,10 +71,11 @@ func handlerHelp(w http.ResponseWriter, rq *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = io.WriteString(
w,
views.Base(err.Error(),
views.Base(
viewutil.MetaFrom(w, rq),
err.Error(),
views.Help(err.Error(), lang, lc),
lc,
user.FromRequest(rq)),
),
)
return
}
@ -84,19 +87,23 @@ func handlerHelp(w http.ResponseWriter, rq *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(
w,
views.Base(lc.Get("help.title"),
views.Base(
viewutil.MetaFrom(w, rq),
lc.Get("help.title"),
views.Help(result, lang, lc),
lc,
user.FromRequest(rq)),
),
)
}
// handlerList shows a list of all hyphae in the wiki in random order.
func handlerList(w http.ResponseWriter, rq *http.Request) {
u := user.FromRequest(rq)
var lc = l18n.FromRequest(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.
@ -104,7 +111,7 @@ func handlerReindex(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
if ok := user.CanProceed(rq, "reindex"); !ok {
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)
return
}
@ -122,7 +129,7 @@ func handlerUpdateHeaderLinks(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
if ok := user.CanProceed(rq, "update-header-links"); !ok {
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)
return
}
@ -139,7 +146,7 @@ func handlerRandom(w http.ResponseWriter, rq *http.Request) {
)
if amountOfHyphae == 0 {
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
}
i := rand.Intn(amountOfHyphae)
@ -160,7 +167,11 @@ func handlerAbout(w http.ResponseWriter, rq *http.Request) {
lc = l18n.FromRequest(rq)
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 {
log.Println(err)
}

View File

@ -3,6 +3,7 @@ package web
import (
"fmt"
"github.com/bouncepaw/mycorrhiza/viewutil"
"io"
"mime"
"net/http"
@ -21,12 +22,14 @@ import (
var stylesheets = []string{"default.css", "custom.css"}
// 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) {
w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
w.WriteHeader(status)
// TODO: get rid of this abomination
func httpErr(meta viewutil.Meta, lc *l18n.Localizer, status int, name, errMsg string) {
meta.W.(http.ResponseWriter).Header().Set("Content-Type", mime.TypeByExtension(".html"))
meta.W.(http.ResponseWriter).WriteHeader(status)
fmt.Fprint(
w,
meta.W,
views.Base(
meta,
"Error",
fmt.Sprintf(
`<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,
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)
w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
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) {