2018-11-09 17:25:50 +00:00
|
|
|
package tree
|
2018-05-27 15:36:03 +00:00
|
|
|
|
2018-10-25 16:09:03 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2020-08-12 18:13:01 +00:00
|
|
|
|
2020-11-06 17:50:36 +00:00
|
|
|
"git.sp4ke.xyz/sp4ke/gomark/bookmarks"
|
|
|
|
"git.sp4ke.xyz/sp4ke/gomark/index"
|
|
|
|
"git.sp4ke.xyz/sp4ke/gomark/logging"
|
2018-10-25 16:09:03 +00:00
|
|
|
|
|
|
|
"github.com/xlab/treeprint"
|
|
|
|
)
|
|
|
|
|
2018-11-09 17:25:50 +00:00
|
|
|
var log = logging.GetLogger("")
|
|
|
|
|
|
|
|
type Bookmark = bookmarks.Bookmark
|
|
|
|
|
2018-06-08 15:38:57 +00:00
|
|
|
type Node struct {
|
|
|
|
Name string
|
2018-10-24 16:16:42 +00:00
|
|
|
Type string // folder, tag, url
|
2018-06-08 15:38:57 +00:00
|
|
|
URL string
|
|
|
|
Tags []string
|
|
|
|
Desc string
|
|
|
|
HasChanged bool
|
|
|
|
NameHash uint64 // hash of the metadata
|
|
|
|
Parent *Node
|
|
|
|
Children []*Node
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:09:03 +00:00
|
|
|
func (node *Node) GetRoot() *Node {
|
|
|
|
nodePtr := node
|
|
|
|
|
2022-09-28 20:07:03 +00:00
|
|
|
for nodePtr.Name != "root" {
|
2018-10-25 16:09:03 +00:00
|
|
|
nodePtr = nodePtr.Parent
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodePtr
|
|
|
|
}
|
|
|
|
|
2018-11-13 16:11:16 +00:00
|
|
|
// Insert *Node in nodeList if it does not already exists
|
|
|
|
func Insert(nodeList []*Node, node *Node) []*Node {
|
|
|
|
for _, n := range nodeList {
|
|
|
|
if node == n {
|
2020-09-13 23:57:39 +00:00
|
|
|
log.Errorf("<%s> Node already exists", node.URL)
|
2018-11-13 16:11:16 +00:00
|
|
|
return nodeList
|
|
|
|
} else {
|
|
|
|
nodeList = append(nodeList, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodeList
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:09:03 +00:00
|
|
|
// Returns all parent tags for URL nodes
|
|
|
|
func (node *Node) GetParentTags() []*Node {
|
|
|
|
var parents []*Node
|
|
|
|
var walk func(node *Node)
|
|
|
|
var nodePtr *Node
|
|
|
|
|
|
|
|
root := node.GetRoot()
|
|
|
|
|
|
|
|
walk = func(n *Node) {
|
|
|
|
nodePtr = n
|
|
|
|
|
|
|
|
if nodePtr.Type == "url" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(nodePtr.Children) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range nodePtr.Children {
|
|
|
|
if v.URL == node.URL &&
|
|
|
|
nodePtr.Type == "tag" {
|
|
|
|
parents = append(parents, nodePtr)
|
|
|
|
}
|
|
|
|
walk(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
walk(root)
|
|
|
|
return parents
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintTree(root *Node) {
|
|
|
|
var walk func(node *Node, tree treeprint.Tree)
|
|
|
|
tree := treeprint.New()
|
|
|
|
|
|
|
|
walk = func(node *Node, t treeprint.Tree) {
|
|
|
|
|
|
|
|
if len(node.Children) > 0 {
|
|
|
|
t = t.AddBranch(fmt.Sprintf("%s <%s>", node.Type, node.Name))
|
|
|
|
|
|
|
|
for _, child := range node.Children {
|
2018-10-25 16:19:15 +00:00
|
|
|
go walk(child, t)
|
2018-10-25 16:09:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.AddNode(fmt.Sprintf("%s <%s>", node.Type, node.URL))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
walk(root, tree)
|
2018-11-09 17:25:50 +00:00
|
|
|
fmt.Println(tree.String())
|
2018-10-25 16:09:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 15:36:03 +00:00
|
|
|
// Rebuilds the memory url index after parsing all bookmarks.
|
|
|
|
// Keeps memory index in sync with last known state of browser bookmarks
|
2018-11-09 17:25:50 +00:00
|
|
|
func WalkBuildIndex(node *Node, index index.HashTree) {
|
2018-05-27 15:36:03 +00:00
|
|
|
if node.Type == "url" {
|
2018-11-09 17:25:50 +00:00
|
|
|
index.Insert(node.URL, node)
|
2018-05-27 15:36:03 +00:00
|
|
|
//log.Debugf("Inserted URL: %s and Hash: %v", node.URL, node.NameHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(node.Children) > 0 {
|
|
|
|
for _, node := range node.Children {
|
2019-03-01 18:14:12 +00:00
|
|
|
WalkBuildIndex(node, index)
|
2018-05-27 15:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 17:25:50 +00:00
|
|
|
func (node *Node) GetBookmark() *Bookmark {
|
|
|
|
return &Bookmark{
|
|
|
|
URL: node.URL,
|
|
|
|
Metadata: node.Name,
|
|
|
|
Desc: node.Desc,
|
|
|
|
Tags: node.Tags,
|
2018-05-27 15:36:03 +00:00
|
|
|
}
|
|
|
|
}
|