-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved kepub code into a subpackage to allow use as library
- Loading branch information
Showing
8 changed files
with
176 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ generate: | |
|
||
.PHONY: test | ||
test: | ||
go test -v . | ||
go test -v . ./kepub | ||
|
||
.PHONY: build | ||
build: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package kepub | ||
|
||
import ( | ||
"bytes" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package kepub | ||
|
||
import ( | ||
"crypto/sha256" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package kepub | ||
|
||
import ( | ||
"archive/zip" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters