mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-04-29 13:53:10 +00:00
Render templates from html files
This commit is contained in:
parent
2879096258
commit
ea44003aaf
12
config.go
12
config.go
@ -1,15 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
const (
|
const (
|
||||||
FooterText = `
|
|
||||||
<hr>
|
|
||||||
This website runs <a href="https://github.com/bouncepaw/mycorrhiza">MycorrhizaWiki</a>.
|
|
||||||
`
|
|
||||||
TitleTemplate = `%s at MycorrhizaWiki`
|
TitleTemplate = `%s at MycorrhizaWiki`
|
||||||
|
DefaultTitle = "MycorrhizaWiki"
|
||||||
|
DefaultHeaderText = `MycorrhizaWiki`
|
||||||
|
DefaultFooterText = "MycorrhizaWiki"
|
||||||
|
DefaultSidebar = ""
|
||||||
|
DefaultContent = "It is empty here"
|
||||||
DefaultStyles = `
|
DefaultStyles = `
|
||||||
<link rel="stylesheet" href="/sys/main.css?action=raw">
|
<link rel="stylesheet" href="/sys/main.css?action=raw">
|
||||||
`
|
`
|
||||||
DefaultHeader = `
|
|
||||||
<h1 class="header__site-title">MycorrhizaWiki</h1>
|
|
||||||
` // TODO: Search input
|
|
||||||
)
|
)
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Boilerplate code present in many handlers. Good to have it.
|
// Boilerplate code present in many handlers. Good to have it.
|
||||||
|
4
main.go
4
main.go
@ -2,13 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
// "strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetRevision(hyphae map[string]*Hypha, hyphaName string, rev string, w http.ResponseWriter) (Revision, bool) {
|
func GetRevision(hyphae map[string]*Hypha, hyphaName string, rev string, w http.ResponseWriter) (Revision, bool) {
|
||||||
|
150
render.go
150
render.go
@ -1,100 +1,76 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"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 {
|
func EditHyphaPage(name, text_mime, binary_mime, content, tags string) string {
|
||||||
template := `
|
keys := map[string]string{
|
||||||
<div class="naviwrapper">
|
"Title": fmt.Sprintf(TitleTemplate, name),
|
||||||
<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,
|
|
||||||
}
|
}
|
||||||
|
page := map[string]string{
|
||||||
return Layout(args)
|
"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 {
|
func HyphaPage(hyphae map[string]*Hypha, rev Revision, content string) string {
|
||||||
template := `
|
keys := map[string]string{
|
||||||
<div class="naviwrapper">
|
"Title": fmt.Sprintf(TitleTemplate, rev.FullName),
|
||||||
<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,
|
|
||||||
}
|
}
|
||||||
|
return renderBase(renderFromString(content, "Hypha/index.html"), keys)
|
||||||
return Layout(args)
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gomarkdown/markdown"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gomarkdown/markdown"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Revision struct {
|
type Revision struct {
|
||||||
@ -106,12 +107,10 @@ func (r *Revision) ActionView(w http.ResponseWriter, layoutFun func(map[string]*
|
|||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
fmt.Fprint(w, layoutFun(hyphae, *r, html))
|
fmt.Fprint(w, layoutFun(hyphae, *r, html))
|
||||||
log.Println("Rendering", r.FullName)
|
log.Println("Rendering", r.FullName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Revision) Name() string {
|
|
||||||
return r.FullName
|
|
||||||
}
|
|
||||||
|
36
templates/Hypha/edit.html
Normal file
36
templates/Hypha/edit.html
Normal 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>
|
9
templates/Hypha/index.html
Normal file
9
templates/Hypha/index.html
Normal 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
11
templates/base.html
Normal 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
1
templates/footer.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<h1 class="header__site-title">{{ . }}</h1>
|
1
templates/header.html
Normal file
1
templates/header.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<h1 class="header__site-title">{{ . }}</h1>
|
Loading…
x
Reference in New Issue
Block a user