mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 07:42:37 +02:00
improve part html
This commit is contained in:
parent
6444eb65a3
commit
fd80342a19
@ -49,10 +49,8 @@ type EPub struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type EpubPart struct {
|
type EpubPart struct {
|
||||||
Idx int
|
|
||||||
Cover *Image
|
Cover *Image
|
||||||
Images []*Image
|
Images []*Image
|
||||||
Suffix string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEpub(path string) *EPub {
|
func NewEpub(path string) *EPub {
|
||||||
@ -221,7 +219,7 @@ func (e *EPub) LoadDir(dirname string) *EPub {
|
|||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EPub) GetParts() <-chan *EpubPart {
|
func (e *EPub) GetParts() []*EpubPart {
|
||||||
images := make([]*Image, e.ImagesCount)
|
images := make([]*Image, e.ImagesCount)
|
||||||
totalSize := 0
|
totalSize := 0
|
||||||
bar := progressbar.Default(int64(e.ImagesCount), "Processing")
|
bar := progressbar.Default(int64(e.ImagesCount), "Processing")
|
||||||
@ -232,51 +230,42 @@ func (e *EPub) GetParts() <-chan *EpubPart {
|
|||||||
}
|
}
|
||||||
bar.Close()
|
bar.Close()
|
||||||
|
|
||||||
epubPart := make(chan *EpubPart)
|
epubPart := make([]*EpubPart, 0)
|
||||||
go func() {
|
|
||||||
defer close(epubPart)
|
|
||||||
cover := images[0]
|
|
||||||
images = images[1:]
|
|
||||||
fmt.Println("Limit: ", e.LimitMb, e.LimitMb == 0)
|
|
||||||
if e.LimitMb == 0 {
|
|
||||||
epubPart <- &EpubPart{
|
|
||||||
Idx: 1,
|
|
||||||
Cover: cover,
|
|
||||||
Images: images,
|
|
||||||
Suffix: "",
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
maxSize := e.LimitMb * 1024 * 1024
|
cover := images[0]
|
||||||
currentSize := 512*1024 + len(cover.Data)
|
images = images[1:]
|
||||||
currentImages := make([]*Image, 0)
|
if e.LimitMb == 0 {
|
||||||
part := 1
|
epubPart = append(epubPart, &EpubPart{
|
||||||
|
Cover: cover,
|
||||||
|
Images: images,
|
||||||
|
})
|
||||||
|
return epubPart
|
||||||
|
}
|
||||||
|
|
||||||
for _, img := range images {
|
maxSize := e.LimitMb * 1024 * 1024
|
||||||
if len(currentImages) > 0 && currentSize+len(img.Data) > maxSize {
|
currentSize := 512*1024 + len(cover.Data)
|
||||||
epubPart <- &EpubPart{
|
currentImages := make([]*Image, 0)
|
||||||
Idx: part,
|
part := 1
|
||||||
Cover: cover,
|
|
||||||
Images: currentImages,
|
for _, img := range images {
|
||||||
Suffix: fmt.Sprintf(" PART_%03d", part),
|
if len(currentImages) > 0 && currentSize+len(img.Data) > maxSize {
|
||||||
}
|
epubPart = append(epubPart, &EpubPart{
|
||||||
part += 1
|
|
||||||
currentSize = 512*1024 + len(cover.Data)
|
|
||||||
currentImages = make([]*Image, 0)
|
|
||||||
}
|
|
||||||
currentSize += len(img.Data)
|
|
||||||
currentImages = append(currentImages, img)
|
|
||||||
}
|
|
||||||
if len(currentImages) > 0 {
|
|
||||||
epubPart <- &EpubPart{
|
|
||||||
Idx: part,
|
|
||||||
Cover: cover,
|
Cover: cover,
|
||||||
Images: currentImages,
|
Images: currentImages,
|
||||||
Suffix: fmt.Sprintf(" PART_%03d", part),
|
})
|
||||||
}
|
part += 1
|
||||||
|
currentSize = 512*1024 + len(cover.Data)
|
||||||
|
currentImages = make([]*Image, 0)
|
||||||
}
|
}
|
||||||
}()
|
currentSize += len(img.Data)
|
||||||
|
currentImages = append(currentImages, img)
|
||||||
|
}
|
||||||
|
if len(currentImages) > 0 {
|
||||||
|
epubPart = append(epubPart, &EpubPart{
|
||||||
|
Cover: cover,
|
||||||
|
Images: currentImages,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return epubPart
|
return epubPart
|
||||||
}
|
}
|
||||||
@ -291,10 +280,17 @@ func (e *EPub) Write() error {
|
|||||||
Content any
|
Content any
|
||||||
}
|
}
|
||||||
|
|
||||||
for part := range e.GetParts() {
|
epubParts := e.GetParts()
|
||||||
fmt.Printf("Writing part %d...\n", part.Idx)
|
totalParts := len(epubParts)
|
||||||
|
|
||||||
|
for i, part := range epubParts {
|
||||||
|
fmt.Printf("Writing part %d...\n", i+1)
|
||||||
ext := filepath.Ext(e.Path)
|
ext := filepath.Ext(e.Path)
|
||||||
path := fmt.Sprintf("%s%s%s", e.Path[0:len(e.Path)-len(ext)], part.Suffix, ext)
|
suffix := ""
|
||||||
|
if totalParts > 1 {
|
||||||
|
suffix = fmt.Sprintf(" PART_%02d", i+1)
|
||||||
|
}
|
||||||
|
path := fmt.Sprintf("%s%s%s", e.Path[0:len(e.Path)-len(ext)], suffix, ext)
|
||||||
w, err := os.Create(path)
|
w, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -307,7 +303,11 @@ func (e *EPub) Write() error {
|
|||||||
{"OEBPS/toc.ncx", e.Render(TEMPLATE_TOC, map[string]any{"Info": e, "Images": part.Images})},
|
{"OEBPS/toc.ncx", e.Render(TEMPLATE_TOC, map[string]any{"Info": e, "Images": part.Images})},
|
||||||
{"OEBPS/nav.xhtml", e.Render(TEMPLATE_NAV, map[string]any{"Info": e, "Images": part.Images})},
|
{"OEBPS/nav.xhtml", e.Render(TEMPLATE_NAV, map[string]any{"Info": e, "Images": part.Images})},
|
||||||
{"OEBPS/Text/style.css", TEMPLATE_STYLE},
|
{"OEBPS/Text/style.css", TEMPLATE_STYLE},
|
||||||
{"OEBPS/Text/part.xhtml", e.Render(TEMPLATE_PART, map[string]any{"Info": e, "Part": part})},
|
{"OEBPS/Text/part.xhtml", e.Render(TEMPLATE_PART, map[string]any{
|
||||||
|
"Info": e,
|
||||||
|
"Part": i + 1,
|
||||||
|
"Total": totalParts,
|
||||||
|
})},
|
||||||
{"OEBPS/Text/cover.xhtml", e.Render(TEMPLATE_TEXT, map[string]any{
|
{"OEBPS/Text/cover.xhtml", e.Render(TEMPLATE_TEXT, map[string]any{
|
||||||
"Id": "cover",
|
"Id": "cover",
|
||||||
"Width": part.Cover.Width,
|
"Width": part.Cover.Width,
|
||||||
|
@ -2,13 +2,18 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
|
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
|
||||||
<head>
|
<head>
|
||||||
<title>Part {{ .Part.Idx }}</title>
|
<title>Part {{ .Part }}</title>
|
||||||
<link href="style.css" type="text/css" rel="stylesheet"/>
|
<link href="style.css" type="text/css" rel="stylesheet"/>
|
||||||
<meta name="viewport" content="width={{ .Info.ViewWidth }}, height={{ .Info.ViewHeight }}"/>
|
<meta name="viewport" content="width={{ .Info.ViewWidth }}, height={{ .Info.ViewHeight }}"/>
|
||||||
</head>
|
</head>
|
||||||
<body style="">
|
<body style="">
|
||||||
<h1 style="text-align:center;top:50%;">
|
<h1 style="text-align:center;top:0%;padding-top:50%;font-size:4em">
|
||||||
Part {{ .Part.Idx }}
|
<p>
|
||||||
|
{{ .Info.Title }}
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Part {{ .Part }} / {{ .Total }}
|
||||||
|
</p>
|
||||||
</h1>
|
</h1>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
x
Reference in New Issue
Block a user