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": `