1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-01-07 10:20:26 +00:00

Modify Chekoopa's code for no real reason

I made some functions non-exported.

I like your approach with the interface for backlink index operations!
This commit is contained in:
Timur Ismagilov 2021-09-05 22:28:20 +03:00
parent a220ca2002
commit 44596e251c

View File

@ -33,18 +33,18 @@ func fetchText(h *Hypha) string {
return "" return ""
} }
// We'll use a quasi-union type for proper async changes // BacklinkIndexOperation is an operation for the backlink index. This operation is executed async-safe.
type BackIndexOperation interface { type BacklinkIndexOperation interface {
Apply() Apply()
} }
type BackIndexEditing struct { type BacklinkIndexEdit struct {
Name string Name string
OldLinks []string OldLinks []string
NewLinks []string NewLinks []string
} }
func (op BackIndexEditing) Apply() { func (op BacklinkIndexEdit) Apply() {
oldLinks := toLinkSet(op.OldLinks) oldLinks := toLinkSet(op.OldLinks)
newLinks := toLinkSet(op.NewLinks) newLinks := toLinkSet(op.NewLinks)
for link := range oldLinks { for link := range oldLinks {
@ -62,12 +62,12 @@ func (op BackIndexEditing) Apply() {
} }
} }
type BackIndexDeletion struct { type BacklinkIndexDeletion struct {
Name string Name string
Links []string Links []string
} }
func (op BackIndexDeletion) Apply() { func (op BacklinkIndexDeletion) Apply() {
for _, link := range op.Links { for _, link := range op.Links {
if lSet, exists := backlinkIndex[link]; exists { if lSet, exists := backlinkIndex[link]; exists {
delete(lSet, op.Name) delete(lSet, op.Name)
@ -75,13 +75,13 @@ func (op BackIndexDeletion) Apply() {
} }
} }
type BackIndexRenaming struct { type BacklinkIndexRenaming struct {
OldName string OldName string
NewName string NewName string
Links []string Links []string
} }
func (op BackIndexRenaming) Apply() { func (op BacklinkIndexRenaming) Apply() {
for _, link := range op.Links { for _, link := range op.Links {
if lSet, exists := backlinkIndex[link]; exists { if lSet, exists := backlinkIndex[link]; exists {
delete(lSet, op.OldName) delete(lSet, op.OldName)
@ -91,16 +91,18 @@ func (op BackIndexRenaming) Apply() {
} }
var backlinkIndex = make(map[string]linkSet) var backlinkIndex = make(map[string]linkSet)
var backlinkConveyor = make(chan BackIndexOperation, 64) var backlinkConveyor = make(chan BacklinkIndexOperation, 64)
// I hope, the buffer size is enough -- chekoopa // I hope, the buffer size is enough -- chekoopa
// Do we really need the buffer though? Dunno -- bouncepaw
// IndexBacklinks traverses all text hyphae, extracts links from them and forms an initial index // IndexBacklinks traverses all text hyphae, extracts links from them and forms an initial index
func IndexBacklinks() { func IndexBacklinks() {
// It is safe to ignore the mutex, because there is only one worker. // It is safe to ignore the mutex, because there is only one worker.
src := FilterTextHyphae(YieldExistingHyphae()) src := FilterTextHyphae(YieldExistingHyphae())
for h := range src { for h := range src {
links := ExtractHyphaLinksFromContent(h.Name, fetchText(h)) foundLinks := extractHyphaLinksFromContent(h.Name, fetchText(h))
for _, link := range links { for _, link := range foundLinks {
if _, exists := backlinkIndex[link]; !exists { if _, exists := backlinkIndex[link]; !exists {
backlinkIndex[link] = make(linkSet) backlinkIndex[link] = make(linkSet)
} }
@ -118,7 +120,7 @@ func RunBacklinksConveyor() {
} }
} }
// BacklinksCount return an amount of backlinks for a provided hypha // BacklinksCount returns the amount of backlinks to the hypha.
func BacklinksCount(h *Hypha) int { func BacklinksCount(h *Hypha) int {
if _, exists := backlinkIndex[h.Name]; exists { if _, exists := backlinkIndex[h.Name]; exists {
return len(backlinkIndex[h.Name]) return len(backlinkIndex[h.Name])
@ -128,21 +130,21 @@ func BacklinksCount(h *Hypha) int {
// BacklinksOnEdit is a creation/editing hook for backlinks index // BacklinksOnEdit is a creation/editing hook for backlinks index
func BacklinksOnEdit(h *Hypha, oldText string) { func BacklinksOnEdit(h *Hypha, oldText string) {
oldLinks := ExtractHyphaLinksFromContent(h.Name, oldText) oldLinks := extractHyphaLinksFromContent(h.Name, oldText)
newLinks := ExtractHyphaLinks(h) newLinks := extractHyphaLinks(h)
backlinkConveyor <- BackIndexEditing{h.Name, oldLinks, newLinks} backlinkConveyor <- BacklinkIndexEdit{h.Name, oldLinks, newLinks}
} }
// BacklinksOnDelete is a deletion hook for backlinks index // BacklinksOnDelete is a deletion hook for backlinks index
func BacklinksOnDelete(h *Hypha, oldText string) { func BacklinksOnDelete(h *Hypha, oldText string) {
oldLinks := ExtractHyphaLinksFromContent(h.Name, oldText) oldLinks := extractHyphaLinksFromContent(h.Name, oldText)
backlinkConveyor <- BackIndexDeletion{h.Name, oldLinks} backlinkConveyor <- BacklinkIndexDeletion{h.Name, oldLinks}
} }
// BacklinksOnRename is a renaming hook for backlinks index // BacklinksOnRename is a renaming hook for backlinks index
func BacklinksOnRename(h *Hypha, oldName string) { func BacklinksOnRename(h *Hypha, oldName string) {
actualLinks := ExtractHyphaLinks(h) actualLinks := extractHyphaLinks(h)
backlinkConveyor <- BackIndexRenaming{oldName, h.Name, actualLinks} backlinkConveyor <- BacklinkIndexRenaming{oldName, h.Name, actualLinks}
} }
// YieldHyphaBacklinks gets backlinks for a desired hypha, sorts and iterates over them // YieldHyphaBacklinks gets backlinks for a desired hypha, sorts and iterates over them
@ -151,9 +153,9 @@ func YieldHyphaBacklinks(query string) <-chan string {
out := make(chan string) out := make(chan string)
sorted := PathographicSort(out) sorted := PathographicSort(out)
go func() { go func() {
links, exists := backlinkIndex[hyphaName] backlinks, exists := backlinkIndex[hyphaName]
if exists { if exists {
for link := range links { for link := range backlinks {
out <- link out <- link
} }
} }
@ -162,33 +164,17 @@ func YieldHyphaBacklinks(query string) <-chan string {
return sorted return sorted
} }
// YieldHyphaLinks extracts hypha links from a desired hypha, sorts and iterates over them // extractHyphaLinks extracts hypha links from a desired hypha
func YieldHyphaLinks(query string) <-chan string { func extractHyphaLinks(h *Hypha) []string {
// That is merely a debug function, but it could be useful. return extractHyphaLinksFromContent(h.Name, fetchText(h))
// Should we extract them into link-specific subfile? -- chekoopa
hyphaName := util.CanonicalName(query)
out := make(chan string)
go func() {
var h = ByName(hyphaName)
links := ExtractHyphaLinks(h)
for _, link := range links {
out <- link
}
close(out)
}()
return out
} }
// ExtractHyphaLinks extracts hypha links from a desired hypha // extractHyphaLinksFromContent extracts local hypha links from the provided text.
func ExtractHyphaLinks(h *Hypha) []string { func extractHyphaLinksFromContent(hyphaName string, contents string) []string {
return ExtractHyphaLinksFromContent(h.Name, fetchText(h))
}
// ExtractHyphaLinksFromContent extracts hypha links from a provided text
func ExtractHyphaLinksFromContent(hyphaName string, contents string) []string {
ctx, _ := mycocontext.ContextFromStringInput(hyphaName, contents) ctx, _ := mycocontext.ContextFromStringInput(hyphaName, contents)
linkVisitor, getLinks := LinkVisitor(ctx) linkVisitor, getLinks := LinkVisitor(ctx)
mycomarkup.BlockTree(ctx, linkVisitor) // Ignore the result of BlockTree because we call it for linkVisitor.
_ = mycomarkup.BlockTree(ctx, linkVisitor)
foundLinks := getLinks() foundLinks := getLinks()
var result []string var result []string
for _, link := range foundLinks { for _, link := range foundLinks {
@ -200,18 +186,22 @@ func ExtractHyphaLinksFromContent(hyphaName string, contents string) []string {
} }
// LinkVisitor creates a visitor which extracts all the links // LinkVisitor creates a visitor which extracts all the links
//
// We consider inline link, rocket link, image gallery and transclusion targets to be links.
// TODO: replace with the one in Mycomarkup.
func LinkVisitor(ctx mycocontext.Context) ( func LinkVisitor(ctx mycocontext.Context) (
visitor func(block blocks.Block), visitor func(block blocks.Block),
result func() []links.Link, result func() []links.Link,
) { ) {
var ( var (
collected []links.Link collected []links.Link
extractBlock func(block blocks.Block)
) )
var extractBlock func(block blocks.Block)
extractBlock = func(block blocks.Block) { extractBlock = func(block blocks.Block) {
// fmt.Println(reflect.TypeOf(block)) // fmt.Println(reflect.TypeOf(block))
switch b := block.(type) { switch b := block.(type) {
case blocks.Paragraph: case blocks.Paragraph:
// What a wonderful recursion technique you have! I like it.
extractBlock(b.Formatted) extractBlock(b.Formatted)
case blocks.Heading: case blocks.Heading:
extractBlock(b.GetContents()) extractBlock(b.GetContents())