forked from bmaupin/go-epub
-
Notifications
You must be signed in to change notification settings - Fork 9
/
example_test.go
213 lines (176 loc) · 4.36 KB
/
example_test.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
204
205
206
207
208
209
210
211
212
213
package epub_test
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-shiori/go-epub"
)
func ExampleEpub_AddCSS() {
var t *testing.T
e, err := epub.NewEpub("My title")
if err != nil {
t.Error(err)
}
// Add CSS
css1Path, err := e.AddCSS("testdata/cover.css", "epub.css")
if err != nil {
log.Println(err)
return
}
// The filename is optional
css2Path, err := e.AddCSS("testdata/cover.css", "")
if err != nil {
log.Println(err)
return
}
// Use the CSS in a section
sectionBody := ` <h1>Section 1</h1>
<p>This is a paragraph.</p>`
_, err = e.AddSection(sectionBody, "Section 1", "", css1Path)
if err != nil {
log.Println(err)
return
}
fmt.Println(css1Path)
fmt.Println(css2Path)
// Output:
// ../css/epub.css
// ../css/cover.css
}
func ExampleEpub_AddFont() {
var t *testing.T
e, err := epub.NewEpub("My title")
if err != nil {
t.Error(err)
}
// Add a font from a local file
font1Path, err := e.AddFont("testdata/redacted-script-regular.ttf", "font.ttf")
if err != nil {
log.Println(err)
}
// The filename is optional
font2Path, err := e.AddFont("testdata/redacted-script-regular.ttf", "")
if err != nil {
log.Println(err)
}
fmt.Println(font1Path)
fmt.Println(font2Path)
// Output:
// ../fonts/font.ttf
// ../fonts/redacted-script-regular.ttf
}
func ExampleEpub_AddImage() {
fs := http.FileServer(http.Dir("./testdata/"))
// start a test server with the file server handler
server := httptest.NewServer(fs)
defer server.Close()
testImageFromURLSource := server.URL + "/gophercolor16x16.png"
var t *testing.T
e, err := epub.NewEpub("My title")
if err != nil {
t.Error(err)
}
// Add an image from a local file
img1Path, err := e.AddImage("testdata/gophercolor16x16.png", "go-gopher.png")
if err != nil {
log.Println(err)
}
// Add an image from a URL. The filename is optional
img2Path, err := e.AddImage(testImageFromURLSource, "")
if err != nil {
log.Println(err)
}
fmt.Println(img1Path)
fmt.Println(img2Path)
// Output:
// ../images/go-gopher.png
// ../images/gophercolor16x16.png
}
func ExampleEpub_AddSection() {
var t *testing.T
e, err := epub.NewEpub("My title")
if err != nil {
t.Error(err)
}
// Add a section. The CSS path is optional
section1Body := ` <h1>Section 1</h1>
<p>This is a paragraph.</p>`
section1Path, err := e.AddSection(section1Body, "Section 1", "firstsection.xhtml", "")
if err != nil {
log.Println(err)
}
// Link to the first section
section2Body := fmt.Sprintf(` <h1>Section 2</h1>
<a href="%s">Link to section 1</a>`,
section1Path)
// The title and filename are also optional
section2Path, err := e.AddSection(section2Body, "", "", "")
if err != nil {
log.Println(err)
}
fmt.Println(section1Path)
fmt.Println(section2Path)
// Output:
// firstsection.xhtml
// section0001.xhtml
}
func ExampleEpub_AddSubSection() {
var t *testing.T
e, err := epub.NewEpub("My title")
if err != nil {
t.Error(err)
}
// Add a section. The CSS path is optional
section1Body := ` <h1>Section 1</h1>
<p>This is a paragraph.</p>`
section1Path, err := e.AddSection(section1Body, "Section 1", "firstsection.xhtml", "")
if err != nil {
log.Println(err)
}
// Link to the first section
section2Body := fmt.Sprintf(` <h1>Section 2</h1>
<a href="%s">Link to section 1</a>`,
section1Path)
// The title and filename are also optional
section2Path, err := e.AddSubSection(section1Path, section2Body, "", "", "")
if err != nil {
log.Println(err)
}
fmt.Println(section1Path)
fmt.Println(section2Path)
// Output:
// firstsection.xhtml
// section0001.xhtml
}
func ExampleEpub_SetCover() {
var t *testing.T
e, err := epub.NewEpub("My title")
if err != nil {
t.Error(err)
}
// Set the cover. The CSS file is optional
coverImagePath, _ := e.AddImage("testdata/gophercolor16x16.png", "cover.png")
err = e.SetCover(coverImagePath, "")
if err != nil {
t.Error(err)
}
// Update the cover using custom CSS
coverCSSPath, _ := e.AddCSS("testdata/cover.css", "")
err = e.SetCover(coverImagePath, coverCSSPath)
if err != nil {
t.Error(err)
}
}
func ExampleEpub_SetIdentifier() {
var t *testing.T
e, err := epub.NewEpub("My title")
if err != nil {
t.Error(err)
}
// Set the identifier to a UUID
e.SetIdentifier("urn:uuid:a1b0d67e-2e81-4df5-9e67-a64cbe366809")
// Set the identifier to an ISBN
e.SetIdentifier("urn:isbn:9780101010101")
}