diff --git a/internal/epub/epub_tree.go b/internal/epub/epub_tree.go index cac65f0..dc329ed 100644 --- a/internal/epub/epub_tree.go +++ b/internal/epub/epub_tree.go @@ -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("") } diff --git a/internal/epub/tree/epub_tree.go b/internal/epub/tree/epub_tree.go new file mode 100644 index 0000000..3b69b54 --- /dev/null +++ b/internal/epub/tree/epub_tree.go @@ -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() +}