1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 05:20:26 +00:00
mycorrhiza/shroom/backlink.go

37 lines
864 B
Go
Raw Normal View History

2021-02-20 14:03:54 +00:00
package shroom
import (
"io/ioutil"
"log"
"github.com/bouncepaw/mycorrhiza/hyphae"
"github.com/bouncepaw/mycorrhiza/markup"
)
// FindAllBacklinks iterates over all hyphae that have text parts, sets their outlinks and then sets backlinks.
func FindAllBacklinks() {
for h := range hyphae.FilterTextHyphae(hyphae.YieldExistingHyphae()) {
findBacklinkWorker(h)
}
}
func findBacklinkWorker(h *hyphae.Hypha) {
var (
textContents, err = ioutil.ReadFile(h.TextPath)
)
if err == nil {
for outlink := range markup.Doc(h.Name, string(textContents)).OutLinks() {
outlinkHypha := hyphae.ByName(outlink)
if outlinkHypha == h {
break
}
2021-02-20 14:03:54 +00:00
outlinkHypha.AddBackLink(h)
outlinkHypha.InsertIfNewKeepExistence()
h.AddOutLink(outlinkHypha)
2021-02-20 14:03:54 +00:00
}
} else {
log.Println("Error when reading text contents of %s: %s", h.Name, err.Error())
}
}