Move the backlink handlers to the web package
This commit is contained in:
parent
99aefded0c
commit
3b6a55f6a7
@ -4,6 +4,7 @@ package backlinks
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/hyphae"
|
||||
"github.com/bouncepaw/mycorrhiza/util"
|
||||
@ -61,6 +62,25 @@ func BacklinksCount(hyphaName string) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func BacklinksFor(hyphaName string) []string {
|
||||
var backlinks []string
|
||||
for b := range yieldHyphaBacklinks(hyphaName) {
|
||||
backlinks = append(backlinks, b)
|
||||
}
|
||||
return backlinks
|
||||
}
|
||||
|
||||
func Orphans() []string {
|
||||
var orphans []string
|
||||
for h := range hyphae.YieldExistingHyphae() {
|
||||
if BacklinksCount(h.CanonicalName()) == 0 {
|
||||
orphans = append(orphans, h.CanonicalName())
|
||||
}
|
||||
}
|
||||
sort.Strings(orphans)
|
||||
return orphans
|
||||
}
|
||||
|
||||
// Using set here seems like the most appropriate solution
|
||||
type linkSet map[string]struct{}
|
||||
|
||||
|
||||
@ -1,79 +0,0 @@
|
||||
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"
|
||||
"net/http"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func InitHandlers(rtr *mux.Router) {
|
||||
rtr.PathPrefix("/backlinks/").HandlerFunc(handlerBacklinks)
|
||||
rtr.PathPrefix("/orphans").HandlerFunc(handlerOrphans)
|
||||
chainBacklinks = viewutil.CopyEnRuWith(fs, "view_backlinks.html", ruTranslation)
|
||||
pageOrphans = newtmpl.NewPage(fs, "view_orphans.html", map[string]string{
|
||||
"orphaned hyphae": "Гифы-сироты",
|
||||
"orphan description": "Ниже перечислены гифы без ссылок на них.",
|
||||
})
|
||||
}
|
||||
|
||||
// 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.CanonicalName()) == 0 {
|
||||
orphans = append(orphans, h.CanonicalName())
|
||||
}
|
||||
}
|
||||
sort.Strings(orphans)
|
||||
|
||||
_ = pageOrphans.RenderTo(viewutil.MetaFrom(w, rq),
|
||||
map[string]any{
|
||||
"Addr": "/orphans",
|
||||
"Orphans": 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}}
|
||||
`
|
||||
chainBacklinks viewutil.Chain
|
||||
|
||||
pageOrphans *newtmpl.Page
|
||||
)
|
||||
|
||||
type backlinksData struct {
|
||||
*viewutil.BaseData
|
||||
HyphaName string
|
||||
Backlinks []string
|
||||
}
|
||||
|
||||
func viewBacklinks(meta viewutil.Meta, hyphaName string, backlinks []string) {
|
||||
viewutil.ExecutePage(meta, chainBacklinks, backlinksData{
|
||||
BaseData: &viewutil.BaseData{
|
||||
Addr: "/backlinks/" + hyphaName,
|
||||
},
|
||||
HyphaName: hyphaName,
|
||||
Backlinks: backlinks,
|
||||
})
|
||||
}
|
||||
23
web/pages.go
Normal file
23
web/pages.go
Normal file
@ -0,0 +1,23 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"github.com/bouncepaw/mycorrhiza/newtmpl"
|
||||
)
|
||||
|
||||
//go:embed views/*.html
|
||||
var fs embed.FS
|
||||
|
||||
var pageOrphans, pageBacklinks *newtmpl.Page
|
||||
|
||||
func initPages() {
|
||||
pageOrphans = newtmpl.NewPage(fs, "views/orphans.html", map[string]string{
|
||||
"orphaned hyphae": "Гифы-сироты",
|
||||
"orphan description": "Ниже перечислены гифы без ссылок на них.",
|
||||
})
|
||||
pageBacklinks = newtmpl.NewPage(fs, "views/backlinks.html", map[string]string{
|
||||
"backlinks to text": `Обратные ссылки на {{.}}`,
|
||||
"backlinks to link": `Обратные ссылки на <a href="/hypha/{{.}}">{{beautifulName .}}</a>`,
|
||||
"description": `Ниже перечислены гифы, на которых есть ссылка на эту гифу, трансклюзия этой гифы или эта гифа вставлена как изображение.`,
|
||||
})
|
||||
}
|
||||
@ -3,6 +3,7 @@ package web
|
||||
import (
|
||||
"fmt"
|
||||
"git.sr.ht/~bouncepaw/mycomarkup/v5"
|
||||
"github.com/bouncepaw/mycorrhiza/backlinks"
|
||||
"github.com/bouncepaw/mycorrhiza/categories"
|
||||
"github.com/bouncepaw/mycorrhiza/files"
|
||||
views2 "github.com/bouncepaw/mycorrhiza/hypview"
|
||||
@ -38,6 +39,10 @@ func initReaders(r *mux.Router) {
|
||||
r.PathPrefix("/media/").HandlerFunc(handlerMedia)
|
||||
r.Path("/today").HandlerFunc(handlerToday)
|
||||
r.Path("/edit-today").HandlerFunc(handlerEditToday)
|
||||
|
||||
// Backlinks
|
||||
r.PathPrefix("/backlinks/").HandlerFunc(handlerBacklinks)
|
||||
r.PathPrefix("/orphans").HandlerFunc(handlerOrphans)
|
||||
}
|
||||
|
||||
func handlerEditToday(w http.ResponseWriter, rq *http.Request) {
|
||||
@ -238,3 +243,23 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) {
|
||||
openGraph))
|
||||
}
|
||||
}
|
||||
|
||||
// handlerBacklinks lists all backlinks to a hypha.
|
||||
func handlerBacklinks(w http.ResponseWriter, rq *http.Request) {
|
||||
hyphaName := util.HyphaNameFromRq(rq, "backlinks")
|
||||
|
||||
_ = pageBacklinks.RenderTo(viewutil.MetaFrom(w, rq),
|
||||
map[string]any{
|
||||
"Addr": "/backlinks/" + hyphaName,
|
||||
"HyphaName": hyphaName,
|
||||
"Backlinks": backlinks.BacklinksFor(hyphaName),
|
||||
})
|
||||
}
|
||||
|
||||
func handlerOrphans(w http.ResponseWriter, rq *http.Request) {
|
||||
_ = pageOrphans.RenderTo(viewutil.MetaFrom(w, rq),
|
||||
map[string]any{
|
||||
"Addr": "/orphans",
|
||||
"Orphans": backlinks.Orphans(),
|
||||
})
|
||||
}
|
||||
|
||||
@ -7,15 +7,14 @@ import (
|
||||
"net/url"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/admin"
|
||||
"github.com/bouncepaw/mycorrhiza/settings"
|
||||
"github.com/bouncepaw/mycorrhiza/auth"
|
||||
"github.com/bouncepaw/mycorrhiza/backlinks"
|
||||
"github.com/bouncepaw/mycorrhiza/categories"
|
||||
"github.com/bouncepaw/mycorrhiza/help"
|
||||
"github.com/bouncepaw/mycorrhiza/history/histweb"
|
||||
"github.com/bouncepaw/mycorrhiza/hypview"
|
||||
"github.com/bouncepaw/mycorrhiza/interwiki"
|
||||
"github.com/bouncepaw/mycorrhiza/misc"
|
||||
"github.com/bouncepaw/mycorrhiza/settings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
@ -56,14 +55,13 @@ func Handler() http.Handler {
|
||||
initReaders(wikiRouter)
|
||||
initMutators(wikiRouter)
|
||||
help.InitHandlers(wikiRouter)
|
||||
backlinks.InitHandlers(wikiRouter)
|
||||
categories.InitHandlers(wikiRouter)
|
||||
misc.InitHandlers(wikiRouter)
|
||||
hypview.Init()
|
||||
histweb.InitHandlers(wikiRouter)
|
||||
interwiki.InitHandlers(wikiRouter)
|
||||
|
||||
// Admin routes.
|
||||
// Admin routes
|
||||
if cfg.UseAuth {
|
||||
adminRouter := wikiRouter.PathPrefix("/admin").Subrouter()
|
||||
adminRouter.Use(groupMiddleware("admin"))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user