2021-02-20 14:03:54 +00:00
|
|
|
package shroom
|
2021-02-17 18:41:35 +00:00
|
|
|
|
|
|
|
import (
|
2022-02-04 17:04:39 +00:00
|
|
|
"errors"
|
2021-02-17 18:41:35 +00:00
|
|
|
"os"
|
|
|
|
|
2021-05-12 13:34:24 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
2021-02-20 14:03:54 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
2021-02-17 18:41:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FetchTextPart tries to read text file of the given hypha. If there is no file, empty string is returned.
|
2022-02-04 10:50:50 +00:00
|
|
|
func FetchTextPart(h hyphae.Hypher) (string, error) {
|
2022-02-04 17:04:39 +00:00
|
|
|
switch h.(type) {
|
|
|
|
case *hyphae.EmptyHypha:
|
|
|
|
return "", errors.New("empty hyphae have no text")
|
|
|
|
}
|
2022-02-03 22:39:21 +00:00
|
|
|
if !h.HasTextPart() {
|
2021-02-17 18:41:35 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2022-02-04 10:50:50 +00:00
|
|
|
text, err := os.ReadFile(h.TextPartPath())
|
2021-02-17 18:41:35 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return "", nil
|
|
|
|
} else if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(text), nil
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:12:16 +00:00
|
|
|
// SetHeaderLinks initializes header links by reading the configured hypha, if there is any, or resorting to default values.
|
2021-02-17 18:41:35 +00:00
|
|
|
func SetHeaderLinks() {
|
2022-02-04 14:56:28 +00:00
|
|
|
switch userLinksHypha := hyphae.ByName(cfg.HeaderLinksHypha).(type) {
|
|
|
|
case *hyphae.EmptyHypha:
|
2021-05-11 08:33:00 +00:00
|
|
|
cfg.SetDefaultHeaderLinks()
|
2022-02-04 14:56:28 +00:00
|
|
|
default:
|
2022-02-04 10:50:50 +00:00
|
|
|
contents, err := os.ReadFile(userLinksHypha.TextPartPath())
|
2021-02-17 18:41:35 +00:00
|
|
|
if err != nil || len(contents) == 0 {
|
2021-05-11 08:33:00 +00:00
|
|
|
cfg.SetDefaultHeaderLinks()
|
2021-02-17 18:41:35 +00:00
|
|
|
} else {
|
|
|
|
text := string(contents)
|
2021-05-24 09:33:57 +00:00
|
|
|
cfg.ParseHeaderLinks(text)
|
2021-02-17 18:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|