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

114 lines
3.1 KiB
Go
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
)
// `Hypha` represents a hypha. It is the thing MycorrhizaWiki generally serves.
// Each hypha has 1 or more revisions.
2020-06-12 16:22:02 +00:00
type Hypha struct {
2020-06-19 13:03:31 +00:00
FullName string `json:"-"`
Path string `json:"-"`
ViewCount int `json:"views"`
Deleted bool `json:"deleted"`
Revisions map[string]*Revision `json:"revisions"`
2020-06-19 13:03:31 +00:00
ChildrenNames []string `json:"-"`
parentName string
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)
}
// GetNewestRevision returns the most recent Revision.
2020-06-19 13:03:31 +00:00
func (h *Hypha) GetNewestRevision() Revision {
return *h.Revisions[h.NewestRevision()]
}
// NewestRevision returns the most recent revision's id as a string.
func (h *Hypha) NewestRevision() string {
2020-06-19 13:03:31 +00:00
return strconv.Itoa(h.NewestRevisionInt())
}
// NewestRevision returns the most recent revision's id as an integer.
func (h *Hypha) NewestRevisionInt() (ret int) {
for k, _ := range h.Revisions {
id, _ := strconv.Atoi(k)
if id > ret {
ret = id
2020-06-13 11:18:11 +00:00
}
}
return ret
2020-06-19 13:03:31 +00:00
}
// MetaJsonPath returns rooted path to the hypha's `meta.json` file.
// It is not promised that the file exists.
2020-06-19 13:03:31 +00:00
func (h *Hypha) MetaJsonPath() string {
return filepath.Join(h.Path, "meta.json")
}
// 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
}