create epub like kcc

This commit is contained in:
Celogeek 2022-12-26 19:01:34 +01:00
parent 8181c13492
commit 1250fa5ea7
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
12 changed files with 423 additions and 7 deletions

9
go.mod
View File

@ -2,12 +2,15 @@ module go-comic-converter
go 1.19
require golang.org/x/image v0.2.0
require (
github.com/bmaupin/go-epub v1.0.1
github.com/vincent-petithory/dataurl v0.0.0-20191104211930-d1553a71de50
golang.org/x/image v0.2.0
golang.org/x/mod v0.7.0
)
require (
github.com/bmaupin/go-epub v1.0.1 // indirect
github.com/gabriel-vasile/mimetype v1.3.1 // indirect
github.com/gofrs/uuid v3.1.0+incompatible // indirect
github.com/vincent-petithory/dataurl v0.0.0-20191104211930-d1553a71de50 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
)

3
go.sum
View File

@ -11,7 +11,10 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.2.0 h1:/DcQ0w3VHKCC5p0/P2B0JpAZ9Z++V2KOo2fyU89CXBQ=
golang.org/x/image v0.2.0/go.mod h1:la7oBXb9w3YFjBqaAwtynVioc1ZvOnNteUNrifGNmAI=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=

176
internal/epub/core.go Normal file
View File

@ -0,0 +1,176 @@
package epub
import (
"archive/zip"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"text/template"
"time"
"github.com/gofrs/uuid"
)
type Images struct {
Id int
Path string
Name string
Title string
Data []byte
Width int
Height int
}
type EPub struct {
Path string
UID string
Title string
Author string
Publisher string
UpdatedAt string
ViewWidth int
ViewHeight int
Quality int
Images []Images
Error error
}
func NewEpub(path string) *EPub {
uid, err := uuid.NewV4()
if err != nil {
panic(err)
}
return &EPub{
Path: path,
UID: uid.String(),
Title: "Unknown title",
Author: "Unknown author",
Publisher: "GO Comic Converter",
UpdatedAt: time.Now().UTC().Format("2006-01-02T15:04:05Z"),
ViewWidth: 0,
ViewHeight: 0,
Quality: 75,
Images: make([]Images, 0),
}
}
func (e *EPub) SetTitle(title string) *EPub {
e.Title = title
return e
}
func (e *EPub) SetAuthor(author string) *EPub {
e.Author = author
return e
}
func (e *EPub) SetSize(w, h int) *EPub {
e.ViewWidth = w
e.ViewHeight = h
return e
}
func (e *EPub) SetQuality(q int) *EPub {
e.Quality = q
return e
}
func (e *EPub) WriteFile(wz *zip.Writer, file, content string) error {
m, err := wz.Create(file)
if err != nil {
return err
}
_, err = m.Write([]byte(content))
return err
}
func (e *EPub) Render(templateString string, data any) string {
tmpl := template.New("parser")
tmpl.Funcs(template.FuncMap{"mod": func(i, j int) bool { return i%j == 0 }})
tmpl.Funcs(template.FuncMap{"zoom": func(s int, z float32) int { return int(float32(s) * z) }})
tmpl, err := tmpl.Parse(templateString)
if err != nil {
panic(err)
}
result := &strings.Builder{}
if err := tmpl.Execute(result, data); err != nil {
panic(err)
}
return result.String()
}
func (e *EPub) LoadDir(dirname string) *EPub {
err := filepath.WalkDir(dirname, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
ext := filepath.Ext(path)
if strings.ToLower(ext) != ".jpg" {
return nil
}
name := filepath.Base(path)
title := name[0 : len(name)-len(ext)]
e.Images = append(e.Images, Images{
Path: path,
Name: name,
Title: title,
})
return nil
})
if err != nil {
e.Error = err
return e
}
sort.SliceStable(e.Images, func(i, j int) bool {
return strings.Compare(e.Images[i].Path, e.Images[j].Path) < 0
})
for i := range e.Images {
e.Images[i].Id = i
}
return e
}
func (e *EPub) Write() error {
if e.Error != nil {
return e.Error
}
w, err := os.Create(e.Path)
if err != nil {
return err
}
zipContent := [][]string{
{"mimetype", TEMPLATE_MIME_TYPE},
{"META-INF/container.xml", TEMPLATE_CONTAINER},
{"OEBPS/content.opf", e.Render(TEMPLATE_CONTENT, e)},
{"OEBPS/toc.ncx", e.Render(TEMPLATE_TOC, e)},
{"OEBPS/nav.xhtml", e.Render(TEMPLATE_NAV, e)},
{"OEBPS/Text/style.css", TEMPLATE_STYLE},
}
for _, img := range e.Images {
filename := fmt.Sprintf("OEBPS/Text/%d.xhtml", img.Id)
zipContent = append(zipContent, []string{filename, e.Render(TEMPLATE_TEXT, img)})
}
wz := zip.NewWriter(w)
defer wz.Close()
for _, content := range zipContent {
if err := e.WriteFile(wz, content[0], content[1]); err != nil {
return err
}
}
return nil
}

View File

@ -0,0 +1,24 @@
package epub
import _ "embed"
//go:embed "templates/mimetype.tmpl"
var TEMPLATE_MIME_TYPE string
//go:embed "templates/container.xml.tmpl"
var TEMPLATE_CONTAINER string
//go:embed "templates/content.opf.tmpl"
var TEMPLATE_CONTENT string
//go:embed "templates/toc.ncx.tmpl"
var TEMPLATE_TOC string
//go:embed "templates/nav.xhtml.tmpl"
var TEMPLATE_NAV string
//go:embed "templates/style.css.tmpl"
var TEMPLATE_STYLE string
//go:embed "templates/text.xhtml.tmpl"
var TEMPLATE_TEXT string

View File

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<package version="3.0" unique-identifier="BookID" xmlns="http://www.idpf.org/2007/opf">
<metadata xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>{{ .Title }}</dc:title>
<dc:language>en-US</dc:language>
<dc:identifier id="BookID">urn:uuid:{{ .UID }}</dc:identifier>
<dc:contributor id="contributor">GO Comic Converter</dc:contributor>
<dc:creator>GO Comic Converter</dc:creator>
<meta property="dcterms:modified">{{ .UpdatedAt }}</meta>
<meta name="cover" content="cover"/>
<meta name="fixed-layout" content="true"/>
<meta name="original-resolution" content="{{ .ViewWidth }}x{{ .ViewHeight }}"/>
<meta name="book-type" content="comic"/>
<meta name="primary-writing-mode" content="horizontal-lr"/>
<meta name="zero-gutter" content="true"/>
<meta name="zero-margin" content="true"/>
<meta name="ke-border-color" content="#FFFFFF"/>
<meta name="ke-border-width" content="0"/>
<meta name="orientation-lock" content="portrait"/>
<meta name="region-mag" content="true"/>
</metadata>
<manifest>
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
<item id="nav" href="nav.xhtml" properties="nav" media-type="application/xhtml+xml"/>
{{ range .Images }}
{{ if eq .Id 0 }}
<item id="cover" href="Images/{{ .Id }}.jpg" media-type="image/jpeg" properties="cover-image"/>
{{ end }}
<item id="page_{{ .Id }}" href="Text/{{ .Id }}.xhtml" media-type="application/xhtml+xml"/>
<item id="img_{{ .Id }}" href="Images/{{ .Id }}.jpg" media-type="image/jpeg"/>
{{ end }}
</manifest>
<spine page-progression-direction="ltr" toc="ncx">
{{ range .Images }}
{{ if mod .Id 2 }}
<itemref idref="page_{{ .Id }} " linear="yes" properties="page-spread-left"/>
{{ else }}
<itemref idref="page_{{ .Id }}" linear="yes" properties="page-spread-right"/>
{{ end }}
{{ end }}
</spine>
</package>

View File

@ -0,0 +1 @@
application/epub+zip

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>{{ .Title }}</title>
<meta charset="utf-8"/>
</head>
<body>
<nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc">
<ol>
<li><a href="Text/0.xhtml">{{ .Title }}</a></li>
</ol>
</nav>
<nav epub:type="page-list">
<ol>
<li><a href="Text/0.xhtml">{{ .Title }}</a></li>
</ol>
</nav>
</body>
</html>

View File

@ -0,0 +1,72 @@
@page {
margin: 0;
}
body {
display: block;
margin: 0;
padding: 0;
}
#PV {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
#PV-T {
top: 0;
width: 100%;
height: 50%;
}
#PV-B {
bottom: 0;
width: 100%;
height: 50%;
}
#PV-L {
left: 0;
width: 49.5%;
height: 100%;
float: left;
}
#PV-R {
right: 0;
width: 49.5%;
height: 100%;
float: right;
}
#PV-TL {
top: 0;
left: 0;
width: 49.5%;
height: 50%;
float: left;
}
#PV-TR {
top: 0;
right: 0;
width: 49.5%;
height: 50%;
float: right;
}
#PV-BL {
bottom: 0;
left: 0;
width: 49.5%;
height: 50%;
float: left;
}
#PV-BR {
bottom: 0;
right: 0;
width: 49.5%;
height: 50%;
float: right;
}
.PV-P {
width: 100%;
height: 100%;
top: 0;
position: absolute;
display: none;
}

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>{{ .Title }}</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="width={{ .Width }}, height={{ .Height }}"/>
</head>
<body style="">
<div style="text-align:center;top:0.0%;">
<img width="{{ .Width }}" height="{{ .Height }}" src="../Images/{{ .Id }}.jpg"/>
</div>
<div id="PV">
<div id="PV-TL">
<a style="display:inline-block;width:100%;height:100%;" class="app-amzn-magnify" data-app-amzn-magnify='{"targetId":"PV-TL-P", "ordinal":1}'></a>
</div>
<div id="PV-TR">
<a style="display:inline-block;width:100%;height:100%;" class="app-amzn-magnify" data-app-amzn-magnify='{"targetId":"PV-TR-P", "ordinal":2}'></a>
</div>
<div id="PV-BL">
<a style="display:inline-block;width:100%;height:100%;" class="app-amzn-magnify" data-app-amzn-magnify='{"targetId":"PV-BL-P", "ordinal":3}'></a>
</div>
<div id="PV-BR">
<a style="display:inline-block;width:100%;height:100%;" class="app-amzn-magnify" data-app-amzn-magnify='{"targetId":"PV-BR-P", "ordinal":4}'></a>
</div>
</div>
<div class="PV-P" id="PV-TL-P" style="">
<img style="position:absolute;left:0;top:0;" src="../Images/{{ .Id }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/>
</div>
<div class="PV-P" id="PV-TR-P" style="">
<img style="position:absolute;right:0;top:0;" src="../Images/{{ .Id }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/>
</div>
<div class="PV-P" id="PV-BL-P" style="">
<img style="position:absolute;left:0;bottom:0;" src="../Images/{{ .Id }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/>
</div>
<div class="PV-P" id="PV-BR-P" style="">
<img style="position:absolute;right:0;bottom:0;" src="../Images/{{ .Id }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/>
</div>
</body>
</html>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<ncx version="2005-1" xml:lang="en-US" xmlns="http://www.daisy.org/z3986/2005/ncx/">
<head>
<meta name="dtb:uid" content="urn:uuid:{{ .UID }}"/>
<meta name="dtb:depth" content="1"/>
<meta name="dtb:totalPageCount" content="0"/>
<meta name="dtb:maxPageNumber" content="0"/>
<meta name="generated" content="true"/>
</head>
<docTitle><text>{{ .Title }}</text></docTitle>
<navMap>
<navPoint id="Text"><navLabel><text>{{ .Title }}</text></navLabel><content src="Text/0.xhtml"/></navPoint>
</navMap>
</ncx>

23
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"go-comic-converter/internal/epub"
imageconverter "go-comic-converter/internal/image-converter"
"io/fs"
"path/filepath"
@ -10,7 +11,7 @@ import (
"strings"
"sync"
"github.com/bmaupin/go-epub"
epub2 "github.com/bmaupin/go-epub"
)
type File struct {
@ -21,7 +22,7 @@ type File struct {
InternalPath string
}
func addImages(doc *epub.Epub, imagesPath []string) {
func addImages(doc *epub2.Epub, imagesPath []string) {
wg := &sync.WaitGroup{}
todos := make(chan string, runtime.NumCPU())
imageResult := make(chan *File)
@ -95,10 +96,10 @@ func getImages(dirname string) []string {
return images
}
func main() {
func main2() {
imagesPath := getImages("/Users/vincent/Downloads/Bleach T01 (Tite KUBO) [eBook officiel 1920]")
doc := epub.NewEpub("Bleach T01 (Tite KUBO) [eBook officiel 1920]")
doc := epub2.NewEpub("Bleach T01 (Tite KUBO) [eBook officiel 1920]")
doc.SetAuthor("Bachelier Vincent")
addImages(doc, imagesPath)
@ -108,3 +109,17 @@ func main() {
}
}
func main() {
err := epub.NewEpub("/Users/vincent/Downloads/test.epub").
SetSize(1860, 2480).
SetQuality(75).
SetTitle("Bleach T01 (Tite KUBO) [eBook officiel 1920]").
SetAuthor("Bachelier Vincent").
LoadDir("/Users/vincent/Downloads/Bleach T01 (Tite KUBO) [eBook officiel 1920]").
Write()
if err != nil {
panic(err)
}
}