2020-06-13 16:42:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-19 14:30:19 +00:00
|
|
|
"bytes"
|
2020-06-13 16:42:43 +00:00
|
|
|
"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"
|
2020-06-13 16:42:43 +00:00
|
|
|
)
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
// EditHyphaPage returns HTML page of hypha editor.
|
2020-06-19 15:17:00 +00:00
|
|
|
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),
|
2020-06-19 15:17:00 +00:00
|
|
|
"Header": renderFromString(name, "Hypha/edit/header.html"),
|
2020-06-19 14:30:19 +00:00
|
|
|
}
|
|
|
|
page := map[string]string{
|
|
|
|
"Text": content,
|
2020-06-19 15:17:00 +00:00
|
|
|
"TextMime": textMime,
|
2020-06-19 14:30:19 +00:00
|
|
|
"Name": name,
|
|
|
|
"Tags": tags,
|
|
|
|
}
|
2020-06-19 15:17:00 +00:00
|
|
|
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
|
|
|
|
2020-06-22 15:04:19 +00:00
|
|
|
// Hypha404 returns 404 page for nonexistent page.
|
|
|
|
func Hypha404(name string) string {
|
|
|
|
return HyphaGeneric(name, name, "Hypha/view/404.html")
|
|
|
|
}
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
// HyphaPage returns HTML page of hypha viewer.
|
|
|
|
func HyphaPage(rev Revision, content string) string {
|
2020-06-22 15:04:19 +00:00
|
|
|
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{
|
2020-06-22 15:04:19 +00:00
|
|
|
"Tree": GetTree(name, true).AsHtml(),
|
2020-06-22 13:58:12 +00:00
|
|
|
}, "Hypha/view/sidebar.html"); bside != "" {
|
|
|
|
sidebar = bside
|
2020-06-19 15:17:00 +00:00
|
|
|
}
|
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),
|
2020-06-19 15:17:00 +00:00
|
|
|
"Sidebar": sidebar,
|
2020-06-17 15:19:52 +00:00
|
|
|
}
|
2020-06-22 15:04:19 +00:00
|
|
|
return renderBase(renderFromString(content, templatePath), keys)
|
2020-06-19 14:30:19 +00:00
|
|
|
}
|
2020-06-17 15:19:52 +00:00
|
|
|
|
2020-06-19 18:11:47 +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
|
|
|
}
|
|
|
|
|
2020-06-19 18:11:47 +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-13 16:42:43 +00:00
|
|
|
}
|
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()
|
|
|
|
}
|
2020-06-13 16:42:43 +00:00
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
// 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()
|
2020-06-13 16:42:43 +00:00
|
|
|
}
|