diff --git a/backlinks/web.go b/backlinks/web.go index ff5cb75..47e95ee 100644 --- a/backlinks/web.go +++ b/backlinks/web.go @@ -3,6 +3,7 @@ package backlinks import ( "embed" "github.com/bouncepaw/mycorrhiza/hyphae" + "github.com/bouncepaw/mycorrhiza/newtmpl" "github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/viewutil" "github.com/gorilla/mux" @@ -14,7 +15,10 @@ func InitHandlers(rtr *mux.Router) { rtr.PathPrefix("/backlinks/").HandlerFunc(handlerBacklinks) rtr.PathPrefix("/orphans").HandlerFunc(handlerOrphans) chainBacklinks = viewutil.CopyEnRuWith(fs, "view_backlinks.html", ruTranslation) - chainOrphans = viewutil.CopyEnRuWith(fs, "view_orphans.html", ruTranslation) + pageOrphans = newtmpl.NewPage(fs, "view_orphans.html", map[string]string{ + "orphaned hyphae": "Гифы-сироты", + "orphan description": "Ниже перечислены гифы без ссылок на них.", + }) } // handlerBacklinks lists all backlinks to a hypha. @@ -37,7 +41,12 @@ func handlerOrphans(w http.ResponseWriter, rq *http.Request) { } } sort.Strings(orphans) - viewOrphans(viewutil.MetaFrom(w, rq), orphans) + + _ = pageOrphans.RenderTo(viewutil.MetaFrom(w, rq), + map[string]any{ + "Addr": "/orphans", + "Orphans": orphans, + }) } var ( @@ -47,11 +56,10 @@ var ( {{define "backlinks to text"}}Обратные ссылки на {{.}}{{end}} {{define "backlinks to link"}}Обратные ссылки на {{beautifulName .}}{{end}} {{define "description"}}Ниже перечислены гифы, на которых есть ссылка на эту гифу, трансклюзия этой гифы или эта гифа вставлена как изображение.{{end}} -{{define "orphaned hyphae"}}Гифы-сироты{{end}} -{{define "orphan description"}}Ниже перечислены гифы без ссылок на них.{{end}} ` chainBacklinks viewutil.Chain - chainOrphans viewutil.Chain + + pageOrphans *newtmpl.Page ) type backlinksData struct { @@ -69,17 +77,3 @@ func viewBacklinks(meta viewutil.Meta, hyphaName string, backlinks []string) { Backlinks: backlinks, }) } - -type orphansData struct { - *viewutil.BaseData - Orphans []string -} - -func viewOrphans(meta viewutil.Meta, orphans []string) { - viewutil.ExecutePage(meta, chainOrphans, orphansData{ - BaseData: &viewutil.BaseData{ - Addr: "/orphans", - }, - Orphans: orphans, - }) -} diff --git a/newtmpl/newtmpl.go b/newtmpl/newtmpl.go index 1f0ce1a..62a526a 100644 --- a/newtmpl/newtmpl.go +++ b/newtmpl/newtmpl.go @@ -4,6 +4,7 @@ import ( "embed" "fmt" "github.com/bouncepaw/mycorrhiza/cfg" + "github.com/bouncepaw/mycorrhiza/util" "html/template" "strings" @@ -13,17 +14,64 @@ import ( //go:embed *.html var fs embed.FS +var base = template.Must(template.ParseFS(fs, "base.html")) + type Page struct { TemplateEnglish *template.Template TemplateRussian *template.Template } -func NewPage(tmpl string, russianTranslation map[string]string) *Page { +func NewPage(fs embed.FS, tmpl string, russianTranslation map[string]string) *Page { must := template.Must + en := must(must(must( + base.Clone()). + Funcs(template.FuncMap{ + "beautifulName": util.BeautifulName, + "inc": func(i int) int { return i + 1 }, + }). + Parse(fmt.Sprintf(` +{{define "wiki name"}}%s{{end}} +{{define "user hypha"}}%s{{end}} +`, cfg.WikiName, cfg.UserHypha))). + ParseFS(fs, tmpl)) + + if cfg.UseAuth { + en = must(en.Parse(` +{{define "auth"}} +
+{{end}} +`)) + } + if cfg.AllowRegistration { + must(en.Parse(`{{define "registration"}} +{{if .Meta.U.Group | eq "anon"}} +