forked from bmaupin/go-epub
-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
133 lines (104 loc) · 2.86 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
package epub_test
import (
"fmt"
"log"
"github.com/bmaupin/go-epub"
)
func ExampleEpub_AddCSS() {
e := epub.NewEpub("My title")
// Add CSS
css1Path, err := e.AddCSS("testdata/cover.css", "epub.css")
if err != nil {
log.Fatal(err)
}
// The filename is optional
css2Path, err := e.AddCSS("testdata/cover.css", "")
if err != nil {
log.Fatal(err)
}
// Use the CSS in a section
sectionBody := ` <h1>Section 1</h1>
<p>This is a paragraph.</p>`
e.AddSection(sectionBody, "Section 1", "", css1Path)
fmt.Println(css1Path)
fmt.Println(css2Path)
// Output:
// ../css/epub.css
// ../css/cover.css
}
func ExampleEpub_AddFont() {
e := epub.NewEpub("My title")
// Add a font from a local file
font1Path, err := e.AddFont("testdata/redacted-script-regular.ttf", "font.ttf")
if err != nil {
log.Fatal(err)
}
// The filename is optional
font2Path, err := e.AddFont("testdata/redacted-script-regular.ttf", "")
if err != nil {
log.Fatal(err)
}
fmt.Println(font1Path)
fmt.Println(font2Path)
// Output:
// ../fonts/font.ttf
// ../fonts/redacted-script-regular.ttf
}
func ExampleEpub_AddImage() {
e := epub.NewEpub("My title")
// Add an image from a local file
img1Path, err := e.AddImage("testdata/gophercolor16x16.png", "go-gopher.png")
if err != nil {
log.Fatal(err)
}
// Add an image from a URL. The filename is optional
img2Path, err := e.AddImage("https://golang.org/doc/gopher/gophercolor16x16.png", "")
if err != nil {
log.Fatal(err)
}
fmt.Println(img1Path)
fmt.Println(img2Path)
// Output:
// ../images/go-gopher.png
// ../images/gophercolor16x16.png
}
func ExampleEpub_AddSection() {
e := epub.NewEpub("My title")
// 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.Fatal(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.Fatal(err)
}
fmt.Println(section1Path)
fmt.Println(section2Path)
// Output:
// firstsection.xhtml
// section0002.xhtml
}
func ExampleEpub_SetCover() {
e := epub.NewEpub("My title")
// Set the cover. The CSS file is optional
coverImagePath, _ := e.AddImage("testdata/gophercolor16x16.png", "cover.png")
e.SetCover(coverImagePath, "")
// Update the cover using custom CSS
coverCSSPath, _ := e.AddCSS("testdata/cover.css", "")
e.SetCover(coverImagePath, coverCSSPath)
}
func ExampleEpub_SetIdentifier() {
e := epub.NewEpub("My title")
// 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")
}