diff --git a/config.go b/config.go index 20ae30c..34fc6d6 100644 --- a/config.go +++ b/config.go @@ -1,14 +1,14 @@ package main const ( - FooterText = ` -This website runs MycorrhizaWiki. -` - TitleTemplate = `%s at MycorrhizaWiki` - DefaultStyles = ` + TitleTemplate = `%s at MycorrhizaWiki` + DefaultTitle = "MycorrhizaWiki" + DefaultHeaderText = `MycorrhizaWiki 🍄` + DefaultFooterText = `This website runs MycorrhizaWiki.` + DefaultSidebar = "" + DefaultBodyBottom = "" + DefaultContent = "It is empty here" + DefaultStyles = ` ` - DefaultHeader = ` -

MycorrhizaWiki 🍄

-` // TODO: Search input ) diff --git a/handlers.go b/handlers.go index 56a6399..3951971 100644 --- a/handlers.go +++ b/handlers.go @@ -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. diff --git a/main.go b/main.go index b75717d..d957eeb 100644 --- a/main.go +++ b/main.go @@ -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) { diff --git a/render.go b/render.go index 6c2eada..21a8f0b 100644 --- a/render.go +++ b/render.go @@ -1,126 +1,85 @@ package main import ( + "bytes" "fmt" + "io/ioutil" + "path" + "text/template" ) -func Layout(f map[string]string) string { - template := ` - - - - %s - %s - - -
- -
-
%s
-
-
-
- -
-
-
%s
- - -
-
-
- %s - - -` - 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 := ` - -` - args := map[string]string{ - "title": fmt.Sprintf(TitleTemplate, "Edit "+name), - "head": DefaultStyles, - "header": `

Edit ` + name + `

`, - "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 := ` - -` - - bodyBottom := ` - -` - - 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() } diff --git a/revision.go b/revision.go index a546cf7..3e0c519 100644 --- a/revision.go +++ b/revision.go @@ -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 -} diff --git a/templates/Hypha/edit/header.html b/templates/Hypha/edit/header.html new file mode 100644 index 0000000..31dfc0f --- /dev/null +++ b/templates/Hypha/edit/header.html @@ -0,0 +1 @@ +

Edit {{ . }}

diff --git a/templates/Hypha/edit/index.html b/templates/Hypha/edit/index.html new file mode 100644 index 0000000..4bea3b7 --- /dev/null +++ b/templates/Hypha/edit/index.html @@ -0,0 +1,35 @@ + diff --git a/templates/Hypha/view/bodybottom.html b/templates/Hypha/view/bodybottom.html new file mode 100644 index 0000000..0759741 --- /dev/null +++ b/templates/Hypha/view/bodybottom.html @@ -0,0 +1,9 @@ + diff --git a/templates/Hypha/view/index.html b/templates/Hypha/view/index.html new file mode 100644 index 0000000..1126a8f --- /dev/null +++ b/templates/Hypha/view/index.html @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/templates/Hypha/view/sidebar.html b/templates/Hypha/view/sidebar.html new file mode 100644 index 0000000..7d3a6b8 --- /dev/null +++ b/templates/Hypha/view/sidebar.html @@ -0,0 +1,10 @@ + diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..03cf231 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,25 @@ + + + {{ .Title }} + {{ .Head }} + + +
+ +
+
{{ .Main }}
+
+
+
+ +
+
+
{{ .Header }}
+ + +
+
+
+ {{ .BodyBottom }} + + diff --git a/templates/footer.html b/templates/footer.html new file mode 100644 index 0000000..d9c9484 --- /dev/null +++ b/templates/footer.html @@ -0,0 +1 @@ +

{{ . }}

\ No newline at end of file diff --git a/templates/header.html b/templates/header.html new file mode 100644 index 0000000..d9c9484 --- /dev/null +++ b/templates/header.html @@ -0,0 +1 @@ +

{{ . }}

\ No newline at end of file diff --git a/w/m/Fruit/Apple/meta.json b/w/m/Fruit/Apple/meta.json index b47c26a..85cfe94 100644 --- a/w/m/Fruit/Apple/meta.json +++ b/w/m/Fruit/Apple/meta.json @@ -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"}}} \ No newline at end of file +{"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"}}}