rebuild toc and content

handle landscape mode
better handling double page with epub 3.3 center spread support
improve support for apple book
This commit is contained in:
Celogeek 2023-04-16 11:45:00 +02:00
parent ed1a312027
commit a7a29d6326
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
14 changed files with 361 additions and 240 deletions

197
internal/epub/content.go Normal file
View File

@ -0,0 +1,197 @@
package epub
import (
"fmt"
"github.com/beevik/etree"
)
type Content struct {
doc *etree.Document
}
type TagAttrs map[string]string
type Tag struct {
name string
attrs TagAttrs
value string
}
func (e *ePub) getMeta(title string, part *epubPart, currentPart, totalPart int) []Tag {
metas := []Tag{
{"meta", TagAttrs{"property": "dcterms:modified"}, e.UpdatedAt},
{"meta", TagAttrs{"property": "rendition:layout"}, "pre-paginated"},
{"meta", TagAttrs{"property": "rendition:spread"}, "auto"},
{"meta", TagAttrs{"property": "rendition:orientation"}, "auto"},
{"meta", TagAttrs{"property": "ibooks:specified-fonts"}, "true"},
{"meta", TagAttrs{"property": "schema:accessMode"}, "visual"},
{"meta", TagAttrs{"property": "schema:accessModeSufficient"}, "visual"},
{"meta", TagAttrs{"property": "schema:accessibilityHazard"}, "noFlashingHazard"},
{"meta", TagAttrs{"property": "schema:accessibilityHazard"}, "noMotionSimulationHazard"},
{"meta", TagAttrs{"property": "schema:accessibilityHazard"}, "noSoundHazard"},
{"meta", TagAttrs{"name": "book-type", "content": "comic"}, ""},
{"opf:meta", TagAttrs{"name": "fixed-layout", "content": "true"}, ""},
{"opf:meta", TagAttrs{"name": "original-resolution", "content": fmt.Sprintf("%dx%d", e.ViewWidth, e.ViewHeight)}, ""},
{"dc:title", TagAttrs{}, title},
{"dc:identifier", TagAttrs{"id": "ean"}, fmt.Sprintf("urn:uuid:%s", e.UID)},
{"dc:language", TagAttrs{}, "en"},
{"dc:creator", TagAttrs{}, e.Author},
{"dc:publisher", TagAttrs{}, e.Publisher},
{"dc:contributor", TagAttrs{}, "Go Comic Convertor"},
{"dc:date", TagAttrs{}, e.UpdatedAt},
}
if e.Manga {
metas = append(metas, Tag{"meta", TagAttrs{"name": "primary-writing-mode", "content": "horizontal-rl"}, ""})
} else {
metas = append(metas, Tag{"meta", TagAttrs{"name": "primary-writing-mode", "content": "horizontal-lr"}, ""})
}
if part.Cover != nil {
metas = append(metas, Tag{"meta", TagAttrs{"name": "cover", "content": part.Cover.Key("img")}, ""})
}
if totalPart > 1 {
metas = append(
metas,
Tag{"meta", TagAttrs{"name": "calibre:series", "content": e.Title}, ""},
Tag{"meta", TagAttrs{"name": "calibre:series_index", "content": fmt.Sprint(currentPart)}, ""},
)
}
return metas
}
func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart int) []Tag {
iTag := func(img *Image) Tag {
return Tag{"item", TagAttrs{"id": img.Key("img"), "href": img.ImgPath(), "media-type": "image/jpeg"}, ""}
}
hTag := func(img *Image) Tag {
return Tag{"item", TagAttrs{"id": img.Key("page"), "href": img.TextPath(), "media-type": "application/xhtml+xml"}, ""}
}
sTag := func(img *Image) Tag {
return Tag{"item", TagAttrs{"id": img.SpaceKey("page"), "href": img.SpacePath(), "media-type": "application/xhtml+xml"}, ""}
}
items := []Tag{
{"item", TagAttrs{"id": "toc", "href": "toc.xhtml", "properties": "nav", "media-type": "application/xhtml+xml"}, ""},
{"item", TagAttrs{"id": "css", "href": "Text/style.css", "media-type": "text/css"}, ""},
}
if part.Cover != nil {
items = append(items, iTag(part.Cover), hTag(part.Cover))
}
for _, img := range part.Images {
if img.Part == 1 {
items = append(items, sTag(img))
}
items = append(items, iTag(img), hTag(img))
}
items = append(items, sTag(part.Images[len(part.Images)-1]))
return items
}
func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int) []Tag {
spine := []Tag{}
isOnTheRight := !e.Manga
getSpread := func(doublePageNoBlank bool) string {
isOnTheRight = !isOnTheRight
if doublePageNoBlank {
// Center the double page then start back to comic mode (mange/normal)
isOnTheRight = !e.Manga
return "rendition:page-spread-center"
}
if isOnTheRight {
return "rendition:page-spread-right"
} else {
return "rendition:page-spread-left"
}
}
for _, img := range part.Images {
spine = append(spine, Tag{
"itemref",
TagAttrs{"idref": img.Key("page"), "properties": getSpread(img.DoublePage && e.NoBlankPage)},
"",
})
if img.DoublePage && isOnTheRight && !e.NoBlankPage {
spine = append(spine, Tag{
"itemref",
TagAttrs{"idref": img.SpaceKey("page"), "properties": getSpread(false)},
"",
})
}
}
if e.Manga == isOnTheRight {
spine = append(spine, Tag{
"itemref",
TagAttrs{"idref": part.Images[len(part.Images)-1].SpaceKey("page"), "properties": getSpread(false)},
"",
})
}
return spine
}
func (e *ePub) getGuide(title string, part *epubPart, currentPart, totalPart int) []Tag {
guide := []Tag{}
if part.Cover != nil {
guide = append(guide, Tag{"reference", TagAttrs{"type": "cover", "title": "cover", "href": part.Cover.TextPath()}, ""})
}
guide = append(guide, Tag{"reference", TagAttrs{"type": "text", "title": "content", "href": part.Images[0].TextPath()}, ""})
return guide
}
func (e *ePub) getContent(title string, part *epubPart, currentPart, totalPart int) *Content {
doc := etree.NewDocument()
doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
pkg := doc.CreateElement("package")
pkg.CreateAttr("xmlns", "http://www.idpf.org/2007/opf")
pkg.CreateAttr("unique-identifier", "ean")
pkg.CreateAttr("version", "3.0")
pkg.CreateAttr("prefix", "rendition: http://www.idpf.org/vocab/rendition/# ibooks: http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0/")
addToElement := func(elm *etree.Element, meth func(title string, part *epubPart, currentPart, totalPart int) []Tag) {
for _, p := range meth(title, part, currentPart, totalPart) {
meta := elm.CreateElement(p.name)
for k, v := range p.attrs {
meta.CreateAttr(k, v)
}
meta.SortAttrs()
if p.value != "" {
meta.CreateText(p.value)
}
}
}
metadata := pkg.CreateElement("metadata")
metadata.CreateAttr("xmlns:dc", "http://purl.org/dc/elements/1.1/")
metadata.CreateAttr("xmlns:opf", "http://www.idpf.org/2007/opf")
addToElement(metadata, e.getMeta)
manifest := pkg.CreateElement("manifest")
addToElement(manifest, e.getManifest)
spine := pkg.CreateElement("spine")
if e.Manga {
spine.CreateAttr("page-progression-direction", "rtl")
} else {
spine.CreateAttr("page-progression-direction", "ltr")
}
addToElement(spine, e.getSpine)
guide := pkg.CreateElement("guide")
addToElement(guide, e.getGuide)
return &Content{
doc,
}
}
func (c *Content) String() string {
c.doc.Indent(2)
r, _ := c.doc.WriteToString()
return r
}

View File

@ -1,7 +1,6 @@
package epub
import (
"encoding/xml"
"fmt"
"os"
"path/filepath"
@ -134,7 +133,6 @@ func (e *ePub) getParts() ([]*epubPart, error) {
currentSize := baseSize
currentImages := make([]*Image, 0)
part := 1
imgIsOnRightSide := false
for _, img := range images {
imgSize := img.Data.CompressedSize() + xhtmlSize
@ -144,14 +142,11 @@ func (e *ePub) getParts() ([]*epubPart, error) {
Images: currentImages,
})
part += 1
imgIsOnRightSide = false
currentSize = baseSize
currentImages = make([]*Image, 0)
}
currentSize += imgSize
img.NeedSpace = img.Part == 1 && imgIsOnRightSide
currentImages = append(currentImages, img)
imgIsOnRightSide = !imgIsOnRightSide
}
if len(currentImages) > 0 {
parts = append(parts, &epubPart{
@ -163,59 +158,6 @@ func (e *ePub) getParts() ([]*epubPart, error) {
return parts, nil
}
func (e *ePub) getToc(images []*Image) *TocChildren {
paths := map[string]*TocPart{
".": {},
}
for _, img := range images {
currentPath := "."
for _, path := range strings.Split(img.Path, string(filepath.Separator)) {
parentPath := currentPath
currentPath = filepath.Join(currentPath, path)
if _, ok := paths[currentPath]; ok {
continue
}
part := &TocPart{
Title: TocTitle{
Value: path,
Link: fmt.Sprintf("Text/%d_p%d.xhtml", img.Id, img.Part),
},
}
paths[currentPath] = part
if paths[parentPath].Children == nil {
paths[parentPath].Children = &TocChildren{}
}
paths[parentPath].Children.Tags = append(paths[parentPath].Children.Tags, part)
}
}
children := paths["."].Children
if children != nil && e.StripFirstDirectoryFromToc && len(children.Tags) == 1 {
children = children.Tags[0].Children
}
return children
}
func (e *ePub) getTree(images []*Image, skip_files bool) string {
t := NewTree()
for _, img := range images {
if skip_files {
t.Add(img.Path)
} else {
t.Add(filepath.Join(img.Path, img.Name))
}
}
c := t.Root()
if skip_files && e.StripFirstDirectoryFromToc && len(c.Children) == 1 {
c = c.Children[0]
}
return c.toString("")
}
func (e *ePub) Write() error {
type zipContent struct {
Name string
@ -263,33 +205,11 @@ func (e *ePub) Write() error {
title = fmt.Sprintf("%s [%d/%d]", title, i+1, totalParts)
}
tocChildren := e.getToc(part.Images)
toc := []byte{}
if tocChildren != nil {
toc, err = xml.MarshalIndent(tocChildren.Tags, " ", " ")
if err != nil {
return err
}
}
content := []zipContent{
{"META-INF/container.xml", containerTmpl},
{"OEBPS/content.opf", e.render(contentTmpl, map[string]any{
"Info": e,
"Cover": part.Cover,
"Images": part.Images,
"Title": title,
"Part": i + 1,
"Total": totalParts,
})},
{"OEBPS/toc.ncx", e.render(tocTmpl, map[string]any{
"Info": e,
"Title": title,
})},
{"OEBPS/nav.xhtml", e.render(navTmpl, map[string]any{
"Title": title,
"TOC": string(toc),
})},
{"META-INF/com.apple.ibooks.display-options.xml", appleBooksTmpl},
{"OEBPS/content.opf", e.getContent(title, part, i+1, totalParts).String()},
{"OEBPS/toc.xhtml", e.getToc(title, part.Images)},
{"OEBPS/Text/style.css", styleTmpl},
{"OEBPS/Text/part.xhtml", e.render(partTmpl, map[string]any{
"Info": e,
@ -310,7 +230,17 @@ func (e *ePub) Write() error {
// Cover exist or part > 1
// If no cover, part 2 and more will include the image as a cover
if e.HasCover || i > 0 {
wz.WriteImage(part.Cover.Data)
if err := wz.WriteFile(fmt.Sprintf("OEBPS/%s", part.Cover.TextPath()), e.render(textTmpl, map[string]any{
"Info": e,
"Image": part.Cover,
"Manga": e.Manga,
"Top": fmt.Sprintf("%d", (e.ViewHeight-part.Cover.Height)/2),
})); err != nil {
return err
}
if err := wz.WriteImage(part.Cover.Data); err != nil {
return err
}
}
for i, img := range part.Images {

View File

@ -26,15 +26,35 @@ import (
)
type Image struct {
Id int
Part int
Data *ImageData
Width int
Height int
IsCover bool
NeedSpace bool
Path string
Name string
Id int
Part int
Data *ImageData
Width int
Height int
IsCover bool
DoublePage bool
Path string
Name string
}
func (i *Image) Key(prefix string) string {
return fmt.Sprintf("%s_%d_p%d", prefix, i.Id, i.Part)
}
func (i *Image) SpaceKey(prefix string) string {
return fmt.Sprintf("%s_%d_sp", prefix, i.Id)
}
func (i *Image) TextPath() string {
return fmt.Sprintf("Text/%d_p%d.xhtml", i.Id, i.Part)
}
func (i *Image) ImgPath() string {
return fmt.Sprintf("Images/%d_p%d.jpg", i.Id, i.Part)
}
func (i *Image) SpacePath() string {
return fmt.Sprintf("Text/%d_sp.xhtml", i.Id)
}
type imageTask struct {
@ -130,15 +150,15 @@ func (e *ePub) LoadImages() ([]*Image, error) {
for img := range imageInput {
img.Reader.Close()
images = append(images, &Image{
Id: img.Id,
Part: 0,
Data: nil,
Width: 0,
Height: 0,
IsCover: false,
NeedSpace: false, // NeedSpace reajust during parts computation
Path: img.Path,
Name: img.Name,
Id: img.Id,
Part: 0,
Data: nil,
Width: 0,
Height: 0,
IsCover: false,
DoublePage: false,
Path: img.Path,
Name: img.Name,
})
}
@ -180,15 +200,17 @@ func (e *ePub) LoadImages() ([]*Image, error) {
g.Draw(dst, src)
imageOutput <- &Image{
Id: img.Id,
Part: 0,
Data: newImageData(img.Id, 0, dst, e.ImageOptions.Quality),
Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(),
IsCover: img.Id == 0,
NeedSpace: false,
Path: img.Path,
Name: img.Name,
Id: img.Id,
Part: 0,
Data: newImageData(img.Id, 0, dst, e.ImageOptions.Quality),
Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(),
IsCover: img.Id == 0,
DoublePage: src.Bounds().Dx() > src.Bounds().Dy() &&
src.Bounds().Dx() > e.ImageOptions.ViewHeight &&
src.Bounds().Dy() > e.ImageOptions.ViewWidth,
Path: img.Path,
Name: img.Name,
}
// Auto split double page
@ -205,15 +227,15 @@ func (e *ePub) LoadImages() ([]*Image, error) {
dst := image.NewGray(g.Bounds(src.Bounds()))
g.Draw(dst, src)
imageOutput <- &Image{
Id: img.Id,
Part: part,
Data: newImageData(img.Id, part, dst, e.ImageOptions.Quality),
Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(),
IsCover: false,
NeedSpace: false, // NeedSpace reajust during parts computation
Path: img.Path,
Name: img.Name,
Id: img.Id,
Part: part,
Data: newImageData(img.Id, part, dst, e.ImageOptions.Quality),
Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(),
IsCover: false,
DoublePage: false,
Path: img.Path,
Name: img.Name,
}
}
}

View File

@ -5,14 +5,8 @@ import _ "embed"
//go:embed "templates/container.xml.tmpl"
var containerTmpl string
//go:embed "templates/content.opf.tmpl"
var contentTmpl string
//go:embed "templates/toc.ncx.tmpl"
var tocTmpl string
//go:embed "templates/nav.xhtml.tmpl"
var navTmpl string
//go:embed "templates/applebooks.xml.tmpl"
var appleBooksTmpl string
//go:embed "templates/style.css.tmpl"
var styleTmpl string

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<display_options>
<platform name="*">
<option name="fixed-layout">true</option>
</platform>
</display_options>

View File

@ -7,6 +7,5 @@
<meta name="viewport" content="width={{ .Info.ViewWidth }}, height={{ .Info.ViewHeight }}"/>
</head>
<body>
<h1>{{ if .Info.Manga }}&#8592;{{ else }}&#8594;{{ end }}</h1>
</body>
</html>

View File

@ -1,6 +1,7 @@
<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
<container version="1.0"
xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>

View File

@ -1,57 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<package version="3.0" unique-identifier="BookID" xmlns="http://www.idpf.org/2007/opf">
{{ $info := .Info }}
<metadata xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>{{ .Title }}</dc:title>
<dc:language>en-US</dc:language>
<dc:identifier id="BookID">urn:uuid:{{ $info.UID }}</dc:identifier>
<dc:contributor id="contributor">{{ $info.Publisher }}</dc:contributor>
<dc:publisher>{{ $info.Publisher }}</dc:publisher>
<dc:date>{{ $info.UpdatedAt }}</dc:date>
<dc:creator>{{ $info.Author }}</dc:creator>
<meta property="dcterms:modified">{{ $info.UpdatedAt }}</meta>
<meta name="original-resolution" content="{{ $info.ViewWidth }}x{{ $info.ViewHeight }}"/>
<meta name="book-type" content="comic"/>
<meta name="primary-writing-mode" content="horizontal-{{ if $info.Manga }}rl{{ else }}lr{{ end }}"/>
<meta property="rendition:layout">pre-paginated</meta>
<meta property="rendition:orientation">portrait</meta>
<meta name="orientation-lock" content="portrait"/>
{{ if eq $info.AddPanelView true }}
<meta name="region-mag" content="true"/>
{{ end }}
{{ if gt .Total 1 }}
<meta name="calibre:series" content="{{ $info.Title }}"/>
<meta name="calibre:series_index" content="{{ .Part }}"/>
{{ end }}
</metadata>
<manifest>
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
<item id="nav" href="nav.xhtml" properties="nav" media-type="application/xhtml+xml"/>
<item id="style_css" href="Text/style.css" media-type="text/css"/>
{{ if eq $info.AddPanelView true }}
<item id="panelview_css" href="Text/panelview.css" media-type="text/css"/>
{{ end }}
<item id="cover" href="Images/{{ .Cover.Id }}_p{{ .Cover.Part }}.jpg" media-type="image/jpeg" properties="cover-image"/>
{{ range .Images }}
{{ if eq .IsCover false }}
<item id="img_{{ .Id }}_p{{ .Part}}" href="Images/{{ .Id }}_p{{ .Part}}.jpg" media-type="image/jpeg"/>
{{ end }}
{{ end }}
<item id="page_part" href="Text/part.xhtml" media-type="application/xhtml+xml"/>
{{ range .Images }}
<item id="page_{{ .Id }}_p{{ .Part}}" href="Text/{{ .Id }}_p{{ .Part}}.xhtml" media-type="application/xhtml+xml"/>
{{ if eq .NeedSpace true }}
<item id="page_{{ .Id }}_sp" href="Text/{{ .Id }}_sp.xhtml" media-type="application/xhtml+xml"/>
{{ end }}
{{ end }}
</manifest>
<spine toc="ncx" page-progression-direction="{{ if $info.Manga }}rtl{{ else }}ltr{{ end }}">
<itemref idref="page_part" linear="yes"/>
{{ range .Images }}
{{ if eq .NeedSpace true }}
<itemref idref="page_{{ .Id }}_sp" linear="yes"/>
{{ end }}
<itemref idref="page_{{ .Id }}_p{{ .Part }}" linear="yes"/>
{{ end }}
</spine>
</package>

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>{{ .Title }}</title>
</head>
<body>
<nav epub:type="toc" id="toc">
<h2>Table of content</h2>
<ol>
<li><a href="Text/part.xhtml">{{ .Title }}</a></li>
{{ .TOC }}
</ol>
</nav>
</body>
</html>

View File

@ -7,9 +7,9 @@
<meta name="viewport" content="width={{ .Info.ViewWidth }}, height={{ .Info.ViewHeight }}"/>
</head>
<body>
<h1>{{ .Info.Title }}</h1>
<h2>{{ .Info.Title }}</h2>
{{ if gt .Total 1 }}
<h1>Part {{ .Part }} / {{ .Total }}</h1>
<h3>Part {{ .Part }} / {{ .Total }}</h3>
{{ end }}
</body>
</html>

View File

@ -6,10 +6,8 @@ html {
}
body {
font-size: 16px;
font-size: 1em;
text-align: center;
width: 100%;
height: 100%;
}
body,
@ -64,10 +62,22 @@ h3,
h4,
h5,
h6 {
font-size: 150%;
-webkit-hyphens:none;
font-weight: normal;
}
h1 {
font-size: 200%;
}
h2 {
font-size: 150%;
}
h3 {
font-size: 120%;
}
sup {
vertical-align: text-top;
}

View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ncx version="2005-1" xml:lang="en-US" xmlns="http://www.daisy.org/z3986/2005/ncx/">
<head>
<meta name="dtb:uid" content="urn:uuid:{{ .Info.UID }}"/>
<meta name="dtb:depth" content="1"/>
<meta name="dtb:totalPageCount" content="0"/>
<meta name="dtb:maxPageNumber" content="0"/>
<meta name="generated" content="true"/>
</head>
<docTitle><text>{{ .Title }}</text></docTitle>
<navMap>
<navPoint id="Text"><navLabel><text>{{ .Title }}</text></navLabel><content src="Text/part.xhtml"/></navPoint>
</navMap>
</ncx>

View File

@ -1,22 +1,54 @@
package epub
import (
"encoding/xml"
"path/filepath"
"strings"
"github.com/beevik/etree"
)
type TocTitle struct {
XMLName xml.Name `xml:"a"`
Value string `xml:",innerxml"`
Link string `xml:"href,attr"`
}
func (e *ePub) getToc(title string, images []*Image) string {
doc := etree.NewDocument()
doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
doc.CreateDirective("DOCTYPE html")
type TocChildren struct {
XMLName xml.Name `xml:"ol"`
Tags []*TocPart
}
html := doc.CreateElement("html")
html.CreateAttr("xmlns", "http://www.w3.org/1999/xhtml")
html.CreateAttr("xmlns:epub", "http://www.idpf.org/2007/ops")
type TocPart struct {
XMLName xml.Name `xml:"li"`
Title TocTitle
Children *TocChildren `xml:",omitempty"`
html.CreateElement("head").CreateElement("title").CreateText(title)
body := html.CreateElement("body")
nav := body.CreateElement("nav")
nav.CreateAttr("epub:type", "toc")
nav.CreateAttr("id", "toc")
nav.CreateElement("h2").CreateText(title)
ol := etree.NewElement("ol")
paths := map[string]*etree.Element{".": ol}
for _, img := range images {
currentPath := "."
for _, path := range strings.Split(img.Path, string(filepath.Separator)) {
parentPath := currentPath
currentPath = filepath.Join(currentPath, path)
if _, ok := paths[currentPath]; ok {
continue
}
t := paths[parentPath].CreateElement("li")
link := t.CreateElement("a")
link.CreateAttr("href", img.TextPath())
link.CreateText(path)
paths[currentPath] = t
}
}
if len(ol.ChildElements()) == 1 && e.StripFirstDirectoryFromToc {
ol = ol.ChildElements()[0]
}
if len(ol.ChildElements()) > 0 {
nav.AddChild(ol)
}
doc.Indent(2)
r, _ := doc.WriteToString()
return r
}

View File

@ -51,3 +51,20 @@ func (n *Node) toString(indent string) string {
}
return r.String()
}
func (e *ePub) getTree(images []*Image, skip_files bool) string {
t := NewTree()
for _, img := range images {
if skip_files {
t.Add(img.Path)
} else {
t.Add(filepath.Join(img.Path, img.Name))
}
}
c := t.Root()
if skip_files && e.StripFirstDirectoryFromToc && len(c.Children) == 1 {
c = c.Children[0]
}
return c.toString("")
}