mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 08:12:36 +02:00
factor rendering
This commit is contained in:
parent
8d24f95579
commit
02583cc715
@ -93,6 +93,38 @@ func (e *ePub) render(templateString string, data any) string {
|
|||||||
return stripBlank.ReplaceAllString(result.String(), "\n")
|
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) {
|
func (e *ePub) getParts() ([]*epubPart, error) {
|
||||||
images, err := e.LoadImages()
|
images, err := e.LoadImages()
|
||||||
|
|
||||||
@ -210,8 +242,11 @@ func (e *ePub) Write() error {
|
|||||||
{"META-INF/com.apple.ibooks.display-options.xml", appleBooksTmpl},
|
{"META-INF/com.apple.ibooks.display-options.xml", appleBooksTmpl},
|
||||||
{"OEBPS/content.opf", e.getContent(title, part, i+1, totalParts).String()},
|
{"OEBPS/content.opf", e.getContent(title, part, i+1, totalParts).String()},
|
||||||
{"OEBPS/toc.xhtml", e.getToc(title, part.Images)},
|
{"OEBPS/toc.xhtml", e.getToc(title, part.Images)},
|
||||||
{"OEBPS/Text/style.css", styleTmpl},
|
{"OEBPS/Text/style.css", e.render(styleTmpl, map[string]any{
|
||||||
{"OEBPS/Text/part.xhtml", e.render(partTmpl, map[string]any{
|
"PageWidth": e.ViewWidth,
|
||||||
|
"PageHeight": e.ViewHeight,
|
||||||
|
})},
|
||||||
|
{"OEBPS/Text/title.xhtml", e.render(titleTmpl, map[string]any{
|
||||||
"Info": e,
|
"Info": e,
|
||||||
"Part": i + 1,
|
"Part": i + 1,
|
||||||
"Total": totalParts,
|
"Total": totalParts,
|
||||||
@ -230,41 +265,19 @@ func (e *ePub) Write() error {
|
|||||||
// Cover exist or part > 1
|
// Cover exist or part > 1
|
||||||
// If no cover, part 2 and more will include the image as a cover
|
// If no cover, part 2 and more will include the image as a cover
|
||||||
if e.HasCover || i > 0 {
|
if e.HasCover || i > 0 {
|
||||||
if err := wz.WriteFile(fmt.Sprintf("OEBPS/%s", part.Cover.TextPath()), e.render(textTmpl, map[string]any{
|
if err := e.writeImage(wz, part.Cover); err != nil {
|
||||||
"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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, img := range part.Images {
|
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{
|
if err := e.writeImage(wz, img); err != nil {
|
||||||
"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 {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Double Page or Last Image
|
// Double Page or Last Image
|
||||||
if img.DoublePage || (i+1 == len(part.Images)) {
|
if img.DoublePage || (i+1 == len(part.Images)) {
|
||||||
if err := wz.WriteFile(
|
if err := e.writeBlank(wz, img); err != nil {
|
||||||
fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id),
|
|
||||||
e.render(blankTmpl, map[string]any{
|
|
||||||
"Info": e,
|
|
||||||
"Image": img,
|
|
||||||
}),
|
|
||||||
); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,8 @@ var appleBooksTmpl string
|
|||||||
//go:embed "templates/style.css.tmpl"
|
//go:embed "templates/style.css.tmpl"
|
||||||
var styleTmpl string
|
var styleTmpl string
|
||||||
|
|
||||||
//go:embed "templates/part.xhtml.tmpl"
|
//go:embed "templates/title.xhtml.tmpl"
|
||||||
var partTmpl string
|
var titleTmpl string
|
||||||
|
|
||||||
//go:embed "templates/text.xhtml.tmpl"
|
//go:embed "templates/text.xhtml.tmpl"
|
||||||
var textTmpl string
|
var textTmpl string
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
|
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
|
||||||
<head>
|
<head>
|
||||||
<title>Page {{ .Image.Id }} Space</title>
|
<title>{{ .Title }}</title>
|
||||||
<link href="style.css" type="text/css" rel="stylesheet"/>
|
<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>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
</body>
|
</body>
|
||||||
|
@ -3,12 +3,11 @@ body {
|
|||||||
background: #FFF;
|
background: #FFF;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
|
width: {{ .PageWidth }}px;
|
||||||
|
height: {{ .PageHeight }}px;
|
||||||
}
|
}
|
||||||
|
|
||||||
img {
|
div {
|
||||||
position: absolute;
|
margin:0;
|
||||||
margin: 0;
|
padding:0;
|
||||||
padding: 0;
|
|
||||||
top: 0;
|
|
||||||
left: 0;
|
|
||||||
}
|
}
|
@ -2,13 +2,13 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
|
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
|
||||||
<head>
|
<head>
|
||||||
<title>Page {{ .Image.Id }}_p{{ .Image.Part}}</title>
|
<title>{{ .Title }}</title>
|
||||||
<link href="style.css" type="text/css" rel="stylesheet"/>
|
<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>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div>
|
<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>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
x
Reference in New Issue
Block a user