mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 08:12:36 +02:00
Merge pull request 'improve-sorting' (#1) from improve-sorting into main
Reviewed-on: #1
This commit is contained in:
commit
b1eae59324
@ -7,6 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
@ -102,6 +103,16 @@ func (e *ePub) getParts() ([]*epubPart, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sort.Slice(images, func(i, j int) bool {
|
||||||
|
if images[i].Id < images[j].Id {
|
||||||
|
return true
|
||||||
|
} else if images[i].Id == images[j].Id {
|
||||||
|
return images[i].Part < images[j].Part
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
parts := make([]*epubPart, 0)
|
parts := make([]*epubPart, 0)
|
||||||
cover := images[0]
|
cover := images[0]
|
||||||
if e.HasCover {
|
if e.HasCover {
|
||||||
|
@ -16,13 +16,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
_ "golang.org/x/image/webp"
|
"github.com/celogeek/go-comic-converter/v2/internal/epub/sortpath"
|
||||||
|
|
||||||
"github.com/disintegration/gift"
|
"github.com/disintegration/gift"
|
||||||
"github.com/nwaples/rardecode"
|
"github.com/nwaples/rardecode"
|
||||||
pdfimage "github.com/raff/pdfreader/image"
|
pdfimage "github.com/raff/pdfreader/image"
|
||||||
"github.com/raff/pdfreader/pdfread"
|
"github.com/raff/pdfreader/pdfread"
|
||||||
"golang.org/x/image/tiff"
|
"golang.org/x/image/tiff"
|
||||||
|
_ "golang.org/x/image/webp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Image struct {
|
type Image struct {
|
||||||
@ -138,6 +138,7 @@ func LoadImages(path string, options *ImageOptions, dry bool) ([]*Image, error)
|
|||||||
img.Path,
|
img.Path,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return images, nil
|
return images, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,15 +234,6 @@ func LoadImages(path string, options *ImageOptions, dry bool) ([]*Image, error)
|
|||||||
return nil, fmt.Errorf("image not found")
|
return nil, fmt.Errorf("image not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(images, func(i, j int) bool {
|
|
||||||
if images[i].Id < images[j].Id {
|
|
||||||
return true
|
|
||||||
} else if images[i].Id == images[j].Id && images[i].Part < images[j].Part {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
return images, nil
|
return images, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,7 +267,7 @@ func loadDir(input string) (int, chan *imageTask, error) {
|
|||||||
return 0, nil, fmt.Errorf("image not found")
|
return 0, nil, fmt.Errorf("image not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings(images)
|
sort.Sort(sortpath.By(images))
|
||||||
|
|
||||||
output := make(chan *imageTask)
|
output := make(chan *imageTask)
|
||||||
go func() {
|
go func() {
|
||||||
@ -320,21 +312,28 @@ func loadCbz(input string) (int, chan *imageTask, error) {
|
|||||||
return 0, nil, fmt.Errorf("no images found")
|
return 0, nil, fmt.Errorf("no images found")
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.SliceStable(images, func(i, j int) bool {
|
names := []string{}
|
||||||
return strings.Compare(images[i].Name, images[j].Name) < 0
|
for _, img := range images {
|
||||||
})
|
names = append(names, img.Name)
|
||||||
|
}
|
||||||
|
sort.Sort(sortpath.By(names))
|
||||||
|
|
||||||
|
indexedNames := make(map[string]int)
|
||||||
|
for i, name := range names {
|
||||||
|
indexedNames[name] = i
|
||||||
|
}
|
||||||
|
|
||||||
output := make(chan *imageTask)
|
output := make(chan *imageTask)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(output)
|
defer close(output)
|
||||||
for i, img := range images {
|
for _, img := range images {
|
||||||
f, err := img.Open()
|
f, err := img.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
output <- &imageTask{
|
output <- &imageTask{
|
||||||
Id: i,
|
Id: indexedNames[img.Name],
|
||||||
Reader: f,
|
Reader: f,
|
||||||
Path: filepath.Dir(filepath.Clean(img.Name)),
|
Path: filepath.Dir(filepath.Clean(img.Name)),
|
||||||
Filename: img.Name,
|
Filename: img.Name,
|
||||||
@ -373,7 +372,7 @@ func loadCbr(input string) (int, chan *imageTask, error) {
|
|||||||
return 0, nil, fmt.Errorf("no images found")
|
return 0, nil, fmt.Errorf("no images found")
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings(names)
|
sort.Sort(sortpath.By(names))
|
||||||
|
|
||||||
indexedNames := make(map[string]int)
|
indexedNames := make(map[string]int)
|
||||||
for i, name := range names {
|
for i, name := range names {
|
||||||
|
83
internal/epub/sortpath/core.go
Normal file
83
internal/epub/sortpath/core.go
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
package sortpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var split_path_regex = regexp.MustCompile(`^(.*?)(\d+)$`)
|
||||||
|
|
||||||
|
type part struct {
|
||||||
|
name string
|
||||||
|
number int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a part) Compare(b part) int {
|
||||||
|
if a.name == b.name {
|
||||||
|
return a.number - b.number
|
||||||
|
} else {
|
||||||
|
return strings.Compare(a.name, b.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parsePart(p string) part {
|
||||||
|
r := split_path_regex.FindStringSubmatch(p)
|
||||||
|
if len(r) == 0 {
|
||||||
|
return part{p, 0}
|
||||||
|
}
|
||||||
|
n, err := strconv.ParseInt(r[2], 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return part{p, 0}
|
||||||
|
}
|
||||||
|
return part{r[1], int(n)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parse(filename string) []part {
|
||||||
|
pathname, name := filepath.Split(strings.ToLower(filename))
|
||||||
|
pathname = strings.TrimSuffix(pathname, string(filepath.Separator))
|
||||||
|
ext := filepath.Ext(name)
|
||||||
|
name = name[0 : len(name)-len(ext)]
|
||||||
|
|
||||||
|
f := []part{}
|
||||||
|
for _, p := range strings.Split(pathname, string(filepath.Separator)) {
|
||||||
|
f = append(f, parsePart(p))
|
||||||
|
}
|
||||||
|
f = append(f, parsePart(name))
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func comparePart(a, b []part) int {
|
||||||
|
m := len(a)
|
||||||
|
if m > len(b) {
|
||||||
|
m = len(b)
|
||||||
|
}
|
||||||
|
for i := 0; i < m; i++ {
|
||||||
|
c := a[i].Compare(b[i])
|
||||||
|
if c != 0 {
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return len(a) - len(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
type by struct {
|
||||||
|
filenames []string
|
||||||
|
paths [][]part
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b by) Len() int { return len(b.filenames) }
|
||||||
|
func (b by) Less(i, j int) bool { return comparePart(b.paths[i], b.paths[j]) < 0 }
|
||||||
|
func (b by) Swap(i, j int) {
|
||||||
|
b.filenames[i], b.filenames[j] = b.filenames[j], b.filenames[i]
|
||||||
|
b.paths[i], b.paths[j] = b.paths[j], b.paths[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func By(filenames []string) by {
|
||||||
|
p := [][]part{}
|
||||||
|
for _, filename := range filenames {
|
||||||
|
p = append(p, parse(filename))
|
||||||
|
}
|
||||||
|
return by{filenames, p}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user