-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
203 lines (167 loc) · 4.02 KB
/
data.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package data
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"google.golang.org/protobuf/encoding/prototext"
"ebooker/proto"
)
type Book struct {
Title string
Author string
Chapters []*Chapter
defaultSelector string
}
func NewBook(input proto.Book) *Book {
return &Book{
Title: input.GetTitle(),
Author: input.GetAuthor(),
Chapters: newChapters(input.GetChapters(), input.GetDefaultSelector()),
defaultSelector: input.GetDefaultSelector(),
}
}
func ReadBook(dir string) (*Book, error) {
// Read the textproto file.
infile := path.Join(dir, "book.textproto")
book, err := ReadDataFile(infile)
if err != nil {
return nil, err
}
// Read the contents from the data dir.
for i, chap := range book.Chapters {
var contents []string
for j := 0; j < len(chap.urls); j++ {
fileName := fmt.Sprintf("c%02d_s%02d.html", i, j)
infile := path.Join(dir, fileName)
content, err := ioutil.ReadFile(infile)
if err != nil {
return nil, fmt.Errorf("Unable to read chapter %d, section %d file file %s: %v", i, j, infile, err)
}
contents = append(contents, string(content))
}
chap.SetContent(contents)
}
return book, nil
}
func openDataFile(name string) (io.Reader, error) {
if name == "-" {
return os.Stdin, nil
}
return os.Open(name)
}
func ReadDataFile(name string) (*Book, error) {
r, err := openDataFile(name)
if err != nil {
return nil, err
}
content, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
var input proto.Book
if err := prototext.Unmarshal(content, &input); err != nil {
return nil, err
}
book := NewBook(input)
return book, nil
}
func (b Book) Write(dir string) error {
// Write out the data so far.
err := os.MkdirAll(dir, 0755)
if err != nil {
return fmt.Errorf("Unable to create output directory %s: %v", dir, err)
}
for i, chap := range b.Chapters {
for j, cont := range chap.Content() {
fileName := fmt.Sprintf("c%02d_s%02d.html", i, j)
outfile := path.Join(dir, fileName)
err = ioutil.WriteFile(outfile, []byte(cont), 0644)
if err != nil {
return fmt.Errorf("Unable to write chapter %d, section %d to file %s: %v", i, j, outfile, err)
}
}
}
// Write out the updated book data file.
outfile := path.Join(dir, "book.textproto")
err = b.writeDataFile(outfile)
if err != nil {
return fmt.Errorf("Unable to write book data to %q: %v", outfile, err)
}
return nil
}
func (b Book) writeDataFile(name string) error {
opts := prototext.MarshalOptions{
Multiline: true,
}
result, err := opts.Marshal(b.AsProto())
if err != nil {
return err
}
if name == "-" {
fmt.Printf("%s\n", string(result))
} else {
err := ioutil.WriteFile(name, result, 0644)
if err != nil {
return err
}
}
return nil
}
func (b Book) AsProto() *proto.Book {
return &proto.Book{
Title: b.Title,
Author: b.Author,
DefaultSelector: b.defaultSelector,
Chapters: chaptersAsProtos(b.Chapters),
}
}
type Chapter struct {
Name string
content []string
urls []string
selector string
}
func newChapters(chapters []*proto.Chapter, defaultSelector string) []*Chapter {
var results []*Chapter
for _, chapter := range chapters {
selector := chapter.GetSelector()
if selector == "" {
selector = defaultSelector
}
result := &Chapter{
Name: chapter.GetName(),
urls: chapter.GetUrl(),
selector: selector,
}
results = append(results, result)
}
return results
}
func chaptersAsProtos(chapters []*Chapter) []*proto.Chapter {
var results []*proto.Chapter
for _, chapter := range chapters {
results = append(results, chapter.AsProto())
}
return results
}
func (c *Chapter) Urls() []string {
return c.urls
}
func (c *Chapter) Selector() string {
return c.selector
}
func (c *Chapter) Content() []string {
return c.content
}
func (c *Chapter) SetContent(newContent []string) {
c.content = newContent
}
func (c *Chapter) AsProto() *proto.Chapter {
return &proto.Chapter{
Name: c.Name,
Selector: c.selector,
Url: c.urls,
}
}