diff --git a/config.go b/config.go index e325a85..c648696 100644 --- a/config.go +++ b/config.go @@ -1,15 +1,13 @@ package main const ( - FooterText = ` -
-This website runs MycorrhizaWiki. -` - TitleTemplate = `%s at MycorrhizaWiki` - DefaultStyles = ` + TitleTemplate = `%s at MycorrhizaWiki` + DefaultTitle = "MycorrhizaWiki" + DefaultHeaderText = `MycorrhizaWiki` + DefaultFooterText = "MycorrhizaWiki" + DefaultSidebar = "" + DefaultContent = "It is empty here" + DefaultStyles = ` ` - DefaultHeader = ` -

MycorrhizaWiki

-` // TODO: Search input ) diff --git a/handlers.go b/handlers.go index 5f3dfac..4a60771 100644 --- a/handlers.go +++ b/handlers.go @@ -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. diff --git a/main.go b/main.go index 0f3189d..be1daa0 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 d8267d2..ff74fdf 100644 --- a/render.go +++ b/render.go @@ -1,100 +1,76 @@ package main import ( + "bytes" "fmt" + "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["header"], f["main"], f["sidebar"], FooterText, f["bodyBottom"]) -} - func EditHyphaPage(name, text_mime, binary_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, 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 := ` - -` - 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() } diff --git a/revision.go b/revision.go index ee27137..9e7cf51 100644 --- a/revision.go +++ b/revision.go @@ -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 -} diff --git a/templates/Hypha/edit.html b/templates/Hypha/edit.html new file mode 100644 index 0000000..45536c5 --- /dev/null +++ b/templates/Hypha/edit.html @@ -0,0 +1,36 @@ + \ No newline at end of file diff --git a/templates/Hypha/index.html b/templates/Hypha/index.html new file mode 100644 index 0000000..1126a8f --- /dev/null +++ b/templates/Hypha/index.html @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..f37d0b9 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,11 @@ + + + {{ .Title }} + + +
{{ .Header }}
+
{{ .Main }}
+ + + + \ No newline at end of file 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