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

Move stuff around

This commit is contained in:
Timur Ismagilov 2022-06-06 17:41:33 +03:00
parent 2b14fa8de1
commit 5e2c20c559
8 changed files with 66 additions and 63 deletions

36
hyphae/deprecated.go Normal file
View File

@ -0,0 +1,36 @@
package hyphae
import (
"errors"
"os"
)
// FetchMycomarkupFile tries to read text file of the given hypha. If there is no file, empty string is returned.
//
// TODO: Get rid of this function.
func FetchMycomarkupFile(h Hypha) (string, error) {
switch h := h.(type) {
case *EmptyHypha:
return "", errors.New("empty hyphae have no text")
case *MediaHypha:
if !h.HasTextFile() {
return "", nil
}
text, err := os.ReadFile(h.TextFilePath())
if os.IsNotExist(err) {
return "", nil
} else if err != nil {
return "", err
}
return string(text), nil
case *TextualHypha:
text, err := os.ReadFile(h.TextFilePath())
if os.IsNotExist(err) {
return "", nil
} else if err != nil {
return "", err
}
return string(text), nil
}
panic("unreachable")
}

View File

@ -1,3 +1,4 @@
// Package hyphae manages hypha storage and hypha types.
package hyphae
import (

View File

@ -15,7 +15,7 @@ func Delete(u *user.User, h hyphae.ExistingHypha) error {
WithMsg(fmt.Sprintf("Delete %s", h.CanonicalName())).
WithUser(u)
originalText, _ := FetchTextFile(h)
originalText, _ := hyphae.FetchMycomarkupFile(h)
switch h := h.(type) {
case *hyphae.MediaHypha:
if h.HasTextFile() {

View File

@ -4,11 +4,30 @@ import (
"github.com/bouncepaw/mycomarkup/v5"
"github.com/bouncepaw/mycomarkup/v5/blocks"
"github.com/bouncepaw/mycomarkup/v5/mycocontext"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/hyphae"
"github.com/bouncepaw/mycorrhiza/viewutil"
"os"
)
// SetDefaultHeaderLinks sets the header links to the default list of: home hypha, recent changes, hyphae list, random hypha.
func SetDefaultHeaderLinks() {
// SetHeaderLinks initializes header links by reading the configured hypha, if there is any, or resorting to default values.
func SetHeaderLinks() {
switch userLinksHypha := hyphae.ByName(cfg.HeaderLinksHypha).(type) {
case *hyphae.EmptyHypha:
setDefaultHeaderLinks()
case hyphae.ExistingHypha:
contents, err := os.ReadFile(userLinksHypha.TextFilePath())
if err != nil || len(contents) == 0 {
setDefaultHeaderLinks()
} else {
text := string(contents)
parseHeaderLinks(text)
}
}
}
// setDefaultHeaderLinks sets the header links to the default list of: home hypha, recent changes, hyphae list, random hypha.
func setDefaultHeaderLinks() {
viewutil.HeaderLinks = []viewutil.HeaderLink{
{"/recent-changes", "Recent changes"},
{"/list", "All hyphae"},
@ -18,8 +37,8 @@ func SetDefaultHeaderLinks() {
}
}
// ParseHeaderLinks extracts all rocketlinks from the given text and saves them as header links.
func ParseHeaderLinks(text string) {
// parseHeaderLinks extracts all rocketlinks from the given text and saves them as header links.
func parseHeaderLinks(text string) {
viewutil.HeaderLinks = []viewutil.HeaderLink{}
ctx, _ := mycocontext.ContextFromStringInput(text, MarkupOptions(""))
// We call for side-effects

View File

@ -35,9 +35,9 @@ func MarkupOptions(hyphaName string) options.Options {
case *hyphae.EmptyHypha:
err = errors.New("Hypha " + hyphaName + " does not exist")
case *hyphae.TextualHypha:
rawText, err = FetchTextFile(h)
rawText, err = hyphae.FetchMycomarkupFile(h)
case *hyphae.MediaHypha:
rawText, err = FetchTextFile(h)
rawText, err = hyphae.FetchMycomarkupFile(h)
binaryBlock = views.MediaRaw(h)
}
return

View File

@ -90,7 +90,7 @@ func UploadText(h hyphae.Hypha, data []byte, userMessage string, u *user.User) e
hyphae.Insert(H)
backlinks.UpdateBacklinksAfterEdit(H, "")
case *hyphae.MediaHypha:
oldText, err := FetchTextFile(h)
oldText, err := hyphae.FetchMycomarkupFile(h)
if err != nil {
hop.Abort()
return err
@ -111,7 +111,7 @@ func UploadText(h hyphae.Hypha, data []byte, userMessage string, u *user.User) e
backlinks.UpdateBacklinksAfterEdit(h, oldText)
case *hyphae.TextualHypha:
oldText, err := FetchTextFile(h)
oldText, err := hyphae.FetchMycomarkupFile(h)
if err != nil {
hop.Abort()
return err

View File

@ -1,53 +0,0 @@
package shroom
import (
"errors"
"os"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/hyphae"
)
// FetchTextFile tries to read text file of the given hypha. If there is no file, empty string is returned.
func FetchTextFile(h hyphae.Hypha) (string, error) {
switch h := h.(type) {
case *hyphae.EmptyHypha:
return "", errors.New("empty hyphae have no text")
case *hyphae.MediaHypha:
if !h.HasTextFile() {
return "", nil
}
text, err := os.ReadFile(h.TextFilePath())
if os.IsNotExist(err) {
return "", nil
} else if err != nil {
return "", err
}
return string(text), nil
case *hyphae.TextualHypha:
text, err := os.ReadFile(h.TextFilePath())
if os.IsNotExist(err) {
return "", nil
} else if err != nil {
return "", err
}
return string(text), nil
}
panic("unreachable")
}
// SetHeaderLinks initializes header links by reading the configured hypha, if there is any, or resorting to default values.
func SetHeaderLinks() {
switch userLinksHypha := hyphae.ByName(cfg.HeaderLinksHypha).(type) {
case *hyphae.EmptyHypha:
SetDefaultHeaderLinks()
case hyphae.ExistingHypha:
contents, err := os.ReadFile(userLinksHypha.TextFilePath())
if err != nil || len(contents) == 0 {
SetDefaultHeaderLinks()
} else {
text := string(contents)
ParseHeaderLinks(text)
}
}
}

View File

@ -171,7 +171,7 @@ func handlerEdit(w http.ResponseWriter, rq *http.Request) {
case *hyphae.EmptyHypha:
warning = fmt.Sprintf(`<p class="warning warning_new-hypha">%s</p>`, lc.Get("edit.new_hypha"))
default:
textAreaFill, err = shroom.FetchTextFile(h)
textAreaFill, err = hyphae.FetchMycomarkupFile(h)
if err != nil {
log.Println(err)
viewutil.HttpErr(meta, http.StatusInternalServerError, hyphaName, lc.Get("ui.error_text_fetch"))