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() {
|
2021-02-20 16:23:47 +00:00
|
|
|
|
outlinkHypha := hyphae.ByName(outlink)
|
|
|
|
|
if outlinkHypha == h {
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-02-20 14:03:54 +00:00
|
|
|
|
|
2021-02-20 16:23:47 +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())
|
|
|
|
|
}
|
|
|
|
|
}
|