Skip to content

Commit

Permalink
Moved kepub code into a subpackage to allow use as library
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Jul 28, 2017
1 parent 1ca2d62 commit 7dead88
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 108 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ generate:

.PHONY: test
test:
go test -v .
go test -v . ./kepub

.PHONY: build
build:
Expand Down
2 changes: 1 addition & 1 deletion content.go → kepub/content.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package kepub

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion content_test.go → kepub/content_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package kepub

import (
"crypto/sha256"
Expand Down
2 changes: 1 addition & 1 deletion epub.go → kepub/epub.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package kepub

import (
"archive/zip"
Expand Down
17 changes: 17 additions & 0 deletions kepub/epub_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
136 changes: 136 additions & 0 deletions kepub/kepub.go
Original file line number Diff line number Diff line change
@@ -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
}
118 changes: 14 additions & 104 deletions kepubify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,124 +2,34 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"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 {
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions epub_test.go → kepubify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 7dead88

Please sign in to comment.