factor rendering

This commit is contained in:
Celogeek 2023-04-16 18:02:31 +02:00
parent 8d24f95579
commit 02583cc715
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
6 changed files with 52 additions and 40 deletions

View File

@ -93,6 +93,38 @@ func (e *ePub) render(templateString string, data any) string {
return stripBlank.ReplaceAllString(result.String(), "\n")
}
func (e *ePub) writeImage(wz *epubZip, img *Image) error {
err := wz.WriteFile(
fmt.Sprintf("OEBPS/%s", img.TextPath()),
e.render(textTmpl, map[string]any{
"Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part),
"ViewPort": fmt.Sprintf("width=%d, height=%d", e.ViewWidth, e.ViewHeight),
"ImageStyle": fmt.Sprintf(
"width:%dpx; height:%dpx;",
img.Width,
img.Height,
),
"ImagePath": img.ImgPath(),
}),
)
if err == nil {
err = wz.WriteImage(img.Data)
}
return err
}
func (e *ePub) writeBlank(wz *epubZip, img *Image) error {
return wz.WriteFile(
fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id),
e.render(blankTmpl, map[string]any{
"Title": fmt.Sprintf("Blank Page %d", img.Id),
"ViewPort": fmt.Sprintf("width=%d, height=%d", e.ViewWidth, e.ViewHeight),
}),
)
}
func (e *ePub) getParts() ([]*epubPart, error) {
images, err := e.LoadImages()
@ -210,8 +242,11 @@ func (e *ePub) Write() error {
{"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{
{"OEBPS/Text/style.css", e.render(styleTmpl, map[string]any{
"PageWidth": e.ViewWidth,
"PageHeight": e.ViewHeight,
})},
{"OEBPS/Text/title.xhtml", e.render(titleTmpl, map[string]any{
"Info": e,
"Part": i + 1,
"Total": totalParts,
@ -230,41 +265,19 @@ 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 {
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 {
if err := e.writeImage(wz, part.Cover); err != nil {
return err
}
}
for i, img := range part.Images {
if err := wz.WriteFile(fmt.Sprintf("OEBPS/Text/%d_p%d.xhtml", img.Id, img.Part), e.render(textTmpl, map[string]any{
"Info": e,
"Image": img,
"Top": fmt.Sprintf("%d", (e.ViewHeight-img.Height)/2),
})); err != nil {
return err
}
if err := wz.WriteImage(img.Data); err != nil {
if err := e.writeImage(wz, img); err != nil {
return err
}
// Double Page or Last Image
if img.DoublePage || (i+1 == len(part.Images)) {
if err := wz.WriteFile(
fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id),
e.render(blankTmpl, map[string]any{
"Info": e,
"Image": img,
}),
); err != nil {
if err := e.writeBlank(wz, img); err != nil {
return err
}
}

View File

@ -11,8 +11,8 @@ var appleBooksTmpl string
//go:embed "templates/style.css.tmpl"
var styleTmpl string
//go:embed "templates/part.xhtml.tmpl"
var partTmpl string
//go:embed "templates/title.xhtml.tmpl"
var titleTmpl string
//go:embed "templates/text.xhtml.tmpl"
var textTmpl string

View File

@ -2,9 +2,9 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Page {{ .Image.Id }} Space</title>
<title>{{ .Title }}</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="width={{ .Info.ViewWidth }}, height={{ .Info.ViewHeight }}"/>
<meta name="viewport" content="{{ .ViewPort }}"/>
</head>
<body>
</body>

View File

@ -3,12 +3,11 @@ body {
background: #FFF;
margin: 0;
padding: 0;
width: {{ .PageWidth }}px;
height: {{ .PageHeight }}px;
}
img {
position: absolute;
margin: 0;
padding: 0;
top: 0;
left: 0;
div {
margin:0;
padding:0;
}

View File

@ -2,13 +2,13 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Page {{ .Image.Id }}_p{{ .Image.Part}}</title>
<title>{{ .Title }}</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="width={{ .Info.ViewWidth }}, height={{ .Info.ViewHeight }}"/>
<meta name="viewport" content="{{ .ViewPort }}"/>
</head>
<body>
<div>
<img style="width:{{ .Image.Width }}px; height:{{ .Image.Height }}px; top:{{ .Top }}px" src="../Images/{{ .Image.Id }}_p{{ .Image.Part}}.jpg"/>
<img style="{{ .ImageStyle }}" src="../{{ .ImagePath }}"/>
</div>
</body>
</html>