/
book.go
92 lines (75 loc) · 1.8 KB
/
book.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package ldsorg
import (
"fmt"
"github.com/duckbrain/ldss/lib"
"github.com/duckbrain/ldss/lib/dl"
)
// Represents a book in the catalog or one of it's folders and
// provides a way to access the nodes in it's database if
// downloaded.
// Used for parsing books in the catalog's JSON file
type book struct {
ID int `json:"id"`
JSONName string `json:"name"`
DownloadURL string `json:"url"`
GlURI string `json:"gl_uri"`
catalog *catalog
parent lib.Item
dl.Template
children []lib.Item
}
var _ lib.Item = (*book)(nil)
var _ dl.Downloader = (*book)(nil)
func (b *book) init(catalog *catalog, parent lib.Item) {
b.catalog = catalog
b.parent = parent
}
func (b *book) Open() error {
path := bookPath(b)
l, err := opendb(path)
if err != nil {
return err
}
// Populate Children
nodes, err := l.childrenByParentID(0, b, b)
if err != nil {
return err
}
b.children = nodes
langCode := b.Lang().Code()
for _, node := range nodes {
itemsByLangAndPath[ref{langCode, node.Path()}] = node
}
return nil
}
func (b *book) Hash() string {
return fmt.Sprintf("ldsorg:%v?lang=%v", b.Path(), b.Lang().Code())
}
// The name of this book.
func (b *book) Name() string {
return b.JSONName
}
// The Gospel Library Path of this book, unique within it's language
func (b *book) Path() string {
return b.GlURI
}
// The language this book is in
func (b *book) Lang() lib.Lang {
return b.catalog.lang
}
// Children in this book.
func (b *book) Children() []lib.Item {
return b.children
}
// Parent Folder or Catalog of this book
func (b *book) Parent() lib.Item {
return b.parent
}
// Next book in the Folder
func (b *book) Next() lib.Item {
return lib.GenericNextPrevious(b, 1)
}
// Previous book in the Folder
func (b *book) Prev() lib.Item {
return lib.GenericNextPrevious(b, -1)
}