move tree

This commit is contained in:
Celogeek 2023-04-23 14:10:35 +02:00
parent 2444dfee3b
commit a150128e73
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
2 changed files with 57 additions and 50 deletions

View File

@ -2,58 +2,12 @@ package epub
import (
"path/filepath"
"strings"
"github.com/celogeek/go-comic-converter/v2/internal/epub/tree"
)
type Tree struct {
Nodes map[string]*Node
}
type Node struct {
Value string
Children []*Node
}
func NewTree() *Tree {
return &Tree{map[string]*Node{
".": {".", []*Node{}},
}}
}
func (n *Tree) Root() *Node {
return n.Nodes["."]
}
func (n *Tree) Add(filename string) {
cn := n.Root()
cp := ""
for _, p := range strings.Split(filepath.Clean(filename), string(filepath.Separator)) {
cp = filepath.Join(cp, p)
if _, ok := n.Nodes[cp]; !ok {
n.Nodes[cp] = &Node{Value: p, Children: []*Node{}}
cn.Children = append(cn.Children, n.Nodes[cp])
}
cn = n.Nodes[cp]
}
}
func (n *Node) toString(indent string) string {
r := strings.Builder{}
if indent != "" {
r.WriteString(indent)
r.WriteString("- ")
r.WriteString(n.Value)
r.WriteString("\n")
}
indent += " "
for _, c := range n.Children {
r.WriteString(c.toString(indent))
}
return r.String()
}
func (e *ePub) getTree(images []*Image, skip_files bool) string {
t := NewTree()
t := tree.New()
for _, img := range images {
if skip_files {
t.Add(img.Path)
@ -66,5 +20,5 @@ func (e *ePub) getTree(images []*Image, skip_files bool) string {
c = c.Children[0]
}
return c.toString("")
return c.ToString("")
}

View File

@ -0,0 +1,53 @@
package tree
import (
"path/filepath"
"strings"
)
type Tree struct {
Nodes map[string]*Node
}
type Node struct {
Value string
Children []*Node
}
func New() *Tree {
return &Tree{map[string]*Node{
".": {".", []*Node{}},
}}
}
func (n *Tree) Root() *Node {
return n.Nodes["."]
}
func (n *Tree) Add(filename string) {
cn := n.Root()
cp := ""
for _, p := range strings.Split(filepath.Clean(filename), string(filepath.Separator)) {
cp = filepath.Join(cp, p)
if _, ok := n.Nodes[cp]; !ok {
n.Nodes[cp] = &Node{Value: p, Children: []*Node{}}
cn.Children = append(cn.Children, n.Nodes[cp])
}
cn = n.Nodes[cp]
}
}
func (n *Node) ToString(indent string) string {
r := strings.Builder{}
if indent != "" {
r.WriteString(indent)
r.WriteString("- ")
r.WriteString(n.Value)
r.WriteString("\n")
}
indent += " "
for _, c := range n.Children {
r.WriteString(c.ToString(indent))
}
return r.String()
}