-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample_test.go
More file actions
64 lines (51 loc) · 1.65 KB
/
example_test.go
File metadata and controls
64 lines (51 loc) · 1.65 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
package mobi_test
import (
"fmt"
"strings"
"os"
"image/jpeg"
"github.com/sblinch/mobi"
)
func ExampleNewWriter() {
w, err := mobi.NewWriter("/tmp/example.mobi")
if err != nil {
panic(err)
}
defer w.Close()
w.Title("Book Title")
w.Compression(mobi.CompressionNone) // LZ77 compression is also possible using mobi.CompressionPalmDoc
// Meta data
w.NewExthRecord(mobi.EXTH_DOCTYPE, "EBOK")
w.NewExthRecord(mobi.EXTH_AUTHOR, "Book Author Name")
// See exth.go for additional EXTH record IDs
// Add chapters and subchapters
ch1 := w.NewChapter("Chapter 1", []byte("Some text here"))
ch1.AddSubChapter("Chapter 1-1", []byte("Some text here"))
ch1.AddSubChapter("Chapter 1-2", []byte("Some text here"))
w.NewChapter("Chapter 2", []byte("Some text here")).AddSubChapter("Chapter 2-1", []byte("Some text here")).AddSubChapter("Chapter 2-2", []byte("Some text here"))
w.NewChapter("Chapter 3", []byte("Some text here")).AddSubChapter("Chapter 3-1", []byte("Some text here"))
w.NewChapter("Chapter 4", []byte("Some text here")).AddSubChapter("Chapter 4-1", []byte("Some text here"))
// Output MOBI File
w.Write()
// Output:
}
func ExampleNewReader() {
r, err := mobi.NewReader("/tmp/example.mobi")
if err != nil {
panic(err)
}
defer r.Close()
fmt.Printf("Document type: %s\n", r.DocType())
fmt.Printf("Author(s): %s\n", strings.Join(r.Authors(), ", "))
fmt.Printf("Title: %s\n", r.BestTitle())
// See reader.go for additional record types
if r.HasCover() {
img, _ := r.Cover()
f, _ := os.Create("/tmp/mobi_example.jpg")
jpeg.Encode(f, img, nil)
f.Close()
}
// Output: Document type: EBOK
//Author(s): Book Author Name
//Title: Book Title
}