1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 19:56:16 +00:00
mycorrhiza/hypha.go

114 lines
2.6 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-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
}
func (h *Hypha) AddChild(childName string) {
h.ChildrenNames = append(h.ChildrenNames, childName)
2020-06-12 16:22:02 +00:00
}
// Used with action=zen|view
func (h *Hypha) AsHtml(hyphae map[string]*Hypha, rev string) (string, error) {
if "0" == rev {
rev = h.NewestRevision()
2020-06-13 11:18:11 +00:00
}
r, ok := h.Revisions[rev]
if !ok {
return "", fmt.Errorf("Hypha %v has no such revision: %v", h.FullName, rev)
}
html, err := r.AsHtml(hyphae)
return html, err
2020-06-12 16:22:02 +00:00
}
func (h *Hypha) Name() string {
return h.FullName
}
2020-06-19 13:03:31 +00:00
func (h *Hypha) GetNewestRevision() Revision {
return *h.Revisions[h.NewestRevision()]
}
func (h *Hypha) NewestRevision() string {
2020-06-19 13:03:31 +00:00
return strconv.Itoa(h.NewestRevisionInt())
}
func (h *Hypha) NewestRevisionInt() int {
var largest int
for k, _ := range h.Revisions {
rev, _ := strconv.Atoi(k)
if rev > largest {
largest = rev
2020-06-13 11:18:11 +00:00
}
}
2020-06-19 13:03:31 +00:00
return largest
}
func (h *Hypha) MetaJsonPath() string {
return filepath.Join(h.Path, "meta.json")
}
func (h *Hypha) CreateDir() error {
return os.MkdirAll(h.Path, 0644)
}
func (h *Hypha) ParentName() string {
return h.parentName
2020-06-12 16:22:02 +00:00
}
2020-06-17 15:19:52 +00:00
2020-06-19 13:03:31 +00:00
func (h *Hypha) SaveJson() {
data, err := json.Marshal(h)
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)
}
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
2020-06-17 15:19:52 +00:00
hypha, ok := hyphae[hyphaName]
if !ok {
initContents = "Describe " + hyphaName + "here."
initTextMime = "text/markdown"
} else {
newestRev := hypha.Revisions[hypha.NewestRevision()]
contents, err := ioutil.ReadFile(newestRev.TextPath)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("<b>Sorry, something went wrong</b>"))
return
}
initContents = string(contents)
initTextMime = newestRev.TextMime
initTags = strings.Join(newestRev.Tags, ",")
}
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
}