mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 16:22:37 +02:00
Compare commits
No commits in common. "07e04e514b1b588f7c3f885efb59d0c2db7e5450" and "b340305f33a02285f2c7bb6c61eb02a8e60bb376" have entirely different histories.
07e04e514b
...
b340305f33
@ -30,8 +30,8 @@ func New() *Converter {
|
||||
order: make([]Order, 0),
|
||||
}
|
||||
|
||||
var cmdOutput strings.Builder
|
||||
cmd.SetOutput(&cmdOutput)
|
||||
cmdOutput := &strings.Builder{}
|
||||
cmd.SetOutput(cmdOutput)
|
||||
cmd.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage of %s:\n", filepath.Base(os.Args[0]))
|
||||
for _, o := range conv.order {
|
||||
|
@ -14,7 +14,6 @@ import (
|
||||
epubimageprocessing "github.com/celogeek/go-comic-converter/v2/internal/epub/image_processing"
|
||||
epubprogress "github.com/celogeek/go-comic-converter/v2/internal/epub/progress"
|
||||
epubtemplates "github.com/celogeek/go-comic-converter/v2/internal/epub/templates"
|
||||
epubtree "github.com/celogeek/go-comic-converter/v2/internal/epub/tree"
|
||||
epubzip "github.com/celogeek/go-comic-converter/v2/internal/epub/zip"
|
||||
"github.com/gofrs/uuid"
|
||||
)
|
||||
@ -49,7 +48,12 @@ type epubPart struct {
|
||||
}
|
||||
|
||||
func New(options *Options) *ePub {
|
||||
uid := uuid.Must(uuid.NewV4())
|
||||
uid, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
tmpl := template.New("parser")
|
||||
tmpl.Funcs(template.FuncMap{
|
||||
"mod": func(i, j int) bool { return i%j == 0 },
|
||||
@ -66,12 +70,20 @@ func New(options *Options) *ePub {
|
||||
}
|
||||
|
||||
func (e *ePub) render(templateString string, data any) string {
|
||||
var result strings.Builder
|
||||
tmpl := template.Must(e.templateProcessor.Parse(templateString))
|
||||
if err := tmpl.Execute(&result, data); err != nil {
|
||||
panic(err)
|
||||
tmpl, err := e.templateProcessor.Parse(templateString)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return regexp.MustCompile("\n+").ReplaceAllString(result.String(), "\n")
|
||||
result := &strings.Builder{}
|
||||
if err := tmpl.Execute(result, data); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
stripBlank := regexp.MustCompile("\n+")
|
||||
|
||||
return stripBlank.ReplaceAllString(result.String(), "\n")
|
||||
}
|
||||
|
||||
func (e *ePub) writeImage(wz *epubzip.EpubZip, img *epubimage.Image) error {
|
||||
@ -180,23 +192,6 @@ func (e *ePub) getParts() ([]*epubPart, error) {
|
||||
return parts, nil
|
||||
}
|
||||
|
||||
func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string {
|
||||
t := epubtree.New()
|
||||
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.WriteString("")
|
||||
}
|
||||
|
||||
func (e *ePub) Write() error {
|
||||
type zipContent struct {
|
||||
Name string
|
||||
@ -252,19 +247,8 @@ func (e *ePub) Write() error {
|
||||
content := []zipContent{
|
||||
{"META-INF/container.xml", epubtemplates.Container},
|
||||
{"META-INF/com.apple.ibooks.display-options.xml", epubtemplates.AppleBooks},
|
||||
{"OEBPS/content.opf", epubtemplates.Content(&epubtemplates.ContentOptions{
|
||||
Title: title,
|
||||
UID: e.UID,
|
||||
Author: e.Author,
|
||||
Publisher: e.Publisher,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
ImageOptions: e.Image,
|
||||
Cover: part.Cover,
|
||||
Images: part.Images,
|
||||
Current: i + 1,
|
||||
Total: totalParts,
|
||||
})},
|
||||
{"OEBPS/toc.xhtml", epubtemplates.Toc(title, e.StripFirstDirectoryFromToc, part.Images)},
|
||||
{"OEBPS/content.opf", e.getContent(title, part, i+1, totalParts).String()},
|
||||
{"OEBPS/toc.xhtml", e.getToc(title, part.Images)},
|
||||
{"OEBPS/Text/style.css", e.render(epubtemplates.Style, map[string]any{
|
||||
"PageWidth": e.Image.ViewWidth,
|
||||
"PageHeight": e.Image.ViewHeight,
|
||||
|
202
internal/epub/epub_content.go
Normal file
202
internal/epub/epub_content.go
Normal file
@ -0,0 +1,202 @@
|
||||
package epub
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/beevik/etree"
|
||||
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
|
||||
)
|
||||
|
||||
type Content struct {
|
||||
doc *etree.Document
|
||||
}
|
||||
|
||||
func (c *Content) String() string {
|
||||
c.doc.Indent(2)
|
||||
r, _ := c.doc.WriteToString()
|
||||
return r
|
||||
}
|
||||
|
||||
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": "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.Image.ViewWidth, e.Image.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.Image.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 *epubimage.Image) Tag {
|
||||
return Tag{"item", TagAttrs{"id": img.Key("img"), "href": img.ImgPath(), "media-type": "image/jpeg"}, ""}
|
||||
}
|
||||
hTag := func(img *epubimage.Image) Tag {
|
||||
return Tag{"item", TagAttrs{"id": img.Key("page"), "href": img.TextPath(), "media-type": "application/xhtml+xml"}, ""}
|
||||
}
|
||||
sTag := func(img *epubimage.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"}, ""},
|
||||
{"item", TagAttrs{"id": "page_title", "href": "Text/title.xhtml", "media-type": "application/xhtml+xml"}, ""},
|
||||
{"item", TagAttrs{"id": "img_title", "href": "Images/title.jpg", "media-type": "image/jpeg"}, ""},
|
||||
}
|
||||
|
||||
if e.Image.HasCover || currentPart > 1 {
|
||||
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 {
|
||||
isOnTheRight := !e.Image.Manga
|
||||
getSpread := func(doublePageNoBlank bool) string {
|
||||
isOnTheRight = !isOnTheRight
|
||||
if doublePageNoBlank {
|
||||
// Center the double page then start back to comic mode (mange/normal)
|
||||
isOnTheRight = !e.Image.Manga
|
||||
return "rendition:page-spread-center"
|
||||
}
|
||||
if isOnTheRight {
|
||||
return "rendition:page-spread-right"
|
||||
} else {
|
||||
return "rendition:page-spread-left"
|
||||
}
|
||||
}
|
||||
|
||||
spine := []Tag{
|
||||
{"itemref", TagAttrs{"idref": "page_title", "properties": getSpread(true)}, ""},
|
||||
}
|
||||
for _, img := range part.Images {
|
||||
spine = append(spine, Tag{
|
||||
"itemref",
|
||||
TagAttrs{"idref": img.Key("page"), "properties": getSpread(img.DoublePage && e.Image.NoBlankPage)},
|
||||
"",
|
||||
})
|
||||
if img.DoublePage && isOnTheRight && !e.Image.NoBlankPage {
|
||||
spine = append(spine, Tag{
|
||||
"itemref",
|
||||
TagAttrs{"idref": img.SpaceKey("page"), "properties": getSpread(false)},
|
||||
"",
|
||||
})
|
||||
}
|
||||
}
|
||||
if e.Image.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/#")
|
||||
|
||||
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.Image.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,
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package epubtemplates
|
||||
package epub
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
@ -8,7 +8,7 @@ import (
|
||||
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
|
||||
)
|
||||
|
||||
func Toc(title string, stripFirstDirectoryFromToc bool, images []*epubimage.Image) string {
|
||||
func (e *ePub) getToc(title string, images []*epubimage.Image) string {
|
||||
doc := etree.NewDocument()
|
||||
doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
|
||||
doc.CreateDirective("DOCTYPE html")
|
||||
@ -42,7 +42,7 @@ func Toc(title string, stripFirstDirectoryFromToc bool, images []*epubimage.Imag
|
||||
}
|
||||
}
|
||||
|
||||
if len(ol.ChildElements()) == 1 && stripFirstDirectoryFromToc {
|
||||
if len(ol.ChildElements()) == 1 && e.StripFirstDirectoryFromToc {
|
||||
ol = ol.FindElement("/li/ol")
|
||||
}
|
||||
|
25
internal/epub/epub_tree.go
Normal file
25
internal/epub/epub_tree.go
Normal file
@ -0,0 +1,25 @@
|
||||
package epub
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
|
||||
epubtree "github.com/celogeek/go-comic-converter/v2/internal/epub/tree"
|
||||
)
|
||||
|
||||
func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string {
|
||||
t := epubtree.New()
|
||||
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("")
|
||||
}
|
@ -28,18 +28,16 @@ type Options struct {
|
||||
}
|
||||
|
||||
func (o *Options) mustExtractImage(imageOpener func() (io.ReadCloser, error)) *bytes.Buffer {
|
||||
var b bytes.Buffer
|
||||
if o.Dry {
|
||||
return &b
|
||||
return &bytes.Buffer{}
|
||||
}
|
||||
|
||||
f, err := imageOpener()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var b bytes.Buffer
|
||||
_, err = io.Copy(&b, f)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
|
@ -21,42 +21,30 @@ func (img *ImageData) CompressedSize() uint64 {
|
||||
return img.Header.CompressedSize64 + 30 + uint64(len(img.Header.Name))
|
||||
}
|
||||
|
||||
func exitWithError(err error) {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func New(id int, part int, img image.Image, quality int) *ImageData {
|
||||
name := fmt.Sprintf("OEBPS/Images/%d_p%d.jpg", id, part)
|
||||
return NewRaw(name, img, quality)
|
||||
}
|
||||
|
||||
func NewRaw(name string, img image.Image, quality int) *ImageData {
|
||||
var (
|
||||
data, cdata bytes.Buffer
|
||||
err error
|
||||
)
|
||||
|
||||
err = jpeg.Encode(&data, img, &jpeg.Options{Quality: quality})
|
||||
if err != nil {
|
||||
exitWithError(err)
|
||||
data := bytes.NewBuffer([]byte{})
|
||||
if err := jpeg.Encode(data, img, &jpeg.Options{Quality: quality}); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
wcdata, err := flate.NewWriter(&cdata, flate.BestCompression)
|
||||
cdata := bytes.NewBuffer([]byte{})
|
||||
wcdata, err := flate.NewWriter(cdata, flate.BestCompression)
|
||||
if err != nil {
|
||||
exitWithError(err)
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
_, err = wcdata.Write(data.Bytes())
|
||||
wcdata.Write(data.Bytes())
|
||||
wcdata.Close()
|
||||
if err != nil {
|
||||
exitWithError(err)
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = wcdata.Close()
|
||||
if err != nil {
|
||||
exitWithError(err)
|
||||
}
|
||||
|
||||
t := time.Now()
|
||||
return &ImageData{
|
||||
&zip.FileHeader{
|
||||
|
@ -1,206 +0,0 @@
|
||||
package epubtemplates
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/beevik/etree"
|
||||
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
|
||||
)
|
||||
|
||||
type ContentOptions struct {
|
||||
Title string
|
||||
UID string
|
||||
Author string
|
||||
Publisher string
|
||||
UpdatedAt string
|
||||
ImageOptions *epubimage.Options
|
||||
Cover *epubimage.Image
|
||||
Images []*epubimage.Image
|
||||
Current int
|
||||
Total int
|
||||
}
|
||||
|
||||
type tagAttrs map[string]string
|
||||
|
||||
type tag struct {
|
||||
name string
|
||||
attrs tagAttrs
|
||||
value string
|
||||
}
|
||||
|
||||
func Content(o *ContentOptions) string {
|
||||
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/#")
|
||||
|
||||
addToElement := func(elm *etree.Element, meth func(o *ContentOptions) []tag) {
|
||||
for _, p := range meth(o) {
|
||||
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, getMeta)
|
||||
|
||||
manifest := pkg.CreateElement("manifest")
|
||||
addToElement(manifest, getManifest)
|
||||
|
||||
spine := pkg.CreateElement("spine")
|
||||
if o.ImageOptions.Manga {
|
||||
spine.CreateAttr("page-progression-direction", "rtl")
|
||||
} else {
|
||||
spine.CreateAttr("page-progression-direction", "ltr")
|
||||
}
|
||||
addToElement(spine, getSpine)
|
||||
|
||||
guide := pkg.CreateElement("guide")
|
||||
addToElement(guide, getGuide)
|
||||
|
||||
doc.Indent(2)
|
||||
r, _ := doc.WriteToString()
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func getMeta(o *ContentOptions) []tag {
|
||||
metas := []tag{
|
||||
{"meta", tagAttrs{"property": "dcterms:modified"}, o.UpdatedAt},
|
||||
{"meta", tagAttrs{"property": "rendition:layout"}, "pre-paginated"},
|
||||
{"meta", tagAttrs{"property": "rendition:spread"}, "auto"},
|
||||
{"meta", tagAttrs{"property": "rendition:orientation"}, "auto"},
|
||||
{"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", o.ImageOptions.ViewWidth, o.ImageOptions.ViewHeight)}, ""},
|
||||
{"dc:title", tagAttrs{}, o.Title},
|
||||
{"dc:identifier", tagAttrs{"id": "ean"}, fmt.Sprintf("urn:uuid:%s", o.UID)},
|
||||
{"dc:language", tagAttrs{}, "en"},
|
||||
{"dc:creator", tagAttrs{}, o.Author},
|
||||
{"dc:publisher", tagAttrs{}, o.Publisher},
|
||||
{"dc:contributor", tagAttrs{}, "Go Comic Convertor"},
|
||||
{"dc:date", tagAttrs{}, o.UpdatedAt},
|
||||
}
|
||||
|
||||
if o.ImageOptions.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 o.Cover != nil {
|
||||
metas = append(metas, tag{"meta", tagAttrs{"name": "cover", "content": o.Cover.Key("img")}, ""})
|
||||
}
|
||||
|
||||
if o.Total > 1 {
|
||||
metas = append(
|
||||
metas,
|
||||
tag{"meta", tagAttrs{"name": "calibre:series", "content": o.Title}, ""},
|
||||
tag{"meta", tagAttrs{"name": "calibre:series_index", "content": fmt.Sprint(o.Current)}, ""},
|
||||
)
|
||||
}
|
||||
|
||||
return metas
|
||||
}
|
||||
|
||||
func getManifest(o *ContentOptions) []tag {
|
||||
itag := func(img *epubimage.Image) tag {
|
||||
return tag{"item", tagAttrs{"id": img.Key("img"), "href": img.ImgPath(), "media-type": "image/jpeg"}, ""}
|
||||
}
|
||||
htag := func(img *epubimage.Image) tag {
|
||||
return tag{"item", tagAttrs{"id": img.Key("page"), "href": img.TextPath(), "media-type": "application/xhtml+xml"}, ""}
|
||||
}
|
||||
stag := func(img *epubimage.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"}, ""},
|
||||
{"item", tagAttrs{"id": "page_title", "href": "Text/title.xhtml", "media-type": "application/xhtml+xml"}, ""},
|
||||
{"item", tagAttrs{"id": "img_title", "href": "Images/title.jpg", "media-type": "image/jpeg"}, ""},
|
||||
}
|
||||
|
||||
if o.ImageOptions.HasCover || o.Current > 1 {
|
||||
items = append(items, itag(o.Cover), htag(o.Cover))
|
||||
}
|
||||
|
||||
for _, img := range o.Images {
|
||||
if img.Part == 1 {
|
||||
items = append(items, stag(img))
|
||||
}
|
||||
items = append(items, itag(img), htag(img))
|
||||
}
|
||||
items = append(items, stag(o.Images[len(o.Images)-1]))
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
func getSpine(o *ContentOptions) []tag {
|
||||
isOnTheRight := !o.ImageOptions.Manga
|
||||
getSpread := func(doublePageNoBlank bool) string {
|
||||
isOnTheRight = !isOnTheRight
|
||||
if doublePageNoBlank {
|
||||
// Center the double page then start back to comic mode (mange/normal)
|
||||
isOnTheRight = !o.ImageOptions.Manga
|
||||
return "rendition:page-spread-center"
|
||||
}
|
||||
if isOnTheRight {
|
||||
return "rendition:page-spread-right"
|
||||
} else {
|
||||
return "rendition:page-spread-left"
|
||||
}
|
||||
}
|
||||
|
||||
spine := []tag{
|
||||
{"itemref", tagAttrs{"idref": "page_title", "properties": getSpread(true)}, ""},
|
||||
}
|
||||
for _, img := range o.Images {
|
||||
spine = append(spine, tag{
|
||||
"itemref",
|
||||
tagAttrs{"idref": img.Key("page"), "properties": getSpread(img.DoublePage && o.ImageOptions.NoBlankPage)},
|
||||
"",
|
||||
})
|
||||
if img.DoublePage && isOnTheRight && !o.ImageOptions.NoBlankPage {
|
||||
spine = append(spine, tag{
|
||||
"itemref",
|
||||
tagAttrs{"idref": img.SpaceKey("page"), "properties": getSpread(false)},
|
||||
"",
|
||||
})
|
||||
}
|
||||
}
|
||||
if o.ImageOptions.Manga == isOnTheRight {
|
||||
spine = append(spine, tag{
|
||||
"itemref",
|
||||
tagAttrs{"idref": o.Images[len(o.Images)-1].SpaceKey("page"), "properties": getSpread(false)},
|
||||
"",
|
||||
})
|
||||
}
|
||||
|
||||
return spine
|
||||
}
|
||||
|
||||
func getGuide(o *ContentOptions) []tag {
|
||||
guide := []tag{}
|
||||
if o.Cover != nil {
|
||||
guide = append(guide, tag{"reference", tagAttrs{"type": "cover", "title": "cover", "href": o.Cover.TextPath()}, ""})
|
||||
}
|
||||
guide = append(guide, tag{"reference", tagAttrs{"type": "text", "title": "content", "href": o.Images[0].TextPath()}, ""})
|
||||
return guide
|
||||
}
|
@ -5,39 +5,39 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type tree struct {
|
||||
Nodes map[string]*node
|
||||
type Tree struct {
|
||||
Nodes map[string]*Node
|
||||
}
|
||||
|
||||
type node struct {
|
||||
type Node struct {
|
||||
Value string
|
||||
Children []*node
|
||||
Children []*Node
|
||||
}
|
||||
|
||||
func New() *tree {
|
||||
return &tree{map[string]*node{
|
||||
".": {".", []*node{}},
|
||||
func New() *Tree {
|
||||
return &Tree{map[string]*Node{
|
||||
".": {".", []*Node{}},
|
||||
}}
|
||||
}
|
||||
|
||||
func (n *tree) Root() *node {
|
||||
func (n *Tree) Root() *Node {
|
||||
return n.Nodes["."]
|
||||
}
|
||||
|
||||
func (n *tree) Add(filename string) {
|
||||
func (n *Tree) Add(filename string) {
|
||||
cn := n.Root()
|
||||
cp := ""
|
||||
for _, p := range strings.Split(filepath.Clean(filename), string(filepath.Separator)) {
|
||||
cp = filepath.Join(cp, p)
|
||||
if _, ok := n.Nodes[cp]; !ok {
|
||||
n.Nodes[cp] = &node{Value: p, Children: []*node{}}
|
||||
n.Nodes[cp] = &Node{Value: p, Children: []*Node{}}
|
||||
cn.Children = append(cn.Children, n.Nodes[cp])
|
||||
}
|
||||
cn = n.Nodes[cp]
|
||||
}
|
||||
}
|
||||
|
||||
func (n *node) WriteString(indent string) string {
|
||||
func (n *Node) ToString(indent string) string {
|
||||
r := strings.Builder{}
|
||||
if indent != "" {
|
||||
r.WriteString(indent)
|
||||
@ -47,7 +47,7 @@ func (n *node) WriteString(indent string) string {
|
||||
}
|
||||
indent += " "
|
||||
for _, c := range n.Children {
|
||||
r.WriteString(c.WriteString(indent))
|
||||
r.WriteString(c.ToString(indent))
|
||||
}
|
||||
return r.String()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user