diff --git a/fs/fs.go b/fs/fs.go index 4323340..e140b58 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -21,7 +21,6 @@ func InitStorage() *Storage { root: cfg.WikiDir, } s.indexHyphae(s.root) - log.Println(s.paths) log.Printf("Indexed %v hyphae\n", len(s.paths)) return s } @@ -50,7 +49,6 @@ func (s *Storage) indexHyphae(path string) { case matchesHypha && node.IsDir(): s.indexHyphae(name) case node.Name() == "meta.json" && !node.IsDir(): - log.Printf("%v seems to be a hypha, adding it to the list\n", path) s.paths[hyphaName(path)] = path } } diff --git a/fs/hypha.go b/fs/hypha.go index 559457a..225acd0 100644 --- a/fs/hypha.go +++ b/fs/hypha.go @@ -3,6 +3,7 @@ package fs import ( "encoding/json" "errors" + "fmt" "io/ioutil" "log" "net/http" @@ -10,6 +11,7 @@ import ( "strconv" "github.com/bouncepaw/mycorrhiza/cfg" + "github.com/gomarkdown/markdown" ) type Hypha struct { @@ -57,7 +59,6 @@ func (s *Storage) Open(name string) (*Hypha, error) { err = h.OnRevision("0") return h, err } - log.Println(h) return h, nil } @@ -108,6 +109,45 @@ func (h *Hypha) mimeTypeForActionRaw() string { return h.actual.TextMime } +// hasBinaryData returns true if the revision has any binary data associated. +// During initialisation, it is guaranteed that r.BinaryMime is set to "" if the revision has no binary data. (is it?) +func (h *Hypha) hasBinaryData() bool { + return h.actual.BinaryMime != "" +} + +func (h *Hypha) asHtml() (string, error) { + rev := h.actual + ret := `
+

` + rev.FullName + `

+` + // What about using
? + if h.hasBinaryData() { + ret += fmt.Sprintf(``, rev.FullName, rev.Id) + } + + contents, err := ioutil.ReadFile(rev.TextPath) + if err != nil { + log.Println("Failed to render", rev.FullName, ":", err) + return "", err + } + + // TODO: support more markups. + // TODO: support mycorrhiza extensions like transclusion. + switch rev.TextMime { + case "text/markdown": + html := markdown.ToHTML(contents, nil, nil) + ret += string(html) + default: + ret += fmt.Sprintf(`
%s
`, contents) + } + + ret += ` +
` + + return ret, nil +} + // ActionRaw is used with `?action=raw`. // It writes text content of the revision without any parsing or rendering. func (h *Hypha) ActionRaw(w http.ResponseWriter) { @@ -135,3 +175,18 @@ func (h *Hypha) ActionGetBinary(w http.ResponseWriter) { w.Write(fileContents) h.PlainLog("Serving raw text") } + +// ActionZen is used with `?action=zen`. +// It renders the hypha but without any layout or styles. Pure. Zen. +func (h *Hypha) ActionZen(w http.ResponseWriter) { + html, err := h.asHtml() + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "text/html;charset=utf-8") + w.WriteHeader(http.StatusOK) + w.Write([]byte(html)) + h.PlainLog("Rendering zen") +} diff --git a/handlers.go b/handlers.go index 4479ea1..1b8a9a2 100644 --- a/handlers.go +++ b/handlers.go @@ -47,13 +47,13 @@ func HandlerGetBinary(w http.ResponseWriter, rq *http.Request) { } } -/* func HandlerZen(w http.ResponseWriter, rq *http.Request) { if h, ok := HandlerBase(w, rq); ok { h.ActionZen(w) } } +/* func HandlerView(w http.ResponseWriter, rq *http.Request) { if h, ok := HandlerBase(w, rq); ok { h.ActionView(w, HyphaPage) @@ -65,25 +65,6 @@ func HandlerEdit(w http.ResponseWriter, rq *http.Request) { ActionEdit(vars["hypha"], w) } -func HandlerHistory(w http.ResponseWriter, rq *http.Request) { - w.WriteHeader(http.StatusNotImplemented) - log.Println("Attempt to access an unimplemented thing") -} - -func HandlerRewind(w http.ResponseWriter, rq *http.Request) { - w.WriteHeader(http.StatusNotImplemented) - log.Println("Attempt to access an unimplemented thing") -} - -func HandlerDelete(w http.ResponseWriter, rq *http.Request) { - w.WriteHeader(http.StatusNotImplemented) - log.Println("Attempt to access an unimplemented thing") -} - -func HandlerRename(w http.ResponseWriter, rq *http.Request) { - w.WriteHeader(http.StatusNotImplemented) - log.Println("Attempt to access an unimplemented thing") -} */ /* diff --git a/main.go b/main.go index b4301de..8d3cdb1 100644 --- a/main.go +++ b/main.go @@ -50,31 +50,20 @@ func main() { HandlerFunc(HandlerRaw) r.Queries("action", "raw").Path(cfg.HyphaUrl). HandlerFunc(HandlerRaw) - /* - r.Queries("action", "zen", "rev", revQuery).Path(hyphaUrl). - HandlerFunc(HandlerZen) - r.Queries("action", "zen").Path(hyphaUrl). - HandlerFunc(HandlerZen) + r.Queries("action", "zen", "rev", cfg.RevQuery).Path(cfg.HyphaUrl). + HandlerFunc(HandlerZen) + r.Queries("action", "zen").Path(cfg.HyphaUrl). + HandlerFunc(HandlerZen) + + /* r.Queries("action", "view", "rev", revQuery).Path(hyphaUrl). HandlerFunc(HandlerView) r.Queries("action", "view").Path(hyphaUrl). HandlerFunc(HandlerView) - r.Queries("action", "history").Path(hyphaUrl). - HandlerFunc(HandlerHistory) - - r.Queries("action", "edit").Path(hyphaUrl). - HandlerFunc(HandlerEdit) - - r.Queries("action", "rewind", "rev", revQuery).Path(hyphaUrl). - HandlerFunc(HandlerRewind) - - r.Queries("action", "delete").Path(hyphaUrl). - HandlerFunc(HandlerDelete) - - r.Queries("action", "rename", "to", hyphaPattern).Path(hyphaUrl). - HandlerFunc(HandlerRename) + r.Queries("action", "edit").Path(hyphaUrl). + HandlerFunc(HandlerEdit) r.Queries("action", "update").Path(hyphaUrl).Methods("POST"). HandlerFunc(HandlerUpdate)