1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-13 05:50:27 +00:00
mycorrhiza/hypha.gog

75 lines
2.0 KiB
Plaintext
Raw Normal View History

2020-06-12 16:22:02 +00:00
package main
import (
2020-06-19 13:03:31 +00:00
"encoding/json"
2020-06-12 16:22:02 +00:00
"fmt"
2020-06-17 15:19:52 +00:00
"io/ioutil"
2020-06-19 13:03:31 +00:00
"log"
2020-06-17 15:19:52 +00:00
"net/http"
2020-06-19 13:03:31 +00:00
"os"
"path/filepath"
2020-06-13 11:18:11 +00:00
"strconv"
2020-06-17 15:19:52 +00:00
"strings"
2020-06-23 17:53:05 +00:00
"github.com/bouncepaw/mycorrhiza/cfg"
2020-06-12 16:22:02 +00:00
)
// AsHtml returns HTML representation of the hypha.
// No layout or navigation are present here. Just the hypha.
func (h *Hypha) AsHtml(id string, w http.ResponseWriter) (string, error) {
if "0" == id {
id = h.NewestRevision()
2020-06-13 11:18:11 +00:00
}
if rev, ok := h.Revisions[id]; ok {
return rev.AsHtml(w)
}
return "", fmt.Errorf("Hypha %v has no such revision: %v", h.FullName, id)
}
// CreateDir creates directory where the hypha must reside.
// It is meant to be used with new hyphae.
2020-06-19 13:03:31 +00:00
func (h *Hypha) CreateDir() error {
2020-06-22 13:58:12 +00:00
return os.MkdirAll(h.Path, os.ModePerm)
}
// SaveJson dumps the hypha's metadata to `meta.json` file.
2020-06-19 13:03:31 +00:00
func (h *Hypha) SaveJson() {
2020-06-20 15:17:13 +00:00
data, err := json.MarshalIndent(h, "", "\t")
2020-06-19 13:03:31 +00:00
if err != nil {
log.Println("Failed to create JSON of hypha.", err)
return
}
err = ioutil.WriteFile(h.MetaJsonPath(), data, 0644)
if err != nil {
log.Println("Failed to save JSON of hypha.", err)
return
}
log.Println("Saved JSON data of", h.FullName)
}
// ActionEdit is called with `?acton=edit`.
// It represents the hypha editor.
2020-06-17 15:19:52 +00:00
func ActionEdit(hyphaName string, w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
2020-06-19 13:03:31 +00:00
var initContents, initTextMime, initTags string
if h, ok := hyphae[hyphaName]; ok {
newestRev := h.GetNewestRevision()
2020-06-17 15:19:52 +00:00
contents, err := ioutil.ReadFile(newestRev.TextPath)
if err != nil {
log.Println("Could not read", newestRev.TextPath)
2020-06-17 15:19:52 +00:00
w.WriteHeader(http.StatusInternalServerError)
2020-06-23 17:53:05 +00:00
w.Write([]byte(cfg.GenericErrorMsg))
2020-06-17 15:19:52 +00:00
return
}
initContents = string(contents)
initTextMime = newestRev.TextMime
initTags = strings.Join(newestRev.Tags, ",")
} else {
initContents = "Describe " + hyphaName + "here."
initTextMime = "text/markdown"
2020-06-17 15:19:52 +00:00
}
w.WriteHeader(http.StatusOK)
2020-06-19 13:03:31 +00:00
w.Write([]byte(EditHyphaPage(hyphaName, initTextMime, initContents, initTags)))
2020-06-17 15:19:52 +00:00
}