2021-02-20 19:03:54 +05:00
|
|
|
package shroom
|
2021-02-17 23:41:35 +05:00
|
|
|
|
|
|
|
import (
|
2022-02-04 22:04:39 +05:00
|
|
|
"errors"
|
2021-02-17 23:41:35 +05:00
|
|
|
"os"
|
|
|
|
|
2021-05-12 18:34:24 +05:00
|
|
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
2021-02-20 19:03:54 +05:00
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
2021-02-17 23:41:35 +05:00
|
|
|
)
|
|
|
|
|
2022-02-19 11:26:38 +03:00
|
|
|
// 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) {
|
2022-02-04 22:04:39 +05:00
|
|
|
case *hyphae.EmptyHypha:
|
|
|
|
return "", errors.New("empty hyphae have no text")
|
2022-02-19 11:26:38 +03:00
|
|
|
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
|
2022-02-04 22:04:39 +05:00
|
|
|
}
|
2022-02-19 11:26:38 +03:00
|
|
|
panic("unreachable")
|
2021-02-17 23:41:35 +05:00
|
|
|
}
|
|
|
|
|
2021-10-02 01:12:16 +08:00
|
|
|
// SetHeaderLinks initializes header links by reading the configured hypha, if there is any, or resorting to default values.
|
2021-02-17 23:41:35 +05:00
|
|
|
func SetHeaderLinks() {
|
2022-02-04 19:56:28 +05:00
|
|
|
switch userLinksHypha := hyphae.ByName(cfg.HeaderLinksHypha).(type) {
|
|
|
|
case *hyphae.EmptyHypha:
|
2021-05-11 13:33:00 +05:00
|
|
|
cfg.SetDefaultHeaderLinks()
|
2022-02-19 11:26:38 +03:00
|
|
|
case hyphae.ExistingHypha:
|
|
|
|
contents, err := os.ReadFile(userLinksHypha.TextFilePath())
|
2021-02-17 23:41:35 +05:00
|
|
|
if err != nil || len(contents) == 0 {
|
2021-05-11 13:33:00 +05:00
|
|
|
cfg.SetDefaultHeaderLinks()
|
2021-02-17 23:41:35 +05:00
|
|
|
} else {
|
|
|
|
text := string(contents)
|
2021-05-24 14:33:57 +05:00
|
|
|
cfg.ParseHeaderLinks(text)
|
2021-02-17 23:41:35 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|