mycorrhiza/hypview/hypview.go
Timur Ismagilov 41733c50bd
New templates #117 (#236)
Didn't have the chance to migrate //all// templates just yet. We'll get there.

* Implement yet another template system

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

* Link orphans in the admin panel

* Move the backlink handlers to the web package

* Move auth routing to web

* Move /user-list to the new system

* Move change password and translate it

* Move stuff

* Move admin-related stuff to the web

* Move a lot of files into internal dir

Outside of it are web and stuff that needs further refactoring

* Fix static not loading and de-qtpl tree

* Move tree to internal

* Keep the globe on the same line #230

* Revert "Keep the globe on the same line #230"

This reverts commit ae78e5e459.

* Migrate templates from hypview: delete, edit, start empty and existing WIP

The delete media view was removed, I didn't even know it still existed as a GET. A rudiment.

* Make views multi-file and break compilation

* Megarefactoring of hypha views

* Auth-related stuffs

* Fix some of those weird imports

* Migrate cat views

* Fix cat js

* Lower standards

* Internalize trauma
2024-09-07 21:22:41 +03:00

88 lines
2.5 KiB
Go

package hypview
import (
"embed"
"html/template"
"log"
"strings"
"github.com/bouncepaw/mycorrhiza/internal/backlinks"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/web/viewutil"
)
var (
//go:embed *.html
fs embed.FS
ruTranslation = `
{{define "rename hypha?"}}Переименовать {{beautifulName .}}?{{end}}
{{define "rename [[hypha]]?"}}Переименовать <a href="/hypha/{{.}}">{{beautifulName .}}</a>?{{end}}
{{define "new name"}}Новое название:{{end}}
{{define "rename recursively"}}Также переименовать подгифы{{end}}
{{define "rename tip"}}Переименовывайте аккуратно. <a href="/help/en/rename">Документация на английском.</a>{{end}}
{{define "leave redirection"}}Оставить перенаправление{{end}}
`
chainNaviTitle viewutil.Chain
chainRenameHypha viewutil.Chain
)
func Init() {
chainNaviTitle = viewutil.CopyEnRuWith(fs, "view_navititle.html", "")
chainRenameHypha = viewutil.CopyEnRuWith(fs, "view_rename.html", ruTranslation)
}
type renameData struct {
*viewutil.BaseData
HyphaName string
LeaveRedirectionDefault bool
}
func RenameHypha(meta viewutil.Meta, hyphaName string) {
viewutil.ExecutePage(meta, chainRenameHypha, renameData{
BaseData: &viewutil.BaseData{
Addr: "/rename/" + hyphaName,
},
HyphaName: hyphaName,
LeaveRedirectionDefault: backlinks.BacklinksCount(hyphaName) != 0,
})
}
type naviTitleData struct {
HyphaNameParts []string
HyphaNamePartsWithParents []string
Icon string
HomeHypha string
}
func NaviTitle(meta viewutil.Meta, hyphaName string) template.HTML {
parts, partsWithParents := naviTitleify(hyphaName)
var buf strings.Builder
err := chainNaviTitle.Get(meta).ExecuteTemplate(&buf, "navititle", naviTitleData{
HyphaNameParts: parts,
HyphaNamePartsWithParents: partsWithParents,
Icon: cfg.NaviTitleIcon,
HomeHypha: cfg.HomeHypha,
})
if err != nil {
log.Println(err)
}
return template.HTML(buf.String())
}
func naviTitleify(hyphaName string) ([]string, []string) {
var (
prevAcc = "/hypha"
parts = strings.Split(hyphaName, "/")
partsWithParents []string
)
for _, part := range parts {
prevAcc += "/" + part
partsWithParents = append(partsWithParents, prevAcc)
}
return parts, partsWithParents
}