1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-15 06:20:27 +00:00
mycorrhiza/hypview/hypview.go
Timur Ismagilov 59deabd0d1 Reimplement Navititle with go templates
That was rough
2022-04-23 23:35:36 +03:00

63 lines
1.4 KiB
Go

package hypview
import (
"embed"
"fmt"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/viewutil"
"log"
"strings"
"text/template"
)
var (
//go:embed *.html
fs embed.FS
ruTranslation = ``
chainNaviTitle viewutil.Chain
)
func Init() {
chainNaviTitle = viewutil.
En(viewutil.CopyEnWith(fs, "view_navititle.html")).
Ru(template.Must(viewutil.CopyRuWith(fs, "view_navititle.html").Parse(ruTranslation)))
}
type naviTitleData struct {
HyphaNameParts []string
HyphaNamePartsWithParents []string
Icon string
HomeHypha string
}
func NaviTitle(meta viewutil.Meta, hyphaName string) string {
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 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)
}
fmt.Printf("%q — %q\n", parts, partsWithParents)
return parts, partsWithParents
}