forked from aiialzy/txt2azw3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_opf.go
49 lines (41 loc) · 889 Bytes
/
content_opf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"os"
"path"
"text/template"
)
type OpfItem struct {
Id string
Href string
}
func genContentOpf(novel Novel) {
t, err := template.ParseFiles("templates/content.opf.tmpl")
if err != nil {
panic(err)
}
filePath := path.Join(novel.Title, "content.opf")
f, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0766)
if err != nil {
panic(err)
}
var items []OpfItem
for _, chapter := range novel.Chapters {
item := OpfItem{
Id: fmt.Sprintf("chapter%d", chapter.Index),
Href: path.Join("text", chapter.Filename),
}
items = append(items, item)
}
args := map[string]interface{}{
"title": novel.Title,
"author": novel.Author,
"uuid": novel.UUID,
"items": items,
"language": novel.Language,
}
err = t.Execute(f, args)
if err != nil {
panic(err)
}
}