1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-13 05:50:27 +00:00

Render templates from html files

This commit is contained in:
Dan Konshin 2020-06-19 19:30:19 +05:00
parent 2879096258
commit ea44003aaf
10 changed files with 136 additions and 104 deletions

View File

@ -1,15 +1,13 @@
package main
const (
FooterText = `
<hr>
This website runs <a href="https://github.com/bouncepaw/mycorrhiza">MycorrhizaWiki</a>.
`
TitleTemplate = `%s at MycorrhizaWiki`
DefaultStyles = `
TitleTemplate = `%s at MycorrhizaWiki`
DefaultTitle = "MycorrhizaWiki"
DefaultHeaderText = `MycorrhizaWiki`
DefaultFooterText = "MycorrhizaWiki"
DefaultSidebar = ""
DefaultContent = "It is empty here"
DefaultStyles = `
<link rel="stylesheet" href="/sys/main.css?action=raw">
`
DefaultHeader = `
<h1 class="header__site-title">MycorrhizaWiki</h1>
` // TODO: Search input
)

View File

@ -1,9 +1,10 @@
package main
import (
"github.com/gorilla/mux"
"log"
"net/http"
"github.com/gorilla/mux"
)
// Boilerplate code present in many handlers. Good to have it.

View File

@ -2,13 +2,13 @@ package main
import (
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"path/filepath"
// "strconv"
"time"
"github.com/gorilla/mux"
)
func GetRevision(hyphae map[string]*Hypha, hyphaName string, rev string, w http.ResponseWriter) (Revision, bool) {

150
render.go
View File

@ -1,100 +1,76 @@
package main
import (
"bytes"
"fmt"
"path"
"text/template"
)
func Layout(f map[string]string) string {
template := `
<!doctype html>
<html>
<head>
<title>%s</title>
%s
</head>
<body>
<header class="header">%s</header>
<main class="main">%s</main>
<aside class="sidebar">%s</aside>
<footer class="footer">%s</footer>
%s
</body>
</html>
`
return fmt.Sprintf(template, f["title"], f["head"], f["header"], f["main"], f["sidebar"], FooterText, f["bodyBottom"])
}
func EditHyphaPage(name, text_mime, binary_mime, content, tags string) string {
template := `
<div class="naviwrapper">
<form class="naviwrapper__edit edit-box">
<div class="naviwrapper__buttons">
<input type="submit" name="action" value="update"/>
</div>
<div class="edit-box__left">
<h4>Edit box</h4>
<textarea class="edit-box__text" name="text" cols="80" rows="25">
%s
</textarea>
<h4>Upload file</h4>
<p>If this hypha has a file like that, the text above is meant to be a description of it</p>
<input type="file" name="binary"/>
</div>
<div class="edit-box__right">
<h4>Text MIME-type</h4>
<p>Good types are <code>text/markdown</code> and <code>text/plain</code></p>
<input type="text" name="text_mime" value="%s"/>
<h4>Media MIME-type</h4>
<p>For now, only image formats are supported. Choose any, but <code>image/jpeg</code> and <code>image/png</code> are recommended</p>
<input type="text" name="binary_mime" value="%s"/>
<h4>Revision comment</h4>
<p>Please make your comment helpful</p>
<input type="text" name="comment" value="%s"/>
<h4>Edit tags</h4>
<p>Tags are separated by commas, whitespace is ignored</p>
<input type="text" name="comment" value="%s"/>
</div>
</form>
</div>
`
args := map[string]string{
"title": fmt.Sprintf(TitleTemplate, "Edit "+name),
"head": DefaultStyles,
"header": `<h1 class="header__edit-title">Edit ` + name + `</h1>`,
"main": fmt.Sprintf(template, content, text_mime, binary_mime, "Update "+name, tags),
"sidebar": "",
"footer": FooterText,
keys := map[string]string{
"Title": fmt.Sprintf(TitleTemplate, name),
}
return Layout(args)
page := map[string]string{
"Text": content,
"TextMime": text_mime,
"BinMime": binary_mime,
"Name": name,
"Tags": tags,
}
return renderBase(renderFromMap(page, "Hypha/edit.html"), keys)
}
func HyphaPage(hyphae map[string]*Hypha, rev Revision, content string) string {
template := `
<div class="naviwrapper">
<form class="naviwrapper__buttons">
<input type="submit" name="action" value="edit"/>
<input type="submit" name="action" value="getBinary"/>
<input type="submit" name="action" value="zen"/>
<input type="submit" name="action" value="raw"/>
</form>
%s
</div>
`
args := map[string]string{
"title": fmt.Sprintf(TitleTemplate, rev.FullName),
"head": DefaultStyles,
"header": DefaultHeader,
"main": fmt.Sprintf(template, content),
"sidebar": "",
"footer": FooterText,
keys := map[string]string{
"Title": fmt.Sprintf(TitleTemplate, rev.FullName),
}
return Layout(args)
return renderBase(renderFromString(content, "Hypha/index.html"), keys)
}
/*
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{
"Title": DefaultTitle,
"Header": renderFromString(DefaultHeaderText, "header.html"),
"Footer": renderFromString(DefaultFooterText, "footer.html"),
"Sidebar": DefaultSidebar,
"Main": DefaultContent,
}
for key, val := range keys {
page[key] = val
}
page["Main"] = content
return renderFromMap(page, "base.html")
}
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()
}
buf := new(bytes.Buffer)
if err := tmpl.Execute(buf, data); err != nil {
return err.Error()
}
return buf.String()
}
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()
}

View File

@ -2,10 +2,11 @@ package main
import (
"fmt"
"github.com/gomarkdown/markdown"
"io/ioutil"
"log"
"net/http"
"github.com/gomarkdown/markdown"
)
type Revision struct {
@ -106,12 +107,10 @@ func (r *Revision) ActionView(w http.ResponseWriter, layoutFun func(map[string]*
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, layoutFun(hyphae, *r, html))
log.Println("Rendering", r.FullName)
}
func (r *Revision) Name() string {
return r.FullName
}

36
templates/Hypha/edit.html Normal file
View File

@ -0,0 +1,36 @@
<div class="naviwrapper">
<form class="naviwrapper__edit edit-box">
<div class="naviwrapper__buttons">
<input type="submit" name="action" value="update"/>
</div>
<div class="edit-box__left">
<h4>Edit box</h4>
<textarea class="edit-box__text" name="text" cols="80" rows="25">
{{ .Text }}
</textarea>
<h4>Upload file</h4>
<p>If this hypha has a file like that, the text above is meant to be a description of it</p>
<input type="file" name="binary"/>
</div>
<div class="edit-box__right">
<h4>Text MIME-type</h4>
<p>Good types are <code>text/markdown</code> and <code>text/plain</code></p>
<input type="text" name="text_mime" value="{{ .TextMime }}"/>
<h4>Media MIME-type</h4>
<p>For now, only image formats are supported. Choose any, but <code>image/jpeg</code> and <code>image/png</code> are recommended</p>
<input type="text" name="binary_mime" value="{{ .BinMime }}"/>
<h4>Revision comment</h4>
<p>Please make your comment helpful</p>
<input type="text" name="comment" value="Update {{ .Name }}"/>
<h4>Edit tags</h4>
<p>Tags are separated by commas, whitespace is ignored</p>
<input type="text" name="comment" value="{{ .Tags }}"/>
</div>
</form>
</div>

View File

@ -0,0 +1,9 @@
<div class="naviwrapper">
<form class="naviwrapper__buttons">
<input type="submit" name="action" value="edit"/>
<input type="submit" name="action" value="getBinary"/>
<input type="submit" name="action" value="zen"/>
<input type="submit" name="action" value="raw"/>
</form>
{{ . }}
</div>

11
templates/base.html Normal file
View File

@ -0,0 +1,11 @@
<html>
<head>
<title>{{ .Title }}</title>
</head>
<body>
<header class="header">{{ .Header }}</header>
<main class="main">{{ .Main }}</main>
<aside class="sidebar">{{ .Sidebar }}</aside>
<footer class="footer">{{ .Footer }}</footer>
</body>
</html>

1
templates/footer.html Normal file
View File

@ -0,0 +1 @@
<h1 class="header__site-title">{{ . }}</h1>

1
templates/header.html Normal file
View File

@ -0,0 +1 @@
<h1 class="header__site-title">{{ . }}</h1>