mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 08:12:36 +02:00
move tree
This commit is contained in:
parent
2444dfee3b
commit
a150128e73
@ -2,58 +2,12 @@ package epub
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
"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 {
|
func (e *ePub) getTree(images []*Image, skip_files bool) string {
|
||||||
t := NewTree()
|
t := tree.New()
|
||||||
for _, img := range images {
|
for _, img := range images {
|
||||||
if skip_files {
|
if skip_files {
|
||||||
t.Add(img.Path)
|
t.Add(img.Path)
|
||||||
@ -66,5 +20,5 @@ func (e *ePub) getTree(images []*Image, skip_files bool) string {
|
|||||||
c = c.Children[0]
|
c = c.Children[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.toString("")
|
return c.ToString("")
|
||||||
}
|
}
|
||||||
|
53
internal/epub/tree/epub_tree.go
Normal file
53
internal/epub/tree/epub_tree.go
Normal 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()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user