mirror of
https://codeberg.org/scip/epuppy.git
synced 2025-12-17 20:41:00 +01:00
Fix crash and add support for content in <div> (#9)
* fix #8: better regex to remove html entities * add opf and ncx debug * make epub content retrieval more flexible * fix epub content retrieval: also support html files with <div>
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/repr"
|
||||
)
|
||||
|
||||
// Open open a epub file
|
||||
@@ -53,32 +55,56 @@ func Open(fn string, dumpxml bool) (*Book, error) {
|
||||
}
|
||||
}
|
||||
|
||||
for _, file := range bk.Files() {
|
||||
content, err := bk.readBytes(file)
|
||||
type section struct {
|
||||
file, title string
|
||||
}
|
||||
|
||||
sections := []section{}
|
||||
|
||||
if len(bk.Ncx.Points) > 0 {
|
||||
for _, block := range bk.Ncx.Points {
|
||||
sections = append(sections,
|
||||
section{
|
||||
file: "OEBPS/" + block.Content.Src,
|
||||
title: block.Text,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
for _, file := range bk.Files() {
|
||||
sections = append(sections,
|
||||
section{
|
||||
file: file,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for _, section := range sections {
|
||||
content, err := bk.readBytes(section.file)
|
||||
if err != nil {
|
||||
return &bk, err
|
||||
}
|
||||
|
||||
ct := Content{Src: file}
|
||||
if strings.Contains(string(content), "<?xml") {
|
||||
if err := ct.String(content); err != nil {
|
||||
return &bk, err
|
||||
}
|
||||
|
||||
bk.Content = append(bk.Content, ct)
|
||||
|
||||
if dumpxml {
|
||||
fmt.Println(string(ct.XML))
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(file, bk.CoverFile) {
|
||||
if strings.Contains(section.file, bk.CoverFile) {
|
||||
bk.CoverImage = content
|
||||
}
|
||||
|
||||
ct := Content{Src: section.file, Title: section.title}
|
||||
|
||||
if strings.Contains(string(content), "<?xml") || strings.Contains(string(content), "<!DOCTYPE") {
|
||||
if err := ct.String(content); err != nil {
|
||||
return &bk, err
|
||||
}
|
||||
}
|
||||
|
||||
if dumpxml {
|
||||
fmt.Println(string(ct.XML))
|
||||
}
|
||||
|
||||
bk.Content = append(bk.Content, ct)
|
||||
}
|
||||
|
||||
if dumpxml {
|
||||
repr.Println(sections)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user