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

Merge pull request #6 from DanInSpace104/features/templates-from-html

Features/templates from html
This commit is contained in:
Timur Ismagilov 2020-06-19 20:51:45 +05:00 committed by GitHub
commit 550bcbd444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 182 additions and 131 deletions

View File

@ -1,14 +1,14 @@
package main
const (
FooterText = `
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 = `This website runs <a href="https://github.com/bouncepaw/mycorrhiza">MycorrhizaWiki</a>.`
DefaultSidebar = ""
DefaultBodyBottom = ""
DefaultContent = "It is empty here"
DefaultStyles = `
<link rel="stylesheet" href="/sys/main.css?action=raw">
`
DefaultHeader = `
<h1 class="site-title">MycorrhizaWiki 🍄</h1>
` // TODO: Search input
)

View File

@ -1,7 +1,6 @@
package main
import (
"github.com/gorilla/mux"
"io/ioutil"
"log"
"net/http"
@ -9,6 +8,8 @@ import (
"strconv"
"strings"
"time"
"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) {

187
render.go
View File

@ -1,126 +1,85 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"path"
"text/template"
)
func Layout(f map[string]string) string {
template := `
<!doctype html>
<html>
<head>
<title>%s</title>
%s
</head>
<body>
<div class="shroom">
<button class="shroom__button" id="shroomBtn"><span>🍄</span> Open mycelium</button>
</div>
<main class="main">%s</main>
<div class="left-panel" id="shroomburgerMenu">
<div class="left-panel__in">
<div class="shroom mushroom">
<button class="shroom__button" id="mushroomBtn"><span>🍄</span> Close mycelium</button>
</div>
<div class="left-panel__contents">
<header class="header">%s</header>
<aside class="sidebar">%s</aside>
<footer class="footer">%s</footer>
</div>
</div>
</div>
%s
</body>
</html>
`
return fmt.Sprintf(template, f["title"], f["head"], f["main"], f["header"], f["sidebar"], FooterText, f["bodyBottom"])
}
func EditHyphaPage(name, text_mime, content, tags string) string {
template := `
<div class="naviwrapper">
<form class="naviwrapper__edit edit-box"
method="POST"
enctype="multipart/form-data"
action="?action=update">
<div class="naviwrapper__buttons">
<input type="submit" 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>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="tags" 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, "Update "+name, tags),
"sidebar": "",
"footer": FooterText,
func EditHyphaPage(name, textMime, content, tags string) string {
keys := map[string]string{
"Title": fmt.Sprintf(TitleTemplate, "Edit "+name),
"Header": renderFromString(name, "Hypha/edit/header.html"),
}
return Layout(args)
page := map[string]string{
"Text": content,
"TextMime": textMime,
"Name": name,
"Tags": tags,
}
return renderBase(renderFromMap(page, "Hypha/edit/index.html"), keys)
}
func HyphaPage(hyphae map[string]*Hypha, rev Revision, content string) string {
sidebar := `
<div class="naviwrapper">
<div class="hypha-actions">
<ul>
<li><a href="?action=edit">Edit</a>
<li><a href="?action=getBinary">Download</a>
<li><a href="?action=zen">Zen mode</a>
<li><a href="?action=raw">View raw</a>
</ul>
</div>
</div>
`
bodyBottom := `
<script type="text/javascript">
var menu = document.getElementById('shroomburgerMenu');
document.getElementById('shroomBtn').addEventListener('click', function() {
menu.classList.add('active');
});
document.getElementById('mushroomBtn').addEventListener('click', function() {
menu.classList.remove('active');
});
</script>
`
args := map[string]string{
"title": fmt.Sprintf(TitleTemplate, rev.FullName),
"head": DefaultStyles,
"header": DefaultHeader,
"main": content,
"sidebar": sidebar,
"footer": FooterText,
"bodyBottom": bodyBottom,
sidebar := DefaultSidebar
bside, err := ioutil.ReadFile("Hypha/view/sidebar.html")
if err == nil {
sidebar = string(bside)
}
return Layout(args)
keys := map[string]string{
"Title": fmt.Sprintf(TitleTemplate, rev.FullName),
"Sidebar": sidebar,
}
return renderBase(renderFromString(content, "Hypha/view/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,
"Head": DefaultStyles,
"Sidebar": DefaultSidebar,
"Main": DefaultContent,
"BodyBottom": DefaultBodyBottom,
"Header": renderFromString(DefaultHeaderText, "header.html"),
"Footer": renderFromString(DefaultFooterText, "footer.html"),
}
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,11 +2,13 @@ package main
import (
"fmt"
"github.com/gomarkdown/markdown"
"io/ioutil"
"log"
"net/http"
"strconv"
"github.com/gomarkdown/markdown"
)
// In different places, revision variable is called `r`. But when there is an http.Request as well, the revision becomes `rev`. TODO: name them consistently.
@ -112,12 +114,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
}

View File

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

View File

@ -0,0 +1,35 @@
<div class="naviwrapper">
<form class="naviwrapper__edit edit-box"
method="POST"
enctype="multipart/form-data"
action="?action=update">
<div class="naviwrapper__buttons">
<input type="submit" 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>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="tags" value="{{ .Tags }}"/>
</div>
</form>
</div>

View File

@ -0,0 +1,9 @@
<script type="text/javascript">
var menu = document.getElementById('shroomburgerMenu');
document.getElementById('shroomBtn').addEventListener('click', function() {
menu.classList.add('active');
});
document.getElementById('mushroomBtn').addEventListener('click', function() {
menu.classList.remove('active');
});
</script>

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>

View File

@ -0,0 +1,10 @@
<div class="naviwrapper">
<div class="hypha-actions">
<ul>
<li><a href="?action=edit">Edit</a>
<li><a href="?action=getBinary">Download</a>
<li><a href="?action=zen">Zen mode</a>
<li><a href="?action=raw">View raw</a>
</ul>
</div>
</div>

25
templates/base.html Normal file
View File

@ -0,0 +1,25 @@
<html>
<head>
<title>{{ .Title }}</title>
{{ .Head }}
</head>
<body>
<div class="shroom">
<button class="shroom__button" id="shroomBtn"><span>🍄</span> Open mycelium</button>
</div>
<main class="main">{{ .Main }}</main>
<div class="left-panel" id="shroomburgerMenu">
<div class="left-panel__in">
<div class="shroom mushroom">
<button class="shroom__button" id="mushroomBtn"><span>🍄</span> Close mycelium</button>
</div>
<div class="left-panel__contents">
<header class="header">{{ .Header }}</header>
<aside class="sidebar">{{ .Sidebar }}</aside>
<footer class="footer">{{ .Footer }}</footer>
</div>
</div>
</div>
{{ .BodyBottom }}
</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>

View File

@ -1 +1 @@
{"views":0,"deleted":false,"revisions":{"1":{"tags":["img"],"name":"Apple","comment":"add apple pic hehehe","author":"bouncepaw","time":1591639464,"text_mime":"text/plain","binary_mime":"image/jpeg"},"2":{"tags":null,"name":"","comment":"Update Fruit/Apple","author":"","time":1592570366,"text_mime":"text/plain","binary_mime":"image/jpeg"},"3":{"tags":null,"name":"","comment":"Test fs dumping","author":"","time":1592570926,"text_mime":"text/plain","binary_mime":"image/jpeg"}}}
{"views":0,"deleted":false,"revisions":{"1":{"tags":["img"],"name":"Apple","comment":"add apple pic hehehe","author":"bouncepaw","time":1591639464,"text_mime":"text/plain","binary_mime":"image/jpeg"},"2":{"tags":null,"name":"","comment":"Update Fruit/Apple","author":"","time":1592570366,"text_mime":"text/plain","binary_mime":"image/jpeg"},"3":{"tags":null,"name":"","comment":"Test fs dumping","author":"","time":1592570926,"text_mime":"text/plain","binary_mime":"image/jpeg"}}}