mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-12-13 05:50:27 +00:00
Reimplement action=zen
This commit is contained in:
parent
8f2515b37a
commit
7202cf5ab7
2
fs/fs.go
2
fs/fs.go
@ -21,7 +21,6 @@ func InitStorage() *Storage {
|
|||||||
root: cfg.WikiDir,
|
root: cfg.WikiDir,
|
||||||
}
|
}
|
||||||
s.indexHyphae(s.root)
|
s.indexHyphae(s.root)
|
||||||
log.Println(s.paths)
|
|
||||||
log.Printf("Indexed %v hyphae\n", len(s.paths))
|
log.Printf("Indexed %v hyphae\n", len(s.paths))
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
@ -50,7 +49,6 @@ func (s *Storage) indexHyphae(path string) {
|
|||||||
case matchesHypha && node.IsDir():
|
case matchesHypha && node.IsDir():
|
||||||
s.indexHyphae(name)
|
s.indexHyphae(name)
|
||||||
case node.Name() == "meta.json" && !node.IsDir():
|
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
|
s.paths[hyphaName(path)] = path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
57
fs/hypha.go
57
fs/hypha.go
@ -3,6 +3,7 @@ package fs
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -10,6 +11,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||||
|
"github.com/gomarkdown/markdown"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Hypha struct {
|
type Hypha struct {
|
||||||
@ -57,7 +59,6 @@ func (s *Storage) Open(name string) (*Hypha, error) {
|
|||||||
err = h.OnRevision("0")
|
err = h.OnRevision("0")
|
||||||
return h, err
|
return h, err
|
||||||
}
|
}
|
||||||
log.Println(h)
|
|
||||||
return h, nil
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,6 +109,45 @@ func (h *Hypha) mimeTypeForActionRaw() string {
|
|||||||
return h.actual.TextMime
|
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`.
|
// ActionRaw is used with `?action=raw`.
|
||||||
// It writes text content of the revision without any parsing or rendering.
|
// It writes text content of the revision without any parsing or rendering.
|
||||||
func (h *Hypha) ActionRaw(w http.ResponseWriter) {
|
func (h *Hypha) ActionRaw(w http.ResponseWriter) {
|
||||||
@ -135,3 +175,18 @@ func (h *Hypha) ActionGetBinary(w http.ResponseWriter) {
|
|||||||
w.Write(fileContents)
|
w.Write(fileContents)
|
||||||
h.PlainLog("Serving raw text")
|
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")
|
||||||
|
}
|
||||||
|
21
handlers.go
21
handlers.go
@ -47,13 +47,13 @@ func HandlerGetBinary(w http.ResponseWriter, rq *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
func HandlerZen(w http.ResponseWriter, rq *http.Request) {
|
func HandlerZen(w http.ResponseWriter, rq *http.Request) {
|
||||||
if h, ok := HandlerBase(w, rq); ok {
|
if h, ok := HandlerBase(w, rq); ok {
|
||||||
h.ActionZen(w)
|
h.ActionZen(w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func HandlerView(w http.ResponseWriter, rq *http.Request) {
|
func HandlerView(w http.ResponseWriter, rq *http.Request) {
|
||||||
if h, ok := HandlerBase(w, rq); ok {
|
if h, ok := HandlerBase(w, rq); ok {
|
||||||
h.ActionView(w, HyphaPage)
|
h.ActionView(w, HyphaPage)
|
||||||
@ -65,25 +65,6 @@ func HandlerEdit(w http.ResponseWriter, rq *http.Request) {
|
|||||||
ActionEdit(vars["hypha"], w)
|
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")
|
|
||||||
}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
19
main.go
19
main.go
@ -50,32 +50,21 @@ func main() {
|
|||||||
HandlerFunc(HandlerRaw)
|
HandlerFunc(HandlerRaw)
|
||||||
r.Queries("action", "raw").Path(cfg.HyphaUrl).
|
r.Queries("action", "raw").Path(cfg.HyphaUrl).
|
||||||
HandlerFunc(HandlerRaw)
|
HandlerFunc(HandlerRaw)
|
||||||
/*
|
|
||||||
r.Queries("action", "zen", "rev", revQuery).Path(hyphaUrl).
|
r.Queries("action", "zen", "rev", cfg.RevQuery).Path(cfg.HyphaUrl).
|
||||||
HandlerFunc(HandlerZen)
|
HandlerFunc(HandlerZen)
|
||||||
r.Queries("action", "zen").Path(hyphaUrl).
|
r.Queries("action", "zen").Path(cfg.HyphaUrl).
|
||||||
HandlerFunc(HandlerZen)
|
HandlerFunc(HandlerZen)
|
||||||
|
|
||||||
|
/*
|
||||||
r.Queries("action", "view", "rev", revQuery).Path(hyphaUrl).
|
r.Queries("action", "view", "rev", revQuery).Path(hyphaUrl).
|
||||||
HandlerFunc(HandlerView)
|
HandlerFunc(HandlerView)
|
||||||
r.Queries("action", "view").Path(hyphaUrl).
|
r.Queries("action", "view").Path(hyphaUrl).
|
||||||
HandlerFunc(HandlerView)
|
HandlerFunc(HandlerView)
|
||||||
|
|
||||||
r.Queries("action", "history").Path(hyphaUrl).
|
|
||||||
HandlerFunc(HandlerHistory)
|
|
||||||
|
|
||||||
r.Queries("action", "edit").Path(hyphaUrl).
|
r.Queries("action", "edit").Path(hyphaUrl).
|
||||||
HandlerFunc(HandlerEdit)
|
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", "update").Path(hyphaUrl).Methods("POST").
|
r.Queries("action", "update").Path(hyphaUrl).Methods("POST").
|
||||||
HandlerFunc(HandlerUpdate)
|
HandlerFunc(HandlerUpdate)
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user