-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
159 lines (132 loc) · 3.79 KB
/
Copy pathexample_test.go
File metadata and controls
159 lines (132 loc) · 3.79 KB
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
package epub_test
import (
"fmt"
"strings"
"github.com/raitucarp/epub"
)
// Example demonstrates the core write-then-read workflow. A publication is
// built in memory, serialized to bytes, and read back to verify its metadata.
func Example() {
w := epub.New("urn:example:book")
w.Title("The Example Book")
w.Author("Jane Doe")
w.Languages("en")
w.AddContent("chapter-1.xhtml", []byte(`<html xmlns="http://www.w3.org/1999/xhtml"><body><h1>Chapter 1</h1><p>Hello, world.</p></body></html>`))
if err := w.TableOfContents("toc", epub.TOC{
Title: "Contents",
Items: []epub.TOC{{Title: "Chapter 1", Href: "chapter-1.xhtml"}},
}); err != nil {
panic(err)
}
data, err := w.WriteBytes()
if err != nil {
panic(err)
}
book, err := epub.NewReader(data)
if err != nil {
panic(err)
}
fmt.Println(strings.Join(book.Title(), ", "))
fmt.Println(strings.Join(book.Author(), ", "))
fmt.Println(book.UID())
// Output:
// The Example Book
// Jane Doe
// urn:example:book
}
// ExampleOpenReader demonstrates opening an EPUB file from disk and reading its
// metadata and table of contents.
func ExampleOpenReader() {
book, err := epub.OpenReader("book.epub")
if err != nil {
panic(err)
}
fmt.Println(strings.Join(book.Title(), ", "))
fmt.Println(book.Version())
toc, err := book.TableOfContents()
if err != nil {
panic(err)
}
for _, item := range toc.Items {
fmt.Println(item.Title, item.Href)
}
}
// ExampleWriter demonstrates building a publication with a cover image and
// multiple chapters before writing it to disk.
func ExampleWriter() {
w := epub.New("urn:example:writer")
w.Title("A Novel")
w.Author("Jane Doe")
w.Languages("en")
w.AddContent("chapter-1.xhtml", []byte(`<html xmlns="http://www.w3.org/1999/xhtml"><body><h1>Chapter 1</h1></body></html>`))
w.AddContent("chapter-2.xhtml", []byte(`<html xmlns="http://www.w3.org/1999/xhtml"><body><h1>Chapter 2</h1></body></html>`))
toc := epub.TOC{
Title: "Contents",
Items: []epub.TOC{
{Title: "Chapter 1", Href: "chapter-1.xhtml"},
{Title: "Chapter 2", Href: "chapter-2.xhtml"},
},
}
if err := w.TableOfContents("toc", toc); err != nil {
panic(err)
}
if err := w.Write("book.epub"); err != nil {
panic(err)
}
}
// ExampleWriter_AddMarkdown demonstrates converting a single Markdown document
// into an EPUB content document.
func ExampleWriter_AddMarkdown() {
w := epub.New("urn:example:markdown")
w.Title("A Markdown Book")
w.Languages("en")
res, err := w.AddMarkdown("chapter-1.md", []byte("# Chapter 1\n\nOnce upon a time...\n"))
if err != nil {
panic(err)
}
fmt.Println(res.Href)
fmt.Println(res.MIMEType)
// Output:
// chapter-1.xhtml
// application/xhtml+xml
}
// ExampleWriter_AddMarkdownDirectory demonstrates building an entire EPUB from
// a directory of Markdown files. The table of contents is derived from the
// headings in each file.
func ExampleWriter_AddMarkdownDirectory() {
w := epub.New("urn:example:markdown-dir")
w.Title("A Markdown Directory Book")
w.Author("Jane Doe")
w.Languages("en")
if err := w.AddMarkdownDirectory("manuscript"); err != nil {
panic(err)
}
if err := w.Write("book.epub"); err != nil {
panic(err)
}
}
// Example_roundTrip demonstrates reading an existing publication, adjusting its
// metadata, and writing a new EPUB.
func Example_roundTrip() {
book, err := epub.OpenReader("original.epub")
if err != nil {
panic(err)
}
w := epub.New(book.UID())
w.Title(append([]string{"Second Edition"}, book.Title()...)...)
w.Author(book.Author()...)
w.Languages(book.Language()...)
for _, res := range book.Spine() {
w.AddContent(res.Href, res.Content)
}
toc, err := book.TableOfContents()
if err != nil {
panic(err)
}
if err := w.TableOfContents("toc", toc); err != nil {
panic(err)
}
if err := w.Write("second-edition.epub"); err != nil {
panic(err)
}
}