diff --git a/go.mod b/go.mod index a8cb4f2..7390d7b 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/gofrs/uuid v3.1.0+incompatible - github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4 + github.com/schollz/progressbar/v3 v3.12.2 golang.org/x/image v0.2.0 ) @@ -12,8 +12,6 @@ require ( github.com/mattn/go-runewidth v0.0.14 // indirect github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/rivo/uniseg v0.4.3 // indirect - github.com/schollz/progressbar/v3 v3.12.2 // indirect - golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect golang.org/x/sys v0.3.0 // indirect golang.org/x/term v0.3.0 // indirect ) diff --git a/go.sum b/go.sum index 55a3731..25233bd 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,5 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gofrs/uuid v3.1.0+incompatible h1:q2rtkjaKT4YEr6E1kamy0Ha4RtepWlQBedyHx0uzKwA= github.com/gofrs/uuid v3.1.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= @@ -8,6 +9,7 @@ github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWV github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= @@ -15,9 +17,8 @@ github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc github.com/schollz/progressbar/v3 v3.12.2 h1:yLqqqpQNMxGxHY8uEshRihaHWwa0rf0yb7/Zrpgq2C0= github.com/schollz/progressbar/v3 v3.12.2/go.mod h1:HFJYIYQQJX32UJdyoigUl19xoV6aMwZt6iX/C30RWfg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4 h1:0sw0nJM544SpsihWx1bkXdYLQDlzRflMgFJQ4Yih9ts= -github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -26,7 +27,6 @@ golang.org/x/image v0.2.0/go.mod h1:la7oBXb9w3YFjBqaAwtynVioc1ZvOnNteUNrifGNmAI= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 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-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/internal/epub/core.go b/internal/epub/core.go index 87e2a5c..565ddae 100644 --- a/internal/epub/core.go +++ b/internal/epub/core.go @@ -15,7 +15,6 @@ import ( "github.com/gofrs/uuid" "github.com/schollz/progressbar/v3" - "github.com/yosssi/gohtml" imageconverter "go-comic-converter/internal/image-converter" ) @@ -112,6 +111,28 @@ func (e *EPub) SetLimitMb(l int) *EPub { return e } +func (e *EPub) WriteMagic(wz *zip.Writer) error { + t := time.Now() + fh := &zip.FileHeader{ + Name: "mimetype", + Method: zip.Store, + Modified: t, + ModifiedTime: uint16(t.Second()/2 + t.Minute()<<5 + t.Hour()<<11), + ModifiedDate: uint16(t.Day() + int(t.Month())<<5 + (t.Year()-1980)<<9), + CompressedSize64: 20, + UncompressedSize64: 20, + CRC32: 0x2cab616f, + } + fh.SetMode(0600) + m, err := wz.CreateRaw(fh) + + if err != nil { + return err + } + _, err = m.Write([]byte("application/epub+zip")) + return err +} + func (e *EPub) WriteFile(wz *zip.Writer, file string, data any) error { var content []byte switch b := data.(type) { @@ -126,6 +147,7 @@ func (e *EPub) WriteFile(wz *zip.Writer, file string, data any) error { m, err := wz.CreateHeader(&zip.FileHeader{ Name: file, Modified: time.Now(), + Method: zip.Deflate, }) if err != nil { return err @@ -144,7 +166,7 @@ func (e *EPub) Render(templateString string, data any) string { panic(err) } - return gohtml.Format(result.String()) + return result.String() } func (e *EPub) LoadDir(dirname string) *EPub { @@ -283,8 +305,8 @@ func (e *EPub) Write() error { epubParts := e.GetParts() totalParts := len(epubParts) + bar := progressbar.Default(int64(totalParts), "Writing Part") for i, part := range epubParts { - fmt.Printf("Writing part %d...\n", i+1) ext := filepath.Ext(e.Path) suffix := "" if totalParts > 1 { @@ -297,10 +319,9 @@ func (e *EPub) Write() error { } zipContent := []ZipContent{ - {"mimetype", TEMPLATE_MIME_TYPE}, - {"META-INF/container.xml", gohtml.Format(TEMPLATE_CONTAINER)}, + {"META-INF/container.xml", TEMPLATE_CONTAINER}, {"OEBPS/content.opf", e.Render(TEMPLATE_CONTENT, map[string]any{"Info": e, "Images": part.Images})}, - {"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, "Image": part.Images[0]})}, {"OEBPS/nav.xhtml", e.Render(TEMPLATE_NAV, map[string]any{"Info": e, "Image": part.Images[0]})}, {"OEBPS/Text/style.css", TEMPLATE_STYLE}, {"OEBPS/Text/part.xhtml", e.Render(TEMPLATE_PART, map[string]any{ @@ -312,6 +333,11 @@ func (e *EPub) Write() error { } wz := zip.NewWriter(w) + defer wz.Close() + + if err = e.WriteMagic(wz); err != nil { + return err + } for _, content := range zipContent { if err := e.WriteFile(wz, content.Name, content.Content); err != nil { return err @@ -328,8 +354,9 @@ func (e *EPub) Write() error { return err } } - wz.Close() + bar.Add(1) } + bar.Close() return nil } diff --git a/internal/epub/templates.go b/internal/epub/templates.go index 484dfde..51ac917 100644 --- a/internal/epub/templates.go +++ b/internal/epub/templates.go @@ -2,9 +2,6 @@ package epub import _ "embed" -//go:embed "templates/mimetype.tmpl" -var TEMPLATE_MIME_TYPE string - //go:embed "templates/container.xml.tmpl" var TEMPLATE_CONTAINER string diff --git a/internal/epub/templates/content.opf.tmpl b/internal/epub/templates/content.opf.tmpl index 52ed07f..0ab3aeb 100644 --- a/internal/epub/templates/content.opf.tmpl +++ b/internal/epub/templates/content.opf.tmpl @@ -23,6 +23,7 @@ + {{ range .Images }} diff --git a/internal/epub/templates/mimetype.tmpl b/internal/epub/templates/mimetype.tmpl deleted file mode 100644 index 57ef03f..0000000 --- a/internal/epub/templates/mimetype.tmpl +++ /dev/null @@ -1 +0,0 @@ -application/epub+zip \ No newline at end of file diff --git a/internal/epub/templates/part.xhtml.tmpl b/internal/epub/templates/part.xhtml.tmpl index bb409c0..16a33ce 100644 --- a/internal/epub/templates/part.xhtml.tmpl +++ b/internal/epub/templates/part.xhtml.tmpl @@ -8,14 +8,8 @@
-

-

- {{ .Info.Title }} -

-

- Part {{ .Part }} / {{ .Total }} -

-

+

{{ .Info.Title }}

+

Part {{ .Part }} / {{ .Total }}

\ No newline at end of file diff --git a/internal/epub/templates/toc.ncx.tmpl b/internal/epub/templates/toc.ncx.tmpl index 93e168d..22d9fbe 100644 --- a/internal/epub/templates/toc.ncx.tmpl +++ b/internal/epub/templates/toc.ncx.tmpl @@ -9,6 +9,6 @@ {{ .Info.Title }} -{{ .Info.Title }} +{{ .Info.Title }} \ No newline at end of file