1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-01-08 02:40:26 +00:00
mycorrhiza/backlinks/web.go
Timur Ismagilov b2e504ec06 Implement /orphans
This page lists hyphae which have no backlinks
2022-05-07 01:21:38 +05:00

103 lines
3.0 KiB
Go

package backlinks
import (
"embed"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/hyphae"
"github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/viewutil"
"github.com/gorilla/mux"
"log"
"net/http"
"sort"
"text/template"
)
func InitHandlers(rtr *mux.Router) {
rtr.PathPrefix("/backlinks/").HandlerFunc(handlerBacklinks)
rtr.PathPrefix("/orphans").HandlerFunc(handlerOrphans)
chainBacklinks = viewutil.
En(viewutil.CopyEnWith(fs, "view_backlinks.html")).
Ru(template.Must(viewutil.CopyRuWith(fs, "view_backlinks.html").Parse(ruTranslation)))
chainOrphans = viewutil.
En(viewutil.CopyEnWith(fs, "view_orphans.html")).
Ru(template.Must(viewutil.CopyRuWith(fs, "view_orphans.html").Parse(ruTranslation)))
}
// handlerBacklinks lists all backlinks to a hypha.
func handlerBacklinks(w http.ResponseWriter, rq *http.Request) {
var (
hyphaName = util.HyphaNameFromRq(rq, "backlinks")
backlinks []string
)
for b := range yieldHyphaBacklinks(hyphaName) {
backlinks = append(backlinks, b)
}
viewBacklinks(viewutil.MetaFrom(w, rq), hyphaName, backlinks)
}
func handlerOrphans(w http.ResponseWriter, rq *http.Request) {
var orphans []string
for h := range hyphae.YieldExistingHyphae() {
if BacklinksCount(h) == 0 {
orphans = append(orphans, h.CanonicalName())
}
}
sort.Strings(orphans)
viewOrphans(viewutil.MetaFrom(w, rq), orphans)
}
var (
//go:embed *.html
fs embed.FS
ruTranslation = `
{{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
)
type backlinksData struct {
viewutil.BaseData
HyphaName string
Backlinks []string
}
func viewBacklinks(meta viewutil.Meta, hyphaName string, backlinks []string) {
if err := chainBacklinks.Get(meta).ExecuteTemplate(meta.W, "page", backlinksData{
BaseData: viewutil.BaseData{
Meta: meta,
Addr: "/backlinks/" + hyphaName,
HeaderLinks: cfg.HeaderLinks,
CommonScripts: cfg.CommonScripts,
},
HyphaName: hyphaName,
Backlinks: backlinks,
}); err != nil {
log.Println(err)
}
}
type orphansData struct {
viewutil.BaseData
Orphans []string
}
func viewOrphans(meta viewutil.Meta, orphans []string) {
if err := chainOrphans.Get(meta).ExecuteTemplate(meta.W, "page", orphansData{
BaseData: viewutil.BaseData{
Meta: meta,
Addr: "/orphans",
HeaderLinks: cfg.HeaderLinks,
CommonScripts: cfg.CommonScripts,
},
Orphans: orphans,
}); err != nil {
log.Println(err)
}
}