From 5f8739e936e73c861938da13fa8c457d45426a52 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Wed, 17 Jun 2020 20:19:52 +0500 Subject: [PATCH] Make edit hypha page --- hypha.go | 28 +++++++++++++++++++++++ main.go | 4 ++-- render.go | 51 ++++++++++++++++++++++++++++++++++++++++++ w/m/sys/main.css/1.txt | 5 +++++ 4 files changed, 86 insertions(+), 2 deletions(-) diff --git a/hypha.go b/hypha.go index 4655092..b29b602 100644 --- a/hypha.go +++ b/hypha.go @@ -2,7 +2,10 @@ package main import ( "fmt" + "io/ioutil" + "net/http" "strconv" + "strings" ) type Hypha struct { @@ -50,3 +53,28 @@ func (h *Hypha) NewestRevision() string { func (h *Hypha) ParentName() string { return h.parentName } + +func ActionEdit(hyphaName string, w http.ResponseWriter) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + var initContents, initTextMime, initBinaryMime, initTags string + hypha, ok := hyphae[hyphaName] + if !ok { + initContents = "Describe " + hyphaName + "here." + initTextMime = "text/markdown" + } else { + newestRev := hypha.Revisions[hypha.NewestRevision()] + contents, err := ioutil.ReadFile(newestRev.TextPath) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Sorry, something went wrong")) + return + } + initContents = string(contents) + initTextMime = newestRev.TextMime + initBinaryMime = newestRev.BinaryMime + initTags = strings.Join(newestRev.Tags, ",") + } + + w.WriteHeader(http.StatusOK) + w.Write([]byte(EditHyphaPage(hyphaName, initTextMime, initBinaryMime, initContents, initTags))) +} diff --git a/main.go b/main.go index bc62f5f..434bb48 100644 --- a/main.go +++ b/main.go @@ -118,8 +118,8 @@ func HandlerHistory(w http.ResponseWriter, r *http.Request) { } func HandlerEdit(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusNotImplemented) - log.Println("Attempt to access an unimplemented thing") + vars := mux.Vars(r) + ActionEdit(vars["hypha"], w) } func HandlerRewind(w http.ResponseWriter, r *http.Request) { diff --git a/render.go b/render.go index 0a13779..d8267d2 100644 --- a/render.go +++ b/render.go @@ -24,6 +24,57 @@ func Layout(f map[string]string) string { 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, + } + + return Layout(args) +} + func HyphaPage(hyphae map[string]*Hypha, rev Revision, content string) string { template := `