1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 05:20:26 +00:00

Reimplement action=zen

This commit is contained in:
Timur Ismagilov 2020-06-24 21:19:44 +05:00
parent 8f2515b37a
commit 7202cf5ab7
4 changed files with 65 additions and 42 deletions

View File

@ -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
}
}

View File

@ -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 := `<article class="page">
<h1 class="page__title">` + rev.FullName + `</h1>
`
// What about using <figure>?
if h.hasBinaryData() {
ret += fmt.Sprintf(`<img src="/%s?action=getBinary&rev=%d" cla
ss="page__amnt"/>`, 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(`<pre>%s</pre>`, contents)
}
ret += `
</article>`
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")
}

View File

@ -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")
}
*/
/*

27
main.go
View File

@ -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)