improve part html

This commit is contained in:
Celogeek 2022-12-27 21:15:48 +01:00
parent 6444eb65a3
commit fd80342a19
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
2 changed files with 55 additions and 50 deletions

View File

@ -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,20 +230,16 @@ 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] cover := images[0]
images = images[1:] images = images[1:]
fmt.Println("Limit: ", e.LimitMb, e.LimitMb == 0)
if e.LimitMb == 0 { if e.LimitMb == 0 {
epubPart <- &EpubPart{ epubPart = append(epubPart, &EpubPart{
Idx: 1,
Cover: cover, Cover: cover,
Images: images, Images: images,
Suffix: "", })
} return epubPart
return
} }
maxSize := e.LimitMb * 1024 * 1024 maxSize := e.LimitMb * 1024 * 1024
@ -255,12 +249,10 @@ func (e *EPub) GetParts() <-chan *EpubPart {
for _, img := range images { for _, img := range images {
if len(currentImages) > 0 && currentSize+len(img.Data) > maxSize { if len(currentImages) > 0 && currentSize+len(img.Data) > maxSize {
epubPart <- &EpubPart{ epubPart = append(epubPart, &EpubPart{
Idx: part,
Cover: cover, Cover: cover,
Images: currentImages, Images: currentImages,
Suffix: fmt.Sprintf(" PART_%03d", part), })
}
part += 1 part += 1
currentSize = 512*1024 + len(cover.Data) currentSize = 512*1024 + len(cover.Data)
currentImages = make([]*Image, 0) currentImages = make([]*Image, 0)
@ -269,14 +261,11 @@ func (e *EPub) GetParts() <-chan *EpubPart {
currentImages = append(currentImages, img) currentImages = append(currentImages, img)
} }
if len(currentImages) > 0 { if len(currentImages) > 0 {
epubPart <- &EpubPart{ epubPart = append(epubPart, &EpubPart{
Idx: part,
Cover: cover, Cover: cover,
Images: currentImages, Images: currentImages,
Suffix: fmt.Sprintf(" PART_%03d", part), })
} }
}
}()
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,

View File

@ -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>