diff --git a/hyphae/deprecated.go b/hyphae/deprecated.go new file mode 100644 index 0000000..2d3d5bc --- /dev/null +++ b/hyphae/deprecated.go @@ -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") +} diff --git a/hyphae/hypha.go b/hyphae/hypha.go index c4c185c..e531c81 100644 --- a/hyphae/hypha.go +++ b/hyphae/hypha.go @@ -1,3 +1,4 @@ +// Package hyphae manages hypha storage and hypha types. package hyphae import ( diff --git a/shroom/delete.go b/shroom/delete.go index 48c9a21..38dbd54 100644 --- a/shroom/delete.go +++ b/shroom/delete.go @@ -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() { diff --git a/shroom/header_links.go b/shroom/header_links.go index a4c6065..c4e18c7 100644 --- a/shroom/header_links.go +++ b/shroom/header_links.go @@ -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 diff --git a/shroom/mycomarkup_options.go b/shroom/mycomarkup_options.go index 600b35f..4acfa50 100644 --- a/shroom/mycomarkup_options.go +++ b/shroom/mycomarkup_options.go @@ -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 diff --git a/shroom/upload.go b/shroom/upload.go index 54d34cf..28c3376 100644 --- a/shroom/upload.go +++ b/shroom/upload.go @@ -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 diff --git a/shroom/view.go b/shroom/view.go deleted file mode 100644 index 2f0e94d..0000000 --- a/shroom/view.go +++ /dev/null @@ -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) - } - } -} diff --git a/web/mutators.go b/web/mutators.go index 032af46..0a1153d 100644 --- a/web/mutators.go +++ b/web/mutators.go @@ -171,7 +171,7 @@ func handlerEdit(w http.ResponseWriter, rq *http.Request) { case *hyphae.EmptyHypha: warning = fmt.Sprintf(`

%s

`, 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"))