1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 13:30:26 +00:00
mycorrhiza/render.go

100 lines
2.9 KiB
Go
Raw Normal View History

package main
import (
2020-06-19 14:30:19 +00:00
"bytes"
"fmt"
2020-06-19 14:30:19 +00:00
"path"
"text/template"
2020-06-23 17:53:05 +00:00
"github.com/bouncepaw/mycorrhiza/cfg"
)
// EditHyphaPage returns HTML page of hypha editor.
func EditHyphaPage(name, textMime, content, tags string) string {
2020-06-19 14:30:19 +00:00
keys := map[string]string{
2020-06-23 17:53:05 +00:00
"Title": fmt.Sprintf(cfg.TitleEditTemplate, name),
"Header": renderFromString(name, "Hypha/edit/header.html"),
2020-06-19 14:30:19 +00:00
}
page := map[string]string{
"Text": content,
"TextMime": textMime,
2020-06-19 14:30:19 +00:00
"Name": name,
"Tags": tags,
}
return renderBase(renderFromMap(page, "Hypha/edit/index.html"), keys)
2020-06-19 14:30:19 +00:00
}
2020-06-17 15:19:52 +00:00
// Hypha404 returns 404 page for nonexistent page.
func Hypha404(name string) string {
return HyphaGeneric(name, name, "Hypha/view/404.html")
}
// HyphaPage returns HTML page of hypha viewer.
func HyphaPage(rev Revision, content string) string {
return HyphaGeneric(rev.FullName, content, "Hypha/view/index.html")
}
// HyphaGeneric is used when building renderers for all types of hypha pages
func HyphaGeneric(name string, content, templatePath string) string {
2020-06-23 17:53:05 +00:00
sidebar := cfg.DefaultSidebar
2020-06-22 13:58:12 +00:00
if bside := renderFromMap(map[string]string{
"Tree": GetTree(name, true).AsHtml(),
2020-06-22 13:58:12 +00:00
}, "Hypha/view/sidebar.html"); bside != "" {
sidebar = bside
}
2020-06-19 14:30:19 +00:00
keys := map[string]string{
2020-06-23 17:53:05 +00:00
"Title": fmt.Sprintf(cfg.TitleTemplate, name),
"Sidebar": sidebar,
2020-06-17 15:19:52 +00:00
}
return renderBase(renderFromString(content, templatePath), keys)
2020-06-19 14:30:19 +00:00
}
2020-06-17 15:19:52 +00:00
// renderBase collects and renders page from base template
// Args:
// content: string or pre-rendered template
// keys: map with replaced standart fields
2020-06-19 14:30:19 +00:00
func renderBase(content string, keys map[string]string) string {
page := map[string]string{
2020-06-23 17:53:05 +00:00
"Title": cfg.DefaultTitle,
"Head": cfg.DefaultStyles,
"Sidebar": cfg.DefaultSidebar,
"Main": cfg.DefaultContent,
"BodyBottom": cfg.DefaultBodyBottom,
"Header": renderFromString(cfg.DefaultHeaderText, "header.html"),
"Footer": renderFromString(cfg.DefaultFooterText, "footer.html"),
2020-06-19 14:30:19 +00:00
}
for key, val := range keys {
page[key] = val
}
page["Main"] = content
return renderFromMap(page, "base.html")
2020-06-17 15:19:52 +00:00
}
// renderFromMap applies `data` map to template in `templatePath` and returns the result.
2020-06-19 14:30:19 +00:00
func renderFromMap(data map[string]string, templatePath string) string {
2020-06-23 17:53:05 +00:00
filePath := path.Join(cfg.TemplatesDir, templatePath)
2020-06-19 14:30:19 +00:00
tmpl, err := template.ParseFiles(filePath)
if err != nil {
return err.Error()
}
2020-06-19 14:30:19 +00:00
buf := new(bytes.Buffer)
if err := tmpl.Execute(buf, data); err != nil {
return err.Error()
}
return buf.String()
}
// renderFromMap applies `data` string to template in `templatePath` and returns the result.
2020-06-19 14:30:19 +00:00
func renderFromString(data string, templatePath string) string {
2020-06-23 17:53:05 +00:00
filePath := path.Join(cfg.TemplatesDir, templatePath)
2020-06-19 14:30:19 +00:00
tmpl, err := template.ParseFiles(filePath)
if err != nil {
return err.Error()
}
buf := new(bytes.Buffer)
if err := tmpl.Execute(buf, data); err != nil {
return err.Error()
}
return buf.String()
}