From 1f36af66a53f88b0854be7318a74a76720ebbe30 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Tue, 21 Dec 2021 00:08:21 +0300 Subject: [PATCH] Move backlinks stuff to a separate module --- hyphae/{ => backlinks}/backlinks.go | 27 ++-- hyphae/files.go | 2 - main.go | 4 +- shroom/delete.go | 3 +- shroom/rename.go | 3 +- shroom/upload.go | 3 +- views/nav.qtpl | 3 +- views/nav.qtpl.go | 209 ++++++++++++++-------------- web/backlinks.go | 6 +- web/stuff.go | 2 + 10 files changed, 136 insertions(+), 126 deletions(-) rename hyphae/{ => backlinks}/backlinks.go (88%) diff --git a/hyphae/backlinks.go b/hyphae/backlinks/backlinks.go similarity index 88% rename from hyphae/backlinks.go rename to hyphae/backlinks/backlinks.go index 7bce899..444f373 100644 --- a/hyphae/backlinks.go +++ b/hyphae/backlinks/backlinks.go @@ -1,14 +1,15 @@ -package hyphae +// Package backlinks maintains the index of backlinks and lets you update it and query it. +package backlinks import ( - "github.com/bouncepaw/mycomarkup/v3/tools" - "os" - + "github.com/bouncepaw/mycorrhiza/hyphae" "github.com/bouncepaw/mycorrhiza/util" + "os" "github.com/bouncepaw/mycomarkup/v3" "github.com/bouncepaw/mycomarkup/v3/links" "github.com/bouncepaw/mycomarkup/v3/mycocontext" + "github.com/bouncepaw/mycomarkup/v3/tools" ) // Using set here seems like the most appropriate solution @@ -22,7 +23,7 @@ func toLinkSet(xs []string) linkSet { return result } -func fetchText(h *Hypha) string { +func fetchText(h *hyphae.Hypha) string { if h.TextPath == "" { return "" } @@ -70,7 +71,7 @@ type backlinkIndexDeletion struct { Links []string } -// Apply changes backlink index respective to the operation data +// apply changes backlink index respective to the operation data func (op backlinkIndexDeletion) apply() { for _, link := range op.Links { if lSet, exists := backlinkIndex[link]; exists { @@ -105,7 +106,7 @@ var backlinkConveyor = make(chan backlinkIndexOperation, 64) // IndexBacklinks traverses all text hyphae, extracts links from them and forms an initial index func IndexBacklinks() { // It is safe to ignore the mutex, because there is only one worker. - src := FilterTextHyphae(YieldExistingHyphae()) + src := hyphae.FilterTextHyphae(hyphae.YieldExistingHyphae()) for h := range src { foundLinks := extractHyphaLinksFromContent(h.Name, fetchText(h)) for _, link := range foundLinks { @@ -127,7 +128,7 @@ func RunBacklinksConveyor() { } // BacklinksCount returns the amount of backlinks to the hypha. -func BacklinksCount(h *Hypha) int { +func BacklinksCount(h *hyphae.Hypha) int { if _, exists := backlinkIndex[h.Name]; exists { return len(backlinkIndex[h.Name]) } @@ -135,20 +136,20 @@ func BacklinksCount(h *Hypha) int { } // BacklinksOnEdit is a creation/editing hook for backlinks index -func BacklinksOnEdit(h *Hypha, oldText string) { +func BacklinksOnEdit(h *hyphae.Hypha, oldText string) { oldLinks := extractHyphaLinksFromContent(h.Name, oldText) newLinks := extractHyphaLinks(h) backlinkConveyor <- backlinkIndexEdit{h.Name, oldLinks, newLinks} } // BacklinksOnDelete is a deletion hook for backlinks index -func BacklinksOnDelete(h *Hypha, oldText string) { +func BacklinksOnDelete(h *hyphae.Hypha, oldText string) { oldLinks := extractHyphaLinksFromContent(h.Name, oldText) backlinkConveyor <- backlinkIndexDeletion{h.Name, oldLinks} } // BacklinksOnRename is a renaming hook for backlinks index -func BacklinksOnRename(h *Hypha, oldName string) { +func BacklinksOnRename(h *hyphae.Hypha, oldName string) { actualLinks := extractHyphaLinks(h) backlinkConveyor <- backlinkIndexRenaming{oldName, h.Name, actualLinks} } @@ -157,7 +158,7 @@ func BacklinksOnRename(h *Hypha, oldName string) { func YieldHyphaBacklinks(query string) <-chan string { hyphaName := util.CanonicalName(query) out := make(chan string) - sorted := PathographicSort(out) + sorted := hyphae.PathographicSort(out) go func() { backlinks, exists := backlinkIndex[hyphaName] if exists { @@ -171,7 +172,7 @@ func YieldHyphaBacklinks(query string) <-chan string { } // extractHyphaLinks extracts hypha links from a desired hypha -func extractHyphaLinks(h *Hypha) []string { +func extractHyphaLinks(h *hyphae.Hypha) []string { return extractHyphaLinksFromContent(h.Name, fetchText(h)) } diff --git a/hyphae/files.go b/hyphae/files.go index a4f5923..6f80318 100644 --- a/hyphae/files.go +++ b/hyphae/files.go @@ -26,8 +26,6 @@ func Index(path string) { h.insert() } } - - IndexBacklinks() log.Println("Indexed", Count(), "hyphae") } diff --git a/main.go b/main.go index 290838e..7f6c23c 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ package main import ( + "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" "log" "os" @@ -39,7 +40,8 @@ func main() { // Init the subsystems: hyphae.Index(files.HyphaeDir()) - go hyphae.RunBacklinksConveyor() + backlinks.IndexBacklinks() + go backlinks.RunBacklinksConveyor() user.InitUserDatabase() history.Start() history.InitGitRepo() diff --git a/shroom/delete.go b/shroom/delete.go index fcacb7a..017e36f 100644 --- a/shroom/delete.go +++ b/shroom/delete.go @@ -2,6 +2,7 @@ package shroom import ( "fmt" + "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" "github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/hyphae" @@ -25,7 +26,7 @@ func DeleteHypha(u *user.User, h *hyphae.Hypha, lc *l18n.Localizer) (hop *histor WithUser(u). Apply() if !hop.HasErrors() { - hyphae.BacklinksOnDelete(h, originalText) + backlinks.BacklinksOnDelete(h, originalText) h.Delete() } return hop, "" diff --git a/shroom/rename.go b/shroom/rename.go index d68897d..e812937 100644 --- a/shroom/rename.go +++ b/shroom/rename.go @@ -3,6 +3,7 @@ package shroom import ( "errors" "fmt" + "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" "regexp" "github.com/bouncepaw/mycorrhiza/history" @@ -74,7 +75,7 @@ func RenameHypha(h *hyphae.Hypha, newHypha *hyphae.Hypha, recursive bool, u *use h.TextPath = replaceName(h.TextPath) h.BinaryPath = replaceName(h.BinaryPath) h.Unlock() - hyphae.BacklinksOnRename(h, oldName) + backlinks.BacklinksOnRename(h, oldName) } } return hop, "" diff --git a/shroom/upload.go b/shroom/upload.go index 176eb49..d9b3f0e 100644 --- a/shroom/upload.go +++ b/shroom/upload.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" "io" "log" "mime/multipart" @@ -105,7 +106,7 @@ func uploadHelp(h *hyphae.Hypha, hop *history.Op, ext string, data []byte, u *us } *originalFullPath = fullPath if hop.Type == history.TypeEditText { - hyphae.BacklinksOnEdit(h, originalText) + backlinks.BacklinksOnEdit(h, originalText) } return hop.WithFiles(fullPath).WithUser(u).Apply(), "" } diff --git a/views/nav.qtpl b/views/nav.qtpl index 700d1b9..be9c520 100644 --- a/views/nav.qtpl +++ b/views/nav.qtpl @@ -1,5 +1,6 @@ {% import "net/http" %} {% import "strings" %} +{% import "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" %} {% import "github.com/bouncepaw/mycorrhiza/l18n" %} {% import "github.com/bouncepaw/mycorrhiza/user" %} {% import "github.com/bouncepaw/mycorrhiza/hyphae" %} @@ -16,7 +17,7 @@ {% code u := user.FromRequest(rq) lc := l18n.FromRequest(rq) - backs := hyphae.BacklinksCount(h) + backs := backlinks.BacklinksCount(h) %}