From 7dead8863b3724b7e735e1a8b11efb509e61c86a Mon Sep 17 00:00:00 2001 From: Patrick G Date: Fri, 28 Jul 2017 10:37:32 -0400 Subject: [PATCH] Moved kepub code into a subpackage to allow use as library --- Makefile | 2 +- content.go => kepub/content.go | 2 +- content_test.go => kepub/content_test.go | 2 +- epub.go => kepub/epub.go | 2 +- kepub/epub_test.go | 17 +++ kepub/kepub.go | 136 +++++++++++++++++++++++ kepubify.go | 118 +++----------------- epub_test.go => kepubify_test.go | 5 + 8 files changed, 176 insertions(+), 108 deletions(-) rename content.go => kepub/content.go (99%) rename content_test.go => kepub/content_test.go (99%) rename epub.go => kepub/epub.go (99%) create mode 100644 kepub/epub_test.go create mode 100644 kepub/kepub.go rename epub_test.go => kepubify_test.go (57%) diff --git a/Makefile b/Makefile index 3dce0f9..3db5ed9 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ generate: .PHONY: test test: - go test -v . + go test -v . ./kepub .PHONY: build build: diff --git a/content.go b/kepub/content.go similarity index 99% rename from content.go rename to kepub/content.go index 93fdc35..2a82228 100644 --- a/content.go +++ b/kepub/content.go @@ -1,4 +1,4 @@ -package main +package kepub import ( "bytes" diff --git a/content_test.go b/kepub/content_test.go similarity index 99% rename from content_test.go rename to kepub/content_test.go index fc34c3b..ca37c50 100644 --- a/content_test.go +++ b/kepub/content_test.go @@ -1,4 +1,4 @@ -package main +package kepub import ( "crypto/sha256" diff --git a/epub.go b/kepub/epub.go similarity index 99% rename from epub.go rename to kepub/epub.go index 3666688..a745919 100644 --- a/epub.go +++ b/kepub/epub.go @@ -1,4 +1,4 @@ -package main +package kepub import ( "archive/zip" diff --git a/kepub/epub_test.go b/kepub/epub_test.go new file mode 100644 index 0000000..3b796ee --- /dev/null +++ b/kepub/epub_test.go @@ -0,0 +1,17 @@ +package kepub + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestExists(t *testing.T) { + assert.True(t, exists("."), ". should exist") + assert.False(t, exists("./nonexistent"), "./nonexistent should not exist") +} + +func TestIsDir(t *testing.T) { + assert.True(t, isDir("."), ". should be a dir") + assert.False(t, isDir("./epub_test.go"), "./epub_test.go should not be a dir") +} diff --git a/kepub/kepub.go b/kepub/kepub.go new file mode 100644 index 0000000..f1d5897 --- /dev/null +++ b/kepub/kepub.go @@ -0,0 +1,136 @@ +package kepub + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "time" + + "github.com/beevik/etree" + "github.com/cheggaaa/pb" + zglob "github.com/mattn/go-zglob" +) + +// Kepubify converts a .epub into a .kepub.epub +func Kepubify(src, dest string, printlog bool) error { + td, err := ioutil.TempDir("", "kepubify") + if err != nil { + return fmt.Errorf("Could not create temp dir: %s", err) + } + defer os.RemoveAll(td) + + if printlog { + fmt.Println("Unpacking ePub.") + } + UnpackEPUB(src, td, true) + if printlog { + fmt.Println() + } + + a, err := zglob.Glob(filepath.Join(td, "**", "*.html")) + if err != nil { + return fmt.Errorf("Could not create find content files: %s", err) + } + b, err := zglob.Glob(filepath.Join(td, "**", "*.xhtml")) + if err != nil { + return fmt.Errorf("Could not create find content files: %s", err) + } + c, err := zglob.Glob(filepath.Join(td, "**", "*.htm")) + if err != nil { + return fmt.Errorf("Could not create find content files: %s", err) + } + contentfiles := append(append(a, b...), c...) + + if printlog { + fmt.Printf("Processing %v content files.\n", len(contentfiles)) + } + + var bar *pb.ProgressBar + + if printlog { + bar = pb.New(len(contentfiles)) + bar.SetRefreshRate(time.Millisecond * 300) + bar.SetMaxWidth(60) + bar.Format("[=> ]") + bar.Start() + } + + for _, cf := range contentfiles { + buf, err := ioutil.ReadFile(cf) + if err != nil { + return fmt.Errorf("Could not open content file \"%s\" for reading: %s", cf, err) + } + str := string(buf) + err = process(&str) + if err != nil { + return fmt.Errorf("Error processing content file \"%s\": %s", cf, err) + } + err = ioutil.WriteFile(cf, []byte(str), 0644) + if err != nil { + return fmt.Errorf("Error writing content file \"%s\": %s", cf, err) + } + time.Sleep(time.Millisecond * 25) + if printlog { + bar.Increment() + } + } + + if printlog { + bar.Finish() + fmt.Println() + + fmt.Println("Cleaning content.opf.") + fmt.Println() + } + + rsk, err := os.Open(filepath.Join(td, "META-INF", "container.xml")) + if err != nil { + return fmt.Errorf("Error parsing container.xml: %s", err) + } + defer rsk.Close() + + container := etree.NewDocument() + _, err = container.ReadFrom(rsk) + if err != nil { + return fmt.Errorf("Error parsing container.xml: %s", err) + } + + rootfile := "" + for _, e := range container.FindElements("//rootfiles/rootfile[@full-path]") { + rootfile = e.SelectAttrValue("full-path", "") + } + if rootfile == "" { + return fmt.Errorf("Error parsing container.xml") + } + + buf, err := ioutil.ReadFile(filepath.Join(td, rootfile)) + if err != nil { + return fmt.Errorf("Error parsing content.opf: %s", err) + } + + opf := string(buf) + + err = cleanOPF(&opf) + if err != nil { + return fmt.Errorf("Error cleaning content.opf: %s", err) + } + + err = ioutil.WriteFile(filepath.Join(td, rootfile), []byte(opf), 0644) + if err != nil { + return fmt.Errorf("Error writing new content.opf: %s", err) + } + + if printlog { + fmt.Println("Cleaning epub files.") + fmt.Println() + } + cleanFiles(td) + + if printlog { + fmt.Println("Packing ePub.") + fmt.Println() + } + PackEPUB(td, dest, true) + return nil +} diff --git a/kepubify.go b/kepubify.go index 3e0202a..8ac1533 100644 --- a/kepubify.go +++ b/kepubify.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "os" "path" "path/filepath" @@ -10,116 +9,27 @@ import ( "strings" "time" - "github.com/beevik/etree" - "github.com/cheggaaa/pb" - zglob "github.com/mattn/go-zglob" + "github.com/geek1011/kepubify/kepub" + cli "gopkg.in/urfave/cli.v1" ) var version = "dev" -func kepubify(src, dest string) error { - td, err := ioutil.TempDir("", "kepubify") - if err != nil { - return fmt.Errorf("Could not create temp dir: %s", err) - } - defer os.RemoveAll(td) - - fmt.Println("Unpacking ePub.") - UnpackEPUB(src, td, true) - fmt.Println() - - a, err := zglob.Glob(filepath.Join(td, "**", "*.html")) - if err != nil { - return fmt.Errorf("Could not create find content files: %s", err) - } - b, err := zglob.Glob(filepath.Join(td, "**", "*.xhtml")) - if err != nil { - return fmt.Errorf("Could not create find content files: %s", err) - } - c, err := zglob.Glob(filepath.Join(td, "**", "*.htm")) - if err != nil { - return fmt.Errorf("Could not create find content files: %s", err) - } - contentfiles := append(append(a, b...), c...) - - fmt.Printf("Processing %v content files.\n", len(contentfiles)) - - bar := pb.New(len(contentfiles)) - bar.SetRefreshRate(time.Millisecond * 300) - bar.SetMaxWidth(60) - bar.Format("[=> ]") - bar.Start() - - for _, cf := range contentfiles { - buf, err := ioutil.ReadFile(cf) - if err != nil { - return fmt.Errorf("Could not open content file \"%s\" for reading: %s", cf, err) - } - str := string(buf) - err = process(&str) - if err != nil { - return fmt.Errorf("Error processing content file \"%s\": %s", cf, err) - } - err = ioutil.WriteFile(cf, []byte(str), 0644) - if err != nil { - return fmt.Errorf("Error writing content file \"%s\": %s", cf, err) - } - time.Sleep(time.Millisecond * 25) - bar.Increment() - } - - bar.Finish() - fmt.Println() - - fmt.Println("Cleaning content.opf.") - fmt.Println() - - rsk, err := os.Open(filepath.Join(td, "META-INF", "container.xml")) - if err != nil { - return fmt.Errorf("Error parsing container.xml: %s", err) - } - defer rsk.Close() - - container := etree.NewDocument() - _, err = container.ReadFrom(rsk) - if err != nil { - return fmt.Errorf("Error parsing container.xml: %s", err) - } - - rootfile := "" - for _, e := range container.FindElements("//rootfiles/rootfile[@full-path]") { - rootfile = e.SelectAttrValue("full-path", "") - } - if rootfile == "" { - return fmt.Errorf("Error parsing container.xml") - } - - buf, err := ioutil.ReadFile(filepath.Join(td, rootfile)) - if err != nil { - return fmt.Errorf("Error parsing content.opf: %s", err) - } - - opf := string(buf) - - err = cleanOPF(&opf) - if err != nil { - return fmt.Errorf("Error cleaning content.opf: %s", err) +// exists checks whether a path exists +func exists(path string) bool { + if _, err := os.Stat(path); !os.IsNotExist(err) { + return true } + return false +} - err = ioutil.WriteFile(filepath.Join(td, rootfile), []byte(opf), 0644) - if err != nil { - return fmt.Errorf("Error writing new content.opf: %s", err) +// isDir checks if a exists and is a dir +func isDir(path string) bool { + if fi, err := os.Stat(path); err == nil && fi.IsDir() { + return true } - - fmt.Println("Cleaning epub files.") - fmt.Println() - cleanFiles(td) - - fmt.Println("Packing ePub.") - fmt.Println() - PackEPUB(td, dest, true) - return nil + return false } func convert(c *cli.Context) error { @@ -175,7 +85,7 @@ func convert(c *cli.Context) error { fmt.Printf("Output file: %s\n", outfile) fmt.Println() - err = kepubify(infile, outfile) + err = kepub.Kepubify(infile, outfile, true) if err != nil { return fmt.Errorf("Error converting epub to kepub: %s.", err) } diff --git a/epub_test.go b/kepubify_test.go similarity index 57% rename from epub_test.go rename to kepubify_test.go index a0c21bb..60e0077 100644 --- a/epub_test.go +++ b/kepubify_test.go @@ -10,3 +10,8 @@ func TestExists(t *testing.T) { assert.True(t, exists("."), ". should exist") assert.False(t, exists("./nonexistent"), "./nonexistent should not exist") } + +func TestIsDir(t *testing.T) { + assert.True(t, isDir("."), ". should be a dir") + assert.False(t, isDir("./kepubify_test.go"), "./kepubify_test.go should not be a dir") +}