-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mathieu Keller
committed
Apr 22, 2023
1 parent
b23ad3c
commit 225f1bc
Showing
10 changed files
with
97 additions
and
65 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Epub parser | ||
|
||
This parser only supports epub version 3.0 so far. | ||
This parser also only reads the MetaData. | ||
|
||
### How to use? | ||
```go | ||
zipReader, err := zip.OpenReader("./test_epub_v3_0.epub") | ||
if err != nil { | ||
t.Log(err.Error()) | ||
t.Fail() | ||
} | ||
defer zipReader.Close() | ||
|
||
book, err := OpenBook(zipReader) | ||
if err != nil { | ||
t.Log(err.Error()) | ||
t.Fail() | ||
} | ||
``` | ||
After that, all metadata can be found in the book Object |
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,58 @@ | ||
package epub | ||
|
||
import ( | ||
"archive/zip" | ||
"encoding/xml" | ||
"fmt" | ||
"io" | ||
"path" | ||
) | ||
|
||
type Book struct { | ||
Opf Package | ||
Container Container | ||
ZipReader *zip.ReadCloser | ||
} | ||
|
||
func OpenBook(reader *zip.ReadCloser) (*Book, error) { | ||
book := &Book{ZipReader: reader} | ||
err := book.readXML("META-INF/container.xml", &book.Container) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = book.readXML(book.Container.Rootfile.Path, &book.Opf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if book.Opf.Version != "3.0" { | ||
return nil, fmt.Errorf("%s not supported yet!", book.Opf.Version) | ||
} | ||
return book, nil | ||
} | ||
|
||
func (book *Book) Open(fileName string) (io.ReadCloser, error) { | ||
return book.open(book.getFileFromRootPath(fileName)) | ||
} | ||
|
||
func (book *Book) getFileFromRootPath(fileName string) string { | ||
return path.Join(path.Dir(book.Container.Rootfile.Path), fileName) | ||
} | ||
|
||
func (book *Book) readXML(fileName string, targetStruct interface{}) error { | ||
reader, err := book.open(fileName) | ||
if err != nil { | ||
return err | ||
} | ||
defer reader.Close() | ||
dec := xml.NewDecoder(reader) | ||
return dec.Decode(targetStruct) | ||
} | ||
|
||
func (book *Book) open(fileName string) (io.ReadCloser, error) { | ||
for _, file := range book.ZipReader.File { | ||
if file.Name == fileName { | ||
return file.Open() | ||
} | ||
} | ||
return nil, fmt.Errorf("file %s not exist", fileName) | ||
} |
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,10 @@ | ||
package epub | ||
|
||
type Container struct { | ||
Rootfile Rootfile `xml:"rootfiles>rootfile"` | ||
} | ||
|
||
type Rootfile struct { | ||
Path string `xml:"full-path,attr"` | ||
Type string `xml:"media-type,attr"` | ||
} |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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
Binary file not shown.
This file was deleted.
Oops, something went wrong.