Move orphans to the new system and fix a bug in it

This commit is contained in:
Timur Ismagilov 2024-06-03 22:46:40 +03:00
parent 04d59927ab
commit 1ac1ee0789
2 changed files with 64 additions and 22 deletions

View File

@ -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"}}Обратные ссылки на <a href="/hypha/{{.}}">{{beautifulName .}}</a>{{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,
})
}

View File

@ -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"}}
<ul class="top-bar__auth auth-links">
<li class="auth-links__box auth-links__user-box">
{{if .Meta.U.Group | eq "anon" }}
<a href="/login" class="auth-links__link auth-links__login-link">
{{block "login" .}}Login{{end}}
</a>
{{else}}
<a href="/hypha/{{block "user hypha" .}}{{end}}/{{.Meta.U.Name}}" class="auth-links__link auth-links__user-link">
{{beautifulName .Meta.U.Name}}
</a>
{{end}}
</li>
{{block "registration" .}}{{end}}
</ul>
{{end}}
`))
}
if cfg.AllowRegistration {
must(en.Parse(`{{define "registration"}}
{{if .Meta.U.Group | eq "anon"}}
<li class="auth-links__box auth-links__register-box">
<a href="/register" class="auth-links__link auth-links__register-link">
{{block "register" .}}Register{{end}}
</a>
</li>
{{end}}
{{end}}`))
}
russianTranslation["search by title"] = "Поиск по названию"
return &Page{
TemplateEnglish: must(template.ParseFS(fs, "base.html")),
TemplateRussian: must(must(template.ParseFS(fs, "base.html")).
TemplateEnglish: en,
TemplateRussian: must(must(en.Clone()).
Parse(translationsIntoTemplates(russianTranslation))),
}
}