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 15:17:00 +00:00
|
|
|
"io/ioutil"
|
2020-06-19 14:30:19 +00:00
|
|
|
"path"
|
|
|
|
"text/template"
|
2020-06-13 16:42:43 +00:00
|
|
|
)
|
|
|
|
|
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-19 15:17:00 +00:00
|
|
|
"Title": fmt.Sprintf(TitleTemplate, "Edit "+name),
|
|
|
|
"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-19 14:30:19 +00:00
|
|
|
func HyphaPage(hyphae map[string]*Hypha, rev Revision, content string) string {
|
2020-06-19 15:17:00 +00:00
|
|
|
sidebar := DefaultSidebar
|
|
|
|
bside, err := ioutil.ReadFile("Hypha/view/sidebar.html")
|
|
|
|
if err == nil {
|
|
|
|
sidebar = string(bside)
|
|
|
|
}
|
2020-06-19 14:30:19 +00:00
|
|
|
keys := map[string]string{
|
2020-06-19 15:17:00 +00:00
|
|
|
"Title": fmt.Sprintf(TitleTemplate, rev.FullName),
|
|
|
|
"Sidebar": sidebar,
|
2020-06-17 15:19:52 +00:00
|
|
|
}
|
2020-06-19 15:17:00 +00:00
|
|
|
return renderBase(renderFromString(content, "Hypha/view/index.html"), keys)
|
2020-06-19 14:30:19 +00:00
|
|
|
}
|
2020-06-17 15:19:52 +00:00
|
|
|
|
2020-06-19 14:30:19 +00:00
|
|
|
/*
|
|
|
|
Collect and render page from base template
|
|
|
|
Args:
|
|
|
|
content: string or pre-rendered template
|
|
|
|
keys: map with replaced standart fields
|
|
|
|
*/
|
|
|
|
func renderBase(content string, keys map[string]string) string {
|
|
|
|
page := map[string]string{
|
2020-06-19 15:17:00 +00:00
|
|
|
"Title": DefaultTitle,
|
|
|
|
"Head": DefaultStyles,
|
|
|
|
"Sidebar": DefaultSidebar,
|
|
|
|
"Main": DefaultContent,
|
|
|
|
"BodyBottom": DefaultBodyBottom,
|
|
|
|
"Header": renderFromString(DefaultHeaderText, "header.html"),
|
|
|
|
"Footer": renderFromString(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 14:30:19 +00:00
|
|
|
func renderFromMap(data map[string]string, templatePath string) string {
|
|
|
|
filePath := path.Join("templates", templatePath)
|
|
|
|
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 14:30:19 +00:00
|
|
|
func renderFromString(data string, templatePath string) string {
|
|
|
|
filePath := path.Join("templates", templatePath)
|
|
|
|
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
|
|
|
}
|