mirror of
https://codeberg.org/scip/epuppy.git
synced 2025-12-17 04:20:59 +01:00
- add color and color config support - fix epub parsing - add variable margin (left and right arrow keys) - add help screen (hit `?`) - add config file support
65 lines
1.0 KiB
Go
65 lines
1.0 KiB
Go
package epub
|
|
|
|
import (
|
|
"archive/zip"
|
|
"strings"
|
|
)
|
|
|
|
// Open open a epub file
|
|
func Open(fn string) (*Book, error) {
|
|
fd, err := zip.OpenReader(fn)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
bk := Book{fd: fd}
|
|
mt, err := bk.readBytes("mimetype")
|
|
if err != nil {
|
|
return &bk, err
|
|
}
|
|
|
|
bk.Mimetype = string(mt)
|
|
|
|
err = bk.readXML("META-INF/container.xml", &bk.Container)
|
|
if err != nil {
|
|
return &bk, err
|
|
}
|
|
|
|
err = bk.readXML(bk.Container.Rootfile.Path, &bk.Opf)
|
|
if err != nil {
|
|
return &bk, err
|
|
}
|
|
|
|
for _, mf := range bk.Opf.Manifest {
|
|
if mf.ID == bk.Opf.Spine.Toc {
|
|
err = bk.readXML(bk.filename(mf.Href), &bk.Ncx)
|
|
if err != nil {
|
|
return &bk, err
|
|
}
|
|
|
|
break
|
|
}
|
|
}
|
|
|
|
for _, file := range bk.Files() {
|
|
content, err := bk.readBytes(file)
|
|
if err != nil {
|
|
return &bk, err
|
|
}
|
|
|
|
ct := Content{Src: file}
|
|
|
|
if strings.Contains(string(content), "DOCTYPE") {
|
|
if err := ct.String(content); err != nil {
|
|
return &bk, err
|
|
}
|
|
}
|
|
|
|
bk.Content = append(bk.Content, ct)
|
|
}
|
|
|
|
return &bk, nil
|
|
}
|