-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathspine.go
73 lines (63 loc) · 1.7 KB
/
spine.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
// Copyright 2012 Ruben Pollan <[email protected]>
// Use of this source code is governed by a LGPL licence
// version 3 or later that can be found in the LICENSE file.
package epubgo
import (
"errors"
"io"
)
// SpineIterator is an iterator on the epub pages spine
//
// With it is possible to navigate throw the pages of the epub.
type SpineIterator struct {
opf *xmlOPF
index int
epub *Epub
}
func newSpineIterator(epub *Epub) (*SpineIterator, error) {
if epub.opf.spineLength() == 0 {
return nil, errors.New("Spine is empty")
}
var spine SpineIterator
spine.epub = epub
spine.opf = epub.opf
spine.index = 0
return &spine, nil
}
// IsFirst returns whether the element is the first of the book
func (spine SpineIterator) IsFirst() bool {
return spine.index == 0
}
// IsLast returns whether the element is the last of the book
func (spine SpineIterator) IsLast() bool {
return spine.index == spine.opf.spineLength()-1
}
// Next advances the iterator to the next element on the spine
//
// Returns an error if is the last
func (spine *SpineIterator) Next() error {
if spine.IsLast() {
return errors.New("It is the last entry")
}
spine.index++
return nil
}
// Previous steps back the iterator to the previous element on the spine
//
// Returns an error if is the first
func (spine *SpineIterator) Previous() error {
if spine.IsFirst() {
return errors.New("It is the first entry")
}
spine.index--
return nil
}
// Open opens the file of the iterator
func (spine SpineIterator) Open() (io.ReadCloser, error) {
url := spine.URL()
return spine.epub.OpenFile(url)
}
// URL returns the url of the item on the iterator
func (spine SpineIterator) URL() string {
return spine.opf.spineURL(spine.index)
}