mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 15:52:38 +02:00
fix export type
This commit is contained in:
parent
23388b5f30
commit
2990367e2f
@ -23,7 +23,7 @@ import (
|
|||||||
"github.com/gofrs/uuid"
|
"github.com/gofrs/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ePub struct {
|
type EPub struct {
|
||||||
*epuboptions.Options
|
*epuboptions.Options
|
||||||
UID string
|
UID string
|
||||||
Publisher string
|
Publisher string
|
||||||
@ -40,7 +40,7 @@ type epubPart struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New initialize EPUB
|
// New initialize EPUB
|
||||||
func New(options *epuboptions.Options) *ePub {
|
func New(options *epuboptions.Options) *EPub {
|
||||||
uid := uuid.Must(uuid.NewV4())
|
uid := uuid.Must(uuid.NewV4())
|
||||||
tmpl := template.New("parser")
|
tmpl := template.New("parser")
|
||||||
tmpl.Funcs(template.FuncMap{
|
tmpl.Funcs(template.FuncMap{
|
||||||
@ -48,7 +48,7 @@ func New(options *epuboptions.Options) *ePub {
|
|||||||
"zoom": func(s int, z float32) int { return int(float32(s) * z) },
|
"zoom": func(s int, z float32) int { return int(float32(s) * z) },
|
||||||
})
|
})
|
||||||
|
|
||||||
return &ePub{
|
return &EPub{
|
||||||
Options: options,
|
Options: options,
|
||||||
UID: uid.String(),
|
UID: uid.String(),
|
||||||
Publisher: "GO Comic Converter",
|
Publisher: "GO Comic Converter",
|
||||||
@ -59,7 +59,7 @@ func New(options *epuboptions.Options) *ePub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// render templates
|
// render templates
|
||||||
func (e *ePub) render(templateString string, data map[string]any) string {
|
func (e *EPub) render(templateString string, data map[string]any) string {
|
||||||
var result strings.Builder
|
var result strings.Builder
|
||||||
tmpl := template.Must(e.templateProcessor.Parse(templateString))
|
tmpl := template.Must(e.templateProcessor.Parse(templateString))
|
||||||
if err := tmpl.Execute(&result, data); err != nil {
|
if err := tmpl.Execute(&result, data); err != nil {
|
||||||
@ -69,7 +69,7 @@ func (e *ePub) render(templateString string, data map[string]any) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// write image to the zip
|
// write image to the zip
|
||||||
func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip.File) error {
|
func (e *EPub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip.File) error {
|
||||||
err := wz.WriteContent(
|
err := wz.WriteContent(
|
||||||
img.EPUBPagePath(),
|
img.EPUBPagePath(),
|
||||||
[]byte(e.render(epubtemplates.Text, map[string]any{
|
[]byte(e.render(epubtemplates.Text, map[string]any{
|
||||||
@ -87,7 +87,7 @@ func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip
|
|||||||
}
|
}
|
||||||
|
|
||||||
// write blank page
|
// write blank page
|
||||||
func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error {
|
func (e *EPub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error {
|
||||||
return wz.WriteContent(
|
return wz.WriteContent(
|
||||||
img.EPUBSpacePath(),
|
img.EPUBSpacePath(),
|
||||||
[]byte(e.render(epubtemplates.Blank, map[string]any{
|
[]byte(e.render(epubtemplates.Blank, map[string]any{
|
||||||
@ -98,7 +98,7 @@ func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// write title image
|
// write title image
|
||||||
func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part, totalParts int) error {
|
func (e *EPub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part, totalParts int) error {
|
||||||
title := "Cover"
|
title := "Cover"
|
||||||
text := ""
|
text := ""
|
||||||
if totalParts > 1 {
|
if totalParts > 1 {
|
||||||
@ -141,7 +141,7 @@ func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// write title image
|
// write title image
|
||||||
func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.Image, title string) error {
|
func (e *EPub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.Image, title string) error {
|
||||||
titleAlign := ""
|
titleAlign := ""
|
||||||
if !e.Image.View.PortraitOnly {
|
if !e.Image.View.PortraitOnly {
|
||||||
if e.Image.Manga {
|
if e.Image.Manga {
|
||||||
@ -197,7 +197,7 @@ func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.Image, title
|
|||||||
}
|
}
|
||||||
|
|
||||||
// extract image and split it into part
|
// extract image and split it into part
|
||||||
func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.StorageImageReader, err error) {
|
func (e *EPub) getParts() (parts []*epubPart, imgStorage *epubzip.StorageImageReader, err error) {
|
||||||
images, err := e.imageProcessor.Load()
|
images, err := e.imageProcessor.Load()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -268,7 +268,7 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.StorageImageRe
|
|||||||
// create a tree from the directories.
|
// create a tree from the directories.
|
||||||
//
|
//
|
||||||
// this is used to simulate the toc.
|
// this is used to simulate the toc.
|
||||||
func (e *ePub) getTree(images []*epubimage.Image, skipFiles bool) string {
|
func (e *EPub) getTree(images []*epubimage.Image, skipFiles bool) string {
|
||||||
t := epubtree.New()
|
t := epubtree.New()
|
||||||
for _, img := range images {
|
for _, img := range images {
|
||||||
if skipFiles {
|
if skipFiles {
|
||||||
@ -278,14 +278,14 @@ func (e *ePub) getTree(images []*epubimage.Image, skipFiles bool) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
c := t.Root()
|
c := t.Root()
|
||||||
if skipFiles && e.StripFirstDirectoryFromToc && len(c.Children) == 1 {
|
if skipFiles && e.StripFirstDirectoryFromToc && c.ChildCount() == 1 {
|
||||||
c = c.Children[0]
|
c = c.FirstChild()
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.WriteString("")
|
return c.WriteString("")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ePub) computeAspectRatio(epubParts []*epubPart) float64 {
|
func (e *EPub) computeAspectRatio(epubParts []*epubPart) float64 {
|
||||||
var (
|
var (
|
||||||
bestAspectRatio float64
|
bestAspectRatio float64
|
||||||
bestAspectRatioCount int
|
bestAspectRatioCount int
|
||||||
@ -312,7 +312,7 @@ func (e *ePub) computeAspectRatio(epubParts []*epubPart) float64 {
|
|||||||
return bestAspectRatio
|
return bestAspectRatio
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ePub) computeViewPort(epubParts []*epubPart) {
|
func (e *EPub) computeViewPort(epubParts []*epubPart) {
|
||||||
if e.Image.View.AspectRatio == -1 {
|
if e.Image.View.AspectRatio == -1 {
|
||||||
return //keep device size
|
return //keep device size
|
||||||
}
|
}
|
||||||
@ -331,7 +331,7 @@ func (e *ePub) computeViewPort(epubParts []*epubPart) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ePub) writePart(path string, currentPart, totalParts int, part *epubPart, imgStorage *epubzip.StorageImageReader) error {
|
func (e *EPub) writePart(path string, currentPart, totalParts int, part *epubPart, imgStorage *epubzip.StorageImageReader) error {
|
||||||
hasTitlePage := e.TitlePage == 1 || (e.TitlePage == 2 && totalParts > 1)
|
hasTitlePage := e.TitlePage == 1 || (e.TitlePage == 2 && totalParts > 1)
|
||||||
|
|
||||||
wz, err := epubzip.New(path)
|
wz, err := epubzip.New(path)
|
||||||
@ -410,7 +410,7 @@ func (e *ePub) writePart(path string, currentPart, totalParts int, part *epubPar
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create the zip
|
// create the zip
|
||||||
func (e *ePub) Write() error {
|
func (e *EPub) Write() error {
|
||||||
epubParts, imgStorage, err := e.getParts()
|
epubParts, imgStorage, err := e.getParts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// AutoContrast Automatically improve contrast
|
// AutoContrast Automatically improve contrast
|
||||||
func AutoContrast() *autocontrast {
|
func AutoContrast() gift.Filter {
|
||||||
return &autocontrast{}
|
return &autocontrast{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,52 +23,60 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tree struct {
|
type Tree struct {
|
||||||
Nodes map[string]*node
|
nodes map[string]*Node
|
||||||
}
|
}
|
||||||
|
|
||||||
type node struct {
|
type Node struct {
|
||||||
Value string
|
value string
|
||||||
Children []*node
|
children []*Node
|
||||||
}
|
}
|
||||||
|
|
||||||
// New initialize tree with a root node
|
// New initialize tree with a root node
|
||||||
func New() *tree {
|
func New() *Tree {
|
||||||
return &tree{map[string]*node{
|
return &Tree{map[string]*Node{
|
||||||
".": {".", []*node{}},
|
".": {".", []*Node{}},
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Root root node
|
// Root root node
|
||||||
func (n *tree) Root() *node {
|
func (n *Tree) Root() *Node {
|
||||||
return n.Nodes["."]
|
return n.nodes["."]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the filename to the tree
|
// Add the filename to the tree
|
||||||
func (n *tree) Add(filename string) {
|
func (n *Tree) Add(filename string) {
|
||||||
cn := n.Root()
|
cn := n.Root()
|
||||||
cp := ""
|
cp := ""
|
||||||
for _, p := range strings.Split(filepath.Clean(filename), string(filepath.Separator)) {
|
for _, p := range strings.Split(filepath.Clean(filename), string(filepath.Separator)) {
|
||||||
cp = filepath.Join(cp, p)
|
cp = filepath.Join(cp, p)
|
||||||
if _, ok := n.Nodes[cp]; !ok {
|
if _, ok := n.nodes[cp]; !ok {
|
||||||
n.Nodes[cp] = &node{Value: p, Children: []*node{}}
|
n.nodes[cp] = &Node{value: p, children: []*Node{}}
|
||||||
cn.Children = append(cn.Children, n.Nodes[cp])
|
cn.children = append(cn.children, n.nodes[cp])
|
||||||
}
|
}
|
||||||
cn = n.Nodes[cp]
|
cn = n.nodes[cp]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Node) ChildCount() int {
|
||||||
|
return len(n.children)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node) FirstChild() *Node {
|
||||||
|
return n.children[0]
|
||||||
|
}
|
||||||
|
|
||||||
// WriteString string version of the tree
|
// WriteString string version of the tree
|
||||||
func (n *node) WriteString(indent string) string {
|
func (n *Node) WriteString(indent string) string {
|
||||||
r := strings.Builder{}
|
r := strings.Builder{}
|
||||||
if indent != "" {
|
if indent != "" {
|
||||||
r.WriteString(indent)
|
r.WriteString(indent)
|
||||||
r.WriteString("- ")
|
r.WriteString("- ")
|
||||||
r.WriteString(n.Value)
|
r.WriteString(n.value)
|
||||||
r.WriteString("\n")
|
r.WriteString("\n")
|
||||||
}
|
}
|
||||||
indent += " "
|
indent += " "
|
||||||
for _, c := range n.Children {
|
for _, c := range n.children {
|
||||||
r.WriteString(c.WriteString(indent))
|
r.WriteString(c.WriteString(indent))
|
||||||
}
|
}
|
||||||
return r.String()
|
return r.String()
|
||||||
|
@ -38,7 +38,7 @@ func (e *EPUBZip) Close() error {
|
|||||||
//
|
//
|
||||||
// This will be valid with epubcheck tools.
|
// This will be valid with epubcheck tools.
|
||||||
func (e *EPUBZip) WriteMagic() error {
|
func (e *EPUBZip) WriteMagic() error {
|
||||||
t := time.Now()
|
t := time.Now().UTC()
|
||||||
fh := &zip.FileHeader{
|
fh := &zip.FileHeader{
|
||||||
Name: "mimetype",
|
Name: "mimetype",
|
||||||
Method: zip.Store,
|
Method: zip.Store,
|
||||||
@ -49,6 +49,8 @@ func (e *EPUBZip) WriteMagic() error {
|
|||||||
UncompressedSize64: 20,
|
UncompressedSize64: 20,
|
||||||
CRC32: 0x2cab616f,
|
CRC32: 0x2cab616f,
|
||||||
}
|
}
|
||||||
|
fh.CreatorVersion = fh.CreatorVersion&0xff00 | 20 // preserve compatibility byte
|
||||||
|
fh.ReaderVersion = 20
|
||||||
fh.SetMode(0600)
|
fh.SetMode(0600)
|
||||||
m, err := e.wz.CreateRaw(fh)
|
m, err := e.wz.CreateRaw(fh)
|
||||||
|
|
||||||
|
@ -26,6 +26,8 @@ Example:
|
|||||||
*/
|
*/
|
||||||
package sortpath
|
package sortpath
|
||||||
|
|
||||||
|
import "sort"
|
||||||
|
|
||||||
// struct that implement interface for sort.Sort
|
// struct that implement interface for sort.Sort
|
||||||
type by struct {
|
type by struct {
|
||||||
filenames []string
|
filenames []string
|
||||||
@ -40,7 +42,7 @@ func (b by) Swap(i, j int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// By use sortpath.By with sort.Sort
|
// By use sortpath.By with sort.Sort
|
||||||
func By(filenames []string, mode int) by {
|
func By(filenames []string, mode int) sort.Interface {
|
||||||
var p [][]part
|
var p [][]part
|
||||||
for _, filename := range filenames {
|
for _, filename := range filenames {
|
||||||
p = append(p, parse(filename, mode))
|
p = append(p, parse(filename, mode))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user